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/util/factory ConfigurableFactory.java
Date Wed, 05 Mar 2003 23:52:10 GMT
arminw      2003/03/05 15:52:09

  Modified:    .        release-notes.txt build.xml
               src/test/org/apache/ojb repository_junit.xml OJB.properties
               src/test/org/apache/ojb/broker AllTests.java
               src/schema ojbtest-schema.xml
               src/java/org/apache/ojb/broker/singlevm
                        PersistenceBrokerConfiguration.java
               src/java/org/apache/ojb/broker/cache package.html
                        package.gif ObjectCachePerBrokerImpl.java
                        ObjectCacheJCSImpl.java ObjectCacheFactory.java
                        ObjectCacheEmptyImpl.java
                        ObjectCacheDefaultImpl.java ObjectCache.java
                        AbstractMetaCache.java
               src/java/org/apache/ojb/broker/util/factory
                        ConfigurableFactory.java
  Added:       src/test/org/apache/ojb/broker ObjectCacheTest.java
               src/java/org/apache/ojb/broker/cache
                        MetaObjectCachePerClassImpl.java
                        MetaObjectCacheJCSImpl.java JCSHelper.java
                        CacheFilterRegistry.java
                        CacheFilterPackageImpl.java
                        CacheFilterClassImpl.java CacheFilter.java
  Log:
  - refactored cache package
  - add possibility to pre-filter
  invoke of caching methods
  - add simple test case for cache
  implementations
  
  Revision  Changes    Path
  1.8       +18 -0     db-ojb/release-notes.txt
  
  Index: release-notes.txt
  ===================================================================
  RCS file: /home/cvs/db-ojb/release-notes.txt,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- release-notes.txt	3 Mar 2003 19:40:28 -0000	1.7
  +++ release-notes.txt	5 Mar 2003 23:52:07 -0000	1.8
  @@ -8,6 +8,23 @@
   
   
   ---------------------------------------------------------------------
  +Release 1.0 rc2
  +---------------------------------------------------------------------
  +
  +NEW FEATURES:
  +- With this release we are feature complete for the 1.0 release!
  +For 1.0 you should not expect more features to be added.
  +
  +
  +CHANGES:
  +- improve cache package. Now the used cache is no longer a singleton.
  +- Add CacheFilter interface to allow easy pre-filtering of cache methods.
  +
  +
  +BUG FIXES:
  +
  +
  +---------------------------------------------------------------------
   Release 1.0 rc1
   ---------------------------------------------------------------------
   
  @@ -21,6 +38,7 @@
   - MetadataManager: new methods, allow merge of new ConnectionDescriptor and RepositoryDescriptor
   (read from any repository file at runtime) with the existing ones.
   - add custom attribute support for DescriptorRepository and JdbcConnectionDescriptor.
  +
   
   BUG FIXES:
   
  
  
  
  1.78      +1 -4      db-ojb/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/build.xml,v
  retrieving revision 1.77
  retrieving revision 1.78
  diff -u -r1.77 -r1.78
  --- build.xml	4 Mar 2003 06:57:59 -0000	1.77
  +++ build.xml	5 Mar 2003 23:52:07 -0000	1.78
  @@ -272,7 +272,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="Test_*,database*,repository*,*.properties,*.dtd,*.jdo"/>
  +            <fileset dir="${build.srctest}/org/apache/ojb" includes="Test_*,database*,repository*,*.properties,*.dtd,*.jdo,cache.ccf"/>
               <filterset>
                   <filter token="JCD_ALIAS" value="${jcdAlias}" />
                   <filter token="DBMS_NAME" value="${dbmsName}" />
  @@ -1114,9 +1114,6 @@
                antfile="${basedir}/build-xdoclet.xml"
                target="prepare-jboss"/>
       </target>
  -
  -
  -
   
       <!-- ================================================================== -->
       <!-- End of targets                                                     -->
  
  
  
  1.50      +42 -0     db-ojb/src/test/org/apache/ojb/repository_junit.xml
  
  Index: repository_junit.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit.xml,v
  retrieving revision 1.49
  retrieving revision 1.50
  diff -u -r1.49 -r1.50
  --- repository_junit.xml	4 Mar 2003 12:38:30 -0000	1.49
  +++ repository_junit.xml	5 Mar 2003 23:52:08 -0000	1.50
  @@ -3218,6 +3218,48 @@
         />
      </class-descriptor>
   
  +<!-- ************************************************* -->
  +<!--      ObjectCache test descriptors                 -->
  +<!-- ************************************************* -->
  +    <class-descriptor
  +        class="org.apache.ojb.broker.ObjectCacheTest$CacheObject"
  +        table="CACHE_TEST">
  +
  +        <field-descriptor
  +         name="objId"
  +         column="OBJ_ID"
  +         jdbc-type="INTEGER"
  +         primarykey="true"
  +         autoincrement="true"
  +        />
  +        <field-descriptor
  +         name="name"
  +         column="NAME"
  +         jdbc-type="VARCHAR"
  +        />
  +    </class-descriptor>
  +
  +    <class-descriptor
  +        class="org.apache.ojb.broker.ObjectCacheTest$CacheObject_2"
  +        table="CACHE_TEST">
  +        <!-- when cache filtering is enabled this class was not cached -->
  +        <attribute
  +            attribute-name="cacheable"
  +            attribute-value="false"/>
  +
  +        <field-descriptor
  +         name="objId"
  +         column="OBJ_ID"
  +         jdbc-type="INTEGER"
  +         primarykey="true"
  +         autoincrement="true"
  +        />
  +        <field-descriptor
  +         name="name"
  +         column="NAME"
  +         jdbc-type="VARCHAR"
  +        />
  +    </class-descriptor>
   
   
   <!-- Mapping of classes used in junit tests and tutorials ends here -->
  
  
  
  1.41      +21 -6     db-ojb/src/test/org/apache/ojb/OJB.properties
  
  Index: OJB.properties
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/OJB.properties,v
  retrieving revision 1.40
  retrieving revision 1.41
  diff -u -r1.40 -r1.41
  --- OJB.properties	19 Feb 2003 21:13:26 -0000	1.40
  +++ OJB.properties	5 Mar 2003 23:52:08 -0000	1.41
  @@ -176,7 +176,23 @@
   # implementation is to be used.
   ObjectCacheClass=org.apache.ojb.broker.cache.ObjectCacheDefaultImpl
   #ObjectCacheClass=org.apache.ojb.broker.cache.ObjectCacheEmptyImpl
  -#
  +#ObjectCacheClass=org.apache.ojb.broker.cache.ObjectCachePerBrokerImpl
  +#ObjectCacheClass=org.apache.ojb.broker.cache.MetaObjectCacheJCSImpl
  +#ObjectCacheClass=org.apache.ojb.broker.cache.MetaObjectCachePerClassImpl
  +#
  +#
  +# Use CacheFilters to do filter operations before caching methods were
  +# called. Build your own filter class by implementing org.apache.ojb.cache.CacheFilter.
  +# It is possible to use a arbitrary number of CacheFilters, but this slows
  +# down the performance of the cache, thus handle with care.
  +#
  +# - org.apache.ojb.broker.cache.CacheFilterClassImpl
  +# allows filtering of classes
  +# - org.apache.ojb.broker.cache.CacheFilterPackageImpl
  +# allows filtering of packages
  +# More info see Javadoc of the according classes.
  +# Set a comma separated list of CacheFilter.
  +#ObjectCacheFilter=org.apache.ojb.broker.cache.CacheFilterClassImpl,org.apache.ojb.broker.cache.CacheFilterPackageImpl
   #
   #----------------------------------------------------------------------------------------
   # Locking
  @@ -348,20 +364,19 @@
   # (org.apache.ojb.odmg.JTATxManager)
   #
   # JBoss Transaction Manager Factory
  -JTATransactionManagerClass=org.apache.ojb.otm.transaction.factory.JBossTransactionManagerFactory
  +JTATransactionManagerClass=org.apache.ojb.odmg.transaction.factory.JBossTransactionManagerFactory
   # Weblogic Transaction Manager Factory
  -#JTATransactionManagerClass=org.apache.ojb.otm.transaction.factory.WeblogicTransactionManagerFactory
  +#JTATransactionManagerClass=org.apache.ojb.odmg.transaction.factory.WeblogicTransactionManagerFactory
   # WebSphere transaction manager factory
  -#JTATransactionManagerClass=org.apache.ojb.otm.transaction.factory.WebSphereTransactionManagerFactory
  +#JTATransactionManagerClass=org.apache.ojb.odmg.transaction.factory.WebSphereTransactionManagerFactory
   # Orion transaction manager factory
  -#JTATransactionManagerClass=org.apache.ojb.otm.transaction.factory.OrionTransactionManagerFactory
  +#JTATransactionManagerClass=org.apache.ojb.odmg.transaction.factory.OrionTransactionManagerFactory
   #
   #
   #----------------------------------------------------------------------------------------
   # Auto DB Creation / Transparent Datastore
   #----------------------------------------------------------------------------------------
   autobuild=false
  -#
   #
   #----------------------------------------------------------------------------------------
   # End of OJB.properties file
  
  
  
  1.20      +1 -0      db-ojb/src/test/org/apache/ojb/broker/AllTests.java
  
  Index: AllTests.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/AllTests.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- AllTests.java	28 Feb 2003 17:05:41 -0000	1.19
  +++ AllTests.java	5 Mar 2003 23:52:08 -0000	1.20
  @@ -69,6 +69,7 @@
           suite.addTest(new TestSuite(FieldConversionTest_2.class));
           suite.addTest(new TestSuite(FieldConversionTest_3.class));
           suite.addTest(new TestSuite(BatchModeTest.class));
  +        suite.addTest(new TestSuite(ObjectCacheTest.class));
           return suite;
       }
   
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/broker/ObjectCacheTest.java
  
  Index: ObjectCacheTest.java
  ===================================================================
  package org.apache.ojb.broker;
  
  import junit.framework.TestCase;
  import org.apache.ojb.broker.cache.MetaObjectCacheJCSImpl;
  import org.apache.ojb.broker.cache.ObjectCache;
  import org.apache.ojb.broker.cache.ObjectCacheDefaultImpl;
  import org.apache.ojb.broker.cache.ObjectCacheEmptyImpl;
  import org.apache.ojb.broker.cache.ObjectCacheJCSImpl;
  import org.apache.ojb.broker.cache.MetaObjectCachePerClassImpl;
  import org.apache.ojb.broker.cache.ObjectCachePerBrokerImpl;
  import org.apache.ojb.broker.cache.ObjectCacheFactory;
  import org.apache.ojb.broker.cache.CacheFilterPackageImpl;
  import org.apache.ojb.broker.cache.CacheFilterClassImpl;
  import org.apache.ojb.broker.util.configuration.impl.OjbConfiguration;
  import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
  import org.apache.ojb.broker.metadata.MetadataManager;
  import org.apache.ojb.broker.sequence.SMKey;
  
  import java.io.Serializable;
  
  /**
   * Do some basic tests using ObjectCache implementations.
   *
   * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
   * @version $Id: ObjectCacheTest.java,v 1.1 2003/03/05 23:52:08 arminw Exp $
   */
  public class ObjectCacheTest extends TestCase
  {
      static final String EXCLUDE_PACKAGE = "org.apache.ojb.broker.sequence";
      static final String EXCLUDE_PACKAGE_NOT_EXIST = "org.apache.ojb.broker.sequence.xyz";
  
      Class[] objectCacheImpls = new Class[]{
          ObjectCacheEmptyImpl.class,
          ObjectCacheDefaultImpl.class,
          ObjectCachePerBrokerImpl.class,
          ObjectCacheJCSImpl.class,
          MetaObjectCacheJCSImpl.class,
          MetaObjectCachePerClassImpl.class
      };
  
      String[] cacheFilter = new String[]{
          CacheFilterClassImpl.class.getName(),
          CacheFilterPackageImpl.class.getName()
      };
      Class old_ObjectCache;
      String[] old_CacheFilter;
  
      public ObjectCacheTest(String s)
      {
          super(s);
      }
  
      public static void main(String[] args)
      {
          String[] arr = {ObjectCacheTest.class.getName()};
          junit.textui.TestRunner.main(arr);
      }
  
      protected void setUp() throws Exception
      {
          super.setUp();
          old_CacheFilter = getConfig().getCacheFilters();
          old_ObjectCache = ObjectCacheFactory.getInstance().getClassToServe();
      }
  
      protected void tearDown() throws Exception
      {
          super.tearDown();
          getConfig().setCacheFilters(old_CacheFilter);
          ObjectCacheFactory.getInstance().setClassToServe(old_ObjectCache);
          MetadataManager.getInstance().getRepository().
                  addAttribute(CacheFilterPackageImpl.EXCLUDE_PACKAGES, null);
      }
  
      private OjbConfiguration getConfig()
      {
          return (OjbConfiguration) OjbConfigurator.getInstance().getConfigurationFor(null);
      }
  
      public void testSimpleObjectCacheFunctions() throws Exception
      {
          for(int i=0;i<objectCacheImpls.length;i++)
          {
              ObjectCacheFactory.getInstance().setClassToServe(objectCacheImpls[i]);
              checkBaseFunctions();
          }
      }
  
      public void testCacheFilterFunctions() throws Exception
      {
          getConfig().setCacheFilters(cacheFilter);
          MetadataManager.getInstance().getRepository().
                  addAttribute(CacheFilterPackageImpl.EXCLUDE_PACKAGES, EXCLUDE_PACKAGE);
          MetadataManager.getInstance().connectionRepository().getDescriptor(TestHelper.DEF_KEY).
                  addAttribute(CacheFilterPackageImpl.EXCLUDE_PACKAGES, EXCLUDE_PACKAGE_NOT_EXIST);
          for(int i=0;i<objectCacheImpls.length;i++)
          {
              ObjectCacheFactory.getInstance().setClassToServe(objectCacheImpls[i]);
              PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
              try
              {
                  ObjectCache cache = ObjectCacheFactory.getInstance().createObjectCache(broker);
                  CacheObject obj = new CacheObject(null, "CacheObject persistent obj");
                  Identity oid = new Identity(obj, broker);
  
                  CacheObject filterOutObject = new CacheObject_2(null, "CacheObject_2 persistent obj");
                  Identity filterOutOid = new Identity(filterOutObject, broker);
  
                  SMKey filterOutPackageObject = new SMKey();
                  filterOutPackageObject.setName("ObjectCacheTest: package filter");
                  Identity filterOutPackageOid = new Identity(filterOutPackageObject, broker);
  
                  Object result = null;
                  cache.clear();
                  result = (CacheObject) cache.lookup(oid);
                  assertNull(result);
                  result = (CacheObject) cache.lookup(filterOutOid);
                  assertNull(result);
                  result = (CacheObject) cache.lookup(filterOutPackageOid);
                  assertNull(result);
  
                  // cache it
                  cache.cache(oid, obj);
                  cache.cache(filterOutPackageOid, filterOutPackageObject);
                  cache.cache(filterOutOid, filterOutObject);
  
                  // lookup things
                  result = (CacheObject) cache.lookup(oid);
                  assertNotNull(result);
                  assertEquals(obj, result);
                  result = (CacheObject) cache.lookup(filterOutOid);
                  assertNull(result);
                  result = (SMKey) cache.lookup(filterOutPackageOid);
                  assertNull(result);
              }
              finally
              {
                  if (broker != null) broker.close();
              }
          }
      }
  
      private void checkBaseFunctions() throws Exception
      {
          PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
          try
          {
              ObjectCache cache = ObjectCacheFactory.getInstance().createObjectCache(broker);
              CacheObject obj = new CacheObject(null, "ObjectCache test");
              Identity oid = new Identity(obj, broker);
              CacheObject obj2 = new CacheObject(null, "ObjectCache test 2");
              Identity oid2 = new Identity(obj2, broker);
              cache.clear();
              Object result = (CacheObject) cache.lookup(oid);
              assertNull(result);
  
              cache.cache(oid, obj);
              cache.cache(oid2, obj2);
              result = (CacheObject) cache.lookup(oid);
              assertNotNull(result);
              assertEquals(obj, result);
              assertNotSame(obj2, result);
  
              cache.clear();
              result = (CacheObject) cache.lookup(oid);
              assertNull(result);
              result = (CacheObject) cache.lookup(oid2);
              assertNull(result);
          }
          finally
          {
              if (broker != null) broker.close();
          }
      }
  
  
  
      // **********************************************************************
      // inner class
      // **********************************************************************
      public static class CacheObject implements Serializable
      {
          private Integer objId;
          private String name;
  
          public CacheObject(Integer objId, String name)
          {
              this.objId = objId;
              this.name = name;
          }
  
          public CacheObject()
          {
          }
  
          public Integer getObjId()
          {
              return objId;
          }
  
          public void setObjId(Integer objId)
          {
              this.objId = objId;
          }
  
          public String getName()
          {
              return name;
          }
  
          public void setName(String name)
          {
              this.name = name;
          }
      }
  
      public static class CacheObject_2 extends CacheObject
      {
          public CacheObject_2(Integer objId, String name)
          {
              super(objId, name);
          }
  
          public CacheObject_2()
          {
          }
      }
  
  }
  
  
  
  1.25      +10 -0     db-ojb/src/schema/ojbtest-schema.xml
  
  Index: ojbtest-schema.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/schema/ojbtest-schema.xml,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- ojbtest-schema.xml	28 Feb 2003 12:35:51 -0000	1.24
  +++ ojbtest-schema.xml	5 Mar 2003 23:52:08 -0000	1.25
  @@ -577,4 +577,14 @@
           <column name="SUPPLIER_ID" type="INTEGER"/>
           <column name="PRODUCT_GROUP_ID" type="INTEGER"/>
       </table>
  +
  +
  +    <!-- =================================================== -->
  +    <!-- ObjectCache tests                                   -->
  +    <!-- =================================================== -->
  +    <table name="CACHE_TEST">
  +        <column name="OBJ_ID" required="true" primaryKey="true" type="INTEGER"/>
  +        <column name="NAME" type="VARCHAR" size="100"/>
  +    </table>
  +
   </database>
  
  
  
  1.7       +24 -16    db-ojb/src/java/org/apache/ojb/broker/singlevm/PersistenceBrokerConfiguration.java
  
  Index: PersistenceBrokerConfiguration.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/singlevm/PersistenceBrokerConfiguration.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- PersistenceBrokerConfiguration.java	13 Feb 2003 20:10:52 -0000	1.6
  +++ PersistenceBrokerConfiguration.java	5 Mar 2003 23:52:08 -0000	1.7
  @@ -1,5 +1,6 @@
   package org.apache.ojb.broker.singlevm;
   
  +
   /* ====================================================================
    * The Apache Software License, Version 1.1
    *
  @@ -62,22 +63,29 @@
   
   public interface PersistenceBrokerConfiguration
   {
  -	public String[] getServers();
  -	
  -	public boolean isRunningInServerMode();
  -	
  -	public Class getConnectionFactoryClass();
  -	
  -	public Class getObjectCacheClass();
  -	
  -	public Class getPersistentFieldClass();
  -	
  -	public String getRepositoryFilename();
  -	
  -	public Class getPersistenceBrokerClass();
  +    public String[] getServers();
  +
  +    public boolean isRunningInServerMode();
  +
  +    public Class getConnectionFactoryClass();
  +
  +    public Class getObjectCacheClass();
  +
  +    /**
  +     * Returns an array of the declared
  +     * {@link org.apache.ojb.broker.cache.CacheFilter},
  +     * or <code>null</code> if none found.
  +     */
  +    public String[] getCacheFilters();
  +
  +    public Class getPersistentFieldClass();
  +
  +    public String getRepositoryFilename();
  +
  +    public Class getPersistenceBrokerClass();
  +
  +    public int getSqlInLimit();
   
  -	public int getSqlInLimit();	
  -    
       /**
        * Answer the class to be used for CollectionProxy
        * @return Class
  
  
  
  1.3       +33 -25    db-ojb/src/java/org/apache/ojb/broker/cache/package.html
  
  Index: package.html
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/package.html,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- package.html	8 Oct 2002 06:13:42 -0000	1.2
  +++ package.html	5 Mar 2003 23:52:08 -0000	1.3
  @@ -13,56 +13,64 @@
   </P>
   
   <P>
  -Applications use the ObjectCacheFactory to create new ObjectCaches. 
  -These caches supply methods for storing and retrieving objects. 
  -Objects can be looked up by their identity which is represented by the class Identiy. 
  -Each implementation of ObjectCache represents a different caching strategy. 
  +Applications use the ObjectCacheFactory to create new ObjectCaches.
  +These caches supply methods for storing and retrieving objects.
  +Objects can be looked up by their identity which is represented by the class Identiy.
  +Each implementation of ObjectCache represents a different caching strategy.
   Currently the following caching strategies are supported:
   
   <ul>
   	<li>
   		<b>ObjectCacheDefaultImpl</b><br/>
  -		Caches objects by keeping a soft reference to a cached object in order to enable the garbage collector to reclaim storage.
  -	</li>
  -	<li>
  -		<b>PermanentObjectCacheImpl</b><br/>
  -		This cache keeps strong references to the cached objects. 
  -		Therefore the garbage collector is not able to reclaim storage. 
  -		This cache should be used for objects which don't change during the lifetime of the application and are referenced often.
  +		Caches objects by keeping a soft reference to a cached object in order to enable
  +        the garbage collector to reclaim storage.
   	</li>
   	<li>
   		<b>ObjectCacheEmptyImpl</b><br/>
  -		Empty implementation of the ObjectCache interface. Does no caching at all.
  +		Empty implementation of the ObjectCache interface.
  +        Does no caching at all.
   	</li>
  -	<li>
  -		<b>MetaObjectCacheImpl</b><br/>
  -		A hierachical cache that uses one ObjectCache per class.
  +    <li>
  +		<b>ObjectCachePerBrokerImpl</b><br/>
  +		Use separate caches for each <code>PersistenceBroker</code>
  +        instance. The cache will be cleared on every <code>PersistenceBroker#close()</code>
  +        method call (means when the broker instance was returned to pool).
   	</li>
   	<li>
   		<b>ObjectCacheJCSImpl</b><br/>
  -		A JCS based cache implementation.
  +		A JCS based cache implementation. For detailed information about JCS see
  +        <a href="http://jakarta.apache.org/turbine/jcs/index.html">JCS website</a>.
  +	</li>
  +    <li>
  +		<b>MetaObjectCachePerClassImpl</b><br/>
  +		A hierachical cache that uses one ObjectCache per class.
   	</li>
   	<li>
  -		<b>MetaObjectJCSCacheImpl</b><br/>
  +		<b>MetaObjectCacheJCSImpl</b><br/>
   		A hierachical cache that uses a JCS region per each class.
  +        For detailed information about JCS see
  +        <a href="http://jakarta.apache.org/turbine/jcs/index.html">JCS website</a>.
   	</li>
  -	
  +
   </ul>
   
   
   <P>
   Patterns applied:
   Configurable Factory: ObjectCacheFactory is a factory for objects implementing the ObjectCache interface.
  -Proxy, Composite: MetaObjectCacheImpl is a kind of proxy for the real object caches. (Although it is not a proxy for a single object but for all caches.)
  +Proxy, Composite: MetaObjectCacheImpl is a kind of proxy for the real object caches.
  +(Although it is not a proxy for a single object but for all caches.)
   
   <P>
   Todo:
  -   1. Is it possible to remove the dependency of this package on ojb.broker and ojb.broker.metadata? 
  -   A cache might me useful outside of OJB. 
  -   The dependency comes from using Identy and PersistenceBrokerFactory. 
  -   Is Identy really necessary? 
  -   We might place the burden of creating a Identy on the class which wants to cache objects (simply by removing the method cache(Object)). 
  -   Removing the dependency on Identy removes the dependency on ClassNotPersistenceCapableException as well since this exception is thrown in the constructor of Identy.
  +   1. Is it possible to remove the dependency of this package on ojb.broker and ojb.broker.metadata?
  +   A cache might me useful outside of OJB.
  +   The dependency comes from using Identy and PersistenceBrokerFactory.
  +   Is Identy really necessary?
  +   We might place the burden of creating a Identy on the class which wants to cache objects
  +   (simply by removing the method cache(Object)).
  +   Removing the dependency on Identy removes the dependency on ClassNotPersistenceCapableException
  +   as well since this exception is thrown in the constructor of Identy.
   
   </body>
   </html>
  
  
  
  1.2       +92 -23    db-ojb/src/java/org/apache/ojb/broker/cache/package.gif
  
  	<<Binary file>>
  
  
  1.4       +1 -4      db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCachePerBrokerImpl.java
  
  Index: ObjectCachePerBrokerImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCachePerBrokerImpl.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ObjectCachePerBrokerImpl.java	19 Feb 2003 22:10:17 -0000	1.3
  +++ ObjectCachePerBrokerImpl.java	5 Mar 2003 23:52:08 -0000	1.4
  @@ -56,13 +56,10 @@
   
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.PersistenceBroker;
  -import org.apache.ojb.broker.PersistenceBrokerListener;
   import org.apache.ojb.broker.PersistenceBrokerException;
  -import org.apache.ojb.broker.util.logging.Logger;
  -import org.apache.ojb.broker.util.logging.LoggerFactory;
  +import org.apache.ojb.broker.PersistenceBrokerListener;
   
   import java.lang.ref.SoftReference;
  -import java.util.Hashtable;
   import java.util.HashMap;
   import java.util.Map;
   
  
  
  
  1.7       +34 -29    db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheJCSImpl.java
  
  Index: ObjectCacheJCSImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheJCSImpl.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ObjectCacheJCSImpl.java	19 Feb 2003 22:10:17 -0000	1.6
  +++ ObjectCacheJCSImpl.java	5 Mar 2003 23:52:08 -0000	1.7
  @@ -53,44 +53,52 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    */
  +
  +import org.apache.commons.lang.builder.ToStringBuilder;
  +import org.apache.commons.lang.builder.ToStringStyle;
   import org.apache.jcs.JCS;
   import org.apache.jcs.access.exception.CacheException;
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.PersistenceBroker;
  -import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
   
   /**
  - * Cache used in conjunction with {@link MetaObjectJCSCacheImpl}.
  + * {@link ObjectCache} implementation only for use in conjunction
  + * with {@link MetaObjectJCSCacheImpl}.
    *
    * @author Matthew Baird (mattbaird@yahoo.com);
    * @version $Id$
    */
   public class ObjectCacheJCSImpl implements ObjectCache
   {
  -    private JCS m_region;
  +    private JCS jcsCache;
       /**
        * if no regionname is passed in, we use the default region.
        */
  -    private String m_regionName = "region.ObjectBridge";
  +    private String regionName = JCSHelper.DEFAULT_REGION;
   
  +    /**
  +     * Constructor used by the {@link MetaObjectCacheJCSImpl}
  +     */
       public ObjectCacheJCSImpl(String name)
       {
  -        m_regionName = name;
  +        regionName = name;
  +        jcsCache = JCSHelper.newInstance(regionName);
       }
   
  -    private void ensureOpen()
  +    /**
  +     * Constructor used by {@link ObjectFactory}
  +     */
  +    public ObjectCacheJCSImpl(PersistenceBroker broker)
       {
  -        if (m_region == null)
  -        {
  -            try
  -            {
  -                m_region = JCS.getInstance(m_regionName);
  -            }
  -            catch (CacheException e)
  -            {
  -                throw new RuntimeCacheException(e);
  -            }
  -        }
  +        jcsCache = JCSHelper.newInstance(regionName);
  +    }
  +
  +    public String toString()
  +    {
  +        ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
  +        buf.append("JCS region name", regionName);
  +        buf.append("JCS region", jcsCache);
  +        return buf.toString();
       }
   
       /**
  @@ -98,12 +106,11 @@
        */
       public void cache(Identity oid, Object obj)
       {
  -        ensureOpen();
           try
           {
  -            m_region.put(oid, obj);
  +            jcsCache.put(oid, obj);
           }
  -        catch (org.apache.jcs.access.exception.CacheException e)
  +        catch (CacheException e)
           {
               throw new RuntimeCacheException(e.getMessage());
           }
  @@ -115,8 +122,7 @@
        */
       public Object lookup(Identity oid)
       {
  -        ensureOpen();
  -        return m_region.get(oid);
  +        return jcsCache.get(oid);
       }
   
       /**
  @@ -125,10 +131,9 @@
        */
       public void remove(Identity oid)
       {
  -        ensureOpen();
           try
           {
  -            m_region.remove(oid);
  +            jcsCache.remove(oid);
           }
           catch (CacheException e)
           {
  @@ -138,12 +143,12 @@
   
       public void setRegionName(String region)
       {
  -        m_regionName = region;
  +        regionName = region;
       }
   
       public String getRegionName()
       {
  -        return m_regionName;
  +        return regionName;
       }
   
       /**
  @@ -151,11 +156,11 @@
        */
       public void clear()
       {
  -        if (m_region != null)
  +        if (jcsCache != null)
           {
               try
               {
  -                m_region.remove();
  +                jcsCache.remove();
               }
               catch (CacheException e)
               {
  
  
  
  1.11      +57 -30    db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheFactory.java
  
  Index: ObjectCacheFactory.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheFactory.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ObjectCacheFactory.java	19 Feb 2003 22:10:17 -0000	1.10
  +++ ObjectCacheFactory.java	5 Mar 2003 23:52:08 -0000	1.11
  @@ -53,11 +53,18 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    */
  -import org.apache.ojb.broker.util.factory.ConfigurableFactory;
  +
   import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.broker.singlevm.PersistenceBrokerConfiguration;
  +import org.apache.ojb.broker.util.ClassHelper;
  +import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
  +import org.apache.ojb.broker.util.factory.ConfigurableFactory;
   
   /**
    * Factory for {@link ObjectCache} implementation classes.
  + * Each given {@link org.apache.ojb.broker.PersistenceBroker}
  + * was associated with its own <code>ObjectCache</code> instance
  + * and vice versa.
    *
    * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
    * @version $Id$
  @@ -65,47 +72,67 @@
   
   public class ObjectCacheFactory extends ConfigurableFactory
   {
  -
  -	private static ObjectCacheFactory INSTANCE = null;
  -
  -    private ObjectCache CACHE = null;
  +    private static ObjectCacheFactory INSTANCE = new ObjectCacheFactory();
   
       public static ObjectCacheFactory getInstance()
       {
  -    	if (INSTANCE == null)
  -    	{
  -    		INSTANCE = new ObjectCacheFactory();
  -    	}
  -    	return INSTANCE;
  +        return INSTANCE;
       }
   
       public ObjectCache createObjectCache(PersistenceBroker broker)
       {
  -        if (CACHE == null)
  +        ObjectCache objectCache;
  +        try
           {
  -            synchronized (this)
  +            ObjectCache realCache = (ObjectCache) this.createNewInstance(PersistenceBroker.class, broker);
  +
  +            // if cache filtering was enabled, we wrap the object class
  +            // with a class manages the filter classes
  +            if (useCacheFilter())
               {
  -				try
  -				{
  -					CACHE = (ObjectCache) this.createNewInstance(PersistenceBroker.class, broker);
  -				}
  -				catch (Exception e)
  -				{
  -					CACHE = new ObjectCacheDefaultImpl(null);
  -				}
  +                CacheFilterRegistry preCache = new CacheFilterRegistry(realCache);
  +                String[] filters = getFiltersFromConfiguration();
  +                for (int i = 0; i < filters.length; i++)
  +                {
  +                    preCache.addCacheFilter(
  +                            (CacheFilter) ClassHelper.newInstance(
  +                                    filters[i],
  +                                    new Class[]{PersistenceBroker.class, ObjectCache.class},
  +                                    new Object[]{broker, realCache}));
  +                }
  +                objectCache = preCache;
               }
  -
  +            else
  +            {
  +                objectCache = realCache;
  +            }
  +            getLogger().debug("Object cache created, using cache:" + objectCache);
           }
  -        return CACHE;
  +        catch (Exception e)
  +        {
  +            getLogger().error("Error while ObjectCache initiation, please check your configuration" +
  +                    " files and the used implementation class - use default cache instead", e);
  +            objectCache = new ObjectCacheDefaultImpl(null);
  +        }
  +        return objectCache;
  +    }
   
  +    /**
  +     * @see org.apache.ojb.broker.util.factory.ConfigurableFactory#getConfigurationKey()
  +     */
  +    protected String getConfigurationKey()
  +    {
  +        return "ObjectCacheClass";
       }
   
  -	/**
  -	 * @see org.apache.ojb.broker.util.factory.ConfigurableFactory#getConfigurationKey()
  -	 */
  -	protected String getConfigurationKey()
  -	{
  -		return "ObjectCacheClass";
  -	}
  +    private boolean useCacheFilter()
  +    {
  +        return (getFiltersFromConfiguration() != null);
  +    }
   
  +    private String[] getFiltersFromConfiguration()
  +    {
  +        return ((PersistenceBrokerConfiguration) OjbConfigurator.
  +                getInstance().getConfigurationFor(null)).getCacheFilters();
  +    }
   }
  
  
  
  1.7       +15 -4     db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheEmptyImpl.java
  
  Index: ObjectCacheEmptyImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheEmptyImpl.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ObjectCacheEmptyImpl.java	26 Feb 2003 00:43:56 -0000	1.6
  +++ ObjectCacheEmptyImpl.java	5 Mar 2003 23:52:08 -0000	1.7
  @@ -56,17 +56,20 @@
   
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.PersistenceBroker;
  -import org.apache.ojb.broker.PersistenceBrokerListener;
   import org.apache.ojb.broker.PersistenceBrokerException;
  +import org.apache.ojb.broker.PersistenceBrokerListener;
  +import org.apache.commons.lang.builder.ToStringBuilder;
  +import org.apache.commons.lang.builder.ToStringStyle;
   
  -import java.util.Map;
   import java.util.HashMap;
  +import java.util.Map;
   
   /**
    * This is an 'empty' ObjectCache implementation.
    * Useful when caching was not desired.
    * To support 'circular references' this implementation
  - * use a temporary map while store and delete operation.
  + * use a temporary map while store and delete operation
  + * (this may change in further versions).
    *
    * @author Thomas Mahler
    * @version $Id$
  @@ -111,6 +114,14 @@
       public void clear()
       {
           tempMap.clear();
  +    }
  +
  +    public String toString()
  +    {
  +        ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
  +        buf.append("CACHE STATISTICS");
  +        buf.append("Count of temporarily cached objects", tempMap.keySet().size());
  +        return buf.toString();
       }
   
       public void afterStore(Object instance) throws PersistenceBrokerException
  
  
  
  1.10      +4 -3      db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheDefaultImpl.java
  
  Index: ObjectCacheDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheDefaultImpl.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ObjectCacheDefaultImpl.java	26 Feb 2003 00:43:56 -0000	1.9
  +++ ObjectCacheDefaultImpl.java	5 Mar 2003 23:52:08 -0000	1.10
  @@ -165,7 +165,8 @@
       {
           String eol = SystemUtils.LINE_SEPARATOR;
           ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
  -        buf.append("OJB CACHE STATISTICS");
  +        buf.append("CACHE STATISTICS");
  +        buf.append("Count of cached objects", objectTable.keySet().size());
           buf.append("lookups", hitCount);
           buf.append("failures", failCount);
           buf.append("reclaimed", gcCount);
  @@ -175,6 +176,6 @@
       public void finalize()
       {
           Logger logger = LoggerFactory.getDefaultLogger();
  -        logger.info(this.toString());
  +        logger.debug(this.toString());
       }
   }
  
  
  
  1.6       +16 -14    db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCache.java
  
  Index: ObjectCache.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCache.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ObjectCache.java	15 Feb 2003 10:06:48 -0000	1.5
  +++ ObjectCache.java	5 Mar 2003 23:52:08 -0000	1.6
  @@ -55,37 +55,39 @@
    */
   
   import org.apache.ojb.broker.Identity;
  -import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
   
   /**
  - * The ObjectCache stores all Objects loaded by the PersistenceBroker from a DB.
  + * The <code>ObjectCache</code> stores all Objects loaded by the
  + * {@link org.apache.ojb.broker.PersistenceBroker} from a DB.
    * When the PersistenceBroker tries to get an Object by its Primary key values
    * it first lookups the cache if the object has been already loaded and cached.
  - *
  + * <br/><br/>
    * Using an ObjectCache has several advantages:
    * - it increases performance as it reduces DB lookups.
    * - it allows to perform circular lookups (as by crossreferenced objects)
    * that would result in non-terminating loops without such a cache.
    * - it maintains the uniqueness of objects as any Db row will be mapped to
    * exactly one object.
  - *
  + * <br/><br/>
    * This interface allows to have userdefined Cache implementations.
    * The ObjectCacheFactory is responsible for generating cache instances.
  - * by default it uses the OJB ObjectCacheDefaultImpl.
  + * by default it uses the OJB {@link ObjectCacheDefaultImpl}.
  + * <br/><br/>
  + * <b>Note:</b> Each {@link org.apache.ojb.broker.PersistenceBroker} was
  + * associated with its own <code>ObjectCache</code> instance at creation
  + * time.
  + * <br/>
  + * {@link ObjectCacheFactory} is responsible for creating <code>ObjectCache</code>
  + * instances. To make the <code>ObjectCache</code> implementation work, it will
  + * need a constructor with <code>PersistenceBroker</code> argument.
    *
    * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
  + * @author <a href="mailto:armin@codeaulait.de">Armin Waibel<a>
  + *
    * @version $Id$
    */
   public interface ObjectCache
   {
  -
  -//    /**
  -//     * Make object obj persistent to Objectcache.
  -//     * compute objects identity and use it as key for the hashmap
  -//     */
  -//    public void cache(Object obj) throws ClassNotPersistenceCapableException;
  -
  -
       /**
        * makes object obj persistent to the Objectcache under the key oid.
        */
  
  
  
  1.2       +8 -5      db-ojb/src/java/org/apache/ojb/broker/cache/AbstractMetaCache.java
  
  Index: AbstractMetaCache.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/AbstractMetaCache.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractMetaCache.java	15 Feb 2003 10:06:48 -0000	1.1
  +++ AbstractMetaCache.java	5 Mar 2003 23:52:09 -0000	1.2
  @@ -63,8 +63,11 @@
    */
   public abstract class AbstractMetaCache implements ObjectCache
   {
  +    public static final int METHOD_CACHE = 1;
  +    public static final int METHOD_LOOKUP = 2;
  +    public static final int METHOD_REMOVE = 3;
   
  -    protected abstract ObjectCache getCache(Identity oid);
  +    public abstract ObjectCache getCache(Identity oid, Object obj, int callingMethod);
   
       /**
        * Caches the given object using the given Identity as key
  @@ -76,7 +79,7 @@
       {
           if (oid != null && obj != null)
           {
  -            ObjectCache cache = getCache(oid);
  +            ObjectCache cache = getCache(oid, obj, METHOD_CACHE);
               if (cache != null)
               {
                   cache.cache(oid, obj);
  @@ -95,7 +98,7 @@
           Object ret = null;
           if (oid != null)
           {
  -            ObjectCache cache = getCache(oid);
  +            ObjectCache cache = getCache(oid, null, METHOD_LOOKUP);
               if (cache != null)
               {
                   ret = cache.lookup(oid);
  @@ -113,7 +116,7 @@
       {
           if (oid == null) return;
   
  -        ObjectCache cache = getCache(oid);
  +        ObjectCache cache = getCache(oid, null, METHOD_REMOVE);
           if (cache != null)
           {
               cache.remove(oid);
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/cache/MetaObjectCachePerClassImpl.java
  
  Index: MetaObjectCachePerClassImpl.java
  ===================================================================
  package org.apache.ojb.broker.cache;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache ObjectRelationalBridge" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.ojb.broker.Identity;
  import org.apache.ojb.broker.PersistenceBroker;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  
  /**
   * This global cache makes it possible to have separate cache implementations for
   * each class. When an object is cached / looked up the MetaObjectCachePerClassImpl
   * checks if a special ObjectCache has been set for this class. It recursively
   * looks up the superclasses of the given object to look for a special cache. If
   * no special cache is found it uses the {@link ObjectCacheDefaultImpl} to cache the
   * object.<br>
   * It is also possible to switch off caching for a specific class by setting
   * the object cache to null.
   *
   * @author  rzj7l2
   * @version $Id: MetaObjectCachePerClassImpl.java,v 1.1 2003/03/05 23:52:08 arminw Exp $
   */
  public class MetaObjectCachePerClassImpl extends AbstractMetaCache
  {
      private static Map cachesByClass = new HashMap();
  
      /**
       * Constructor for the MetaObjectCachePerClassImpl object
       */
      public MetaObjectCachePerClassImpl(PersistenceBroker broker)
      {
          setClassCache(Object.class, new ObjectCacheDefaultImpl(broker));
      }
  
      public ObjectCache getCache(Identity oid, Object obj, int methodCall)
      {
          return getCachePerClass(oid.getObjectsClass());
      }
  
      /**
       * Clears the cache
       */
      public void clear()
      {
          Iterator it = cachesByClass.values().iterator();
          while (it.hasNext())
          {
              ObjectCache cache = (ObjectCache) it.next();
              if (cache != null)
              {
                  cache.clear();
              }
          }
      }
  
      /**
       * Sets the ObjectCache implementation to use for objects with the given
       * type and subclasses
       *
       * @param objectClass  The object's class, use java.lang.Object to alter
       *                     default caching for all objects which have no special
       *                     caching defined
       * @param cache        The new ObjectCache implementation to use for this
       *                     class and subclasses, null to switch off caching
       *                     for the given class
       */
      public void setClassCache(Class objectClass, ObjectCache cache)
  
      {
          setClassCache(objectClass.getName(), cache);
      }
  
      /**
       * Sets the ObjectCache implementation for the given class name
       *
       * @param className  The name of the class to cache
       * @param cache      The ObjectCache to use for this class and subclasses
       */
      private void setClassCache(String className, ObjectCache cache)
      {
          cachesByClass.put(className, cache);
      }
  
      /**
       * Gets the cache for the given class
       *
       * @param objectClass  The class to look up the cache for
       * @return             The cache
       */
      private ObjectCache getCachePerClass(Class objectClass)
      {
          ObjectCache cache = (ObjectCache) cachesByClass.get(objectClass.getName());
          if (cache == null)
          {
              if (cachesByClass.containsKey(objectClass.getName()))
              {
                  //there is a null-cache present => do not cache this class
              }
              else if (!objectClass.equals(Object.class))
              {
                  //check for superclasses cache
                  cache = getCachePerClass(objectClass.getSuperclass());
              }
          }
          return cache;
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/cache/MetaObjectCacheJCSImpl.java
  
  Index: MetaObjectCacheJCSImpl.java
  ===================================================================
  package org.apache.ojb.broker.cache;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache ObjectRelationalBridge" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.ojb.broker.Identity;
  import org.apache.ojb.broker.PersistenceBroker;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  
  /**
   * A global {@link ObjectCache} implementation using JCS region for
   * each classname. More info see <a href="http://jakarta.apache.org/turbine/jcs/index.html">
   * turbine-JCS</a>.
   *
   * @author  Matthew Baird  (mattbaird@yahoo.com)
   * @version $Id: MetaObjectCacheJCSImpl.java,v 1.1 2003/03/05 23:52:08 arminw Exp $
   */
  
  public class MetaObjectCacheJCSImpl extends AbstractMetaCache
  {
      private Map cachesByClass = new HashMap();
  
      /**
       * Constructor for the MetaObjectCachePerClassImpl object
       */
      public MetaObjectCacheJCSImpl(PersistenceBroker broker)
      {
      }
  
      public ObjectCache getCache(Identity oid, Object obj, int methodCall)
      {
          return getCachePerClass(oid.getObjectsClass());
      }
  
      /**
       * Clears the cache
       */
      public void clear()
      {
          Iterator it = cachesByClass.values().iterator();
          while (it.hasNext())
          {
              ObjectCache cache = (ObjectCache) it.next();
              if (cache != null)
              {
                  cache.clear();
              }
          }
      }
  
      /**
       * Gets the cache for the given class
       *
       * @param objectClass  The class to look up the cache for
       * @return             The cache
       */
      private ObjectCache getCachePerClass(Class objectClass)
      {
          ObjectCache cache = (ObjectCache) cachesByClass.get(objectClass.getName());
          if (cache == null)
          {
              /**
               * the cache wasn't found, and the cachesByClass didn't contain the key with a
               * null value, so create a new cache for this classtype
               */
              cache = new ObjectCacheJCSImpl(objectClass.getName());
              cachesByClass.put(objectClass.getName(), cache);
          }
          return cache;
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/cache/JCSHelper.java
  
  Index: JCSHelper.java
  ===================================================================
  package org.apache.ojb.broker.cache;
  
  import org.apache.jcs.JCS;
  import org.apache.jcs.access.exception.CacheException;
  
  /**
   *
   * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
   * @version $Id: JCSHelper.java,v 1.1 2003/03/05 23:52:09 arminw Exp $
   */
  public class JCSHelper
  {
      public static final String DEFAULT_REGION = "ojbDefault";
  
      public static JCS newInstance(String region)
      {
          try
          {
              return JCS.getInstance(region);
          }
          catch (CacheException e)
          {
              throw new RuntimeCacheException("Instantiation of JCS cache failed", e);
          }
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/cache/CacheFilterRegistry.java
  
  Index: CacheFilterRegistry.java
  ===================================================================
  package org.apache.ojb.broker.cache;
  
  import org.apache.commons.lang.builder.ToStringBuilder;
  import org.apache.commons.lang.builder.ToStringStyle;
  import org.apache.ojb.broker.Identity;
  
  import java.util.ArrayList;
  
  /**
   * This {@link ObjectCache Meta-ObjectCache} implementation enabled
   * the {@link CacheFilter} feature support.
   * <br/>
   * With the {@link #addCacheFilter} method {@link CacheFilter}
   * could be add.
   * <br/><br/>
   * <b>Note:</b> Using this meta cache will affect the
   * cache performance, thus do not add too many {@link CacheFilter}.
   *
   * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
   * @version $Id: CacheFilterRegistry.java,v 1.1 2003/03/05 23:52:09 arminw Exp $
   */
  public class CacheFilterRegistry extends AbstractMetaCache
  {
      private ArrayList filterList = new ArrayList();
      private ObjectCache realCache;
  
      public CacheFilterRegistry(ObjectCache realCache)
      {
          this.realCache = realCache;
      }
  
      /**
       * Add a {@link CacheFilter}.
       */
      protected void addCacheFilter(CacheFilter filter)
      {
          filterList.add(filter);
      }
  
      public ObjectCache getCache(Identity oid, Object obj, int callingMethod)
      {
          return checkFilterList(callingMethod, oid, obj) ? realCache : null;
      }
  
      public void clear()
      {
          realCache.clear();
      }
  
      private boolean checkFilterList(int callingMethod, Identity oid, Object obj)
      {
          boolean result = true;
          if (callingMethod == METHOD_CACHE)
          {
              for (int i = filterList.size() - 1; i >= 0; i--)
              {
                  result = result && ((CacheFilter) filterList.get(i)).beforeCache(oid, obj);
                  if(result == false) return false;
              }
          }
          else if (callingMethod == METHOD_LOOKUP)
          {
              for (int i = filterList.size() - 1; i >= 0; i--)
              {
                  result = result && ((CacheFilter) filterList.get(i)).beforeLookup(oid);
                  if(result == false) return false;
              }
          }
          else if (callingMethod == METHOD_REMOVE)
          {
              for (int i = filterList.size() - 1; i >= 0; i--)
              {
                  result = result && ((CacheFilter) filterList.get(i)).beforeRemove(oid);
                  if(result == false) return false;
              }
          }
          else
              throw new RuntimeCacheException("Unkown filter method id: " + callingMethod);
  
          return result;
      }
  
      public String toString()
      {
          ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
          buf.append(" - used filters", filterList).
                  append(" - underlying ObjectCache", realCache);
          return buf.toString();
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/cache/CacheFilterPackageImpl.java
  
  Index: CacheFilterPackageImpl.java
  ===================================================================
  package org.apache.ojb.broker.cache;
  
  import org.apache.ojb.broker.Identity;
  import org.apache.ojb.broker.PersistenceBroker;
  
  import java.util.StringTokenizer;
  
  /**
   * A {@link CacheFilter} implementation for filtering objects
   * before cached when the given objects package name match a
   * given package, defined as custom attribute within the
   * <code>descriptor-repository</code> element or
   * <code>jdbc-connection-descriptor</code> in the repository file.
   * <br/>
   * <b>Example:</b>
   * <br/>
   * Add this custom attribute to a <code>jdbc-connection-descriptor</code>
   * to avoid caching of package trees for the described connection.
   *
   * <pre>
   * &lt;attribute
   *    attribute-name="exclude-packages"
   *    attribute-value="org.my.test,org.my.admin"/&gt;
   * </pre>
   *
   * To enable a global exclude of caching, add the custom attribute
   * entry under the <code>descriptor-repository</code> element.
   *
   * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
   * @version $Id: CacheFilterPackageImpl.java,v 1.1 2003/03/05 23:52:09 arminw Exp $
   */
  public class CacheFilterPackageImpl implements CacheFilter
  {
      public static final String EXCLUDE_PACKAGES = "exclude-packages";
      private static final String COMMA = ",";
  
      private PersistenceBroker broker;
      private ObjectCache cache;
      private StringTokenizer tok;
      private String filterPackages;
  
      public CacheFilterPackageImpl(PersistenceBroker broker, ObjectCache cache)
      {
          this.broker = broker;
          this.cache = cache;
      }
  
      public boolean beforeCache(Identity oid, Object obj)
      {
          boolean result = true;
          filterPackages = null;
          filterPackages = broker.serviceConnectionManager().
                  getConnectionDescriptor().getAttribute(EXCLUDE_PACKAGES);
          filterPackages = broker.getDescriptorRepository().getAttribute(EXCLUDE_PACKAGES) +
                  (filterPackages != null ? COMMA + filterPackages : "");
          return matchFilterPackages(filterPackages, oid.getObjectsClass());
      }
  
      public boolean beforeLookup(Identity oid)
      {
          return true;
      }
  
      public boolean beforeRemove(Identity oid)
      {
          return true;
      }
  
      public ObjectCache getObjectCache()
      {
          return cache;
      }
  
      private boolean matchFilterPackages(String filterPackages, Class clazz)
      {
          boolean result = true;
          if (filterPackages != null && filterPackages != "")
          {
              String token;
              tok = new StringTokenizer(filterPackages, COMMA);
              while (tok.hasMoreTokens())
              {
                  token = tok.nextToken().trim();
                  if (!token.equals("") && clazz.getName().startsWith(token))
                  {
                      result = false;
                      break;
                  }
              }
          }
          return result;
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/cache/CacheFilterClassImpl.java
  
  Index: CacheFilterClassImpl.java
  ===================================================================
  package org.apache.ojb.broker.cache;
  
  import org.apache.ojb.broker.Identity;
  import org.apache.ojb.broker.PersistenceBroker;
  
  /**
   * A {@link CacheFilter} implementation for filtering objects
   * before cached, using custom attribute <code>cacheable</code>
   * in <code>class-descriptor</code> in repository file.
   *
   * <br/>
   * <b>Example:</b>
   * <br/>
   * Add this custom attribute to your <code>class-descriptor</code> to avoid
   * caching of the class.
   * <pre>
   * &lt;attribute
   *    attribute-name="cacheable"
   *    attribute-value="false"/&gt;
   * </pre>
   *
   * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
   * @version $Id: CacheFilterClassImpl.java,v 1.1 2003/03/05 23:52:09 arminw Exp $
   */
  public class CacheFilterClassImpl implements CacheFilter
  {
      public static final String CACHEABLE = "cacheable";
      private PersistenceBroker broker;
      private ObjectCache cache;
  
      public CacheFilterClassImpl(PersistenceBroker broker, ObjectCache cache)
      {
          this.broker = broker;
          this.cache = cache;
      }
  
      public boolean beforeCache(Identity oid, Object obj)
      {
          String cacheable = broker.getClassDescriptor(oid.getObjectsClass()).getAttribute(CACHEABLE);
          if (cacheable == null || (new Boolean(cacheable)).booleanValue())
          {
              return true;
          }
          else
          {
              return false;
          }
      }
  
      public boolean beforeLookup(Identity oid)
      {
          return true;
      }
  
      public boolean beforeRemove(Identity oid)
      {
          return true;
      }
  
      public ObjectCache getObjectCache()
      {
          return cache;
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/cache/CacheFilter.java
  
  Index: CacheFilter.java
  ===================================================================
  package org.apache.ojb.broker.cache;
  
  import org.apache.ojb.broker.Identity;
  
  /**
   * Implementations of this interface could be used do filter
   * operations, checks or whatever before the cache implementation
   * was called.
   * <br>
   * <b>Note:</b> Need a constructor with {@link org.apache.ojb.broker.PersistenceBroker}
   * + {@link ObjectCache} parameter.
   *
   * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
   * @version $Id: CacheFilter.java,v 1.1 2003/03/05 23:52:09 arminw Exp $
   */
  public interface CacheFilter
  {
      /**
       * Called before an object was put in cache - If <code>false</code>
       * was returned the object was not put in cache.
       */
      public boolean beforeCache(Identity oid, Object obj);
  
      /**
       * Called before an object was looked up in cache - If <code>false</code>
       * was returned the object was not looked up in cache.
       */
      public boolean beforeLookup(Identity oid);
  
      /**
       * Called before an object was removed from cache - If <code>false</code>
       * was returned the object was not removed from in cache.
       */
      public boolean beforeRemove(Identity oid);
  
      /**
       * Returns the underlying {@link ObjectCache}.
       */
      public ObjectCache getObjectCache();
  }
  
  
  
  1.9       +7 -2      db-ojb/src/java/org/apache/ojb/broker/util/factory/ConfigurableFactory.java
  
  Index: ConfigurableFactory.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/factory/ConfigurableFactory.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ConfigurableFactory.java	18 Feb 2003 10:51:14 -0000	1.8
  +++ ConfigurableFactory.java	5 Mar 2003 23:52:09 -0000	1.9
  @@ -232,16 +232,21 @@
        * Returns the classToServe.
        * @return Class
        */
  -    protected Class getClassToServe()
  +    public Class getClassToServe()
       {
           return classToServe;
       }
   
       /**
        * Sets the classToServe.
  +     * <br/>
  +     * Normally this is done by the factory using
  +     * {@link #getConfigurationKey}.
  +     * <br/>
  +     * <b>Note:</b> For internal use only!
        * @param classToServe The classToServe to set
        */
  -    protected void setClassToServe(Class classToServe)
  +    public void setClassToServe(Class classToServe)
       {
           this.classToServe = classToServe;
       }
  
  
  

Mime
View raw message