You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2006/07/12 01:42:44 UTC

svn commit: r421032 - /db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/MetadataManager.java

Author: arminw
Date: Tue Jul 11 16:42:43 2006
New Revision: 421032

URL: http://svn.apache.org/viewvc?rev=421032&view=rev
Log:
make RepositoryPersistor class pluggable

Modified:
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/MetadataManager.java

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/MetadataManager.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/MetadataManager.java?rev=421032&r1=421031&r2=421032&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/MetadataManager.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/MetadataManager.java Tue Jul 11 16:42:43 2006
@@ -41,9 +41,9 @@
  * Central class for metadata operations/manipulations - manages OJB's
  * metadata objects, in particular:
  * <ul>
- * <li>{@link org.apache.ojb.broker.metadata.DescriptorRepository} contains
+ * <li>{@link DescriptorRepository} contains
  * metadata of persistent objects</li>
- * <li>{@link org.apache.ojb.broker.metadata.ConnectionRepository} contains
+ * <li>{@link ConnectionRepository} contains
  * all connection metadata information</li>
  * </ul>
  *
@@ -51,8 +51,8 @@
  *
  * <p>
  * <b>How to read/merge metadata</b><br/>
- * Per default OJB loads default {@link org.apache.ojb.broker.metadata.DescriptorRepository}
- * and {@link org.apache.ojb.broker.metadata.ConnectionRepository} instances, by reading the
+ * Per default OJB loads default {@link DescriptorRepository}
+ * and {@link ConnectionRepository} instances, by reading the
  * specified repository file. This is done first time the <code>MetadataManager</code> instance
  * was used.
  * <br/>
@@ -70,16 +70,16 @@
  * <a name="perThread"/>
  * <h3>Per thread handling of metadata</h3>
  * <p>
- * Per default the manager handle one global {@link org.apache.ojb.broker.metadata.DescriptorRepository}
+ * Per default the manager handle one global {@link DescriptorRepository}
  * for all calling threads, but it is ditto possible to use different metadata <i>profiles</i> in a per thread
- * manner - <i>profiles</i> means different copies of {@link org.apache.ojb.broker.metadata.DescriptorRepository}
+ * manner - <i>profiles</i> means different copies of {@link DescriptorRepository}
  * objects.
  * <p/>
  *
  * <p>
  * <a name="enablePerThreadMode"/>
  * <b>Enable the per thread mode</b><br/>
- * To enable the 'per thread' mode for {@link org.apache.ojb.broker.metadata.DescriptorRepository}
+ * To enable the 'per thread' mode for {@link DescriptorRepository}
  * instances:
  * <pre>
  *   MetadataManager mm = MetadataManager.getInstance();
@@ -90,7 +90,7 @@
  * This could be done e.g. at start up.<br/>
  * Now it's possible to use dedicated <code>DescriptorRepository</code> instances
  * per thread:
- *  <pre>
+ * <pre>
  *   // e.g we get a coppy of the global repository
  *   DescriptorRepository dr = mm.copyOfGlobalRepository();
  *   // now we can manipulate the persistent object metadata of the copy
@@ -112,7 +112,7 @@
  * <b>How to use different metadata profiles</b><br/>
  * MetadataManager was shipped with a simple mechanism to
  * add, remove and load different persistent objects metadata
- * profiles (different {@link org.apache.ojb.broker.metadata.DescriptorRepository}
+ * profiles (different {@link DescriptorRepository}
  * instances) in a per thread manner. Use
  * <ul>
  * <li>{@link #addProfile addProfile} add different persistent object metadata profiles</li>
@@ -142,6 +142,7 @@
     private ConnectionRepository connectionRepository;
     private boolean enablePerThreadChanges;
     private PBKey defaultPBKey;
+    private Class repositoryPersistorClass;
 
     // singleton
     private MetadataManager()
@@ -153,6 +154,7 @@
     {
         metadataProfiles = new Hashtable();
         Configuration conf = OjbConfigurator.getInstance().getConfigurationFor(null);
+        repositoryPersistorClass = conf.getClass("RepositoryPersistorClass", RepositoryPersistor.class);
         boolean useSerializedRepository = ((MetadataConfiguration) conf).useSerializedRepository();
         File serFile = null;
         if(useSerializedRepository)
@@ -162,7 +164,7 @@
             serFile = new File(pathPrefix + File.separator + OjbConfiguration.OJB_METADATA_FILE
                     + "." + SER_FILE_SUFFIX);
 
-            if (serFile.exists() && serFile.length() > 0)
+            if(serFile.exists() && serFile.length() > 0)
             {
                 try
                 {
@@ -181,24 +183,24 @@
         {
             if(globalRepository == null)
             {
-                globalRepository     = new RepositoryPersistor().readDescriptorRepository(url);
+                globalRepository = createRepositoryPersistor().readDescriptorRepository(url);
                 if(useSerializedRepository)
                 {
                     serialize(globalRepository, serFile);
                     log.info("Write serialized repository to " + serFile);
                 }
             }
-            connectionRepository = new RepositoryPersistor().readConnectionRepository(url);
+            connectionRepository = createRepositoryPersistor().readConnectionRepository(url);
         }
-        catch (FileNotFoundException ex)
+        catch(FileNotFoundException ex)
         {
             log.warn("Could not access '" + url + "' or a DOCTYPE/DTD-dependency. "
-                     + "(Check letter case for file names and HTTP-access if using DOCTYPE PUBLIC)"
-                     + " Starting with empty metadata and connection configurations.", ex);
-            globalRepository     = new DescriptorRepository();
+                    + "(Check letter case for file names and HTTP-access if using DOCTYPE PUBLIC)"
+                    + " Starting with empty metadata and connection configurations.", ex);
+            globalRepository = new DescriptorRepository();
             connectionRepository = new ConnectionRepository();
         }
-        catch (Exception ex)
+        catch(Exception ex)
         {
             throw new MetadataException("Can't read repository file '" + url + "'", ex);
         }
@@ -213,13 +215,11 @@
         singleton = null;
     }
 
-    /**
-     * Returns an instance of this class.
-     */
+    /** Returns an instance of this class. */
     public static synchronized MetadataManager getInstance()
     {
         // lazy initialization
-        if (singleton == null)
+        if(singleton == null)
         {
             singleton = new MetadataManager();
         }
@@ -241,10 +241,10 @@
     public DescriptorRepository getRepository()
     {
         DescriptorRepository repository;
-        if (enablePerThreadChanges)
+        if(enablePerThreadChanges)
         {
             repository = (DescriptorRepository) threadedRepository.get();
-            if (repository == null)
+            if(repository == null)
             {
                 repository = getGlobalRepository();
                 log.info(MSG_STR);
@@ -276,9 +276,7 @@
         return globalRepository;
     }
 
-    /**
-     * Returns the {@link ConnectionRepository}.
-     */
+    /** Returns the {@link ConnectionRepository}. */
     public ConnectionRepository connectionRepository()
     {
         return connectionRepository;
@@ -287,6 +285,7 @@
     /**
      * Merge the given {@link ConnectionRepository} with the existing one (without making
      * a deep copy of the containing connection descriptors).
+     *
      * @see #mergeConnectionRepository(ConnectionRepository targetRepository, ConnectionRepository sourceRepository, boolean deep)
      */
     public void mergeConnectionRepository(ConnectionRepository repository)
@@ -310,10 +309,10 @@
             ConnectionRepository targetRepository, ConnectionRepository sourceRepository, boolean deep)
     {
         List list = sourceRepository.getAllDescriptor();
-        for (Iterator iterator = list.iterator(); iterator.hasNext();)
+        for(Iterator iterator = list.iterator(); iterator.hasNext();)
         {
             JdbcConnectionDescriptor jcd = (JdbcConnectionDescriptor) iterator.next();
-            if (deep)
+            if(deep)
             {
                 //TODO: adopt copy/clone methods for metadata classes?
                 jcd = (JdbcConnectionDescriptor) SerializationUtils.clone(jcd);
@@ -355,10 +354,10 @@
             DescriptorRepository targetRepository, DescriptorRepository sourceRepository, boolean deep)
     {
         Iterator it = sourceRepository.iterator();
-        while (it.hasNext())
+        while(it.hasNext())
         {
             ClassDescriptor cld = (ClassDescriptor) it.next();
-            if (deep)
+            if(deep)
             {
                 //TODO: adopt copy/clone methods for metadata classes?
                 cld = (ClassDescriptor) SerializationUtils.clone(cld);
@@ -370,16 +369,17 @@
 
     /**
      * Read ClassDescriptors from the given repository file.
+     *
      * @see #mergeDescriptorRepository
      */
     public DescriptorRepository readDescriptorRepository(String fileName)
     {
         try
         {
-            RepositoryPersistor persistor = new RepositoryPersistor();
+            RepositoryPersistor persistor = createRepositoryPersistor();
             return persistor.readDescriptorRepository(fileName);
         }
-        catch (Exception e)
+        catch(Exception e)
         {
             throw new MetadataException("Can not read repository " + fileName, e);
         }
@@ -387,16 +387,17 @@
 
     /**
      * Read ClassDescriptors from the given InputStream.
+     *
      * @see #mergeDescriptorRepository
      */
     public DescriptorRepository readDescriptorRepository(InputStream inst)
     {
         try
         {
-            RepositoryPersistor persistor = new RepositoryPersistor();
+            RepositoryPersistor persistor = createRepositoryPersistor();
             return persistor.readDescriptorRepository(inst);
         }
-        catch (Exception e)
+        catch(Exception e)
         {
             throw new MetadataException("Can not read repository " + inst, e);
         }
@@ -411,10 +412,10 @@
     {
         try
         {
-            RepositoryPersistor persistor = new RepositoryPersistor();
+            RepositoryPersistor persistor = createRepositoryPersistor();
             return persistor.readConnectionRepository(fileName);
         }
-        catch (Exception e)
+        catch(Exception e)
         {
             throw new MetadataException("Can not read repository " + fileName, e);
         }
@@ -429,10 +430,10 @@
     {
         try
         {
-            RepositoryPersistor persistor = new RepositoryPersistor();
+            RepositoryPersistor persistor = createRepositoryPersistor();
             return persistor.readConnectionRepository(inst);
         }
-        catch (Exception e)
+        catch(Exception e)
         {
             throw new MetadataException("Can not read repository from " + inst, e);
         }
@@ -448,14 +449,14 @@
      */
     public void setDescriptor(DescriptorRepository repository, boolean global)
     {
-        if (global)
+        if(global)
         {
-            if (log.isDebugEnabled()) log.debug("Set new global repository: " + repository);
+            if(log.isDebugEnabled()) log.debug("Set new global repository: " + repository);
             globalRepository = repository;
         }
         else
         {
-            if (log.isDebugEnabled()) log.debug("Set new threaded repository: " + repository);
+            if(log.isDebugEnabled()) log.debug("Set new threaded repository: " + repository);
             threadedRepository.set(repository);
         }
     }
@@ -473,6 +474,7 @@
     /**
      * Convenience method for
      * {@link #setDescriptor setDescriptor(repository, false)}.
+     *
      * @deprecated use {@link #setDescriptor}
      */
     public void setPerThreadDescriptor(DescriptorRepository repository)
@@ -524,29 +526,27 @@
 
     /**
      * Add a metadata profile.
+     *
      * @see #loadProfile
      */
     public void addProfile(Object key, DescriptorRepository repository)
     {
-        if (metadataProfiles.contains(key))
+        if(metadataProfiles.contains(key))
         {
             throw new MetadataException("Duplicate profile key. Key '" + key + "' already exists.");
         }
         metadataProfiles.put(key, repository);
     }
 
-    /**
-     * Load the given metadata profile for the current thread.
-     *
-     */
+    /** Load the given metadata profile for the current thread. */
     public void loadProfile(Object key)
     {
-        if (!isEnablePerThreadChanges())
+        if(!isEnablePerThreadChanges())
         {
             throw new MetadataException("Can not load profile with disabled per thread mode");
         }
         DescriptorRepository rep = (DescriptorRepository) metadataProfiles.get(key);
-        if (rep == null)
+        if(rep == null)
         {
             throw new MetadataException("Can not find profile for key '" + key + "'");
         }
@@ -556,30 +556,27 @@
 
     /**
      * Returns the last activated profile key.
+     *
      * @return the last activated profile key or null if no profile has been loaded
      * @throws MetadataException if per-thread changes has not been activated
      * @see #loadProfile(Object)
      */
     public Object getCurrentProfileKey() throws MetadataException
     {
-        if (!isEnablePerThreadChanges())
+        if(!isEnablePerThreadChanges())
         {
             throw new MetadataException("Call to this method is undefined, since per-thread mode is disabled.");
         }
         return currentProfileKey.get();
     }
 
-    /**
-     * Remove the given metadata profile.
-     */
+    /** Remove the given metadata profile. */
     public DescriptorRepository removeProfile(Object key)
     {
         return (DescriptorRepository) metadataProfiles.remove(key);
     }
 
-    /**
-     * Remove all metadata profiles.
-     */
+    /** Remove all metadata profiles. */
     public void clearProfiles()
     {
         metadataProfiles.clear();
@@ -628,6 +625,7 @@
      * Note: It's recommended to set this key only once and not to change at runtime
      * of OJB to avoid side-effects.
      * If set more then one time a warning will be logged.
+     *
      * @throws MetadataException if key was set more than one time
      */
     public void setDefaultPBKey(PBKey defaultPBKey)
@@ -643,18 +641,17 @@
     /**
      * Try to build an default PBKey for convenience PB create method.
      *
-     * @return PBKey or <code>null</code> if default key was not declared in
-     * metadata
+     * @return PBKey or <code>null</code> if default key was not declared in metadata
      */
     private PBKey buildDefaultKey()
     {
         List descriptors = connectionRepository().getAllDescriptor();
         JdbcConnectionDescriptor descriptor;
         PBKey result = null;
-        for (Iterator iterator = descriptors.iterator(); iterator.hasNext();)
+        for(Iterator iterator = descriptors.iterator(); iterator.hasNext();)
         {
             descriptor = (JdbcConnectionDescriptor) iterator.next();
-            if (descriptor.isDefaultConnection())
+            if(descriptor.isDefaultConnection())
             {
                 if(result != null)
                 {
@@ -673,7 +670,7 @@
         {
             log.info("No 'default-connection' attribute set in jdbc-connection-descriptors," +
                     " thus it's currently not possible to use 'defaultPersistenceBroker()' " +
-                    " convenience method to lookup PersistenceBroker instances. But it's possible"+
+                    " convenience method to lookup PersistenceBroker instances. But it's possible" +
                     " to enable this at runtime using 'setDefaultKey' method.");
         }
         return result;
@@ -682,7 +679,7 @@
     protected static URL buildRepositoryURL(Configuration conf)
     {
         // 1. search for system property entry
-        String repositoryPath = System.getProperties().getProperty(OjbConfiguration.OJB_METADATA_FILE , null);
+        String repositoryPath = System.getProperties().getProperty(OjbConfiguration.OJB_METADATA_FILE, null);
         String repositoryPath2 = null;
         // 2. lookup repository file path from OJB.properties file
         if(repositoryPath == null)
@@ -702,7 +699,7 @@
             }
         }
 
-        if (url != null)
+        if(url != null)
         {
             log.info("OJB Repository file: " + url);
         }
@@ -718,7 +715,7 @@
         //j2ee compliant lookup of resources
         URL url = ClassHelper.getResource(repositoryPath);
         // don't be too strict: if resource is not on the classpath, try ordinary file lookup
-        if (url == null)
+        if(url == null)
         {
             try
             {
@@ -728,7 +725,7 @@
                     url = file.toURL();
                 }
             }
-            catch (MalformedURLException ignore)
+            catch(MalformedURLException ignore)
             {
                 // ignore
             }
@@ -744,7 +741,7 @@
             // serialize repository
             SerializationUtils.serialize(repository, fos);
         }
-        catch (Exception e)
+        catch(Exception e)
         {
             log.error("Serialization failed, using output path: " + file.getAbsolutePath(), e);
         }
@@ -759,10 +756,15 @@
             // deserialize repository
             result = (DescriptorRepository) SerializationUtils.deserialize(fis);
         }
-        catch (Exception e)
+        catch(Exception e)
         {
             log.error("Deserialisation failed, using input path: " + serFile.getAbsolutePath(), e);
         }
         return result;
+    }
+
+    private RepositoryPersistor createRepositoryPersistor() throws Exception
+    {
+        return (RepositoryPersistor) ClassHelper.newInstance(repositoryPersistorClass);
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org