[hibernate-commits] Hibernate SVN: r10266 - trunk/HibernateExt/ejb/src/java/org/hibernate/ejb

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Aug 15 13:47:45 EDT 2006


Author: epbernard
Date: 2006-08-15 13:47:43 -0400 (Tue, 15 Aug 2006)
New Revision: 10266

Modified:
   trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/Ejb3Configuration.java
   trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/HibernatePersistence.java
Log:
EJB-154 align with the Configuration model, ie configure(...).buildEntityManagerFactory()
ejb3Configration.createEntityManagerFactory() will be removed

Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/Ejb3Configuration.java
===================================================================
--- trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/Ejb3Configuration.java	2006-08-15 17:03:35 UTC (rev 10265)
+++ trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/Ejb3Configuration.java	2006-08-15 17:47:43 UTC (rev 10266)
@@ -75,6 +75,7 @@
  */
 public class Ejb3Configuration implements Serializable {
 	private static final String IMPLEMENTATION_NAME = HibernatePersistence.class.getName();
+	private static final String META_INF_ORM_XML = "META-INF/orm.xml";
 	private static Log log = LogFactory.getLog( Ejb3Configuration.class );
 
 	static {
@@ -85,8 +86,10 @@
 	private SettingsFactory settingsFactory;
 	private EventListenerConfigurator listenerConfigurator;
 	private PersistenceUnitTransactionType transactionType;
-	private static final String META_INF_ORM_XML = "META-INF/orm.xml";
+	private boolean discardOnClose;
+	private ClassLoader overridenClassLoader;
 
+
 	public Ejb3Configuration() {
 		settingsFactory = new InjectionSettingsFactory();
 		cfg = new AnnotationConfiguration( settingsFactory );
@@ -97,7 +100,6 @@
 			}
 		} );
 		listenerConfigurator = new EventListenerConfigurator( this );
-		//transactionType = PersistenceUnitTransactionType.JTA; //default as per the spec
 	}
 
 	/**
@@ -116,8 +118,9 @@
 
 	/**
 	 * create a factory from a parsed persistence.xml
+	 * Especially the scanning of classes and additional jars is done already at this point.
 	 */
-	private EntityManagerFactory createFactory(PersistenceMetadata metadata, Map overrides) {
+	private Ejb3Configuration configure(PersistenceMetadata metadata, Map overrides) {
 		log.debug( "Creating Factory: " + metadata.getName() );
 
 		Map workingVars = new HashMap();
@@ -145,21 +148,23 @@
 		Properties props = new Properties();
 		props.putAll( metadata.getProps() );
 		if ( overrides != null ) props.putAll( overrides ); //yuk!
-		return createEntityManagerFactory( props, workingVars );
+		configure( props, workingVars );
+		return this;
 	}
 
 	/**
-	 * Get an entity manager factory by its entity manager name and given the
+	 * Build the configuration from an entity manager name and given the
 	 * appropriate extra properties. Those properties override the one get through
 	 * the peristence.xml file.
+	 * If the persistence unit name is not found or does not match the Persistence Provider, null is returned
 	 *
-	 * @param emName	  entity manager name
+	 * @param persistenceUnitName persistence unit name
 	 * @param integration properties passed to the persistence provider
-	 * @return initialized EntityManagerFactory
+	 * @return configured Ejb3Configuration or null if no persistence unit match
 	 */
-	public EntityManagerFactory createEntityManagerFactory(String emName, Map integration) {
+	public Ejb3Configuration configure(String persistenceUnitName, Map integration) {
 		try {
-			log.debug( "Trying to find persistence unit: " + emName );
+			log.debug( "Look up for persistence unit: " + persistenceUnitName );
 			integration = integration == null ?
 					integration = CollectionHelper.EMPTY_MAP :
 					Collections.unmodifiableMap( integration );
@@ -180,7 +185,6 @@
 					if ( metadata.getProvider() == null || IMPLEMENTATION_NAME.equalsIgnoreCase(
 							metadata.getProvider()
 					) ) {
-						log.trace( "Archive to be processed by hibernate Entity Manager implementation found" );
 						//correct provider
 						URL jarURL = JarVisitor.getJarURLFromURLEntry( url, "/META-INF/persistence.xml" );
 						JarVisitor.Filter[] persistenceXmlFilter = getFilters( metadata, integration, metadata.getExcludeUnlistedClasses() );
@@ -188,20 +192,17 @@
 						if ( metadata.getName() == null ) {
 							metadata.setName( visitor.getUnqualifiedJarName() );
 						}
-						if ( log.isTraceEnabled() ) log.trace( "Persistence unit name: " + metadata.getName() );
-
-						log.trace( "emname:" + emName + " metadata: " + metadata.getName() );
-						if ( emName == null && xmls.hasMoreElements() ) {
+						if ( persistenceUnitName == null && xmls.hasMoreElements() ) {
 							throw new PersistenceException( "No name provided and several persistence units found" );
 						}
-						else if ( emName == null || metadata.getName().equals( emName ) ) {
+						else if ( persistenceUnitName == null || metadata.getName().equals( persistenceUnitName ) ) {
 							addMetadataFromVisitor( visitor, metadata );
 							JarVisitor.Filter[] otherXmlFilter = getFilters( metadata, integration, false );
 							for ( String jarFile : metadata.getJarFiles() ) {
 								visitor = JarVisitor.getVisitor( jarFile, otherXmlFilter );
 								addMetadataFromVisitor( visitor, metadata );
 							}
-							return createFactory( metadata, integration );
+							return configure( metadata, integration );
 						}
 					}
 				}
@@ -244,9 +245,10 @@
 	}
 
 	/**
-	 * Create a factory from a PersistenceInfo object
+	 * Process configuration from a PersistenceUnitInfo object
+	 * Typically called by the container
 	 */
-	public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map integration) {
+	public Ejb3Configuration configure(PersistenceUnitInfo info, Map integration) {
 		if ( log.isDebugEnabled() ) {
 			log.debug( "Processing " + LogHelper.logPersistenceUnitInfo( info ) );
 		}
@@ -269,10 +271,14 @@
 		//set the classloader
 		Thread thread = Thread.currentThread();
 		ClassLoader contextClassLoader = thread.getContextClassLoader();
-		if ( ! info.getClassLoader().equals( contextClassLoader ) ) {
-			thread.setContextClassLoader( info.getClassLoader() );
+		boolean sameClassLoader = info.getClassLoader().equals( contextClassLoader );
+		if ( ! sameClassLoader ) {
+			overridenClassLoader = info.getClassLoader();
+			thread.setContextClassLoader( overridenClassLoader );
 		}
-		EntityManagerFactory entityManagerFactory;
+		else {
+			overridenClassLoader = null;
+		}
 
 		try {
 			Map workingVars = new HashMap();
@@ -345,15 +351,15 @@
 				);
 			}
 
-			entityManagerFactory = createEntityManagerFactory( properties, workingVars );
+			configure( properties, workingVars );
 		}
 		finally {
 			//After EMF, set the CCL back
-			if ( ! info.getClassLoader().equals( contextClassLoader ) ) {
+			if ( ! sameClassLoader ) {
 				thread.setContextClassLoader( contextClassLoader );
 			}
 		}
-		return entityManagerFactory;
+		return this;
 	}
 
 	private void addXMLEntities(List<String> xmlFiles, PersistenceUnitInfo info, List<String> entities) {
@@ -615,6 +621,7 @@
 	 * HibernatePersistence.LOADED_CLASSES -> Collection<Class> (list of loaded classes)
 	 * <p/>
 	 * <b>Used by JBoss AS only</b>
+	 * @deprecated use the Java Persistence API
 	 */
 	// This is used directly by JBoss so don't remove until further notice.  bill at jboss.org
 	public EntityManagerFactory createEntityManagerFactory(Map workingVars) {
@@ -627,28 +634,49 @@
 			props.remove( HibernatePersistence.HBXML_FILES );
 			props.remove( HibernatePersistence.LOADED_CLASSES );
 		}
-		return createEntityManagerFactory( props, workingVars );
+		configure( props, workingVars );
+		return buildEntityManagerFactory();
 	}
 
 	/**
-	 * Create an EntityManagerFactory <b>when</b> the configuration is ready
+	 * Process configuration and build an EntityManagerFactory <b>when</b> the configuration is ready
+	 * @deprecated
 	 */
 	public EntityManagerFactory createEntityManagerFactory() {
-		return createEntityManagerFactory( cfg.getProperties(), new HashMap() );
+		configure( cfg.getProperties(), new HashMap() );
+		return buildEntityManagerFactory();
 	}
 
-	private EntityManagerFactory buildEntityManagerFactory(boolean discardOnClose) {
-		return new EntityManagerFactoryImpl(
-				cfg.buildSessionFactory(),
-				transactionType,
-				discardOnClose
-		);
+	public EntityManagerFactory buildEntityManagerFactory() {
+		Thread thread = null;
+		ClassLoader contextClassLoader = null;
+		if (overridenClassLoader != null) {
+			thread = Thread.currentThread();
+			contextClassLoader = thread.getContextClassLoader();
+			thread.setContextClassLoader( overridenClassLoader );
+		}
+		try {
+			return new EntityManagerFactoryImpl(
+					cfg.buildSessionFactory(),
+					transactionType,
+					discardOnClose
+			);
+			}
+		catch (HibernateException e) {
+			throw new PersistenceException( e );
+		}
+		finally {
+			if (thread != null) {
+				thread.setContextClassLoader( contextClassLoader );
+			}
+		}
 	}
 
 	/**
 	 * create a factory from a canonical workingVars map and the overriden properties
+	 *
 	 */
-	private EntityManagerFactory createEntityManagerFactory(
+	private Ejb3Configuration configure(
 			Properties properties, Map workingVars
 	) {
 		Properties preparedProperties = prepareProperties( properties, workingVars );
@@ -765,9 +793,9 @@
 		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 )
+		discardOnClose = preparedProperties.getProperty( HibernatePersistence.DISCARD_PC_ON_CLOSE )
 				.equals( "true" );
-		return buildEntityManagerFactory( discardOnClose );
+		return this;
 	}
 
 	private void addClassesToSessionFactory(Map workingVars) {
@@ -984,7 +1012,19 @@
 
 
 	public Settings buildSettings() throws HibernateException {
-		return settingsFactory.buildSettings( cfg.getProperties() );
+		Thread thread = null;
+		ClassLoader contextClassLoader = null;
+		if (overridenClassLoader != null) {
+			thread = Thread.currentThread();
+			contextClassLoader = thread.getContextClassLoader();
+			thread.setContextClassLoader( overridenClassLoader );
+		}
+		try {
+			return settingsFactory.buildSettings( cfg.getProperties() );
+		}
+		finally {
+			if (thread != null) thread.setContextClassLoader( contextClassLoader );
+		}
 	}
 
 	public Ejb3Configuration addProperties(Properties props) {
@@ -993,41 +1033,139 @@
 	}
 
 	public Ejb3Configuration addAnnotatedClass(Class persistentClass) throws MappingException {
-		cfg.addAnnotatedClass( persistentClass );
-		return this;
+		Thread thread = null;
+		ClassLoader contextClassLoader = null;
+		if (overridenClassLoader != null) {
+			thread = Thread.currentThread();
+			contextClassLoader = thread.getContextClassLoader();
+			thread.setContextClassLoader( overridenClassLoader );
+		}
+		try {
+			cfg.addAnnotatedClass( persistentClass );
+			return this;
+		}
+		finally {
+			if (thread != null) thread.setContextClassLoader( contextClassLoader );
+		}
 	}
 
 	public Ejb3Configuration configure(String resource) throws HibernateException {
-		cfg.configure( resource );
-		return this;
+		Thread thread = null;
+		ClassLoader contextClassLoader = null;
+		if (overridenClassLoader != null) {
+			thread = Thread.currentThread();
+			contextClassLoader = thread.getContextClassLoader();
+			thread.setContextClassLoader( overridenClassLoader );
+		}
+		try {
+			Properties properties = new Properties();
+			properties.setProperty( HibernatePersistence.CFG_FILE, resource);
+			configure( properties, new HashMap() );
+			return this;
+		}
+		finally {
+			if (thread != null) thread.setContextClassLoader( contextClassLoader );
+		}
 	}
 
 	public Ejb3Configuration addPackage(String packageName) throws MappingException {
-		cfg.addPackage( packageName );
-		return this;
+		Thread thread = null;
+		ClassLoader contextClassLoader = null;
+		if (overridenClassLoader != null) {
+			thread = Thread.currentThread();
+			contextClassLoader = thread.getContextClassLoader();
+			thread.setContextClassLoader( overridenClassLoader );
+		}
+		try {
+			cfg.addPackage( packageName );
+			return this;
+		}
+		finally {
+			if (thread != null) thread.setContextClassLoader( contextClassLoader );
+		}
 	}
 
 	public Ejb3Configuration addFile(String xmlFile) throws MappingException {
-		cfg.addFile( xmlFile );
-		return this;
+		Thread thread = null;
+		ClassLoader contextClassLoader = null;
+		if (overridenClassLoader != null) {
+			thread = Thread.currentThread();
+			contextClassLoader = thread.getContextClassLoader();
+			thread.setContextClassLoader( overridenClassLoader );
+		}
+		try {
+			cfg.addFile( xmlFile );
+			return this;
+		}
+		finally {
+			if (thread != null) thread.setContextClassLoader( contextClassLoader );
+		}
 	}
 
 	public Ejb3Configuration addClass(Class persistentClass) throws MappingException {
-		cfg.addClass( persistentClass );
-		return this;
+		Thread thread = null;
+		ClassLoader contextClassLoader = null;
+		if (overridenClassLoader != null) {
+			thread = Thread.currentThread();
+			contextClassLoader = thread.getContextClassLoader();
+			thread.setContextClassLoader( overridenClassLoader );
+		}
+		try {
+			cfg.addClass( persistentClass );
+			return this;
+		}
+		finally {
+			if (thread != null) thread.setContextClassLoader( contextClassLoader );
+		}
 	}
 
 	public Ejb3Configuration addFile(File xmlFile) throws MappingException {
-		cfg.addFile( xmlFile );
-		return this;
+		Thread thread = null;
+		ClassLoader contextClassLoader = null;
+		if (overridenClassLoader != null) {
+			thread = Thread.currentThread();
+			contextClassLoader = thread.getContextClassLoader();
+			thread.setContextClassLoader( overridenClassLoader );
+		}
+		try {
+			cfg.addFile( xmlFile );
+			return this;
+		}
+		finally {
+			if (thread != null) thread.setContextClassLoader( contextClassLoader );
+		}
 	}
 
 	public void buildMappings() {
-		cfg.buildMappings();
+		Thread thread = null;
+		ClassLoader contextClassLoader = null;
+		if (overridenClassLoader != null) {
+			thread = Thread.currentThread();
+			contextClassLoader = thread.getContextClassLoader();
+			thread.setContextClassLoader( overridenClassLoader );
+		}
+		try {
+			cfg.buildMappings();
+		}
+		finally {
+			if (thread != null) thread.setContextClassLoader( contextClassLoader );
+		}
 	}
 
 	public Iterator getClassMappings() {
-		return cfg.getClassMappings();
+		Thread thread = null;
+		ClassLoader contextClassLoader = null;
+		if (overridenClassLoader != null) {
+			thread = Thread.currentThread();
+			contextClassLoader = thread.getContextClassLoader();
+			thread.setContextClassLoader( overridenClassLoader );
+		}
+		try {
+			return cfg.getClassMappings();
+		}
+		finally {
+			if (thread != null) thread.setContextClassLoader( contextClassLoader );
+		}
 	}
 
 	public EventListeners getEventListeners() {
@@ -1117,13 +1255,37 @@
 	}
 
 	public Ejb3Configuration addInputStream(InputStream xmlInputStream) throws MappingException {
-		cfg.addInputStream( xmlInputStream );
-		return this;
+		Thread thread = null;
+		ClassLoader contextClassLoader = null;
+		if (overridenClassLoader != null) {
+			thread = Thread.currentThread();
+			contextClassLoader = thread.getContextClassLoader();
+			thread.setContextClassLoader( overridenClassLoader );
+		}
+		try {
+			cfg.addInputStream( xmlInputStream );
+			return this;
+		}
+		finally {
+			if (thread != null) thread.setContextClassLoader( contextClassLoader );
+		}
 	}
 
 	public Ejb3Configuration addResource(String path) throws MappingException {
-		cfg.addResource( path );
-		return this;
+		Thread thread = null;
+		ClassLoader contextClassLoader = null;
+		if (overridenClassLoader != null) {
+			thread = Thread.currentThread();
+			contextClassLoader = thread.getContextClassLoader();
+			thread.setContextClassLoader( overridenClassLoader );
+		}
+		try {
+			cfg.addResource( path );
+			return this;
+		}
+		finally {
+			if (thread != null) thread.setContextClassLoader( contextClassLoader );
+		}
 	}
 
 	public Ejb3Configuration addResource(String path, ClassLoader classLoader) throws MappingException {

Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/HibernatePersistence.java
===================================================================
--- trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/HibernatePersistence.java	2006-08-15 17:03:35 UTC (rev 10265)
+++ trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/HibernatePersistence.java	2006-08-15 17:47:43 UTC (rev 10266)
@@ -111,16 +111,19 @@
 	 */
 	public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map overridenProperties) {
 		Ejb3Configuration cfg = new Ejb3Configuration();
-		return cfg.createEntityManagerFactory( persistenceUnitName, overridenProperties );
+		cfg.configure( persistenceUnitName, overridenProperties );
+		return cfg.buildEntityManagerFactory();
 	}
 
 	public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) {
 		Ejb3Configuration cfg = new Ejb3Configuration();
-		return cfg.createContainerEntityManagerFactory( info, map );
+		cfg.configure( info, map );
+		return cfg.buildEntityManagerFactory();
 	}
 
 	/**
 	 * create a factory from a canonical version
+	 * @deprecated
 	 */
 	// This is used directly by JBoss so don't remove until further notice.  bill at jboss.org
 	public EntityManagerFactory createEntityManagerFactory(Map properties) {




More information about the hibernate-commits mailing list