public void configure() {
//TODO exclude pure hbm file classes?
//TODO move it to each event listener initialize()?
EntityCallbackHandler callbackHandler = new EntityCallbackHandler();
configuration.buildMappings(); //needed to get all the classes
Iterator classes = configuration.getClassMappings();
ReflectionManager reflectionManager = configuration.getHibernateConfiguration().getReflectionManager();
while ( classes.hasNext() ) {
PersistentClass clazz = (PersistentClass) classes.next();
if ( clazz.getClassName() != null ) {
//we can have non java class persisted by hibernate
try {
callbackHandler.add( reflectionManager.classForName( clazz.getClassName(), this.getClass() ), reflectionManager );
}
catch (ClassNotFoundException e) {
throw new MappingException("entity class not found: " + clazz.getNodeName(), e);
}
}
}
EventListeners listenerConfig = configuration.getEventListeners();
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo( listenerConfig.getClass(), Object.class );
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
try {
for ( int i = 0, max = pds.length; i < max ; i++ ) {
final Object listeners = pds[i].getReadMethod().invoke( listenerConfig, READER_METHOD_ARGS );
if ( listeners == null ) {
throw new HibernateException( "Listener [" + pds[i].getName() + "] was null" );
}
if ( listeners instanceof Object[] ) {
int securityListenersNbr = 0;
Object[] listenersArray = (Object[]) listeners;
for ( Object listener : listenersArray ) {
if ( listener != null && listener instanceof CallbackHandlerConsumer ) {
( (CallbackHandlerConsumer) listener ).setCallbackHandler( callbackHandler );
}
if ( listener != null && listener instanceof JACCSecurityListener ) {
if ( !isSecurity ) {
securityListenersNbr++;
}
}
}
if ( !isSecurity ) {
Class clazz = pds[i].getReadMethod().getReturnType().getComponentType();
Object newArray = Array.newInstance( clazz, listenersArray.length - securityListenersNbr );
int index = 0;
for ( Object listener : listenersArray ) {
if ( ! ( listener != null && listener instanceof JACCSecurityListener ) ) {
Array.set( newArray, index++, listener );
}
}
pds[i].getWriteMethod().invoke( listenerConfig, newArray );
}
}
}
}
catch (HibernateException e) {
throw e;
}
catch (Throwable t) {
throw new HibernateException( "Unable to validate listener config", t );
}
}
catch (Exception t) {
throw new HibernateException( "Unable to copy listeners", t );
}
finally {
if ( beanInfo != null ) {
// release the jdk internal caches everytime to ensure this
// plays nicely with destroyable class-loaders
Introspector.flushFromCaches( getClass() );
}
}
}
|