openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From awh...@apache.org
Subject svn commit: r448025 - in /incubator/openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/conf/ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/ openjpa-kernel/src/main/java/org/apache/openjpa/conf/ openjpa-kernel/src/main/java/or...
Date Wed, 20 Sep 2006 01:29:56 GMT
Author: awhite
Date: Tue Sep 19 18:29:55 2006
New Revision: 448025

URL: http://svn.apache.org/viewvc?view=rev&rev=448025
Log:
Review/cleanup of product derivation work.


Added:
    incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryValue.java
Removed:
    incubator/openjpa/trunk/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/ConfigurationTestConfigurationProvider.java
    incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/ConfigurationProviderImpl.java
Modified:
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/conf/JDBCProductDerivation.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/AbstractUpdateManager.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/ClassTableJDBCSeq.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAProductDerivation.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PCState.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PDeletedState.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PDirtyState.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PNewState.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ImplHelper.java
    incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/AbstractProductDerivation.java
    incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationImpl.java
    incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationProvider.java
    incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/Configurations.java
    incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/MapConfigurationProvider.java
    incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ProductDerivation.java
    incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ProductDerivations.java
    incubator/openjpa/trunk/openjpa-lib/src/main/resources/org/apache/openjpa/lib/conf/localizer.properties
    incubator/openjpa/trunk/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/ConfigurationTestProductDerivation.java
    incubator/openjpa/trunk/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/TestConfigurationImpl.java
    incubator/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/JDBCPersistenceProductDerivation.java
    incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
    incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistence.java
    incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
    incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
    incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/conf/JDBCProductDerivation.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/conf/JDBCProductDerivation.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/conf/JDBCProductDerivation.java (original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/conf/JDBCProductDerivation.java Tue Sep 19 18:29:55 2006
@@ -45,8 +45,4 @@
         }
         return false;
     }
-    
-    public ConfigurationProvider newConfigurationProvider() {
-        return null;
-    }
 }

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/AbstractUpdateManager.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/AbstractUpdateManager.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/AbstractUpdateManager.java (original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/AbstractUpdateManager.java Tue Sep 19 18:29:55 2006
@@ -68,10 +68,7 @@
                 conn);
             return flush(states, store, psMgr);
         } finally {
-            try {
-                conn.close();
-            } catch (SQLException se) {
-            }
+            try { conn.close(); } catch (SQLException se) {}
         }
     }
 
@@ -141,7 +138,6 @@
         Collection customs) {
         try {
             BitSet dirty;
-
             if (sm.getPCState() == PCState.PNEW && !sm.isFlushed()) {
                 insert(sm, (ClassMapping) sm.getMetaData(), rowMgr, store,
                     customs);
@@ -156,10 +152,10 @@
                 updateIndicators(sm, (ClassMapping) sm.getMetaData(), rowMgr,
                     store, customs, true);
             } else if (sm.isVersionCheckRequired()) {
-                if (!((ClassMapping) sm.getMetaData()).getVersion()
-                    .checkVersion(sm, store, false))
-                    exceps = addException(exceps, new OptimisticException(sm
-                        .getManagedInstance()));
+                if (!((ClassMapping) sm.getMetaData()).getVersion().
+                    checkVersion(sm, store, false))
+                    exceps = addException(exceps, new OptimisticException(sm.
+                        getManagedInstance()));
             }
         } catch (SQLException se) {
             exceps = addException(exceps, SQLExceptions.getStore(se, dict));

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/ClassTableJDBCSeq.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/ClassTableJDBCSeq.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/ClassTableJDBCSeq.java (original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/ClassTableJDBCSeq.java Tue Sep 19 18:29:55 2006
@@ -29,7 +29,6 @@
 import org.apache.openjpa.jdbc.schema.Table;
 import org.apache.openjpa.jdbc.sql.DBDictionary;
 import org.apache.openjpa.lib.conf.Configurations;
-import org.apache.openjpa.lib.conf.ProductDerivations;
 import org.apache.openjpa.lib.meta.ClassArgParser;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.lib.util.Options;

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAProductDerivation.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAProductDerivation.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAProductDerivation.java (original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAProductDerivation.java Tue Sep 19 18:29:55 2006
@@ -18,14 +18,14 @@
 import org.apache.openjpa.lib.conf.ProductDerivation;
 
 /**
- * 
  * Adds datastore based extension to ProductDerivation.  
  *
  * @since 0.4.1
  * @author Pinaki Poddar
- *
  */
 public interface OpenJPAProductDerivation extends ProductDerivation {
+
+    public static final int TYPE_SPEC = 0;
     public static final int TYPE_STORE = 200;
     public static final int TYPE_SPEC_STORE = 300;
     public static final int TYPE_PRODUCT_STORE = 400;

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PCState.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PCState.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PCState.java (original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PCState.java Tue Sep 19 18:29:55 2006
@@ -403,6 +403,14 @@
     }
 
     /**
+     * Whether this state requires a version check when being flushed, 
+     * assuming the system is configured for version checks.
+     */
+    boolean isVersionCheckRequired(StateManagerImpl context) {
+        return false;
+    }
+
+    /**
      * Throw an error with a localized message identified by the given key.
      */
     PCState error(String key, StateManagerImpl context) {

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PDeletedState.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PDeletedState.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PDeletedState.java (original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PDeletedState.java Tue Sep 19 18:29:55 2006
@@ -69,6 +69,10 @@
         return error("deleted", context);
     }
 
+    boolean isVersionCheckRequired(StateManagerImpl context) {
+        return true;
+    }
+
     boolean isTransactional() {
         return true;
     }

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PDirtyState.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PDirtyState.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PDirtyState.java (original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PDirtyState.java Tue Sep 19 18:29:55 2006
@@ -64,6 +64,10 @@
         return error("dirty", context);
     }
 
+    boolean isVersionCheckRequired(StateManagerImpl context) {
+        return !context.isFlushed() || context.isFlushedDirty(); 
+    }
+
     PCState afterRefresh() {
         return PCLEAN;
     }

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PNewState.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PNewState.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PNewState.java (original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PNewState.java Tue Sep 19 18:29:55 2006
@@ -68,6 +68,10 @@
         return error("new", context);
     }
 
+    boolean isVersionCheckRequired(StateManagerImpl context) {
+        return context.isFlushedDirty(); 
+    }
+
     boolean isTransactional() {
         return true;
     }

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java (original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java Tue Sep 19 18:29:55 2006
@@ -2927,23 +2927,14 @@
      * Returns whether this instance needs a version check.
      */
     public boolean isVersionCheckRequired() {
-
         // explicit flag for version check
-        if ((_flags & FLAG_VERSION_CHECK) > 0)
+        if ((_flags & FLAG_VERSION_CHECK) != 0)
             return true;
 
-        // need to check version if we have any dirty fields, unless we
-        // are in a datastore transaction
-        if (ImplHelper.getUpdateFields(this) != null) {
-            if (_broker.getOptimistic()) {
-                return true;
-            } else {
-                return _broker.getConfiguration().
-                    getCompatibilityInstance().getNonOptimisticVersionCheck();
-            }
-        }
-
-        return false;
+        if (!_broker.getOptimistic() && !_broker.getConfiguration().
+            getCompatibilityInstance().getNonOptimisticVersionCheck())
+            return false;
+        return _state.isVersionCheckRequired(this);
     }
 
     /**

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java (original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java Tue Sep 19 18:29:55 2006
@@ -143,7 +143,7 @@
     private Boolean _interface = null;
     private Class _impl = null;
     private List _interfaces = null;
-    private Map _ifaceMap = new HashMap();
+    private final Map _ifaceMap = new HashMap();
     private int _identity = ID_UNKNOWN;
     private int _idStrategy = ValueStrategies.NONE;
     private int _accessType = ACCESS_UNKNOWN;
@@ -703,8 +703,7 @@
      */
     public Class[] getDeclaredInterfaces() {
         if (_interfaces == null)
-            return _repos.EMPTY_CLASSES;
-
+            return MetaDataRepository.EMPTY_CLASSES;
         return (Class[]) _interfaces.toArray(new Class[_interfaces.size()]);
     }
 
@@ -713,11 +712,11 @@
      * class implements.
      */
     public void addDeclaredInterface(Class iface) {
-        if (_interfaces == null)
-            _interfaces = new ArrayList();
-        if (!iface.isInterface())
+        if (iface == null || !iface.isInterface())
             throw new MetaDataException(_loc.get("declare-non-interface",
                 this, iface));
+        if (_interfaces == null)
+            _interfaces = new ArrayList();
         _interfaces.add(iface);
     }
 
@@ -1657,9 +1656,8 @@
 
         // record implements in the repository
         if (_interfaces != null) {
-            for (Iterator it = _interfaces.iterator(); it.hasNext();) {
+            for (Iterator it = _interfaces.iterator(); it.hasNext();)
                 _repos.addDeclaredInterfaceImpl(this, (Class) it.next());
-            }
         }
 
         // resolve fetch groups

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java (original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java Tue Sep 19 18:29:55 2006
@@ -867,21 +867,16 @@
      */
     void addDeclaredInterfaceImpl(ClassMetaData meta, Class iface) {
         synchronized (_impls) {
-            boolean supDec = false;
             Collection vals = (Collection) _impls.get(iface);
             
             // check to see if the superclass already declares to avoid dups
             if (vals != null) {
                 ClassMetaData sup = meta.getPCSuperclassMetaData();
-                while (vals != null && sup != null && !supDec) {
-                    supDec = vals.contains(sup.getDescribedType());
-                    sup = sup.getPCSuperclassMetaData();
-                }
-                if (supDec)
-                    return;
+                for (; sup != null; sup = sup.getPCSuperclassMetaData())
+                    if (vals.contains(sup.getDescribedType()))
+                        return;
             }
-
-            addToCollection(_impls, iface, meta.getDescribedType(), true);
+            addToCollection(_impls, iface, meta.getDescribedType(), false);
         }
     }
 
@@ -1399,7 +1394,7 @@
         }
 
         // allow users to query on any implemented interfaces unless defaults 
-        // say the user must create persistent interfaces explicitly
+        // say the user must implement persistent interfaces explicitly in meta
         if (!_factory.getDefaults().isDeclaredInterfacePersistent())
             return;
         Class[] ints = check.getInterfaces();

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ImplHelper.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ImplHelper.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ImplHelper.java (original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ImplHelper.java Tue Sep 19 18:29:55 2006
@@ -174,7 +174,6 @@
      * @return the BitSet of fields that need update, or null if none
      */
     public static BitSet getUpdateFields(OpenJPAStateManager sm) {
-
         if ((sm.getPCState() == PCState.PDIRTY
             && (!sm.isFlushed() || sm.isFlushedDirty()))
             || (sm.getPCState() == PCState.PNEW && sm.isFlushedDirty())) {
@@ -183,11 +182,9 @@
                 dirty = (BitSet) dirty.clone();
                 dirty.andNot(sm.getFlushed());
             }
-
             if (dirty.length() > 0)
                 return dirty;
         }
-
         return null;
     }
 

Modified: incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/AbstractProductDerivation.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/AbstractProductDerivation.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/AbstractProductDerivation.java (original)
+++ incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/AbstractProductDerivation.java Tue Sep 19 18:29:55 2006
@@ -19,58 +19,32 @@
 import java.util.Map;
 
 /**
- * Abstract implementation of ProductDerivation loads configuration data using
- * ConfigurationProvider supplied by concrete implementation via 
- * {@link #newConfigurationProvider()} method.<p>
- * This abstract implementation does <em>not</em> provide a concrete 
- * ConfigurationProvider and hence all its loadXXX() methods would return null
- * by default. When the concrete subclass supplies a ConfigurationProvider,
- * this abstratct implementation will use it to load the configuration data. 
+ * Abstract no-op product derivation for easy extension.
  *
  * @author Pinaki Poddar
  * @since 0.4.1
  */
-
 public abstract class AbstractProductDerivation
     implements ProductDerivation {
 
     public ConfigurationProvider loadGlobals(ClassLoader loader)
-            throws Exception {
-        ConfigurationProvider provider = newConfigurationProvider();
-        if (provider != null && provider.loadGlobals(loader))
-            return provider;
+        throws Exception {
         return null;
     }
 
     public ConfigurationProvider loadDefaults(ClassLoader loader)
-            throws Exception {
-        ConfigurationProvider provider = newConfigurationProvider();
-        if (provider != null && provider.loadDefaults(loader))
-            return provider;
+        throws Exception {
         return null;
     }
 
     public ConfigurationProvider load(String resource, String anchor,
-            ClassLoader loader)  throws Exception {
-        ConfigurationProvider provider = newConfigurationProvider();
-        if (provider != null && provider.load(resource, anchor, loader))
-            return provider;
-        return null;
-    }
-    
-    public ConfigurationProvider load(String resource, String anchor,
-            Map map) throws Exception {
-        ConfigurationProvider provider = newConfigurationProvider();
-        if (provider != null && provider.load(resource, anchor, map))
-            return provider;
+        ClassLoader loader) 
+        throws Exception {
         return null;
     }
 
     public ConfigurationProvider load(File file, String anchor)
-            throws Exception {
-        ConfigurationProvider provider = newConfigurationProvider();
-        if (provider != null && provider.load(file, anchor))
-            return provider;
+        throws Exception {
         return null;
     }
 
@@ -85,6 +59,4 @@
     public boolean afterSpecificationSet(Configuration conf) {
         return false;
     }
-    
-    public abstract ConfigurationProvider newConfigurationProvider();
 }

Modified: incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationImpl.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationImpl.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationImpl.java (original)
+++ incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationImpl.java Tue Sep 19 18:29:55 2006
@@ -42,7 +42,6 @@
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.MissingResourceException;
@@ -372,9 +371,9 @@
     public synchronized MethodDescriptor[] getMethodDescriptors() {
         if (_mds != null)
             return _mds;
-        PropertyDescriptor[] pds = getPropertyDescriptors();
 
-        List descs = new LinkedList(); 
+        PropertyDescriptor[] pds = getPropertyDescriptors();
+        List descs = new ArrayList(); 
         for (int i = 0; i < pds.length; i++) {
             Method write = pds[i].getWriteMethod();
             Method read = pds[i].getReadMethod();
@@ -383,7 +382,6 @@
                 descs.add(new MethodDescriptor(read));
             }
         }
-
         _mds = (MethodDescriptor[])descs.
             toArray(new MethodDescriptor[descs.size()]);
         return _mds;

Modified: incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationProvider.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationProvider.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationProvider.java (original)
+++ incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationProvider.java Tue Sep 19 18:29:55 2006
@@ -15,7 +15,6 @@
  */
 package org.apache.openjpa.lib.conf;
 
-import java.io.File;
 import java.util.Map;
 
 /**
@@ -50,48 +49,4 @@
      * Set loaded information into the given configuration.
      */
     public void setInto(Configuration conf);
-    
-    /**
-     * Loads the global resource. The meaning of <em>global</em> is specific
-     * to concrte implementaion. 
-     * @param loader used to locate the resource. If null uses the current
-     * thread's loader.
-     * @return true if located and loaded global configuration 
-     */
-    public boolean loadGlobals(ClassLoader loader) throws Exception;
-
-    /**
-     * Loads the default resource. The meaning of <em>default</em> is specific
-     * to concrte implementaion. 
-     * @param loader used to locate the resource. If null uses the current
-     * thread's loader.
-     * @return true if located and loaded default configuration 
-     */
-    public boolean loadDefaults(ClassLoader loader) throws Exception;
-    
-    /**
-     * Loads the given resource. 
-     * @param resource name of the resource
-     * @param anchor optional named anchor within a resource containing multiple
-     * configuration
-     * @param loader used to locate the resource. If null uses the current
-     * thread's loader.
-     * @return true if located and loaded configuration 
-     */
-    public boolean load(String resource, String anchor, ClassLoader loader) 
-        throws Exception;
-    
-    public boolean load(String resource, String anchor, Map map) 
-        throws Exception;
-
-    /**
-     * Loads the given resource. 
-     * @param file name of the file to load from
-     * @param anchor optional named anchor within a file containing multiple
-     * configuration
-     * @return true if located and loaded configuration 
-     */
-    public boolean load(File file, String anchor) throws Exception;
-
-
 }

Modified: incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/Configurations.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/Configurations.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/Configurations.java (original)
+++ incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/Configurations.java Tue Sep 19 18:29:55 2006
@@ -195,9 +195,14 @@
                 if (file.isFile())
                     provider = ProductDerivations.load(file, anchor, null);
                 else
-                    provider = ProductDerivations.load(path, anchor, (Map)null);
+                    provider = ProductDerivations.load(path, anchor, null);
             }
-            provider.setInto(conf);
+            if (provider != null)
+                provider.setInto(conf);
+            else
+                throw new MissingResourceException(_loc.get("no-provider",
+                    props).getMessage(), Configurations.class.getName(), 
+                    props);
         } else {
             provider = ProductDerivations.loadDefaults(null);
             if (provider != null)

Modified: incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/MapConfigurationProvider.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/MapConfigurationProvider.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/MapConfigurationProvider.java (original)
+++ incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/MapConfigurationProvider.java Tue Sep 19 18:29:55 2006
@@ -15,7 +15,6 @@
  */
 package org.apache.openjpa.lib.conf;
 
-import java.io.File;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
@@ -47,37 +46,9 @@
      * Constructor; supply properties map.
      */
     public MapConfigurationProvider(Map props) {
-        this();
         addProperties(props);
     }
 
-    public boolean loadGlobals(ClassLoader loader) throws Exception {
-        return false;
-    }
-
-    public boolean loadDefaults(ClassLoader loader) throws Exception {
-        return false;
-    }
-
-    public boolean load(String resource, String anchor, ClassLoader loader) 
-        throws Exception {
-        return false;
-    }
-
-    public boolean load(File file, String anchor) throws Exception {
-        return false;
-    }
-
-    /**
-     * Loads only if the given map is non-null.
-     * Always returns false.
-     */
-    public boolean load(String rsrc, String anchor, Map m) throws Exception {
-        if (m != null)
-            addProperties(m);
-        return false;
-    }
-
     public Map getProperties() {
         return (_props == null) ? Collections.EMPTY_MAP : _props;
     }
@@ -107,7 +78,6 @@
     protected void setInto(Configuration conf, Log log) {
         if (log != null && log.isTraceEnabled())
             log.trace(_loc.get("conf-load", getProperties()));
-        
         if (_props != null)
             conf.fromProperties(_props);
     }

Modified: incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ProductDerivation.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ProductDerivation.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ProductDerivation.java (original)
+++ incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ProductDerivation.java Tue Sep 19 18:29:55 2006
@@ -20,14 +20,11 @@
 
 /**
  * Hooks for deriving products with additional functionality.
- * The specific implementation uses corresponding {@link ConfigurationProvider}
- * to locate and parse configuration information from global, default or
- * explictly-specified resources.
- * All implementations of this interface will have a chance to mutate
- * a {@link Configuration} both before and after the user-specified
- * configuration data is loaded. 
- * The order in which the product derivations are evaluated is determined by the 
- * specificity of the derivation type.
+ * Parses configuration information from global, default or explictly-specified
+ * resources. All implementations of this interface will have a chance to mutate
+ * a {@link Configuration} both before and after the user-specified 
+ * configuration data is loaded. The order in which the product derivations are
+ * evaluated is determined by the specificity of the derivation type.
  *
  * @author Abe White
  * @author Pinaki Poddar
@@ -35,7 +32,6 @@
  */
 public interface ProductDerivation {
 
-    public static final int TYPE_SPEC = 0;
     public static final int TYPE_PRODUCT = 100;
     public static final int TYPE_FEATURE = 1000;
 
@@ -45,29 +41,30 @@
     public int getType();
 
     /**
-     * Load globals via the returned ConfigurationProvider, or return null if 
+     * Load globals into the returned ConfigurationProvider, or return null if 
      * no globals is found.
      */
     public ConfigurationProvider loadGlobals(ClassLoader loader) 
         throws Exception;
 
     /**
-     * Load defaults via the returned ConfigurationProvider, or return null if 
+     * Load defaults into the returned ConfigurationProvider, or return null if 
      * no defaults is found.
      */
     public ConfigurationProvider loadDefaults(ClassLoader loader) 
         throws Exception;
 
     /**
-     * Load the given given resource via the returned ConfigurationProvider, or 
-     * return null if it is not a resource this receiver understands. 
+     * Load the given given resource into the returned ConfigurationProvider, 
+     * or return null if it is not a resource this receiver understands. 
      * The given class loader may be null.
      *
      * @param anchor optional named anchor within a multiple-configuration
      * resource
      */
     public ConfigurationProvider load(String resource, String anchor, 
-        ClassLoader loader) throws Exception;
+        ClassLoader loader) 
+        throws Exception;
 
     /**
      * Load given file, or return null if it is not a file this receiver
@@ -79,21 +76,11 @@
         throws Exception;
     
     /**
-     * Load given resource overriding with the given map <code>m</code>, or 
-     * return null if it is not a resource this receiver* understands.
-     *
-     * @param anchor optional named anchor within a multiple-configuration file
-     * @param m optional properties to override the content of the resource.
-     */
-    public ConfigurationProvider load(String rsrc, String anchor, Map m) 
-        throws Exception;
-
-    
-    /**
      * Provides the instance with a callback to mutate the initial properties
      * of the {@link ConfigurationProvider}. This is primarily to alter or
      * add properties that determine what type of configuration is constructed,
      * and therefore is typically used at runtime only.
+     * 
      * @return true if given ConfigurationProvider has been mutated.
      */
     public boolean beforeConfigurationConstruct(ConfigurationProvider cp);
@@ -101,12 +88,14 @@
     /**
      * Provides the instance with the opportunity to mutate
      * <code>conf</code> before the user configuration is applied.
+     *
      * @return true if given Configuration has been mutated.
      */
     public boolean beforeConfigurationLoad(Configuration conf);
 
     /**
      * Called after the specification has been set.
+     *
      * @return true if given Configuration has been mutated.
      */
     public boolean afterSpecificationSet(Configuration conf);

Modified: incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ProductDerivations.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ProductDerivations.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ProductDerivations.java (original)
+++ incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ProductDerivations.java Tue Sep 19 18:29:55 2006
@@ -20,12 +20,9 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
-import java.util.Map;
 import java.util.MissingResourceException;
 
-import org.apache.openjpa.lib.util.JavaVersions;
 import org.apache.openjpa.lib.util.Localizer;
-import org.apache.openjpa.lib.util.Options;
 import org.apache.openjpa.lib.util.Services;
 
 /**
@@ -37,10 +34,9 @@
 public class ProductDerivations {
 
     private static final ProductDerivation[] _derivations;
-
     static {
         Class[] pdcls = Services.getImplementorClasses(ProductDerivation.class,
-          ProductDerivation.class.getClassLoader());
+            ProductDerivation.class.getClassLoader());
         List derivations = new ArrayList(pdcls.length);
         for (int i = 0; i < pdcls.length; i++) {
             try {
@@ -72,7 +68,7 @@
     public static void beforeConfigurationConstruct(ConfigurationProvider cp) {
         for (int i = 0; i < _derivations.length; i++) {
             try {
-                boolean ret = _derivations[i].beforeConfigurationConstruct(cp);
+                _derivations[i].beforeConfigurationConstruct(cp);
             } catch (Exception e) {
                 // can't log; no configuration yet
                 e.printStackTrace();
@@ -87,7 +83,7 @@
     public static void beforeConfigurationLoad(Configuration conf) {
         for (int i = 0; i < _derivations.length; i++) {
             try {
-                boolean ret = _derivations[i].beforeConfigurationLoad(conf);
+                _derivations[i].beforeConfigurationLoad(conf);
             } catch (Exception e) {
                 // logging not configured yet
                 e.printStackTrace();
@@ -102,7 +98,7 @@
     public static void afterSpecificationSet(Configuration conf) {
         for (int i = 0; i < _derivations.length; i++) {
             try {
-                boolean ret = _derivations[i].afterSpecificationSet(conf);
+                _derivations[i].afterSpecificationSet(conf);
             } catch (Exception e) {
                 // logging not configured yet
                 e.printStackTrace();
@@ -125,19 +121,18 @@
             loader = Thread.currentThread().getContextClassLoader();
         ConfigurationProvider provider = null;
         StringBuffer errs = null;
-        for (int i = 0; i < _derivations.length; i++) {
+        // most specific to least
+        for (int i = _derivations.length - 1; i >= 0; i--) {
             try {
                 provider = _derivations[i].load(resource, anchor, loader);
-                if (provider != null) {
+                if (provider != null)
                     return provider;
-                }
             } catch (Throwable t) {
-                errs = (errs == null) ? new StringBuffer() 
-                        : errs.append("\r\n");
+                errs = (errs == null) ? new StringBuffer() : errs.append("\n");
                 errs.append(_derivations[i].getClass().getName() + ":" + t);
             }
         }
-        reportError(errs, resource);
+        reportErrors(errs, resource);
         return null;
     }
 
@@ -155,43 +150,39 @@
             loader = Thread.currentThread().getContextClassLoader();
         ConfigurationProvider provider = null;
         StringBuffer errs = null;
-        for (int i = 0; i < _derivations.length; i++) {
+        // most specific to least
+        for (int i = _derivations.length - 1; i >= 0; i--) {
             try {
                 provider = _derivations[i].load(file, anchor);
-                if (provider != null) {
+                if (provider != null)
                     return provider;
-                }
             } catch (Throwable t) {
-                errs = (errs == null) ? new StringBuffer() 
-                        : errs.append("\r\n");
+                errs = (errs == null) ? new StringBuffer() : errs.append("\n");
                 errs.append(_derivations[i].getClass().getName() + ":" + t);
             }
         }
-        reportError(errs, file.getAbsolutePath());
+        reportErrors(errs, file.getAbsolutePath());
         return null;
     }
-    
-    public static ConfigurationProvider load(String rsrc, String anchor, 
-        Map m) {
-        ConfigurationProvider provider = null;
-        StringBuffer errs = null;
-        for (int i = 0; i < _derivations.length; i++) {
-            try {
-                provider = _derivations[i].load(rsrc, anchor, m);
-                if (provider != null) {
-                    return provider;
-                }
-            } catch (Throwable t) {
-                errs = (errs == null) ? new StringBuffer() 
-                        : errs.append("\r\n");
-                errs.append(_derivations[i].getClass().getName() + ":" + t);
-            }
-        }
-        reportError(errs, rsrc);
-        return null;
+   
+    /**
+     * Return a {@link ConfigurationProvider} that has parsed system defaults.
+     */
+    public static ConfigurationProvider loadDefaults(ClassLoader loader) {
+        return load(loader, false);
     }
-        
-    public static ConfigurationProvider load(ClassLoader loader, 
+
+    /**
+     * Return a {@link ConfigurationProvider} that has parsed system globals.
+     */
+    public static ConfigurationProvider loadGlobals(ClassLoader loader) {
+        return load(loader, true);
+    }
+            
+    /**
+     * Load a built-in resource location.
+     */
+    private static ConfigurationProvider load(ClassLoader loader, 
        boolean globals) {
         if (loader == null)
             loader = Thread.currentThread().getContextClassLoader();
@@ -199,44 +190,32 @@
         ConfigurationProvider provider = null;
         StringBuffer errs = null;
         String type = (globals) ? "globals" : "defaults";
-        for (int i = 0; i < _derivations.length; i++) {
+        // most specific to least
+        for (int i = _derivations.length - 1; i >= 0; i--) {
             try {
                 provider = (globals) ? _derivations[i].loadGlobals(loader) 
-                        : _derivations[i].loadDefaults(loader);
-                if (provider != null) {
+                    : _derivations[i].loadDefaults(loader);
+                if (provider != null)
                    return provider;
-                }
             } catch (Throwable t) {
-                errs = (errs == null) ? new StringBuffer() 
-                        : errs.append("\r\n");
+                errs = (errs == null) ? new StringBuffer() : errs.append("\n");
                 errs.append(_derivations[i].getClass().getName() + ":" + t);
             }
         }
-        reportError(errs, type);
+        reportErrors(errs, type);
         return null;
     }
-    
-    private static void reportError(StringBuffer errs, String resource) {
-        if (errs == null)
-            return;
-        throw new MissingResourceException(
-                errs.toString(), ProductDerivations.class.getName(), resource);
-    }
-
+ 
     /**
-     * Return a {@link ConfigurationProvider} that has parsed system defaults.
+     * Thrown proper exception for given errors.
      */
-    public static ConfigurationProvider loadDefaults(ClassLoader loader) {
-        return load(loader, false);
+    private static void reportErrors(StringBuffer errs, String resource) {
+        if (errs == null)
+            return;
+        throw new MissingResourceException(errs.toString(), 
+            ProductDerivations.class.getName(), resource);
     }
 
-    /**
-     * Return a {@link ConfigurationProvider} that has parsed system globals.
-     */
-    public static ConfigurationProvider loadGlobals(ClassLoader loader) {
-        return load(loader, true);
-    }
-    
     /**
      * Compare {@link ProductDerivation}s.
      */

Modified: incubator/openjpa/trunk/openjpa-lib/src/main/resources/org/apache/openjpa/lib/conf/localizer.properties
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/src/main/resources/org/apache/openjpa/lib/conf/localizer.properties?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/src/main/resources/org/apache/openjpa/lib/conf/localizer.properties (original)
+++ incubator/openjpa/trunk/openjpa-lib/src/main/resources/org/apache/openjpa/lib/conf/localizer.properties Tue Sep 19 18:29:55 2006
@@ -52,12 +52,6 @@
     classpath listing the available provider classes, and some listed class \
     must be instantiable.  Typically this file is bundled as part of the \
     distribution.  Have you unbundled it, or unbundled its listed classes?
-hook-before: An exception occurred while invoking beforeConfigurationLoad() \
-	on "{0}". This exception will be consumed.
-hook-after: An exception occurred while invoking afterConfigurationLoad() \
-	on "{0}". This exception will be consumed.
-closeable-ex: An exception occurred while invoking close() on "{0}". This \
-	exception will be consumed.
 dup-with-different-prefixes: Found multiple properties with different valid \
 	prefixes. Properties: {0}, {1}.
 plugin-creation-exception: An exception occurred while creating a plugin for \

Modified: incubator/openjpa/trunk/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/ConfigurationTestProductDerivation.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/ConfigurationTestProductDerivation.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/ConfigurationTestProductDerivation.java (original)
+++ incubator/openjpa/trunk/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/ConfigurationTestProductDerivation.java Tue Sep 19 18:29:55 2006
@@ -15,17 +15,25 @@
  */
 package org.apache.openjpa.lib.conf.test;
 
+import java.io.File;
+import java.io.InputStream;
+import java.io.IOException;
+import java.net.URL;
+import java.util.MissingResourceException;
+import java.util.Properties;
+
 import org.apache.openjpa.lib.conf.AbstractProductDerivation;
 import org.apache.openjpa.lib.conf.ConfigurationProvider;
+import org.apache.openjpa.lib.conf.MapConfigurationProvider;
 import org.apache.openjpa.lib.conf.ProductDerivation;
 
 /**
  * A Product Derivation to test loading of global and default configuration with
- * System settings. This provider uses 
- * {@link ConfigurationTestConfigurationProvider} which reads its global from
- * a file specified by <code>"openjpatest.properties"</code> system property.
+ * System settings.  Reads its global from a file specified by 
+ * <code>"openjpatest.properties"</code> system property.
  *
  * @author Pinaki Poddar
+ * @author Abe White
  */
 public class ConfigurationTestProductDerivation 
     extends AbstractProductDerivation {
@@ -33,10 +41,60 @@
     public int getType() {
         return ProductDerivation.TYPE_PRODUCT;
     }
-    
-    public ConfigurationProvider newConfigurationProvider() {
-        return new ConfigurationTestConfigurationProvider();
+
+    public ConfigurationProvider loadGlobals(ClassLoader loader)
+        throws IOException {
+        return load(null, loader);
     }
-    
-}
 
+    public ConfigurationProvider load(String rsrc, ClassLoader loader)
+        throws IOException {
+        if (rsrc == null)
+            rsrc = System.getProperty("openjpatest.properties");
+        if (rsrc == null || !rsrc.endsWith(".properties"))
+            return null;
+
+        URL url = findResource(rsrc, loader);
+        if (url == null)
+            throw new MissingResourceException(rsrc, getClass().getName(), 
+                rsrc);
+
+        InputStream in = url.openStream();
+        Properties props = new Properties();
+        if (in != null) {
+            try {
+                props.load(in);
+                return new MapConfigurationProvider(props);
+            } finally {
+                try { in.close(); } catch (Exception e) {}
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Locate the given resource.
+     */
+    private URL findResource(String rsrc, ClassLoader loader)
+        throws IOException {
+        if (loader != null)
+            return loader.getResource(rsrc);
+
+        // in jbuilder the classloader can be null
+        URL url = null;
+        loader = getClass().getClassLoader();
+        if (loader != null)
+            url = loader.getResource(rsrc);
+        if (url == null) {
+            loader = Thread.currentThread().getContextClassLoader();
+            if (loader != null)
+                url = loader.getResource(rsrc);
+        }
+        if (url == null) {
+            loader = ClassLoader.getSystemClassLoader();
+            if (loader != null)
+                url = loader.getResource(rsrc);
+        }
+        return url;
+    }
+}

Modified: incubator/openjpa/trunk/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/TestConfigurationImpl.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/TestConfigurationImpl.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/TestConfigurationImpl.java (original)
+++ incubator/openjpa/trunk/openjpa-lib/src/test/java/org/apache/openjpa/lib/conf/test/TestConfigurationImpl.java Tue Sep 19 18:29:55 2006
@@ -27,9 +27,6 @@
  * properties, which are required for the bean descriptors used by the
  * configuration framework {@link Value}.
  * 
- * This test has been modified to test the same functionality of 
- * ConfigurationProvider but now invoked via ProductDerivation.
- * 
  * This test assumes a <code>"test.properties"</code> file available in
  * classpath and also system property <code>"openjpatest.properties"</code>
  * is set to <code>"test.properties"</code>.

Modified: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/JDBCPersistenceProductDerivation.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/JDBCPersistenceProductDerivation.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/JDBCPersistenceProductDerivation.java (original)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/JDBCPersistenceProductDerivation.java Tue Sep 19 18:29:55 2006
@@ -21,7 +21,6 @@
 import org.apache.openjpa.jdbc.kernel.JDBCStoreManager;
 import org.apache.openjpa.lib.conf.AbstractProductDerivation;
 import org.apache.openjpa.lib.conf.Configuration;
-import org.apache.openjpa.lib.conf.ConfigurationProvider;
 import org.apache.openjpa.persistence.FetchPlan;
 import org.apache.openjpa.persistence.PersistenceProductDerivation;
 
@@ -31,18 +30,21 @@
  * @author Abe White
  * @nojavadoc
  */
-public class JDBCPersistenceProductDerivation extends AbstractProductDerivation 
+public class JDBCPersistenceProductDerivation 
+    extends AbstractProductDerivation 
     implements OpenJPAProductDerivation {
     
     public int getType() {
         return TYPE_SPEC_STORE;
     }
 
+    @Override
     public boolean beforeConfigurationLoad(Configuration c) {
-        if (c instanceof OpenJPAConfiguration) 
-        ((OpenJPAConfiguration)c).getStoreFacadeTypeRegistry().
-            registerImplementation(FetchPlan.class, JDBCStoreManager.class, 
-            JDBCFetchPlanImpl.class);
+        if (c instanceof OpenJPAConfiguration) {
+            ((OpenJPAConfiguration) c).getStoreFacadeTypeRegistry().
+                registerImplementation(FetchPlan.class, JDBCStoreManager.class, 
+                JDBCFetchPlanImpl.class);
+        }
         if (!(c instanceof JDBCConfigurationImpl))
             return false;
 
@@ -67,21 +69,17 @@
         return true;
     }
 
+    @Override
     public boolean afterSpecificationSet(Configuration c) {
-        String jpa = PersistenceProductDerivation.SPEC_JPA;
         if (!(c instanceof JDBCConfigurationImpl))
             return false;
         JDBCConfigurationImpl conf = (JDBCConfigurationImpl) c;
-        
-         if (!jpa.equals(conf.getSpecification()))
+        String jpa = PersistenceProductDerivation.SPEC_JPA;
+        if (!jpa.equals(conf.getSpecification()))
             return false;
         
         conf.mappingDefaultsPlugin.setDefault(jpa);
         conf.mappingDefaultsPlugin.setString(jpa);
         return true;
-    }
-    
-    public ConfigurationProvider newConfigurationProvider() {
-        return null;
     }
 }

Modified: incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java (original)
+++ incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java Tue Sep 19 18:29:55 2006
@@ -53,17 +53,22 @@
     private static final Localizer _loc = Localizer.forPackage
         (EntityManagerFactoryImpl.class);
 
-    private final DelegatingBrokerFactory _factory;
+    private DelegatingBrokerFactory _factory = null;
     private transient Constructor<FetchPlan> _plan = null;
     private transient StoreCache _cache = null;
     private transient QueryResultCache _queryCache = null;
 
     /**
-     * Constructor. Delegate must be provided on construction.
+     * Default constructor provided for auto-instantiation.
+     */
+    public EntityManagerFactoryImpl() {
+    }
+
+    /**
+     * Supply delegate on construction.
      */
     public EntityManagerFactoryImpl(BrokerFactory factory) {
-        _factory = new DelegatingBrokerFactory(factory,
-            PersistenceExceptions.TRANSLATOR);
+        setBrokerFactory(factory);
     }
 
     /**
@@ -73,6 +78,14 @@
         return _factory.getDelegate();
     }
 
+    /**
+     * Delegate must be provided before use.
+     */
+    public void setBrokerFactory(BrokerFactory factory) {
+        _factory = new DelegatingBrokerFactory(factory,
+            PersistenceExceptions.TRANSLATOR);
+    }
+
     public OpenJPAConfiguration getConfiguration() {
         return _factory.getConfiguration();
     }
@@ -175,7 +188,7 @@
         // regardless of PersistenceContextType
         broker.setAutoDetach(AutoDetach.DETACH_CLOSE);
         broker.setDetachedNew(false);
-        OpenJPAEntityManager em = new EntityManagerImpl(this, broker);
+        OpenJPAEntityManager em = newEntityManagerImpl(broker);
 
         // allow setting of other bean properties of EM
         List<RuntimeException> errs = null;
@@ -224,8 +237,14 @@
                 (Throwable[]) errs.toArray(new Throwable[errs.size()]),
                 null, true);
         }
-
         return em;
+    }
+
+    /**
+     * Create a new entity manager around the given broker.
+     */
+    protected EntityManagerImpl newEntityManagerImpl(Broker broker) {
+        return new EntityManagerImpl(this, broker);
     }
 
     public void addLifecycleListener(Object listener, Class... classes) {

Added: incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryValue.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryValue.java?view=auto&rev=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryValue.java (added)
+++ incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryValue.java Tue Sep 19 18:29:55 2006
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openjpa.persistence;
+
+import org.apache.openjpa.conf.OpenJPAConfiguration;
+import org.apache.openjpa.kernel.BrokerFactory;
+import org.apache.openjpa.lib.conf.PluginValue;
+
+/**
+ * Plugin type used to represent the {@link EntityManagerFactory}. 
+ *
+ * @since 0.4.1
+ * @nojavadoc
+ */
+public class EntityManagerFactoryValue
+    extends PluginValue {
+
+    /**
+     * Configuration property key.
+     */
+    public static final String KEY = "EntityManagerFactory";
+
+    public static final String[] ALIASES = {
+        "default", EntityManagerFactoryImpl.class.getName(),
+    };
+
+    /**
+     * Create a new factory of the configured type.
+     */
+    public static OpenJPAEntityManagerFactory newFactory(BrokerFactory bf) {
+        OpenJPAConfiguration conf = bf.getConfiguration();
+        PluginValue val = (PluginValue) conf.getValue(KEY); 
+        if (val == null)
+            return null;
+        EntityManagerFactoryImpl emf = (EntityManagerFactoryImpl) val.
+            instantiate(EntityManagerFactoryImpl.class, conf, true);
+        emf.setBrokerFactory(bf);
+        return emf;
+    }
+
+    public EntityManagerFactoryValue() {
+        super(KEY, false);
+        setAliases(ALIASES);
+        setDefault(ALIASES[0]);
+        setClassName(ALIASES[1]);
+    }
+}

Modified: incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistence.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistence.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistence.java (original)
+++ incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistence.java Tue Sep 19 18:29:55 2006
@@ -65,23 +65,15 @@
     
     public static OpenJPAEntityManagerFactory toEntityManagerFactory
        (BrokerFactory factory) {
-        return toEntityManagerFactory(factory, null);
-    }
-    
-    /**
-     * Return an entity manager factory facade to the given broker factory.
-     */
-    public static OpenJPAEntityManagerFactory toEntityManagerFactory
-        (BrokerFactory factory, ConfigurationProvider cp) {
         if (factory == null)
             return null;
+
         factory.lock();
-        
         try {
             OpenJPAEntityManagerFactory emf = (OpenJPAEntityManagerFactory)
                 factory.getUserObject(EMF_KEY);
             if (emf == null) {
-                emf = newEntityManagerFactory(factory, cp, null);
+                emf = EntityManagerFactoryValue.newFactory(factory);
                 factory.putUserObject(EMF_KEY, emf);
             }
             return emf;
@@ -116,10 +108,9 @@
             if (em == null) {
                 EntityManagerFactoryImpl emf = (EntityManagerFactoryImpl)
                     toEntityManagerFactory(broker.getBrokerFactory());
-                em = new EntityManagerImpl(emf, broker);
+                em = emf.newEntityManagerImpl(broker);
                 broker.putUserObject(EM_KEY, em);
             }
-
             return em;
         } catch (Exception e) {
             throw PersistenceExceptions.toPersistenceException(e);
@@ -127,8 +118,6 @@
             broker.unlock();
         }
     }
-    
-     
 
     /**
      * Return the underlying broker for the given entity manager facade.
@@ -441,49 +430,4 @@
 			return String.class;
 		return oidClass;
 	}
-    
-    private static OpenJPAEntityManagerFactory newEntityManagerFactory (
-        BrokerFactory brokerFactory, ConfigurationProvider conf, 
-        ClassLoader loader) {
-            if (conf == null)
-                conf = new MapConfigurationProvider();
-            ProductDerivations.beforeConfigurationConstruct(conf);
-
-            Class cls = getFactoryClass(conf, loader);
-            Constructor ctr;
-            try {
-                ctr = cls.getConstructor(BrokerFactory.class); 
-                return (OpenJPAEntityManagerFactory)ctr
-                   .newInstance(brokerFactory);
-            } catch (Exception e) {
-                throw PersistenceExceptions.toPersistenceException(e);
-            }
-        }
-
-    /**
-     * Instantiate the factory class designated in properties.
-     */
-    private static Class getFactoryClass(ConfigurationProvider conf,
-        ClassLoader loader) {
-        if (loader == null)
-            loader = Thread.currentThread().getContextClassLoader();
-
-        Object cls = EntityManagerFactoryValue.get(conf);
-        if (cls instanceof Class)
-            return (Class) cls;
-
-        EntityManagerFactoryValue value = new EntityManagerFactoryValue();
-        value.setString((String) cls);
-        String clsName = value.getClassName();
-        if (clsName == null)
-            throw new UserException(_loc.get("no-emf", 
-                conf.getProperties())).setFatal(true);
-
-        try {
-            return Class.forName(clsName, true, loader);
-        } catch (Exception e) {
-            throw new UserException(_loc.get("bad-emf-class",
-                clsName), e).setFatal(true);
-        }
-    }
 }

Modified: incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java (original)
+++ incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java Tue Sep 19 18:29:55 2006
@@ -15,18 +15,44 @@
  */
 package org.apache.openjpa.persistence;
 
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
 import java.util.Map;
+import java.util.MissingResourceException;
+import javax.persistence.spi.PersistenceUnitInfo;
+import javax.persistence.spi.PersistenceUnitTransactionType;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.openjpa.conf.OpenJPAConfiguration;
 import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
 import org.apache.openjpa.conf.OpenJPAProductDerivation;
 import org.apache.openjpa.lib.conf.AbstractProductDerivation;
 import org.apache.openjpa.lib.conf.Configuration;
 import org.apache.openjpa.lib.conf.ConfigurationProvider;
+import org.apache.openjpa.lib.conf.MapConfigurationProvider;
 import org.apache.openjpa.lib.conf.PluginValue;
+import org.apache.openjpa.lib.log.Log;
+import org.apache.openjpa.lib.meta.XMLMetaDataParser;
+import org.apache.openjpa.lib.util.Localizer;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
 
 /**
- * Sets JPA specification defaults.
+ * Sets JPA specification defaults and parses JPA specification XML files.
+ * 
+ * For globals, looks in <code>openjpa.properties</code> system property for
+ * the location of a file to parse. If no system property is defined, the
+ * default resource location of <code>META-INF/openjpa.xml</code> is used.
+ *
+ * For defaults, looks for <code>META-INF/persistence.xml</code>.
+ * Within <code>persistence.xml</code>, look for the named persistence unit, or
+ * if no name given, an OpenJPA unit (preferring an unnamed OpenJPA unit to 
+ * a named one).
  *
  * @author Abe White
  * @nojavadoc
@@ -37,40 +63,17 @@
 
     public static final String SPEC_JPA = "jpa";
     public static final String ALIAS_EJB = "ejb";
+    public static final String RSRC_GLOBAL = "META-INF/openjpa.xml";
+    public static final String RSRC_DEFAULT = "META-INF/persistence.xml";
+
+    private static final Localizer _loc = Localizer.forPackage
+        (PersistenceProductDerivation.class);
 
     public int getType() {
         return TYPE_SPEC;
     }
-
-    @Override
-    public ConfigurationProvider newConfigurationProvider() {
-        return new ConfigurationProviderImpl();
-    }
-
-    @Override
-    public ConfigurationProvider load(String rsrc, String anchor,
-            ClassLoader loader)  throws Exception {
-        if (rsrc != null && !rsrc.endsWith(".xml"))
-            return null;
-        return super.load(rsrc, anchor, loader);
-    }
-    
-    public ConfigurationProvider load(String rsrc, String anchor,
-            Map map) throws Exception {
-        if (rsrc != null && !rsrc.endsWith(".xml"))
-            return null;
-        return super.load(rsrc, anchor, map);
-    }
-    
-    public boolean beforeConfigurationConstruct(ConfigurationProvider cp) {
-        if (EntityManagerFactoryValue.get(cp) == null) {
-            cp.addProperty(EntityManagerFactoryValue.getKey(cp),
-                EntityManagerFactoryImpl.class.getName());
-            return true;
-        }
-        return false;
-    }
     
+    @Override
     public boolean beforeConfigurationLoad(Configuration c) {
         if (!(c instanceof OpenJPAConfigurationImpl))
             return false;
@@ -81,11 +84,11 @@
         conf.metaFactoryPlugin.setAlias(SPEC_JPA,
             PersistenceMetaDataFactory.class.getName());
         
-        PluginValue emfPlugin = new EntityManagerFactoryValue();
-        conf.addValue(emfPlugin);
+        conf.addValue(new EntityManagerFactoryValue());
         return true;
     }
 
+    @Override
     public boolean afterSpecificationSet(Configuration c) {
       if (!(c instanceof OpenJPAConfigurationImpl)
          || !SPEC_JPA.equals(((OpenJPAConfiguration)c).getSpecification()))
@@ -100,4 +103,361 @@
         conf.nontransactionalWrite.set(true);
         return true;
     }
+
+    /**
+     * Load configuration from the given persistence unit with the specified
+     * user properties.
+     */
+    public ConfigurationProvider load(PersistenceUnitInfo pinfo, Map m)
+        throws IOException {
+        if (pinfo == null)
+            return null;
+
+        if (!isOpenJPAPersistenceProvider(pinfo, null))
+            return null;
+
+        ConfigurationProviderImpl cp = new ConfigurationProviderImpl();
+        cp.addProperties(PersistenceUnitInfoImpl.toOpenJPAProperties(pinfo));
+        cp.addProperties(m);
+        if (pinfo instanceof PersistenceUnitInfoImpl) {
+            PersistenceUnitInfoImpl impl = (PersistenceUnitInfoImpl) pinfo;
+            if (impl.getPersistenceXmlFileUrl() != null)
+                cp.setSource(impl.getPersistenceXmlFileUrl().toString());
+        }
+        return cp;
+    }
+
+    /**
+     * Load configuration from the given resource and unit names, which may
+     * be null.
+     */
+    public ConfigurationProvider load(String rsrc, String name, Map m)
+        throws IOException {
+        boolean explicit = !StringUtils.isEmpty(rsrc);
+        if (!explicit)
+            rsrc = RSRC_DEFAULT;
+        
+        ConfigurationProviderImpl cp = new ConfigurationProviderImpl();
+        Boolean ret = load(cp, rsrc, name, m, null, explicit);
+        if (ret != null)
+            return (ret.booleanValue()) ? cp : null;
+        if (explicit)
+            return null;
+
+        // persistence.xml does not exist; just load map
+        PersistenceUnitInfoImpl pinfo = new PersistenceUnitInfoImpl();
+        pinfo.fromUserProperties(m);
+        if (!isOpenJPAPersistenceProvider(pinfo, null))
+            return null;
+        cp.addProperties(pinfo.toOpenJPAProperties());
+        return cp;
+    }
+
+    @Override
+    public ConfigurationProvider load(String rsrc, String anchor, 
+        ClassLoader loader)
+        throws IOException {
+        if (rsrc != null && !rsrc.endsWith(".xml"))
+            return null;
+        ConfigurationProviderImpl cp = new ConfigurationProviderImpl();
+        if (load(cp, rsrc, anchor, null, loader, true) == Boolean.TRUE)
+            return cp;
+        return null;
+    }
+
+    @Override
+    public ConfigurationProvider load(File file, String anchor) 
+        throws IOException {
+        if (!file.getName().endsWith(".xml"))
+            return null;
+
+        ConfigurationParser parser = new ConfigurationParser(null);
+        parser.parse(file);
+        return load(findUnit((List<PersistenceUnitInfoImpl>) 
+            parser.getResults(), anchor, null), null);
+    }
+
+    @Override
+    public ConfigurationProvider loadGlobals(ClassLoader loader)
+        throws IOException {
+        String rsrc = System.getProperty("openjpa.properties");
+        boolean explicit = !StringUtils.isEmpty(rsrc);
+        String anchor = null;
+        int idx = (!explicit) ? -1 : rsrc.lastIndexOf('#');
+        if (idx != -1) {
+            // separate name from <resrouce>#<name> string
+            if (idx < rsrc.length() - 1)
+                anchor = rsrc.substring(idx + 1);
+            rsrc = rsrc.substring(0, idx);
+        }
+        if (StringUtils.isEmpty(rsrc))
+            rsrc = RSRC_GLOBAL;
+        else if (!rsrc.endsWith(".xml"))
+            return null;
+
+        ConfigurationProviderImpl cp = new ConfigurationProviderImpl();
+        if (load(cp, rsrc, anchor, null, loader, explicit) == Boolean.TRUE)
+            return cp;
+        return null;
+    }
+
+    @Override
+    public ConfigurationProvider loadDefaults(ClassLoader loader)
+        throws IOException {
+        ConfigurationProviderImpl cp = new ConfigurationProviderImpl();
+        if (load(cp, RSRC_DEFAULT, null, null, loader, false) == Boolean.TRUE)
+            return cp;
+        return null;
+    }
+
+    /**
+     * Looks through the resources at <code>rsrc</code> for a configuration
+     * file that matches <code>name</code> (or an unnamed one if
+     * <code>name</code> is <code>null</code>), and loads the XML in the
+     * resource into a new {@link PersistenceUnitInfo}. Then, applies the
+     * overrides in <code>m</code>.
+     *
+     * @return {@link Boolean#TRUE} if the resource was loaded, null if it
+     * does not exist, or {@link Boolean#FALSE} if it is not for OpenJPA
+     */
+    private Boolean load(ConfigurationProviderImpl cp, String rsrc, 
+        String name, Map m, ClassLoader loader, boolean explicit)
+        throws IOException {
+        if (loader == null)
+            loader = Thread.currentThread().getContextClassLoader();
+
+        Enumeration<URL> urls = loader.getResources(rsrc);
+        if (!urls.hasMoreElements()) {
+            if (!rsrc.startsWith("META-INF"))
+                urls = loader.getResources("META-INF/" + rsrc);
+            if (!urls.hasMoreElements())
+                return null;
+        }
+
+        ConfigurationParser parser = new ConfigurationParser(m);
+        PersistenceUnitInfoImpl pinfo = parseResources(parser, urls, name, 
+            loader);
+        if (pinfo == null || !isOpenJPAPersistenceProvider(pinfo, loader)) {
+            if (!explicit)
+                return Boolean.FALSE;
+            String msg = (pinfo == null) ? "missing-xml-config"
+                : "cantload-xml-config";
+            throw new MissingResourceException(_loc.get(msg, rsrc,
+                String.valueOf(name)).getMessage(), getClass().getName(), rsrc);
+        }
+
+        cp.addProperties(pinfo.toOpenJPAProperties());
+        cp.setSource(pinfo.getPersistenceXmlFileUrl().toString());
+        return Boolean.TRUE;
+    }
+
+    /**
+     * Parse resources at the given location. Searches for a
+     * PersistenceUnitInfo with the requested name, or an OpenJPA unit if
+     * no name given (preferring an unnamed OpenJPA unit to a named one).
+     */
+    private PersistenceUnitInfoImpl parseResources(ConfigurationParser parser,
+        Enumeration<URL> urls, String name, ClassLoader loader)
+        throws IOException {
+        List<PersistenceUnitInfoImpl> pinfos = 
+            new ArrayList<PersistenceUnitInfoImpl>();
+        for (URL url : Collections.list(urls)) {
+            parser.parse(url);
+            pinfos.addAll((List<PersistenceUnitInfoImpl>) parser.getResults());
+        }
+        return findUnit(pinfos, name, loader);
+    }
+
+    /**
+     * Find the unit with the given name, or an OpenJPA unit if no name is
+     * given (preferring an unnamed OpenJPA unit to a named one).
+     */
+    private PersistenceUnitInfoImpl findUnit(List<PersistenceUnitInfoImpl> 
+        pinfos, String name, ClassLoader loader) {
+        PersistenceUnitInfoImpl ojpa = null;
+        for (PersistenceUnitInfoImpl pinfo : pinfos) {
+            // found named unit?
+            if (name != null) {
+                if (name.equals(pinfo.getPersistenceUnitName()))
+                    return pinfo;
+                continue;
+            }
+
+            if (isOpenJPAPersistenceProvider(pinfo, loader)) {
+                // if no name given and found unnamed unit, return it.  
+                // otherwise record as default unit unless we find a 
+                // better match later
+                if (StringUtils.isEmpty(pinfo.getPersistenceUnitName()))
+                    return pinfo;
+                if (ojpa == null)
+                    ojpa = pinfo;
+            }
+        }
+        return ojpa;
+    }
+
+    /**
+     * Return whether the given persistence unit uses an OpenJPA provider.
+     */
+    private static boolean isOpenJPAPersistenceProvider
+        (PersistenceUnitInfo pinfo, ClassLoader loader) {
+        String name = pinfo.getPersistenceProviderClassName();
+        if (StringUtils.isEmpty(name) 
+            || PersistenceProviderImpl.class.getName().equals(name))
+            return true;
+
+        if (loader == null)
+            loader = Thread.currentThread().getContextClassLoader();
+        try {
+            return PersistenceProviderImpl.class.isAssignableFrom
+                (Class.forName(name, false, loader));
+        } catch (Throwable t) {
+            return false;
+        }
+    }
+
+    /**
+     * Custom configuration provider.   
+     */
+    private static class ConfigurationProviderImpl
+        extends MapConfigurationProvider {
+
+        private String _source;
+
+        /**
+         * Set the source of information in this provider.
+         */
+        public void setSource(String source) {
+            _source = source;
+        }
+
+        @Override
+        public void setInto(Configuration conf) {
+            if (conf instanceof OpenJPAConfiguration)
+                ((OpenJPAConfiguration) conf).setSpecification(SPEC_JPA);
+            super.setInto(conf, null);
+
+            Log log = conf.getConfigurationLog();
+            if (log.isTraceEnabled())
+                log.trace(_loc.get("conf-load", _source, getProperties()));
+        }
+    }
+
+    /**
+     * SAX handler capable of parsing an JPA persistence.xml file.
+     * Package-protected for testing.
+     */
+    static class ConfigurationParser
+        extends XMLMetaDataParser {
+
+        private final Map _map;
+        private PersistenceUnitInfoImpl _info = null;
+        private URL _source = null;
+
+        public ConfigurationParser(Map map) {
+            _map = map;
+            setCaching(false);
+            setValidating(true);
+            setParseText(true);
+        }
+
+        @Override
+        public void parse(URL url)
+            throws IOException {
+            _source = url;
+            super.parse(url);
+        }
+
+        @Override
+        public void parse(File file)
+            throws IOException {
+            _source = file.toURL();
+            super.parse(file);
+        }
+
+        @Override
+        protected Object getSchemaSource() {
+            return getClass().getResourceAsStream("persistence-xsd.rsrc");
+        }
+
+        @Override
+        protected void reset() {
+            super.reset();
+            _info = null;
+            _source = null;
+        }
+
+        protected boolean startElement(String name, Attributes attrs)
+            throws SAXException {
+            if (currentDepth() == 1)
+                startPersistenceUnit(attrs);
+            else if (currentDepth() == 3 && "property".equals(name))
+                _info.setProperty(attrs.getValue("name"),
+                    attrs.getValue("value"));
+            return true;
+        }
+
+        protected void endElement(String name)
+            throws SAXException {
+            if (currentDepth() == 1) {
+                _info.fromUserProperties(_map);
+                addResult(_info);
+            }
+            if (currentDepth() != 2)
+                return;
+
+            switch (name.charAt(0)) {
+                case 'c': // class
+                    _info.addManagedClassName(currentText());
+                case 'e': // exclude-unlisted-classes
+                    _info.setExcludeUnlistedClasses("true".equalsIgnoreCase
+                        (currentText()));
+                    break;
+                case 'j':
+                    if ("jta-data-source".equals(name))
+                        _info.setJtaDataSourceName(currentText());
+                    else // jar-file
+                    {
+                        try {
+                            _info.addJarFileName(currentText());
+                        } catch (IllegalArgumentException iae) {
+                            throw getException(iae.getMessage());
+                        }
+                    }
+                    break;
+                case 'm': // mapping-file
+                    _info.addMappingFileName(currentText());
+                    break;
+                case 'n': // non-jta-data-source
+                    _info.setNonJtaDataSourceName(currentText());
+                    break;
+                case 'p':
+                    if ("provider".equals(name))
+                        _info.setPersistenceProviderClassName(currentText());
+                    break;
+            }
+        }
+
+        /**
+         * Parse persistence-unit element.
+         */
+        private void startPersistenceUnit(Attributes attrs)
+            throws SAXException {
+            _info = new PersistenceUnitInfoImpl();
+            _info.setPersistenceUnitName(attrs.getValue("name"));
+
+            // we only parse this ourselves outside a container, so default
+            // transaction type to local
+            String val = attrs.getValue("transaction-type");
+            if (val == null)
+                _info.setTransactionType
+                    (PersistenceUnitTransactionType.RESOURCE_LOCAL);
+            else
+                _info.setTransactionType(Enum.valueOf
+                    (PersistenceUnitTransactionType.class, val));
+
+            if (_source != null)
+                _info.setPersistenceXmlFileUrl(_source);
+		}
+	}
 }

Modified: incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java (original)
+++ incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java Tue Sep 19 18:29:55 2006
@@ -61,19 +61,20 @@
      * identified by <code>resource</code>, and uses the first resource found
      * when doing this lookup, regardless of the name specified in the XML
      * resource or the name of the jar that the resource is contained in.
-     *  This does no pooling of EntityManagersFactories.
+     * This does no pooling of EntityManagersFactories.
      */
     public EntityManagerFactory createEntityManagerFactory(String name,
         String resource, Map m) {
+        PersistenceProductDerivation pd = new PersistenceProductDerivation();
         try {
-            ConfigurationProvider cp = ProductDerivations.load(resource, name, 
-                 m);
-            if (cp != null) {
-                BrokerFactory factory = Bootstrap.newBrokerFactory(cp, null);
-            	return OpenJPAPersistence.toEntityManagerFactory(factory, cp);
-            }
-            else
-                 return null;
+            ConfigurationProvider cp = pd.load(resource, name, m);
+            if (cp == null)
+                return null;
+
+            if (m != null)
+                cp.addProperties(m);
+            BrokerFactory factory = Bootstrap.newBrokerFactory(cp, null);
+            return OpenJPAPersistence.toEntityManagerFactory(factory);
         } catch (Exception e) {
             throw PersistenceExceptions.toPersistenceException(e);
         }
@@ -84,37 +85,29 @@
     }
 
     public EntityManagerFactory createContainerEntityManagerFactory(
-        PersistenceUnitInfo pui, Map map) {
-        ConfigurationProviderImpl cp = newConfigurationProvider();
+        PersistenceUnitInfo pui, Map m) {
+        PersistenceProductDerivation pd = new PersistenceProductDerivation();
         try {
-            if (cp.load(pui, map)) {
-                BrokerFactory factory = Bootstrap.newBrokerFactory(cp, 
-                     cp.getClassLoader());
-                OpenJPAEntityManagerFactory emf = 
-                    OpenJPAPersistence.toEntityManagerFactory(factory);
-                Properties p = pui.getProperties();
-                String ctOpts = null;
-                if (p != null)
-                    ctOpts = p.getProperty(CLASS_TRANSFORMER_OPTIONS);
-                pui.addTransformer(new ClassTransformerImpl(
-                    emf.getConfiguration(), ctOpts,
-                    pui.getNewTempClassLoader()));
-                return emf;
-            } else
+            ConfigurationProvider cp = pd.load(pui, m);
+            if (cp == null)
                 return null;
+
+            BrokerFactory factory = Bootstrap.newBrokerFactory(cp, 
+                pui.getClassLoader());
+            OpenJPAEntityManagerFactory emf = 
+                OpenJPAPersistence.toEntityManagerFactory(factory);
+            Properties p = pui.getProperties();
+            String ctOpts = null;
+            if (p != null)
+                ctOpts = p.getProperty(CLASS_TRANSFORMER_OPTIONS);
+            pui.addTransformer(new ClassTransformerImpl(emf.getConfiguration(),
+                ctOpts, pui.getNewTempClassLoader()));
+            return emf;
         } catch (Exception e) {
             throw PersistenceExceptions.toPersistenceException(e);
         }
     }
     
-    /**
-     * Gets the configuration provider to locate and load configuration data.
-     * An implementation is returned rather than the interface.
-      */
-    protected ConfigurationProviderImpl newConfigurationProvider() {
-        return new ConfigurationProviderImpl();
-    }
-            
     /**
      * Java EE 5 class transformer.
      */

Modified: incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java?view=diff&rev=448025&r1=448024&r2=448025
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java (original)
+++ incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java Tue Sep 19 18:29:55 2006
@@ -296,6 +296,15 @@
 
     /**
      * Return a {@link Map} containing the properties necessary to create
+     * a {@link Configuration} that reflects the information in this
+     * persistence unit info.
+     */
+    public Map toOpenJPAProperties() {
+        return toOpenJPAProperties(this);
+    }
+
+    /**
+     * Return a {@link Map} containing the properties necessary to create
      * a {@link Configuration} that reflects the information in the given
      * persistence unit info.
      */



Mime
View raw message