geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dwo...@apache.org
Subject svn commit: r797072 - /geronimo/specs/trunk/geronimo-jpa_2.0_spec/src/main/java/javax/persistence/Persistence.java
Date Thu, 23 Jul 2009 13:55:08 GMT
Author: dwoods
Date: Thu Jul 23 13:55:08 2009
New Revision: 797072

URL: http://svn.apache.org/viewvc?rev=797072&view=rev
Log:
GERONIMO-4410 - OPENJPA-1076 Update createEntityManagerFactory() to use PersistenceProviderResolver
and PersistenceProviderResolverHolder

Modified:
    geronimo/specs/trunk/geronimo-jpa_2.0_spec/src/main/java/javax/persistence/Persistence.java

Modified: geronimo/specs/trunk/geronimo-jpa_2.0_spec/src/main/java/javax/persistence/Persistence.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-jpa_2.0_spec/src/main/java/javax/persistence/Persistence.java?rev=797072&r1=797071&r2=797072&view=diff
==============================================================================
--- geronimo/specs/trunk/geronimo-jpa_2.0_spec/src/main/java/javax/persistence/Persistence.java
(original)
+++ geronimo/specs/trunk/geronimo-jpa_2.0_spec/src/main/java/javax/persistence/Persistence.java
Thu Jul 23 13:55:08 2009
@@ -24,17 +24,15 @@
 //
 package javax.persistence;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.URL;
 import java.util.Collections;
-import java.util.Enumeration;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import javax.persistence.spi.PersistenceProvider;
+import javax.persistence.spi.PersistenceProviderResolver;
+import javax.persistence.spi.PersistenceProviderResolverHolder;
 
 /**
  * @version $Rev$ $Date$
@@ -43,6 +41,8 @@
 /**
  * Bootstrap class that is used to obtain {@link javax.persistence.EntityManagerFactory}
  * references.
+ * 
+ * Contains Geronimo implemented code as required by the JPA spec.
  */
 public class Persistence {
 
@@ -78,113 +78,83 @@
      *         specified persistence unit.
      */
     public static EntityManagerFactory createEntityManagerFactory(
-            String persistenceUnitName,
-            Map properties) {
-
+            String persistenceUnitName, Map properties) {
+        EntityManagerFactory factory = null;
         if (properties == null) {
             properties = Collections.EMPTY_MAP;
         }
 
-        // start by loading a provider explicitly specified in properties. The spec
-        // doesn't seem to forbid providers that are not deployed as a service
+        /*
+         * Geronimo/OpenJPA unique behavior - Start by loading a provider
+         * explicitly specified in properties. The spec doesn't seem to forbid
+         *  providers that are not deployed as a service.
+         */
         Object providerName = properties.get(PERSISTENCE_PROVIDER_PROPERTY);
         if (providerName instanceof String) {
-            EntityManagerFactory factory = createFactory(
+            factory = createFactory(
                     providerName.toString(),
                     persistenceUnitName,
                     properties);
-            if (factory != null) {
-                return factory;
-            }
         }
 
-        // load correctly deployed providers
-        ClassLoader loader = Thread.currentThread().getContextClassLoader();
-        try {
-            Enumeration<URL> providers = loader
-                    .getResources(PERSISTENCE_PROVIDER_SERVICE);
-            while (providers.hasMoreElements()) {
-
-                String name = getProviderName(providers.nextElement());
-
-                if (name != null) {
-
-                    EntityManagerFactory factory = createFactory(
-                            name,
-                            persistenceUnitName,
-                            properties);
-
-                    if (factory != null) {
-                        return factory;
-                    }
+        if (factory == null) {
+            /*
+             * Now, the default behavior of loading a provider from our resolver
+             */
+            PersistenceProviderResolver resolver =
+                PersistenceProviderResolverHolder.getPersistenceProviderResolver();
+            List<PersistenceProvider> providers = resolver.getPersistenceProviders();
+            for (PersistenceProvider provider : providers) {
+                factory = provider.createEntityManagerFactory(
+                    persistenceUnitName, properties);
+                if (factory != null) {
+                    break;
                 }
             }
         }
-        catch (IOException e) {
-            // spec doesn't mention any exceptions thrown by this method
-        }
 
-        return null;
+        // spec doesn't mention any exceptions thrown by this method if no emf
+        return factory;
     }
 
-    static String getProviderName(URL url) throws IOException {
-
-        BufferedReader in = new BufferedReader(new InputStreamReader(
-                url.openStream(),
-                "UTF-8"));
-
-        String providerName;
-
-        try {
-            providerName = in.readLine();
-        }
-        finally {
-            in.close();
-        }
-
-        if (providerName != null) {
-            providerName = providerName.trim();
-        }
-
-        return providerName;
-    }
-
-    static EntityManagerFactory createFactory(
+    /*
+     * Geronimo/OpenJPA private helper code for PERSISTENCE_PROVIDER_PROPERTY
+     */
+    private static EntityManagerFactory createFactory(
             String providerName,
             String persistenceUnitName,
             Map properties)
             throws PersistenceException {
 
-        Class providerClass;
+        Class<?> providerClass;
+        ClassLoader cl = Thread.currentThread().getContextClassLoader();
+        if (cl == null)
+            cl = Persistence.class.getClassLoader();
         try {
-            providerClass = Class.forName(providerName, true, Thread
-                    .currentThread().getContextClassLoader());
+            providerClass = Class.forName(providerName, true, cl);
+        } catch (Exception e) {
+            throw new PersistenceException("Invalid or inaccessible provider class: " + providerName,
e);
         }
-        catch (Exception e) {
-            throw new PersistenceException(
-                    "Invalid or inaccessible provider class: " + providerName,
-                    e);
-        }
-
         try {
-            PersistenceProvider provider = (PersistenceProvider) providerClass
-                    .newInstance();
-            return provider.createEntityManagerFactory(persistenceUnitName,
-                    properties);
-        }
-        catch (Exception e) {
-            throw new PersistenceException("Provider error. Provider: "
-                    + providerName, e);
+            PersistenceProvider provider = (PersistenceProvider) providerClass.newInstance();
+            return provider.createEntityManagerFactory(persistenceUnitName, properties);
+        } catch (Exception e) {
+            throw new PersistenceException("Provider error. Provider: " + providerName, e);
         }
     }
     
     /*
     * @return PersistenceUtil instance
+    * @since 2.0
     */
     public static PersistenceUtil getPersistenceUtil() {
-        // TODO: OPENJPA-1076 - Required by JSR-317.  
-        // Dummy impl for Bean Validation testing.
-        return new PersistenceUtil() {
+        return new PersistenceUtilImpl();
+    }
+    
+    /*
+     * Geronimo implementation specific code
+     */
+    private static class PersistenceUtilImpl implements PersistenceUtil {
 
             public boolean isLoaded(Object entity, String attributeName) {
                 // TODO Auto-generated dummy method stub
@@ -195,6 +165,6 @@
                 // TODO Auto-generated dummy method stub
                 return true;
             }
-        };
     }
+    
 }



Mime
View raw message