From: <hib...@li...> - 2006-03-10 00:34:04
|
Author: epbernard Date: 2006-03-09 19:33:43 -0500 (Thu, 09 Mar 2006) New Revision: 9589 Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/Ejb3Configuration.java Log: EJB-143 check that the persistence provider is the right one when processing pui Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/Ejb3Configuration.java =================================================================== --- trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/Ejb3Configuration.java 2006-03-09 21:39:21 UTC (rev 9588) +++ trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/Ejb3Configuration.java 2006-03-10 00:33:43 UTC (rev 9589) @@ -35,6 +35,9 @@ import org.hibernate.Interceptor; import org.hibernate.MappingException; import org.hibernate.SessionFactory; +import org.hibernate.AssertionFailure; +import org.hibernate.transaction.JTATransactionFactory; +import org.hibernate.transaction.JDBCTransactionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; @@ -216,6 +219,11 @@ else { log.info( "Processing PersistenceUnitInfo [\n\tname: " + info.getPersistenceUnitName() + "\n\t...]" ); } + if ( info.getPersistenceProviderClassName() != null + && ! info.getPersistenceProviderClassName().trim().startsWith( IMPLEMENTATION_NAME ) ) { + log.info( "Required a different provider: " + info.getPersistenceProviderClassName() ); + return null; + } if ( info.getClassLoader() == null ) { throw new IllegalStateException("PersistenceUnitInfo.getClassLoader() id null"); } @@ -449,25 +457,127 @@ private EntityManagerFactory createEntityManagerFactory( Properties properties, Map workingVars ) { - Properties preparedProperties = new Properties(); + Properties preparedProperties = prepareProperties( properties, workingVars ); + if ( workingVars == null ) workingVars = CollectionHelper.EMPTY_MAP; - //defaults different to Hibernate - preparedProperties.setProperty( Environment.RELEASE_CONNECTIONS, "auto" ); - //settings that always apply to a compliant EJB3 - preparedProperties.setProperty( Environment.AUTOCOMMIT, "true" ); - preparedProperties.setProperty( Environment.USE_IDENTIFIER_ROLLBACK, "false" ); - preparedProperties.setProperty( Environment.FLUSH_BEFORE_COMPLETION, "true" ); + if ( preparedProperties.containsKey( HibernatePersistence.CFG_FILE ) ) { + String cfgFileName = preparedProperties.getProperty( HibernatePersistence.CFG_FILE ); + cfg.configure( cfgFileName ); + } - //override the new defaults with the user defined ones - if ( properties != null ) preparedProperties.putAll( properties ); + cfg.addProperties( preparedProperties ); //persistence.xml has priority over hibernate.Cfg.xml - if ( properties != null && properties.containsKey( HibernatePersistence.CFG_FILE ) ) { - String cfgFileName = properties.getProperty( HibernatePersistence.CFG_FILE ); - cfg.configure( cfgFileName ); + addClassesToSessionFactory( workingVars ); + + //processes specific properties + List<String> jaccKeys = new ArrayList<String>(); + + + Configuration defaultConf = new AnnotationConfiguration(); + Interceptor defaultInterceptor = defaultConf.getInterceptor(); + NamingStrategy defaultNamingStrategy = defaultConf.getNamingStrategy(); + + Iterator propertyIt = preparedProperties.keySet().iterator(); + while ( propertyIt.hasNext() ) { + Object uncastObject = propertyIt.next(); + //had to be safe + if ( uncastObject != null && uncastObject instanceof String ) { + String propertyKey = (String) uncastObject; + if ( propertyKey.startsWith( HibernatePersistence.CLASS_CACHE_PREFIX ) ) { + setCacheStrategy( propertyKey, preparedProperties, true ); + } + else if ( propertyKey.startsWith( HibernatePersistence.COLLECTION_CACHE_PREFIX ) ) { + setCacheStrategy( propertyKey, preparedProperties, false ); + } + else if ( propertyKey.startsWith( HibernatePersistence.JACC_PREFIX ) + && ! ( propertyKey.equals( HibernatePersistence.JACC_CONTEXT_ID ) + || propertyKey.equals( HibernatePersistence.JACC_ENABLED ) ) ) { + jaccKeys.add( propertyKey ); + } + } } + if ( preparedProperties.containsKey( HibernatePersistence.INTERCEPTOR ) + && ( cfg.getInterceptor() == null + || cfg.getInterceptor().equals( defaultInterceptor ) ) ) { + //cfg.setInterceptor has precedence over configuration file + String interceptorName = preparedProperties.getProperty( HibernatePersistence.INTERCEPTOR ); + try { + Class interceptor = classForName( interceptorName ); + cfg.setInterceptor( (Interceptor) interceptor.newInstance() ); + } + catch (ClassNotFoundException e) { + throw new PersistenceException( "Unable to find interceptor class: " + interceptorName, e ); + } + catch (IllegalAccessException e) { + throw new PersistenceException( + "Unable to access interceptor class: " + interceptorName, e + ); + } + catch (InstantiationException e) { + throw new PersistenceException( + "Unable to instanciate interceptor class: " + interceptorName, e + ); + } + catch (ClassCastException e) { + throw new PersistenceException( + "Interceptor class does not implement Interceptor interface: " + interceptorName, e + ); + } + } + if ( preparedProperties.containsKey( HibernatePersistence.NAMING_STRATEGY ) + && ( cfg.getNamingStrategy() == null + || cfg.getNamingStrategy().equals( defaultNamingStrategy ) ) ) { + //cfg.setNamingStrategy has precedence over configuration file + String namingStrategyName = preparedProperties.getProperty( HibernatePersistence.NAMING_STRATEGY ); + try { + Class namingStragegy = classForName( namingStrategyName ); + cfg.setNamingStrategy( (NamingStrategy) namingStragegy.newInstance() ); + } + catch (ClassNotFoundException e) { + throw new PersistenceException( + "Unable to find naming strategy class: " + namingStrategyName, e + ); + } + catch (IllegalAccessException e) { + throw new PersistenceException( + "Unable to access naming strategy class: " + namingStrategyName, e + ); + } + catch (InstantiationException e) { + throw new PersistenceException( + "Unable to instanciate naming strategy class: " + namingStrategyName, e + ); + } + catch (ClassCastException e) { + throw new PersistenceException( + "Naming strategyy class does not implement NmaingStrategy interface: " + namingStrategyName, + e + ); + } + } - cfg.addProperties( preparedProperties ); - if ( workingVars == null ) workingVars = CollectionHelper.EMPTY_MAP; + if ( jaccKeys.size() > 0 ) { + addSecurity( jaccKeys, preparedProperties ); + } + + //initialize listeners + listenerConfigurator.setProperties( preparedProperties ); + listenerConfigurator.setValidator( true ); + listenerConfigurator.configure(); + + //some spec compliance checking + //TODO centralize that? + if ( ! "true".equalsIgnoreCase( cfg.getProperty( Environment.AUTOCOMMIT ) ) ) { + log.warn( Environment.AUTOCOMMIT + " = false break the EJB3 specification" ); + } + boolean discardOnClose = preparedProperties.getProperty( HibernatePersistence.DISCARD_PC_ON_CLOSE).equals( "true"); + return buildEntityManagerFactory( + (PersistenceUnitTransactionType) workingVars.get(HibernatePersistence.TRANSACTION_TYPE), + discardOnClose + ); + } + + private void addClassesToSessionFactory(Map workingVars) { if ( workingVars.containsKey( HibernatePersistence.CLASS_NAMES ) ) { Collection<String> classNames = (Collection<String>) workingVars.get( HibernatePersistence.CLASS_NAMES @@ -497,114 +607,33 @@ cfg.addInputStream( is ); } } + } - //processes specific properties - List<String> jaccKeys = new ArrayList<String>(); + private Properties prepareProperties(Properties properties, Map workingVars ) { + Properties preparedProperties = new Properties(); - if ( properties != null ) { - Configuration defaultConf = new AnnotationConfiguration(); - Interceptor defaultInterceptor = defaultConf.getInterceptor(); - NamingStrategy defaultNamingStrategy = defaultConf.getNamingStrategy(); - - Iterator propertyIt = properties.keySet().iterator(); - while ( propertyIt.hasNext() ) { - Object uncastObject = propertyIt.next(); - //had to be safe - if ( uncastObject != null && uncastObject instanceof String ) { - String propertyKey = (String) uncastObject; - if ( propertyKey.startsWith( HibernatePersistence.CLASS_CACHE_PREFIX ) ) { - setCacheStrategy( propertyKey, properties, true ); - } - else if ( propertyKey.startsWith( HibernatePersistence.COLLECTION_CACHE_PREFIX ) ) { - setCacheStrategy( propertyKey, properties, false ); - } - else if ( propertyKey.startsWith( HibernatePersistence.JACC_PREFIX ) - && ! ( propertyKey.equals( HibernatePersistence.JACC_CONTEXT_ID ) - || propertyKey.equals( HibernatePersistence.JACC_ENABLED ) ) ) { - jaccKeys.add( propertyKey ); - } - else if ( propertyKey.equals( HibernatePersistence.INTERCEPTOR ) - && ( cfg.getInterceptor() == null - || cfg.getInterceptor().equals( defaultInterceptor ) ) ) { - //cfg.setInterceptor has precedence over configuration file - String interceptorName = properties.getProperty( HibernatePersistence.INTERCEPTOR ); - try { - Class interceptor = classForName( interceptorName ); - cfg.setInterceptor( (Interceptor) interceptor.newInstance() ); - } - catch (ClassNotFoundException e) { - throw new PersistenceException( "Unable to find interceptor class: " + interceptorName, e ); - } - catch (IllegalAccessException e) { - throw new PersistenceException( - "Unable to access interceptor class: " + interceptorName, e - ); - } - catch (InstantiationException e) { - throw new PersistenceException( - "Unable to instanciate interceptor class: " + interceptorName, e - ); - } - catch (ClassCastException e) { - throw new PersistenceException( - "Interceptor class does not implement Interceptor interface: " + interceptorName, e - ); - } - } - else if ( propertyKey.equals( HibernatePersistence.NAMING_STRATEGY ) - && ( cfg.getNamingStrategy() == null - || cfg.getNamingStrategy().equals( defaultNamingStrategy ) ) ) { - //cfg.setNamingStrategy has precedence over configuration file - String namingStrategyName = properties.getProperty( HibernatePersistence.NAMING_STRATEGY ); - try { - Class namingStragegy = classForName( namingStrategyName ); - cfg.setNamingStrategy( (NamingStrategy) namingStragegy.newInstance() ); - } - catch (ClassNotFoundException e) { - throw new PersistenceException( - "Unable to find naming strategy class: " + namingStrategyName, e - ); - } - catch (IllegalAccessException e) { - throw new PersistenceException( - "Unable to access naming strategy class: " + namingStrategyName, e - ); - } - catch (InstantiationException e) { - throw new PersistenceException( - "Unable to instanciate naming strategy class: " + namingStrategyName, e - ); - } - catch (ClassCastException e) { - throw new PersistenceException( - "Naming strategyr class does not implement NmaingStrategy interface: " + namingStrategyName, - e - ); - } - } - } - } + //defaults different to Hibernate + preparedProperties.setProperty( Environment.RELEASE_CONNECTIONS, "auto" ); + //settings that always apply to a compliant EJB3 + preparedProperties.setProperty( Environment.AUTOCOMMIT, "true" ); + preparedProperties.setProperty( Environment.USE_IDENTIFIER_ROLLBACK, "false" ); + preparedProperties.setProperty( Environment.FLUSH_BEFORE_COMPLETION, "true" ); + preparedProperties.setProperty( HibernatePersistence.DISCARD_PC_ON_CLOSE, "false" ); + PersistenceUnitTransactionType type = (PersistenceUnitTransactionType) + workingVars.get(HibernatePersistence.TRANSACTION_TYPE); + if (type == PersistenceUnitTransactionType.JTA) { + preparedProperties.setProperty( Environment.TRANSACTION_STRATEGY, JTATransactionFactory.class.getName() ); } - - if ( jaccKeys.size() > 0 ) { - addSecurity( jaccKeys, properties ); + else if (type == PersistenceUnitTransactionType.RESOURCE_LOCAL) { + preparedProperties.setProperty( Environment.TRANSACTION_STRATEGY, JDBCTransactionFactory.class.getName() ); } + else { + new AssertionFailure("Unknown PersisntenceUnitTransactionType: " + type); + } - //initialize listeners - listenerConfigurator.setProperties( preparedProperties ); - listenerConfigurator.setValidator( true ); - listenerConfigurator.configure(); - - //some spec compliance checking - //TODO centralize that? - if ( ! "true".equalsIgnoreCase( cfg.getProperty( Environment.AUTOCOMMIT ) ) ) { - log.warn( Environment.AUTOCOMMIT + " = false break the EJB3 specification" ); - } - boolean discardOnClose = properties.getProperty( HibernatePersistence.DISCARD_PC_ON_CLOSE, "false" ).equals( "true"); - return buildEntityManagerFactory( - (PersistenceUnitTransactionType) workingVars.get(HibernatePersistence.TRANSACTION_TYPE), - discardOnClose - ); + //override the new defaults with the user defined ones + if ( properties != null ) preparedProperties.putAll( properties ); + return preparedProperties; } private Class classForName( |