openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From curti...@apache.org
Subject svn commit: r808130 - in /openjpa/sandboxes/perf: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
Date Wed, 26 Aug 2009 17:18:43 GMT
Author: curtisr7
Date: Wed Aug 26 17:18:43 2009
New Revision: 808130

URL: http://svn.apache.org/viewvc?rev=808130&view=rev
Log:
MetaDataRepository locking changes. OPENJPA-250.

Modified:
    openjpa/sandboxes/perf/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java
    openjpa/sandboxes/perf/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java

Modified: openjpa/sandboxes/perf/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java
URL: http://svn.apache.org/viewvc/openjpa/sandboxes/perf/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java?rev=808130&r1=808129&r2=808130&view=diff
==============================================================================
--- openjpa/sandboxes/perf/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java
(original)
+++ openjpa/sandboxes/perf/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java
Wed Aug 26 17:18:43 2009
@@ -143,7 +143,7 @@
     /**
      * Representation of the database schema.
      */
-    public synchronized SchemaGroup getSchemaGroup() {
+    public SchemaGroup getSchemaGroup() {
         if (_schema == null)
             _schema = ((JDBCConfiguration) getConfiguration()).
                 getSchemaFactoryInstance().readSchema();
@@ -153,14 +153,14 @@
     /**
      * Representation of the database schema.
      */
-    public synchronized void setSchemaGroup(SchemaGroup schema) {
+    public void setSchemaGroup(SchemaGroup schema) {
         _schema = schema;
     }
 
     /**
      * Installs mapping strategies on components.
      */
-    public synchronized StrategyInstaller getStrategyInstaller() {
+    public StrategyInstaller getStrategyInstaller() {
         if (_installer == null)
             _installer = new RuntimeStrategyInstaller(this);
         return _installer;
@@ -169,20 +169,25 @@
     /**
      * Installs mapping strategies on components.
      */
-    public synchronized void setStrategyInstaller(StrategyInstaller installer) {
+    public void setStrategyInstaller(StrategyInstaller installer) {
         _installer = installer;
     }
 
     /**
      * Return the query result mapping for the given name.
      */
-    public synchronized QueryResultMapping getQueryResultMapping(Class cls,
+    public QueryResultMapping getQueryResultMapping(Class cls,
         String name, ClassLoader envLoader, boolean mustExist) {
-        QueryResultMapping res = getQueryResultMappingInternal(cls, name,
-            envLoader);
-        if (res == null && mustExist)
-            throw new MetaDataException(_loc.get("no-query-res", cls, name));
-        return res;
+        lock();
+        try {
+            QueryResultMapping res = getQueryResultMappingInternal(cls, name,
+                envLoader);
+            if (res == null && mustExist)
+                throw new MetaDataException(_loc.get("no-query-res", cls, name));
+            return res;
+        } finally {
+            unlock();
+        }
     }
 
     /**
@@ -219,48 +224,73 @@
     /**
      * Return all cached query result mappings.
      */
-    public synchronized QueryResultMapping[] getQueryResultMappings() {
-        Collection values = _results.values();
-        return (QueryResultMapping[]) values.toArray
-            (new QueryResultMapping[values.size()]);
+    public QueryResultMapping[] getQueryResultMappings() {
+        lock();
+        try {
+            Collection values = _results.values();
+            return (QueryResultMapping[]) values.toArray
+                (new QueryResultMapping[values.size()]);
+        } finally {
+            unlock();
+        }
     }
 
     /**
      * Return the cached query result mapping with the given name, or null if
      * none.
      */
-    public synchronized QueryResultMapping getCachedQueryResultMapping
+    public QueryResultMapping getCachedQueryResultMapping
         (Class cls, String name) {
-        return (QueryResultMapping) _results.get(getQueryResultKey(cls, name));
+        lock();
+        try {
+            return (QueryResultMapping) _results.get(getQueryResultKey(cls, name));
+        } finally {
+            unlock();
+        }
     }
 
     /**
      * Add a query result mapping.
      */
-    public synchronized QueryResultMapping addQueryResultMapping(Class cls,
+    public QueryResultMapping addQueryResultMapping(Class cls,
         String name) {
-        QueryResultMapping res = new QueryResultMapping(name, this);
-        res.setDefiningType(cls);
-        _results.put(getQueryResultKey(res), res);
-        return res;
+        lock();
+        try {
+            QueryResultMapping res = new QueryResultMapping(name, this);
+            res.setDefiningType(cls);
+            _results.put(getQueryResultKey(res), res);
+            return res;
+        } finally {
+            unlock();
+        }
     }
 
     /**
      * Remove a query result mapping.
      */
-    public synchronized boolean removeQueryResultMapping
+    public boolean removeQueryResultMapping
         (QueryResultMapping res) {
-        return _results.remove(getQueryResultKey(res)) != null;
+        lock();
+        try {
+            return _results.remove(getQueryResultKey(res)) != null;
+        } finally {
+            unlock();
+        }
     }
 
     /**
      * Remove a query result mapping.
      */
-    public synchronized boolean removeQueryResultMapping(Class cls,
+    public boolean removeQueryResultMapping(Class cls,
         String name) {
-        if (name == null)
-            return false;
-        return _results.remove(getQueryResultKey(cls, name)) != null;
+        lock();
+        try {
+            if (name == null)
+                return false;
+            return _results.remove(getQueryResultKey(cls, name)) != null;
+        } finally {
+            unlock();
+        }
     }
 
     /**
@@ -300,10 +330,15 @@
             mustExist);
     }
 
-    public synchronized void clear() {
-        super.clear();
-        _schema = null;
-        _results.clear();
+    public void clear() {
+        lock();
+        try {
+            super.clear();
+            _schema = null;
+            _results.clear();
+        } finally {
+            unlock();
+        }
     }
 
     protected void prepareMapping(ClassMetaData meta) {

Modified: openjpa/sandboxes/perf/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
URL: http://svn.apache.org/viewvc/openjpa/sandboxes/perf/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java?rev=808130&r1=808129&r2=808130&view=diff
==============================================================================
--- openjpa/sandboxes/perf/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
(original)
+++ openjpa/sandboxes/perf/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
Wed Aug 26 17:18:43 2009
@@ -32,13 +32,14 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
+import java.util.concurrent.locks.ReentrantLock;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.openjpa.conf.OpenJPAConfiguration;
 import org.apache.openjpa.enhance.DynamicPersistenceCapable;
 import org.apache.openjpa.enhance.PCRegistry;
-import org.apache.openjpa.enhance.PCRegistry.RegisterClassListener;
 import org.apache.openjpa.enhance.PersistenceCapable;
+import org.apache.openjpa.enhance.PCRegistry.RegisterClassListener;
 import org.apache.openjpa.event.LifecycleEventManager;
 import org.apache.openjpa.lib.conf.Configurable;
 import org.apache.openjpa.lib.conf.Configuration;
@@ -51,6 +52,7 @@
 import org.apache.openjpa.util.InternalException;
 import org.apache.openjpa.util.MetaDataException;
 import org.apache.openjpa.util.OpenJPAId;
+
 import serp.util.Strings;
 
 /**
@@ -63,7 +65,7 @@
 public class MetaDataRepository
     implements PCRegistry.RegisterClassListener, Configurable, Closeable, 
     MetaDataModes, Serializable {
-
+ 
     /**
      * Constant to not validate any metadata.
      */
@@ -106,17 +108,19 @@
     // cache of parsed metadata, oid class to class, and interface class
     // to metadatas
     private final Map _metas = new HashMap();
-    private final Map _oids = Collections.synchronizedMap(new HashMap());
-    private final Map _impls = Collections.synchronizedMap(new HashMap());
-    private final Map _ifaces = Collections.synchronizedMap(new HashMap());
     private final Map _queries = new HashMap();
     private final Map _seqs = new HashMap();
-    private final Map _aliases = Collections.synchronizedMap(new HashMap());
-    private final Map _pawares = Collections.synchronizedMap(new HashMap());
-    private final Map _nonMapped = Collections.synchronizedMap(new HashMap());
+
+    // these are set in endConfiguration()
+    private Map _oids;
+    private Map _impls;
+    private Map _ifaces;
+    private Map _aliases;
+    private Map _pawares;
+    private Map _nonMapped;
     
     // map of classes to lists of their subclasses
-    private final Map _subs = Collections.synchronizedMap(new HashMap());
+    private Map _subs;
 
     // xml mapping
     protected final XMLMetaData[] EMPTY_XMLMETAS;
@@ -146,6 +150,9 @@
     private LifecycleEventManager.ListenerList _listeners =
         new LifecycleEventManager.ListenerList(3);
 
+    private ReentrantLock _lock = null;
+    private boolean _preloadNoLock = false;
+
     /**
      * Default constructor.  Configure via {@link Configurable}.
      */
@@ -272,6 +279,33 @@
             _sourceMode &= ~mode;
     }
 
+    public void setPreloadNoLock(boolean l) {
+		_preloadNoLock = l;
+	}
+    
+    private void preload() {
+		Set<String> classes = getPersistentTypeNames(false, null);
+		if (classes == null || classes.size() == 0) {
+			throw new RuntimeException(
+					"No persistent classes listed when trying to preload the MetaDataRepository");
+		}
+		for (String c : classes) {
+			getMetaData(c, null, false);
+		}
+	}
+    
+    protected void lock() {
+        if (_lock != null) {
+            _lock.lock();
+        }
+    }
+
+    protected void unlock() {
+        if (_lock != null) {
+            _lock.unlock();
+        }
+    }    
+
     /**
      * Return the metadata for the given class.
      *
@@ -280,33 +314,38 @@
      * @param mustExist if true, throws a {@link MetaDataException}
      * if no metadata is found
      */
-    public synchronized ClassMetaData getMetaData(Class cls,
+    public ClassMetaData getMetaData(Class cls,
         ClassLoader envLoader, boolean mustExist) {
-        if (cls != null &&
-            DynamicPersistenceCapable.class.isAssignableFrom(cls))
-            cls = cls.getSuperclass();
-
-        // if cls is a generated interface, use the user interface
-        // to locate metadata
-        if (cls != null && _implGen.isImplType(cls))
-            cls = _implGen.toManagedInterface(cls);
-
-        ClassMetaData meta = getMetaDataInternal(cls, envLoader);
-        if (meta == null && mustExist) {
+        lock();
+        try {
             if (cls != null &&
-                !ImplHelper.isManagedType(_conf, cls))
-                throw new MetaDataException(_loc.get("no-meta-notpc", cls)).
-                    setFatal(false);
+                DynamicPersistenceCapable.class.isAssignableFrom(cls))
+                cls = cls.getSuperclass();
 
-            Set pcNames = getPersistentTypeNames(false, envLoader);
-            if (pcNames != null && pcNames.size() > 0)
-                throw new MetaDataException(_loc.get("no-meta-types",
-                    cls, pcNames));
+            // if cls is a generated interface, use the user interface
+            // to locate metadata
+            if (cls != null && _implGen.isImplType(cls))
+                cls = _implGen.toManagedInterface(cls);
+
+            ClassMetaData meta = getMetaDataInternal(cls, envLoader);
+            if (meta == null && mustExist) {
+                if (cls != null &&
+                    !ImplHelper.isManagedType(_conf, cls))
+                    throw new MetaDataException(_loc.get("no-meta-notpc", cls)).
+                        setFatal(false);
+
+                Set pcNames = getPersistentTypeNames(false, envLoader);
+                if (pcNames != null && pcNames.size() > 0)
+                    throw new MetaDataException(_loc.get("no-meta-types",
+                        cls, pcNames));
 
-            throw new MetaDataException(_loc.get("no-meta", cls));
+                throw new MetaDataException(_loc.get("no-meta", cls));
+            }
+            resolve(meta);
+            return meta;
+        } finally {
+            unlock();
         }
-        resolve(meta);
-        return meta;
     }
 
     /**
@@ -369,8 +408,13 @@
             return null;
         }
 
-        // record that this is an invalid type
-        _aliases.put(alias, null);
+        // We need to synchronize on _aliases because a
+        // ConcurrentModificationException can if there is a thread
+        // in getAliasNames() AND this class isn't using any locking.
+        synchronized (_aliases) {
+            // record that this is an invalid type
+            _aliases.put(alias, null);
+        }
 
         if (!mustExist)
             return null;
@@ -487,6 +531,7 @@
         if (meta != null)
             removeMetaData(meta);
         _metas.put(cls, null);
+
         return null;
     }
 
@@ -741,27 +786,32 @@
     /**
      * Return all the metadata instances currently in the repository.
      */
-    public synchronized ClassMetaData[] getMetaDatas() {
-        // prevent concurrent mod errors when resolving one metadata
-        // introduces others
-        ClassMetaData[] metas = (ClassMetaData[]) _metas.values().
-            toArray(new ClassMetaData[_metas.size()]);
-        for (int i = 0; i < metas.length; i++)
-            if (metas[i] != null)
-                getMetaData(metas[i].getDescribedType(),
-                    metas[i].getEnvClassLoader(), true);
+    public ClassMetaData[] getMetaDatas() {
+        lock();
+        try {
+            // prevent concurrent mod errors when resolving one metadata
+            // introduces others
+            ClassMetaData[] metas = (ClassMetaData[]) _metas.values().
+                toArray(new ClassMetaData[_metas.size()]);
+            for (int i = 0; i < metas.length; i++)
+                if (metas[i] != null)
+                    getMetaData(metas[i].getDescribedType(),
+                        metas[i].getEnvClassLoader(), true);
 
-        List resolved = new ArrayList(_metas.size());
-        ClassMetaData meta;
-        for (Iterator itr = _metas.values().iterator(); itr.hasNext();) {
-            meta = (ClassMetaData) itr.next();
-            if (meta != null)
-                resolved.add(meta);
-        }
-        metas = (ClassMetaData[]) resolved.toArray
-            (newClassMetaDataArray(resolved.size()));
-        Arrays.sort(metas);
-        return metas;
+            List resolved = new ArrayList(_metas.size());
+            ClassMetaData meta;
+            for (Iterator itr = _metas.values().iterator(); itr.hasNext();) {
+                meta = (ClassMetaData) itr.next();
+                if (meta != null)
+                    resolved.add(meta);
+            }
+            metas = (ClassMetaData[]) resolved.toArray
+                (newClassMetaDataArray(resolved.size()));
+            Arrays.sort(metas);
+            return metas;
+        } finally {
+            unlock();
+        }
     }
 
     /**
@@ -795,10 +845,13 @@
 
         // synchronize on this rather than the map, because all other methods
         // that access _metas are synchronized on this
-        synchronized (this) {
+        lock();
+        try {
             if (_pawares.containsKey(cls))
                 throw new MetaDataException(_loc.get("pc-and-aware", cls));
             _metas.put(cls, meta);
+        } finally {
+            unlock();
         }
         return meta;
     }
@@ -919,23 +972,29 @@
      *
      * @return true if removed, false if not in this repository
      */
-    public synchronized boolean removeMetaData(Class cls) {
-        if (cls == null)
+    public boolean removeMetaData(Class cls) {
+        lock();
+        try {
+            if (cls == null)
+                return false;
+            if (_metas.remove(cls) != null) {
+                Class impl = (Class) _ifaces.remove(cls);
+                if (impl != null)
+                    _metas.remove(impl);
+                return true;
+            }
             return false;
-        if (_metas.remove(cls) != null) {
-            Class impl = (Class) _ifaces.remove(cls);
-            if (impl != null)
-                _metas.remove(impl);
-            return true;
+        } finally {
+            unlock();
         }
-        return false;
     }
 
     /**
      * Add the given metadata as declared interface implementation.
      */
     void addDeclaredInterfaceImpl(ClassMetaData meta, Class iface) {
-        synchronized (_impls) {
+        lock();
+        try {
             Collection vals = (Collection) _impls.get(iface);
             
             // check to see if the superclass already declares to avoid dups
@@ -946,25 +1005,32 @@
                         return;
             }
             addToCollection(_impls, iface, meta.getDescribedType(), false);
+        } finally {
+            unlock();
         }
     }
 
     /**
      * Set the implementation for the given managed interface.
      */
-    synchronized void setInterfaceImpl(ClassMetaData meta, Class impl) {
-        if (!meta.isManagedInterface())
-            throw new MetaDataException(_loc.get("not-managed-interface", 
-                meta, impl));
-        _ifaces.put(meta.getDescribedType(), impl);
-        addDeclaredInterfaceImpl(meta, meta.getDescribedType());
-        ClassMetaData sup = meta.getPCSuperclassMetaData();
-        while (sup != null) {
-            // record superclass interface info while we can as well as we
-            // will only register concrete superclass in PCRegistry
-            sup.clearSubclassCache();
-            addToCollection(_subs, sup.getDescribedType(), impl, true);
-            sup = (ClassMetaData) sup.getPCSuperclassMetaData();
+    void setInterfaceImpl(ClassMetaData meta, Class impl) {
+        lock();
+        try {
+            if (!meta.isManagedInterface())
+                throw new MetaDataException(_loc.get("not-managed-interface", 
+                    meta, impl));
+            _ifaces.put(meta.getDescribedType(), impl);
+            addDeclaredInterfaceImpl(meta, meta.getDescribedType());
+            ClassMetaData sup = meta.getPCSuperclassMetaData();
+            while (sup != null) {
+                // record superclass interface info while we can as well as we
+                // will only register concrete superclass in PCRegistry
+                sup.clearSubclassCache();
+                addToCollection(_subs, sup.getDescribedType(), impl, true);
+                sup = (ClassMetaData) sup.getPCSuperclassMetaData();
+            }
+        } finally {
+            unlock();
         }
     }
     
@@ -1091,7 +1157,8 @@
         ClassMetaData meta;
         Collection mapped = null;
         if (vals != null) {
-            synchronized (vals) {
+            lock();
+            try {
                 for (Iterator itr = vals.iterator(); itr.hasNext();) {
                     meta = getMetaData((Class) itr.next(), envLoader, true);
                     if (meta.isMapped()
@@ -1101,6 +1168,8 @@
                         mapped.add(meta);
                     }
                 }
+            } finally {
+                unlock();
             }
         }
 
@@ -1127,11 +1196,14 @@
      * @return empty array if no class has been registered as pers-aware
      */
     public NonPersistentMetaData[] getPersistenceAwares() {
-        synchronized (_pawares) {
+        lock();
+        try {
             if (_pawares.isEmpty())
                 return EMPTY_NON_PERSISTENT;
             return (NonPersistentMetaData[])_pawares.values().toArray
                 (new NonPersistentMetaData[_pawares.size()]);
+        } finally {
+            unlock();
         }
     }
 
@@ -1143,7 +1215,8 @@
     public NonPersistentMetaData addPersistenceAware(Class cls) {
     	if (cls == null)
     		return null;
-        synchronized(this) {
+        lock();
+        try {
             if (_pawares.containsKey(cls))
                 return (NonPersistentMetaData)_pawares.get(cls);
             if (getCachedMetaData(cls) != null)
@@ -1152,7 +1225,9 @@
                 NonPersistentMetaData.TYPE_PERSISTENCE_AWARE);
             _pawares.put(cls, meta);
             return meta;
-    	}
+    	} finally {
+            unlock();
+        }
     }
 
     /**
@@ -1180,11 +1255,14 @@
      * @return empty array if no non-mapped interface has been registered.
      */
     public NonPersistentMetaData[] getNonMappedInterfaces() {
-        synchronized (_nonMapped) {
+        lock();
+        try {
             if (_nonMapped.isEmpty())
                 return EMPTY_NON_PERSISTENT;
             return (NonPersistentMetaData[])_nonMapped.values().toArray
                 (new NonPersistentMetaData[_nonMapped.size()]);
+        } finally {
+            unlock();
         }
     }
 
@@ -1198,7 +1276,8 @@
     		return null;
         if (!iface.isInterface())
             throw new MetaDataException(_loc.get("not-non-mapped", iface));
-        synchronized(this) {
+        lock();
+        try {
             if (_nonMapped.containsKey(iface))
                 return (NonPersistentMetaData)_nonMapped.get(iface);
             if (getCachedMetaData(iface) != null)
@@ -1207,7 +1286,9 @@
                 NonPersistentMetaData.TYPE_NON_MAPPED_INTERFACE);
             _nonMapped.put(iface, meta);
             return meta;
-    	}
+    	} finally {
+            unlock();
+        }
     }
 
     /**
@@ -1223,21 +1304,26 @@
      * Clear the cache of parsed metadata. This method also clears the
      * internal {@link MetaDataFactory MetaDataFactory}'s cache.
      */
-    public synchronized void clear() {
-        if (_log.isTraceEnabled())
-            _log.trace(_loc.get("clear-repos", this));
+    public void clear() {
+        lock();
+        try {
+            if (_log.isTraceEnabled())
+                _log.trace(_loc.get("clear-repos", this));
 
-        _metas.clear();
-        _oids.clear();
-        _subs.clear();
-        _impls.clear();
-        _queries.clear();
-        _seqs.clear();
-        _registered.clear();
-        _factory.clear();
-        _aliases.clear();
-        _pawares.clear();
-        _nonMapped.clear();
+            _metas.clear();
+            _oids.clear();
+            _subs.clear();
+            _impls.clear();
+            _queries.clear();
+            _seqs.clear();
+            _registered.clear();
+            _factory.clear();
+            _aliases.clear();
+            _pawares.clear();
+            _nonMapped.clear();
+        } finally {
+            unlock();
+        }
     }
 
     /**
@@ -1248,9 +1334,14 @@
      * in the classpath if no classes are configured explicitly
      * @param envLoader the class loader to use, or null for default
      */
-    public synchronized Set getPersistentTypeNames(boolean devpath,
+    public Set getPersistentTypeNames(boolean devpath,
         ClassLoader envLoader) {
-        return _factory.getPersistentTypeNames(devpath, envLoader);
+        lock();
+        try {
+            return _factory.getPersistentTypeNames(devpath, envLoader);
+        } finally {
+            unlock();
+        }
     }
 
     /**
@@ -1265,29 +1356,34 @@
      * @param envLoader the class loader to use, or null for default
      * @return the loaded classes, or empty collection if none
      */
-    public synchronized Collection loadPersistentTypes(boolean devpath,
+    public Collection loadPersistentTypes(boolean devpath,
         ClassLoader envLoader) {
-        Set names = getPersistentTypeNames(devpath, envLoader);
-        if (names == null || names.isEmpty())
-            return Collections.EMPTY_LIST;
-
-        // attempt to load classes so that they get processed
-        ClassLoader clsLoader = _conf.getClassResolverInstance().
-            getClassLoader(getClass(), envLoader);
-        List classes = new ArrayList(names.size());
-        Class cls;
-        for (Iterator itr = names.iterator(); itr.hasNext();) {
-            cls = classForName((String) itr.next(), clsLoader);
-            if (cls != null) {
-                classes.add(cls);
-
-                // if the class is an interface, load its metadata to kick
-                // off the impl generator
-                if (cls.isInterface())
-                    getMetaData(cls, clsLoader, false);
+        lock();
+        try {
+            Set names = getPersistentTypeNames(devpath, envLoader);
+            if (names == null || names.isEmpty())
+                return Collections.EMPTY_LIST;
+
+            // attempt to load classes so that they get processed
+            ClassLoader clsLoader = _conf.getClassResolverInstance().
+                getClassLoader(getClass(), envLoader);
+            List classes = new ArrayList(names.size());
+            Class cls;
+            for (Iterator itr = names.iterator(); itr.hasNext();) {
+                cls = classForName((String) itr.next(), clsLoader);
+                if (cls != null) {
+                    classes.add(cls);
+
+                    // if the class is an interface, load its metadata to kick
+                    // off the impl generator
+                    if (cls.isInterface())
+                        getMetaData(cls, clsLoader, false);
+                }
             }
+            return classes;
+        } finally {
+            unlock();
         }
-        return classes;
     }
 
     /**
@@ -1341,8 +1437,11 @@
     public void register(Class cls) {
         // buffer registered classes until an oid metadata request is made,
         // at which point we'll parse everything in the buffer
-        synchronized (_registered) {
+        lock();
+        try {
             _registered.add(cls);
+        } finally {
+            unlock();
         }
     }
 
@@ -1371,9 +1470,12 @@
         // copy into new collection to avoid concurrent mod errors on reentrant
         // registrations
         Class[] reg;
-        synchronized (_registered) {
+        lock();
+        try {
             reg = (Class[]) _registered.toArray(new Class[_registered.size()]);
             _registered.clear();
+        } finally {
+            unlock();
         }
 
         Collection pcNames = getPersistentTypeNames(false, envLoader);
@@ -1400,8 +1502,11 @@
             }
         }
         if (failed != null) {
-            synchronized (_registered) {
+            lock();
+            try {
                 _registered.addAll(failed);
+            } finally {
+                unlock();
             }
         }
         return reg;
@@ -1420,7 +1525,8 @@
         // update subclass lists; synchronize on this because accessing _metas
         // requires it
         Class leastDerived = cls;
-        synchronized (this) {
+        lock();
+        try {
             ClassMetaData meta;
             for (Class anc = cls;
                 (anc = PCRegistry.getPersistentSuperclass(anc)) != null;) {
@@ -1430,6 +1536,8 @@
                     meta.clearSubclassCache();
                 leastDerived = anc;
             }
+        } finally {
+            unlock();
         }
 
         // update oid mappings if this is a base concrete class
@@ -1466,7 +1574,8 @@
         // set alias for class
         String alias = PCRegistry.getTypeAlias(cls);
         if (alias != null) {
-            synchronized (_aliases) {
+            lock();
+            try {
                 List classList = (List) _aliases.get(alias);
                 if (classList == null) {
                     classList = new ArrayList(3);
@@ -1474,6 +1583,8 @@
                 }
                 if (!classList.contains(cls))
                     classList.add(cls);
+            } finally {
+                unlock();
             }
         }
     }
@@ -1527,7 +1638,8 @@
      */
     private void addToCollection(Map map, Class key, Class value,
         boolean inheritance) {
-        synchronized (map) {
+        lock();
+        try {
             Collection coll = (Collection) map.get(key);
             if (coll == null) {
                 if (inheritance) {
@@ -1539,6 +1651,8 @@
                 map.put(key, coll);
             }
             coll.add(value);
+        } finally {
+            unlock();
         }
     }
 
@@ -1555,9 +1669,36 @@
     }
 
     public void endConfiguration() {
+        if (_preloadNoLock == false) {
+            _oids = Collections.synchronizedMap(new HashMap());
+            _impls = Collections.synchronizedMap(new HashMap());
+            _ifaces = Collections.synchronizedMap(new HashMap());
+            _aliases = Collections.synchronizedMap(new HashMap());
+            _pawares = Collections.synchronizedMap(new HashMap());
+            _nonMapped = Collections.synchronizedMap(new HashMap());
+            _subs = Collections.synchronizedMap(new HashMap());
+            _lock = new ReentrantLock();
+        } else {
+            if (_log.isTraceEnabled()){
+                _log.trace("MetaDataRepository _preloadNoLock == true");
+            }
+            _oids = new HashMap();
+            _impls = new HashMap();
+            _ifaces = new HashMap();
+            _aliases = new HashMap();
+            _pawares = new HashMap();
+            _nonMapped = new HashMap();
+            _subs = new HashMap();
+            _lock = null;
+        }
+
         initializeMetaDataFactory();
-        if (_implGen == null)
+        if (_implGen == null) {
             _implGen = new InterfaceImplGenerator(this);
+        }
+        if (_preloadNoLock == true) {
+            preload();
+        }
     }
 
     private void initializeMetaDataFactory() {
@@ -1576,28 +1717,33 @@
     /**
      * Return query metadata for the given class, name, and classloader.
      */
-    public synchronized QueryMetaData getQueryMetaData(Class cls, String name,
+    public QueryMetaData getQueryMetaData(Class cls, String name,
         ClassLoader envLoader, boolean mustExist) {
-        QueryMetaData meta = getQueryMetaDataInternal(cls, name, envLoader);
-        if (meta == null) {
-            // load all the metadatas for all the known classes so that
-            // query names are seen and registered
-            resolveAll(envLoader);
-            meta = getQueryMetaDataInternal(cls, name, envLoader);
-        }
+        lock();
+        try {
+            QueryMetaData meta = getQueryMetaDataInternal(cls, name, envLoader);
+            if (meta == null) {
+                // load all the metadatas for all the known classes so that
+                // query names are seen and registered
+                resolveAll(envLoader);
+                meta = getQueryMetaDataInternal(cls, name, envLoader);
+            }
 
-        if (meta == null && mustExist) {
-            if (cls == null) {
-                throw new MetaDataException(_loc.get
-                    ("no-named-query-null-class", 
-                        getPersistentTypeNames(false, envLoader), name));
-            } else {
-                throw new MetaDataException(_loc.get("no-named-query",
-                    cls, name));
+            if (meta == null && mustExist) {
+                if (cls == null) {
+                    throw new MetaDataException(_loc.get(
+                        "no-named-query-null-class", getPersistentTypeNames(
+                            false, envLoader), name));
+                } else {
+                    throw new MetaDataException(_loc.get("no-named-query", cls,
+                        name));
+                }
             }
-        }
 
-        return meta;
+            return meta;
+        } finally {
+            unlock();
+        }
     }
 
     /** 
@@ -1646,23 +1792,33 @@
     /**
      * Return the cached query metadata.
      */
-    public synchronized QueryMetaData[] getQueryMetaDatas() {
-        return (QueryMetaData[]) _queries.values().toArray
-            (new QueryMetaData[_queries.size()]);
+    public QueryMetaData[] getQueryMetaDatas() {
+        lock();
+        try {
+            return (QueryMetaData[]) _queries.values().toArray
+                (new QueryMetaData[_queries.size()]);
+        } finally {
+            unlock();
+        }
     }
 
     /**
      * Return the cached query metadata for the given name.
      */
-    public synchronized QueryMetaData getCachedQueryMetaData(Class cls,
+    public QueryMetaData getCachedQueryMetaData(Class cls,
         String name) {
-        return (QueryMetaData) _queries.get(getQueryKey(cls, name));
+        lock();
+        try {
+            return (QueryMetaData) _queries.get(getQueryKey(cls, name));
+        } finally {
+            unlock();
+        }
     }
 
     /**
      * Add a new query metadata to the repository and return it.
      */
-    public synchronized QueryMetaData addQueryMetaData(Class cls, String name) {
+    public QueryMetaData addQueryMetaData(Class cls, String name) {
         QueryMetaData meta = newQueryMetaData(cls, name);
         _queries.put(getQueryKey(meta), meta);
         return meta;
@@ -1672,27 +1828,42 @@
      * Create a new query metadata instance.
      */
     protected QueryMetaData newQueryMetaData(Class cls, String name) {
-        QueryMetaData meta = new QueryMetaData(name);
-        meta.setDefiningType(cls);
-        return meta;
+        lock();
+        try {
+            QueryMetaData meta = new QueryMetaData(name);
+            meta.setDefiningType(cls);
+            return meta;
+        } finally {
+            unlock();
+        }
     }
 
     /**
      * Remove the given query metadata from the repository.
      */
-    public synchronized boolean removeQueryMetaData(QueryMetaData meta) {
-        if (meta == null)
-            return false;
-        return _queries.remove(getQueryKey(meta)) != null;
+    public boolean removeQueryMetaData(QueryMetaData meta) {
+        lock();
+        try {
+            if (meta == null)
+                return false;
+            return _queries.remove(getQueryKey(meta)) != null;
+        } finally {
+            unlock();
+        }
     }
 
     /**
      * Remove query metadata for the given class name if in the repository.
      */
-    public synchronized boolean removeQueryMetaData(Class cls, String name) {
-        if (name == null)
-            return false;
-        return _queries.remove(getQueryKey(cls, name)) != null;
+    public boolean removeQueryMetaData(Class cls, String name) {
+        lock();
+        try {
+            if (name == null)
+                return false;
+            return _queries.remove(getQueryKey(cls, name)) != null;
+        } finally {
+            unlock();
+        }
     }
 
     /**
@@ -1724,17 +1895,24 @@
     /**
      * Return sequence metadata for the given name and classloader.
      */
-    public synchronized SequenceMetaData getSequenceMetaData(String name,
+    public SequenceMetaData getSequenceMetaData(String name,
         ClassLoader envLoader, boolean mustExist) {
-        SequenceMetaData meta = getSequenceMetaDataInternal(name, envLoader);
-        if (meta == null && SequenceMetaData.NAME_SYSTEM.equals(name)) {
-            if (_sysSeq == null)
-                _sysSeq = newSequenceMetaData(name);
-            return _sysSeq;
+        lock();
+        try {
+            SequenceMetaData meta = getSequenceMetaDataInternal(
+                name, envLoader);
+            if (meta == null && SequenceMetaData.NAME_SYSTEM.equals(name)) {
+                if (_sysSeq == null)
+                    _sysSeq = newSequenceMetaData(name);
+                return _sysSeq;
+            }
+            if (meta == null && mustExist)
+                throw new MetaDataException(
+                    _loc.get("no-named-sequence", name));
+            return meta;
+        } finally {
+            unlock();
         }
-        if (meta == null && mustExist)
-            throw new MetaDataException(_loc.get("no-named-sequence", name));
-        return meta;
     }
 
     /**
@@ -1797,26 +1975,41 @@
     /**
      * Return the cached sequence metadata.
      */
-    public synchronized SequenceMetaData[] getSequenceMetaDatas() {
-        return (SequenceMetaData[]) _seqs.values().toArray
-            (new SequenceMetaData[_seqs.size()]);
+    public SequenceMetaData[] getSequenceMetaDatas() {
+        lock();
+        try {
+            return (SequenceMetaData[]) _seqs.values().toArray
+                (new SequenceMetaData[_seqs.size()]);
+        } finally {
+            unlock();
+        }
     }
 
     /**
      * Return the cached a sequence metadata for the given name.
      */
-    public synchronized SequenceMetaData getCachedSequenceMetaData(
+    public SequenceMetaData getCachedSequenceMetaData(
         String name) {
-        return (SequenceMetaData) _seqs.get(name);
+        lock();
+        try {
+            return (SequenceMetaData) _seqs.get(name);
+        } finally {
+            unlock();
+        }
     }
 
     /**
      * Add a new sequence metadata to the repository and return it.
      */
-    public synchronized SequenceMetaData addSequenceMetaData(String name) {
-        SequenceMetaData meta = newSequenceMetaData(name);
-        _seqs.put(name, meta);
-        return meta;
+    public SequenceMetaData addSequenceMetaData(String name) {
+        lock();
+        try {
+            SequenceMetaData meta = newSequenceMetaData(name);
+            _seqs.put(name, meta);
+            return meta;
+        } finally {
+            unlock();
+        }
     }
 
     /**
@@ -1829,47 +2022,67 @@
     /**
      * Remove the given sequence metadata from the repository.
      */
-    public synchronized boolean removeSequenceMetaData(SequenceMetaData meta) {
-        if (meta == null)
-            return false;
-        return _seqs.remove(meta.getName()) != null;
+    public boolean removeSequenceMetaData(SequenceMetaData meta) {
+        lock();
+        try {
+            if (meta == null)
+                return false;
+            return _seqs.remove(meta.getName()) != null;
+        } finally {
+            unlock();
+        }
     }
 
     /**
      * Remove sequence metadata for the name if in the repository.
      */
-    public synchronized boolean removeSequenceMetaData(String name) {
-        if (name == null)
-            return false;
-        return _seqs.remove(name) != null;
+    public boolean removeSequenceMetaData(String name) {
+        lock();
+        try {
+            if (name == null)
+                return false;
+            return _seqs.remove(name) != null;
+        } finally {
+            unlock();
+        }
     }
 
     /**
      * Add the given system lifecycle listener.
      */
-    public synchronized void addSystemListener(Object listener) {
-        // copy to avoid issues with ListenerList and avoid unncessary
-        // locking on the list during runtime
-        LifecycleEventManager.ListenerList listeners = new
-            LifecycleEventManager.ListenerList(_listeners);
-        listeners.add(listener);
-        _listeners = listeners;
+    public void addSystemListener(Object listener) {
+        lock();
+        try {
+            // copy to avoid issues with ListenerList and avoid unncessary
+            // locking on the list during runtime
+            LifecycleEventManager.ListenerList listeners = new
+                LifecycleEventManager.ListenerList(_listeners);
+            listeners.add(listener);
+            _listeners = listeners;
+        } finally {
+            unlock();
+        }
     }
 
     /**
      * Remove the given system lifecycle listener.
      */
-    public synchronized boolean removeSystemListener(Object listener) {
-        if (!_listeners.contains(listener))
-            return false;
+    public boolean removeSystemListener(Object listener) {
+        lock();
+        try {
+            if (!_listeners.contains(listener))
+                return false;
 
-        // copy to avoid issues with ListenerList and avoid unncessary
-        // locking on the list during runtime
-        LifecycleEventManager.ListenerList listeners = new
-            LifecycleEventManager.ListenerList(_listeners);
-        listeners.remove(listener);
-        _listeners = listeners;
-        return true;
+            // copy to avoid issues with ListenerList and avoid unncessary
+            // locking on the list during runtime
+            LifecycleEventManager.ListenerList listeners = new
+                LifecycleEventManager.ListenerList(_listeners);
+            listeners.remove(listener);
+            _listeners = listeners;
+            return true;
+        } finally {
+            unlock();
+        }
     }
 
     /**
@@ -1882,11 +2095,16 @@
     /**
      * Free the resources used by this repository. Closes all user sequences.
      */
-    public synchronized void close() {
-        SequenceMetaData[] smds = getSequenceMetaDatas();
-        for (int i = 0; i < smds.length; i++)
-            smds[i].close();
-        clear();
+    public void close() {
+        lock();
+        try {
+            SequenceMetaData[] smds = getSequenceMetaDatas();
+            for (int i = 0; i < smds.length; i++)
+                smds[i].close();
+            clear();
+        } finally {
+            unlock();
+        }
     }
 
     /**
@@ -1922,19 +2140,24 @@
      * @param fmd
      * @return XML metadata
      */
-    public synchronized XMLMetaData getXMLMetaData(FieldMetaData fmd) {
-        Class cls = fmd.getDeclaredType();
-        // check if cached before
-        XMLMetaData xmlmeta = (XMLClassMetaData) _xmlmetas.get(cls);
-        if (xmlmeta != null)
-            return xmlmeta;
-        
-        // load JAXB XML metadata
-        _factory.loadXMLMetaData(fmd);
-        
-        xmlmeta = (XMLClassMetaData) _xmlmetas.get(cls);
-
-        return xmlmeta;
+    public XMLMetaData getXMLMetaData(FieldMetaData fmd) {
+    	try{
+    		lock();
+	        Class cls = fmd.getDeclaredType();
+	        // check if cached before
+	        XMLMetaData xmlmeta = (XMLClassMetaData) _xmlmetas.get(cls);
+	        if (xmlmeta != null)
+	            return xmlmeta;
+	        
+	        // load JAXB XML metadata
+	        _factory.loadXMLMetaData(fmd);
+	        
+	        xmlmeta = (XMLClassMetaData) _xmlmetas.get(cls);
+	
+	        return xmlmeta;
+    	}finally{
+    		unlock();
+    	}
     }
 
     /**
@@ -1946,10 +2169,11 @@
     public XMLClassMetaData addXMLMetaData(Class type, String name) {
         XMLClassMetaData meta = newXMLClassMetaData(type, name);
         
-        // synchronize on this rather than the map, because all other methods
-        // that access _xmlmetas are synchronized on this
-        synchronized (this) {
+        try{
+        	lock();
             _xmlmetas.put(type, meta);
+        }finally{
+        	unlock();
         }
         return meta;
     }



Mime
View raw message