From d67a96e813c16a363c7882e27aa773894b97bd83 Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Mon, 6 May 2013 19:56:13 -0400 Subject: [PATCH] HHH-8225 EMF cannot be created, closed, then re-created in OSGi --- .../org/hibernate/osgi/OsgiClassLoader.java | 19 +++++++++---------- .../osgi/OsgiPersistenceProvider.java | 4 ++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiClassLoader.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiClassLoader.java index d8babd732c..a748770e2c 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiClassLoader.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiClassLoader.java @@ -27,9 +27,11 @@ import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; +import java.util.Set; import org.osgi.framework.Bundle; @@ -41,12 +43,13 @@ import org.osgi.framework.Bundle; * @author Tim Ward */ public class OsgiClassLoader extends ClassLoader { - private List classLoaders = new ArrayList(); - private List bundles = new ArrayList(); + // Leave these as Sets -- addClassLoader or addBundle may be called more + // than once if a SF or EMF is closed and re-created. + private Set classLoaders = new HashSet(); + private Set bundles = new HashSet(); private Map> classCache = new HashMap>(); private Map resourceCache = new HashMap(); - private Map> resourceListCache = new HashMap>(); /** * Load the class and break on first found match. @@ -128,16 +131,15 @@ public class OsgiClassLoader extends ClassLoader { /** * Load the class and break on first found match. + * + * Note: Since they're Enumerations, do not cache these results! + * * TODO: Should this throw a different exception or warn if multiple * classes were found? Naming collisions can and do happen in OSGi... */ @Override @SuppressWarnings("unchecked") protected Enumeration findResources(String name) { - if ( resourceListCache.containsKey( name ) ) { - return resourceListCache.get( name ); - } - final List> enumerations = new ArrayList>(); for ( Bundle bundle : bundles ) { @@ -184,8 +186,6 @@ public class OsgiClassLoader extends ClassLoader { } }; - resourceListCache.put( name, aggEnumeration ); - return aggEnumeration; } @@ -213,7 +213,6 @@ public class OsgiClassLoader extends ClassLoader { public void clear() { classCache.clear(); resourceCache.clear(); - resourceListCache.clear(); } } diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProvider.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProvider.java index c99715b8e5..afb8503e06 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProvider.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProvider.java @@ -90,7 +90,7 @@ public class OsgiPersistenceProvider extends HibernatePersistenceProvider { osgiClassLoader.addBundle( requestingBundle ); - return super.createEntityManagerFactory( persistenceUnitName, properties ); + return super.createEntityManagerFactory( persistenceUnitName, settings ); } @Override @@ -106,7 +106,7 @@ public class OsgiPersistenceProvider extends HibernatePersistenceProvider { osgiClassLoader.addClassLoader( info.getClassLoader() ); - return super.createContainerEntityManagerFactory( info, properties ); + return super.createContainerEntityManagerFactory( info, settings ); } @SuppressWarnings("unchecked")