db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/metadata MetadataManager.java ConnectionRepository.java
Date Tue, 11 Feb 2003 19:34:45 GMT
arminw      2003/02/11 11:34:45

  Modified:    .        build.xml
               src/test/org/apache/ojb/broker/metadata MetadataTest.java
               src/java/org/apache/ojb/broker/metadata MetadataManager.java
                        ConnectionRepository.java
  Added:       src/test/org/apache/ojb Test_ConnectionDescriptor.xml
                        Test_ClassDescriptor.xml
  Log:
  add new methods to MetadataManager,
  add new test cases for methods
  
  Revision  Changes    Path
  1.68      +5 -1      db-ojb/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/build.xml,v
  retrieving revision 1.67
  retrieving revision 1.68
  diff -u -r1.67 -r1.68
  --- build.xml	7 Feb 2003 11:07:54 -0000	1.67
  +++ build.xml	11 Feb 2003 19:34:44 -0000	1.68
  @@ -217,7 +217,7 @@
   	<target name="prepare-repository" depends="checkP6Spy, useP6Spy, dontUseP6Spy">
   		<delete dir="${build.test}" verbose="false"/>
           <copy todir="${build.test}/ojb">
  -            <fileset dir="${build.srctest}/org/apache/ojb" includes="database*,repository*,*.properties,*.dtd,*.jdo"/>
  +            <fileset dir="${build.srctest}/org/apache/ojb" includes="Test_*,database*,repository*,*.properties,*.dtd,*.jdo"/>
               <filterset>
                   <filter token="JCD_ALIAS" value="${jcdAlias}" />
                   <filter token="DBMS_NAME" value="${dbmsName}" />
  @@ -1047,6 +1047,10 @@
                antfile="${basedir}/build-xdoclet.xml"
                target="jar-client"/>
       </target>
  +
  +
  +
  +
   
       <!-- ================================================================== -->
       <!-- End of targets                                                     -->
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/Test_ConnectionDescriptor.xml
  
  Index: Test_ConnectionDescriptor.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  <!--
  This is a test repository used in junit test org.apache.ojb.broker.metadata.MetadataTest
  Runtime reading of repository files.
  DO NOT CHANGE!
  -->
  <!DOCTYPE descriptor-repository SYSTEM "repository.dtd">
  
  
  <descriptor-repository version="0.9.9" isolation-level="read-uncommitted">
  
      <!--
          NOTE: This is a test descriptor used within
          the junit test suite, do not change any value.
      -->
      <jdbc-connection-descriptor
          jcd-alias="runtime"
          platform="Hsqldb"
          jdbc-level="2.0"
          driver="org.hsqldb.jdbcDriver"
          protocol="jdbc"
          subprotocol="hsqldb"
          dbalias="../OJB_FarAway"
          username="sa"
          password=""
          batch-mode="false"
      >
          <connection-pool
              maxActive="5"
              whenExhaustedAction="0"
              validationQuery="select count(*) from OJB_HL_SEQ"
          />
  
          <sequence-manager className="org.apache.ojb.broker.util.sequence.SequenceManagerHighLowImpl">
              <attribute attribute-name="grabSize" attribute-value="5"/>
          </sequence-manager>
      </jdbc-connection-descriptor>
  
  </descriptor-repository>
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/Test_ClassDescriptor.xml
  
  Index: Test_ClassDescriptor.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  <!--
  This is a test repository used in junit test org.apache.ojb.broker.metadata.MetadataTest
  Runtime reading of repository files.
  DO NOT CHANGE!
  -->
  
  <!-- defining entities for include-files -->
  <!DOCTYPE descriptor-repository SYSTEM "repository.dtd">
  
  
  <descriptor-repository version="0.9.9" isolation-level="read-uncommitted">
  
     <!-- Definitions for org.apache.ojb.broker.metadata.MetadataTest$CldTestObject -->
      <class-descriptor
        class="org.apache.ojb.broker.metadata.MetadataTest$CldTestObject"
        table="SM_TAB_MAX_BB"
      >
          <field-descriptor
           name="id"
           column="SM_ID"
           jdbc-type="INTEGER"
           primarykey="true"
           autoincrement="true"
          />
  
          <field-descriptor
           name="name"
           column="NAME"
           jdbc-type="VARCHAR"
          />
      </class-descriptor>
  
  
  </descriptor-repository>
  
  
  
  1.5       +208 -123  db-ojb/src/test/org/apache/ojb/broker/metadata/MetadataTest.java
  
  Index: MetadataTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/metadata/MetadataTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MetadataTest.java	11 Feb 2003 09:41:55 -0000	1.4
  +++ MetadataTest.java	11 Feb 2003 19:34:44 -0000	1.5
  @@ -28,6 +28,14 @@
   public class MetadataTest extends TestCase
   {
       /**
  +     * Path used by the test case to find some test
  +     * repository files.
  +     */
  +    private static final String pathToTestDir = "";
  +    private static final String TEST_REPOSITORY_DESCRIPTOR = "Test_ClassDescriptor.xml";
  +    private static final String TEST_REPOSITORY_CONNECTION = "Test_ConnectionDescriptor.xml";
  +
  +    /**
        * A persistent object class
        */
       private Class testClass = DListImpl.class;
  @@ -64,6 +72,29 @@
   
       }
   
  +    public void testRuntimeMergeConnectionDescriptor() throws Exception
  +    {
  +
  +        MetadataManager mm = MetadataManager.getInstance();
  +        ConnectionRepository cr = mm.readConnectionRepository(TEST_REPOSITORY_CONNECTION);
  +        mm.mergeConnectionRepository(cr);
  +
  +        ConnectionRepository mergedCR = mm.connectionRepository();
  +        JdbcConnectionDescriptor jcd = mergedCR.getDescriptor(new PBKey("runtime"));
  +        assertNotNull("Runtime merge of ConnectionRepository failed",jcd);
  +    }
  +
  +    public void testRuntimeMergeDescriptorRepository() throws Exception
  +    {
  +        MetadataManager mm = MetadataManager.getInstance();
  +        DescriptorRepository dr = mm.readDescriptorRepository(TEST_REPOSITORY_DESCRIPTOR);
  +        mm.mergeDescriptorRepository(dr);
  +
  +        DescriptorRepository mergedDR = mm.getRepository();
  +        ClassDescriptor cld = mergedDR.getDescriptorFor(MetadataTest.CldTestObject.class);
  +        assertNotNull("Runtime merge of DescriptorRepository failed",cld);
  +    }
  +
       public void testRuntimeMetadataChanges() throws Exception
       {
           int loops = 10;
  @@ -98,129 +129,6 @@
           }
       }
   
  -    class GlobalUsingBroker implements Runnable
  -    {
  -        int loops;
  -
  -        public GlobalUsingBroker(int loops)
  -        {
  -            this.loops = loops;
  -        }
  -
  -        public void run()
  -        {
  -            PersistenceBroker broker = null;
  -            int k = 0;
  -            try
  -            {
  -                while (k < loops)
  -                {
  -                    try
  -                    {
  -                        broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  -                        ClassDescriptor cld = broker.getClassDescriptor(SMMaxA.class);
  -                        String name = cld.getClassNameOfObject();
  -                        assertEquals(SMMaxA.class.getName(), name);
  -                        assertTrue(MetadataManager.getInstance().isEnablePerThreadChanges());
  -                    }
  -                    finally
  -                    {
  -                        if (broker != null) broker.close();
  -                    }
  -                    try
  -                    {
  -                        broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  -                        ClassDescriptor cld = broker.getClassDescriptor(SMMaxA.class);
  -                        String name = cld.getClassNameOfObject();
  -                        assertEquals(SMMaxA.class.getName(), name);
  -                        // System.out.println("Default: found "+name);
  -                    }
  -                    finally
  -                    {
  -                        if (broker != null) broker.close();
  -                    }
  -
  -                    k++;
  -                    try
  -                    {
  -                        Thread.sleep(50);
  -                    }
  -                    catch (InterruptedException e)
  -                    {
  -                    }
  -                }
  -            }
  -            catch (PBFactoryException e)
  -            {
  -                e.printStackTrace();
  -            }
  -            catch (PersistenceBrokerException e)
  -            {
  -            }
  -        }
  -    }
  -
  -    class ThreadedUsingBroker implements Runnable
  -    {
  -        int loops;
  -
  -        public ThreadedUsingBroker(int loops)
  -        {
  -            this.loops = loops;
  -        }
  -
  -        public void run()
  -        {
  -            DescriptorRepository dr = MetadataManager.getInstance().copyOfGlobalRepository();
  -            ClassDescriptor cld = dr.getDescriptorFor(SMMaxA.class);
  -            // we change the class descriptor
  -            cld.setClassOfObject(null);
  -            // set the changed repository for this thread
  -            MetadataManager.getInstance().setPerThreadDescriptor(dr);
  -
  -            int k = 0;
  -            while (k < loops)
  -            {
  -                PersistenceBroker broker = null;
  -                try
  -                {
  -                    broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  -                    cld = broker.getClassDescriptor(SMMaxA.class);
  -                    String name = cld.getClassNameOfObject();
  -                    assertNull(name);
  -                    assertTrue(MetadataManager.getInstance().isEnablePerThreadChanges());
  -                }
  -                finally
  -                {
  -                    if (broker != null) broker.close();
  -                }
  -
  -                try
  -                {
  -                    broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  -                    cld = broker.getClassDescriptor(SMMaxA.class);
  -                    String name = cld.getClassNameOfObject();
  -                    assertNull(name);
  -                    // System.out.println("Changed: found "+name);
  -                }
  -                finally
  -                {
  -                    if (broker != null) broker.close();
  -                }
  -
  -                k++;
  -                try
  -                {
  -                    Thread.sleep(50);
  -                }
  -                catch (InterruptedException e)
  -                {
  -                }
  -            }
  -
  -        }
  -    }
  -
       /**
        * test to check PB create with PBKey
        */
  @@ -424,5 +332,182 @@
   
           assertNotNull(seq.getConfigurationProperty("key2"));
           assertEquals("value2", seq.getConfigurationProperty("key2"));
  +    }
  +
  +
  +
  +
  +    // ======================================================================
  +    // inner test class
  +    // ======================================================================
  +    class GlobalUsingBroker implements Runnable
  +    {
  +        int loops;
  +
  +        public GlobalUsingBroker(int loops)
  +        {
  +            this.loops = loops;
  +        }
  +
  +        public void run()
  +        {
  +            PersistenceBroker broker = null;
  +            int k = 0;
  +            try
  +            {
  +                while (k < loops)
  +                {
  +                    try
  +                    {
  +                        broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +                        ClassDescriptor cld = broker.getClassDescriptor(SMMaxA.class);
  +                        String name = cld.getClassNameOfObject();
  +                        assertEquals(SMMaxA.class.getName(), name);
  +                        assertTrue(MetadataManager.getInstance().isEnablePerThreadChanges());
  +                    }
  +                    finally
  +                    {
  +                        if (broker != null) broker.close();
  +                    }
  +                    try
  +                    {
  +                        broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +                        ClassDescriptor cld = broker.getClassDescriptor(SMMaxA.class);
  +                        String name = cld.getClassNameOfObject();
  +                        assertEquals(SMMaxA.class.getName(), name);
  +                        // System.out.println("Default: found "+name);
  +                    }
  +                    finally
  +                    {
  +                        if (broker != null) broker.close();
  +                    }
  +
  +                    k++;
  +                    try
  +                    {
  +                        Thread.sleep(50);
  +                    }
  +                    catch (InterruptedException e)
  +                    {
  +                    }
  +                }
  +            }
  +            catch (PBFactoryException e)
  +            {
  +                e.printStackTrace();
  +            }
  +            catch (PersistenceBrokerException e)
  +            {
  +            }
  +        }
  +    }
  +
  +
  +
  +    // ======================================================================
  +    // inner test class
  +    // ======================================================================
  +    class ThreadedUsingBroker implements Runnable
  +    {
  +        int loops;
  +
  +        public ThreadedUsingBroker(int loops)
  +        {
  +            this.loops = loops;
  +        }
  +
  +        public void run()
  +        {
  +            MetadataManager mm = MetadataManager.getInstance();
  +            DescriptorRepository dr = mm.copyOfGlobalRepository();
  +            ClassDescriptor cld = dr.getDescriptorFor(SMMaxA.class);
  +            // we change the class descriptor
  +            cld.setClassOfObject(null);
  +            // set the changed repository for this thread
  +            mm.setPerThreadDescriptor(dr);
  +
  +            int k = 0;
  +            while (k < loops)
  +            {
  +                PersistenceBroker broker = null;
  +                try
  +                {
  +                    broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +                    cld = broker.getClassDescriptor(SMMaxA.class);
  +                    String name = cld.getClassNameOfObject();
  +                    assertNull(name);
  +                    assertTrue(MetadataManager.getInstance().isEnablePerThreadChanges());
  +                }
  +                finally
  +                {
  +                    if (broker != null) broker.close();
  +                }
  +
  +                try
  +                {
  +                    broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +                    cld = broker.getClassDescriptor(SMMaxA.class);
  +                    String name = cld.getClassNameOfObject();
  +                    assertNull(name);
  +                    // System.out.println("Changed: found "+name);
  +                }
  +                finally
  +                {
  +                    if (broker != null) broker.close();
  +                }
  +
  +                k++;
  +                try
  +                {
  +                    Thread.sleep(50);
  +                }
  +                catch (InterruptedException e)
  +                {
  +                }
  +            }
  +
  +        }
  +    }
  +
  +
  +
  +
  +    // ======================================================================
  +    // inner test class
  +    // ======================================================================
  +    public static class CldTestObject
  +    {
  +        int id;
  +        String name;
  +
  +        public CldTestObject()
  +        {
  +        }
  +
  +        public CldTestObject(int id, String name)
  +        {
  +            this.id = id;
  +            this.name = name;
  +        }
  +
  +        public int getId()
  +        {
  +            return id;
  +        }
  +
  +        public void setId(int id)
  +        {
  +            this.id = id;
  +        }
  +
  +        public String getName()
  +        {
  +            return name;
  +        }
  +
  +        public void setName(String name)
  +        {
  +            this.name = name;
  +        }
       }
   }
  
  
  
  1.6       +123 -14   db-ojb/src/java/org/apache/ojb/broker/metadata/MetadataManager.java
  
  Index: MetadataManager.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/MetadataManager.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- MetadataManager.java	31 Jan 2003 17:55:55 -0000	1.5
  +++ MetadataManager.java	11 Feb 2003 19:34:45 -0000	1.6
  @@ -5,6 +5,12 @@
   import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
  +import org.xml.sax.SAXException;
  +
  +import javax.xml.parsers.ParserConfigurationException;
  +import java.util.List;
  +import java.util.Iterator;
  +import java.io.IOException;
   
   /**
    * Central class for metadata operations - manages all OJB's
  @@ -14,13 +20,45 @@
    * Allow a bunch of different repository handling possibilities -
    * allows a transparent flexible metadata manipulation at runtime.
    * <p>
  + *
  + *
  + * TODO: Is this a really useful feature?
  + *
  + *
  + * <a name="perThread"/>
  + * <b>Per thread handling of metadata</b>
  + * <br/>
    * Per default the manager handle one global {@link org.apache.ojb.broker.metadata.DescriptorRepository}
  - * for all calling threads, but you could {@link #setEnablePerThreadChanges enable a per
thread}
  - * handling. This enables to set a different
  - * {@link org.apache.ojb.broker.metadata.DescriptorRepository DescriptorRepository} for
each calling thread.
  - * This allows a per thread manipulation of the persistent object metadata at runtime.
  + * for all calling threads, but it is ditto possible to change metadata only per thread.
When the thread
  + * dies all made changes were rejected.
  + * <br/>
  + * To use this feature do something similar:
  + * <pre>
  + *   MetadataManager mm = MetadataManager.getInstance();
  + *   // tell the manager to use per thread mode
  + *   mm.setEnablePerThreadChanges(true);
  + *
  + *   // e.g we get a coppy of the global repository
  + *   DescriptorRepository dr = mm.copyOfGlobalRepository();
  + *   // now we can manipulate the metadata of the copy
  + *   ......
  + *
  + *   // set the changed repository for this thread
  + *   mm.setPerThreadDescriptor(dr);
  + *
  + *   // now this thread could lookup a PB with the modified metadata
  + *   // all other threads use the global metadata
  + *   PersistenceBroker broker = Persis......
  + * </pre>
  + * First do {@link #setEnablePerThreadChanges enable the per thread mode}
  + * handling. This could be done e.g. at setup. Now each thread could use
  + * its own metadata, but you have
  + * <br/>a) explicit to set the new repository per thread
  + * <br/>b) do this BEFORE you lookup the {@link PersistenceBroker} for
  + * the thread, because the metadata was bound to the PB at lookup.
    * </p>
    *
  + *
    * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
    * @version $Id$
    */
  @@ -79,11 +117,12 @@
   
       /**
        * Returns the current valid {@link org.apache.ojb.broker.metadata.DescriptorRepository}
for
  -     * the caller. This is the provided way to obtain the {@link org.apache.ojb.broker.metadata.DescriptorRepository}.
  +     * the caller. This is the provided way to obtain the
  +     * {@link org.apache.ojb.broker.metadata.DescriptorRepository}.
        * <br>
        * When {@link #isEnablePerThreadChanges per thread descriptor handling}  is enabled
  -     * we first search for a {@link org.apache.ojb.broker.metadata.DescriptorRepository}
for the calling thread, if not found
  -     * the global descriptor was returned.
  +     * we first search for a {@link org.apache.ojb.broker.metadata.DescriptorRepository}
  +     * for the calling thread, if not found the global descriptor was returned.
        *
        * @see MetadataManager#getGlobalRepository
        * @see MetadataManager#copyOfGlobalRepository
  @@ -129,6 +168,75 @@
       }
   
       /**
  +     * Merge the given {@link ConnectionRepository} with the
  +     * existing one.
  +     */
  +    public void mergeConnectionRepository(ConnectionRepository repository)
  +    {
  +        List list = repository.getAllDescriptor();
  +        for (Iterator iterator = list.iterator(); iterator.hasNext();)
  +        {
  +            JdbcConnectionDescriptor jcd = (JdbcConnectionDescriptor) iterator.next();
  +            connectionRepository().addDescriptor(jcd);
  +        }
  +    }
  +
  +    /**
  +     * Merge the given {@link org.apache.ojb.broker.metadata.DescriptorRepository}
  +     * with the existing one - keep in mind if running in
  +     * <a href="#perThread">per thread mode</a>
  +     * merge maybe only takes effect on current thread.
  +     *
  +     * @see #isEnablePerThreadChanges
  +     * @see #setEnablePerThreadChanges
  +     */
  +    public void mergeDescriptorRepository(DescriptorRepository repository)
  +    {
  +        DescriptorRepository targetRepository = getRepository();
  +        Iterator it = repository.iterator();
  +        while (it.hasNext())
  +        {
  +            ClassDescriptor cld = (ClassDescriptor) it.next();
  +            cld.setRepository(targetRepository);
  +            targetRepository.put(cld.getClassOfObject(), cld);
  +        }
  +    }
  +
  +    /**
  +     * Read all ClassDescriptors from the given repository file.
  +     * @see #mergeDescriptorRepository
  +     */
  +    public DescriptorRepository readDescriptorRepository(String fileName)
  +    {
  +        try
  +        {
  +            RepositoryPersistor persistor = new RepositoryPersistor();
  +            return persistor.readDescriptorRepository(fileName);
  +        }
  +        catch (Exception e)
  +        {
  +            throw new MetadataException("Could not read repository "+fileName, e);
  +        }
  +    }
  +
  +   /**
  +    * Read all JdbcConnectionDescriptors from the given repository file.
  +    * @see #mergeConnectionRepository
  +    */
  +    public ConnectionRepository readConnectionRepository(String fileName)
  +    {
  +        try
  +        {
  +            RepositoryPersistor persistor = new RepositoryPersistor();
  +            return persistor.readConnectionRepository(fileName);
  +        }
  +        catch (Exception e)
  +        {
  +            throw new MetadataException("Could not read repository "+fileName, e);
  +        }
  +    }
  +
  +    /**
        * Set the {@link org.apache.ojb.broker.metadata.DescriptorRepository} - if <i>global</i>
was true, the
        * given descriptor aquire global availability (<i>use with care!</i>),
        * else the given descriptor was associated with the calling thread.
  @@ -172,12 +280,13 @@
       }
   
       /**
  -     * If returns <i>true</i> runtime (per thread) changes
  -     * of the {@link org.apache.ojb.broker.metadata.DescriptorRepository}
  -     * will take effect.
  +     * If returns <i>true</i> if <a href="#perThread">per thread</a>
runtime
  +     * changes of the {@link org.apache.ojb.broker.metadata.DescriptorRepository}
  +     * is enabled and the {@link #getRepository} method returns a threaded
  +     * repository file if set, or the global if no threaded was found.
        * <br>
  -     * If returns <i>false</i> all made and further runtime (per thread)
  -     * changes will be ignored.
  +     * If returns <i>false</i> the {@link #getRepository} method return
  +     * always the {@link getGlobalRepository global} repository.
        *
        * @see #setEnablePerThreadChanges
        */
  @@ -187,7 +296,7 @@
       }
   
       /**
  -     * Enable the possibility of making per thread runtime changes
  +     * Enable the possibility of making <a href="#perThread">per thread</a>
runtime changes
        * of the {@link org.apache.ojb.broker.metadata.DescriptorRepository}.
        *
        * @see #isEnablePerThreadChanges
  
  
  
  1.8       +6 -1      db-ojb/src/java/org/apache/ojb/broker/metadata/ConnectionRepository.java
  
  Index: ConnectionRepository.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/ConnectionRepository.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ConnectionRepository.java	11 Feb 2003 09:41:55 -0000	1.7
  +++ ConnectionRepository.java	11 Feb 2003 19:34:45 -0000	1.8
  @@ -79,6 +79,11 @@
           return null;
       }
   
  +    /**
  +     * Return the matching {@link org.apache.ojb.broker.PBKey} for
  +     * the given jcdAlias name, or <code>null</code> if no match
  +     * was found.
  +     */
       public PBKey getStandardPBKeyForJcdAlias(String jcdAlias)
       {
           return (PBKey) jcdAliasToPBKeyMap.get(jcdAlias);
  
  
  

Mime
View raw message