You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2003/09/18 01:00:16 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/broker/cache ObjectCacheLocalDefaultImpl.java ObjectCacheJCSPerClassImpl.java CacheDistributor.java RuntimeCacheException.java ObjectCacheUnlimitedImpl.java ObjectCacheSoftImpl.java ObjectCachePerClassImpl.java ObjectCachePerBrokerImpl.java ObjectCacheJCSImpl.java ObjectCacheFactory.java ObjectCacheEmptyImpl.java ObjectCacheDefaultImpl.java ObjectCache.java JCSHelper.java CacheFilter.java AbstractMetaCache.java MetaObjectCachePerClassImpl.java MetaObjectCacheJCSImpl.java AttributeBasedObjectCacheImpl.java
arminw 2003/09/17 16:00:16
Modified: src/java/org/apache/ojb/broker/cache
RuntimeCacheException.java
ObjectCacheUnlimitedImpl.java
ObjectCacheSoftImpl.java
ObjectCachePerClassImpl.java
ObjectCachePerBrokerImpl.java
ObjectCacheJCSImpl.java ObjectCacheFactory.java
ObjectCacheEmptyImpl.java
ObjectCacheDefaultImpl.java ObjectCache.java
JCSHelper.java CacheFilter.java
AbstractMetaCache.java
Added: src/java/org/apache/ojb/broker/cache
ObjectCacheLocalDefaultImpl.java
ObjectCacheJCSPerClassImpl.java
CacheDistributor.java
Removed: src/java/org/apache/ojb/broker/cache
MetaObjectCachePerClassImpl.java
MetaObjectCacheJCSImpl.java
AttributeBasedObjectCacheImpl.java
Log:
- add support for per class-descriptor/jdbc-connection-des...
ObjectCache declaration
- add new ObjectCache implementation
- rename MetaObjectCacheJCSImpl to ObjectCacheJCSPerClassImpl
- rename AttributeBasedObjectCacheImpl to CacheDistributor
- remove outdated/not used implementation MetaObjectCachePerClassImpl
- adapt ObjectCache implementations, use new constructor
Revision Changes Path
1.4 +3 -8 db-ojb/src/java/org/apache/ojb/broker/cache/RuntimeCacheException.java
Index: RuntimeCacheException.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/RuntimeCacheException.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- RuntimeCacheException.java 21 Nov 2002 09:39:04 -0000 1.3
+++ RuntimeCacheException.java 17 Sep 2003 23:00:15 -0000 1.4
@@ -1,17 +1,12 @@
package org.apache.ojb.broker.cache;
-import org.apache.commons.lang.exception.NestableRuntimeException;
+import org.apache.ojb.broker.OJBRuntimeException;
/**
* @author Matthew Baird
* @version $Id$
- *
- * To change this generated comment edit the template variable "typecomment":
- * Window>Preferences>Java>Templates.
- * To enable and disable the creation of type comments go to
- * Window>Preferences>Java>Code Generation.
*/
-public class RuntimeCacheException extends NestableRuntimeException
+public class RuntimeCacheException extends OJBRuntimeException
{
public RuntimeCacheException()
{
1.2 +7 -15 db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheUnlimitedImpl.java
Index: ObjectCacheUnlimitedImpl.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheUnlimitedImpl.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ObjectCacheUnlimitedImpl.java 1 Jul 2003 21:08:26 -0000 1.1
+++ ObjectCacheUnlimitedImpl.java 17 Sep 2003 23:00:15 -0000 1.2
@@ -7,13 +7,13 @@
import java.util.HashMap;
import java.util.Map;
+import java.util.Properties;
/**
- * Created by IntelliJ IDEA.
- * User: matthew.baird
- * Date: Jun 23, 2003
- * Time: 1:24:37 PM
- * To change this template use Options | File Templates.
+ * Global {@link ObjectCache} implementation.
+ *
+ * @author matthew.baird
+ * @version $Id$
*/
public class ObjectCacheUnlimitedImpl implements ObjectCache
{
@@ -25,7 +25,7 @@
/**
* public Default Constructor
*/
- public ObjectCacheUnlimitedImpl(PersistenceBroker broker)
+ public ObjectCacheUnlimitedImpl(PersistenceBroker broker, Properties prop)
{
}
@@ -38,9 +38,7 @@
}
/**
- * Makes object persistent to the Objectcache.
- * I'm using soft-references to allow gc reclaim unused objects
- * even if they are still cached.
+ * Makes object persistent to the Objectcache
*/
public void cache(Identity oid, Object obj)
{
@@ -68,11 +66,5 @@
{
objectTable.remove(oid);
}
- }
-
- public void finalize()
- {
- Logger logger = LoggerFactory.getDefaultLogger();
- logger.debug(this.toString());
}
}
1.3 +5 -2 db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheSoftImpl.java
Index: ObjectCacheSoftImpl.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheSoftImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ObjectCacheSoftImpl.java 29 Aug 2003 14:41:39 -0000 1.2
+++ ObjectCacheSoftImpl.java 17 Sep 2003 23:00:15 -0000 1.3
@@ -71,9 +71,12 @@
import org.apache.ojb.broker.Identity;
/**
- * This implementation relies on JDK1.4 features like the LinkedHashMap.
+ * This global {@link ObjectCache} implementation relies on
+ * JDK1.4 features like the LinkedHashMap.
* Thus it can not be used under JDK1.3 or below.
+ *
* @author matthew.baird
+ * @version $Id$
*/
public final class ObjectCacheSoftImpl implements ObjectCache
//#ifdef JDBC30
@@ -269,7 +272,7 @@
}
//#else
/*
-{
+{
public void cache(Identity oid, Object obj)
{
1.3 +14 -13 db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCachePerClassImpl.java
Index: ObjectCachePerClassImpl.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCachePerClassImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ObjectCachePerClassImpl.java 8 Jul 2003 06:05:36 -0000 1.2
+++ ObjectCachePerClassImpl.java 17 Sep 2003 23:00:15 -0000 1.3
@@ -6,29 +6,30 @@
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
+import java.util.Properties;
+import java.util.Collections;
/**
- * Created by IntelliJ IDEA.
- * User: matthew.baird
- * Date: May 21, 2003
- * Time: 12:18:27 PM
- * To change this template use Options | File Templates.
+ * Global {@link ObjectCache} implementation.
+ *
+ * @author matthew.baird
+ * @version $Id$
*/
public class ObjectCachePerClassImpl extends AbstractMetaCache
{
- private static Map cachesByClass = new HashMap();
+ private static Map cachesByClass = Collections.synchronizedMap(new HashMap());
/**
- * Constructor for the MetaObjectCachePerClassImpl object
+ * Constructor for the ObjectCachePerClassImpl object
*/
- public ObjectCachePerClassImpl(PersistenceBroker broker)
+ public ObjectCachePerClassImpl(PersistenceBroker broker, Properties prop)
{
- setClassCache(Object.class, new ObjectCacheDefaultImpl(broker));
+ setClassCache(Object.class, new ObjectCacheDefaultImpl(broker, null));
}
public ObjectCache getCache(Identity oid, Object obj, int methodCall)
{
- return getCachePerClass(oid.getObjectsRealClass());
+ return getCachePerClass(oid.getObjectsRealClass(), methodCall);
}
/**
@@ -81,12 +82,12 @@
* @param objectClass The class to look up the cache for
* @return The cache
*/
- private ObjectCache getCachePerClass(Class objectClass)
+ private ObjectCache getCachePerClass(Class objectClass, int methodCall)
{
ObjectCache cache = (ObjectCache) cachesByClass.get(objectClass.getName());
- if (cache == null)
+ if (cache == null && AbstractMetaCache.METHOD_CACHE == methodCall)
{
- cache = new ObjectCacheDefaultImpl(null);
+ cache = new ObjectCacheDefaultImpl(null, null);
setClassCache(objectClass.getName(), cache);
}
return cache;
1.7 +20 -2 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.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ObjectCachePerBrokerImpl.java 12 May 2003 14:01:32 -0000 1.6
+++ ObjectCachePerBrokerImpl.java 17 Sep 2003 23:00:15 -0000 1.7
@@ -62,12 +62,30 @@
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;
+import java.util.Properties;
/**
- * This local cache implementation allows to have dedicated caches per broker.
+ * This local {@link ObjectCache} implementation allows to have dedicated caches per broker.
* All calls are delegated to the cache associated with the currentBroker.
* When the broker was closed (returned to pool) the cache was cleared.
*
+ * <p>
+ * Implementation configuration properties:
+ * </p>
+ *
+ * <table cellspacing="2" cellpadding="2" border="3" frame="box">
+ * <tr>
+ * <td><strong>Property Key</strong></td>
+ * <td><strong>Property Values</strong></td>
+ * </tr>
+ * <tr>
+ * <td> - </td>
+ * <td>
+ * -
+ * </td>
+ * </tr>
+ * </table>
+ *
* @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
* @version $Id$
*/
@@ -81,7 +99,7 @@
/**
* public Default Constructor
*/
- public ObjectCachePerBrokerImpl(PersistenceBroker broker)
+ public ObjectCachePerBrokerImpl(PersistenceBroker broker, Properties prop)
{
objectTable = new HashMap();
// add this cache as permanent listener
1.8 +43 -28 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.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- ObjectCacheJCSImpl.java 5 Mar 2003 23:52:08 -0000 1.7
+++ ObjectCacheJCSImpl.java 17 Sep 2003 23:00:15 -0000 1.8
@@ -61,9 +61,32 @@
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.PersistenceBroker;
+import java.util.Properties;
+
/**
- * {@link ObjectCache} implementation only for use in conjunction
- * with {@link MetaObjectJCSCacheImpl}.
+ * This local {@link ObjectCache} implementation using
+ * <a href="http://jakarta.apache.org/turbine/jcs/index.html">
+ * turbine-JCS</a> to cache objects is primarily for intern use in
+ * conjunction with {@link ObjectCacheJCSPerClassImpl} implementation. If
+ * used as main <code>ObjectCache</code> all cached objects will be cached
+ * under the same JCS region name (see {@link JCSHelper#DEFAULT_REGION}).
+ *
+ * <p>
+ * Implementation configuration properties:
+ * </p>
+ *
+ * <table cellspacing="2" cellpadding="2" border="3" frame="box">
+ * <tr>
+ * <td><strong>Property Key</strong></td>
+ * <td><strong>Property Values</strong></td>
+ * </tr>
+ * <tr>
+ * <td> - </td>
+ * <td>
+ * -
+ * </td>
+ * </tr>
+ * </table>
*
* @author Matthew Baird (mattbaird@yahoo.com);
* @version $Id$
@@ -76,29 +99,23 @@
*/
private String regionName = JCSHelper.DEFAULT_REGION;
- /**
- * Constructor used by the {@link MetaObjectCacheJCSImpl}
- */
- public ObjectCacheJCSImpl(String name)
+ public ObjectCacheJCSImpl(PersistenceBroker broker, Properties prop)
{
- regionName = name;
- jcsCache = JCSHelper.newInstance(regionName);
+ this(null);
}
/**
- * Constructor used by {@link ObjectFactory}
+ * Constructor used by the {@link ObjectCacheJCSPerClassImpl}
*/
- public ObjectCacheJCSImpl(PersistenceBroker broker)
+ public ObjectCacheJCSImpl(String name)
{
+ regionName = name != null ? name : regionName;
jcsCache = JCSHelper.newInstance(regionName);
}
- public String toString()
+ public String getRegionName()
{
- ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
- buf.append("JCS region name", regionName);
- buf.append("JCS region", jcsCache);
- return buf.toString();
+ return regionName;
}
/**
@@ -112,7 +129,7 @@
}
catch (CacheException e)
{
- throw new RuntimeCacheException(e.getMessage());
+ throw new RuntimeCacheException(e);
}
}
@@ -127,7 +144,7 @@
/**
* removes an Object from the cache.
- * @param obj the Object (or the Identity of the object) to be removed.
+ * @param oid the Identity of the object to be removed.
*/
public void remove(Identity oid)
{
@@ -141,16 +158,6 @@
}
}
- public void setRegionName(String region)
- {
- regionName = region;
- }
-
- public String getRegionName()
- {
- return regionName;
- }
-
/**
* clear the ObjectCache.
*/
@@ -167,5 +174,13 @@
throw new RuntimeCacheException(e);
}
}
+ }
+
+ public String toString()
+ {
+ ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
+ buf.append("JCS region name", regionName);
+ buf.append("JCS region", jcsCache);
+ return buf.toString();
}
}
1.13 +79 -17 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.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- ObjectCacheFactory.java 26 Apr 2003 23:18:25 -0000 1.12
+++ ObjectCacheFactory.java 17 Sep 2003 23:00:15 -0000 1.13
@@ -55,11 +55,15 @@
*/
import org.apache.ojb.broker.PersistenceBroker;
+import org.apache.ojb.broker.metadata.ObjectCacheDescriptor;
import org.apache.ojb.broker.core.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;
+import java.util.Properties;
+import java.lang.reflect.Constructor;
+
/**
* Factory for {@link ObjectCache} implementation classes.
* Each given {@link org.apache.ojb.broker.PersistenceBroker}
@@ -72,48 +76,87 @@
public class ObjectCacheFactory extends ConfigurableFactory
{
- private static ObjectCacheFactory INSTANCE = new ObjectCacheFactory();
+ private static ObjectCacheFactory singleton = new ObjectCacheFactory();
+
+ private ObjectCache defaultCache;
+ /**
+ * Get the ObjectCacheFactory instance.
+ */
public static ObjectCacheFactory getInstance()
{
- return INSTANCE;
+ return singleton;
}
+ public ObjectCacheFactory()
+ {
+ }
+
+ /**
+ * Creates a new {@link InternObjectCache} instance.
+ *
+ * @param broker The PB instance to associate with the cache instance
+ */
public ObjectCache createObjectCache(PersistenceBroker broker)
{
ObjectCache objectCache;
try
{
- 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
+ getLogger().info("Start creating new ObjectCache instance");
+ /*
+ 1.
+ if default cache was not found,
+ create an new instance of the default cache specified in the
+ configuration.
+ 2.
+ Then instantiate AllocatorObjectCache to handle
+ per connection/ per class caching instances.
+ 3.
+ Check if cache filtering is enabled. If true, create filter classes
+ and add to filter registry instance - wraps AllocatorCache.
+ 4.
+ To support intern operations we wrap ObjectCache with an
+ InternalObjectCache implementation
+ */
+
+ defaultCache = (ObjectCache) this.createNewInstance(
+ new Class[]{PersistenceBroker.class, Properties.class}, new Object[]{broker, null});
+
+ objectCache = defaultCache;
+ getLogger().info("Default ObjectCache class was " + objectCache.getClass().getName());
+ objectCache = new CacheDistributor(broker, objectCache);
+ getLogger().info("Instantiate new " + objectCache.getClass().getName() + " class object");
+ /*
+ if cache filtering was enabled, we wrap the cache
+ with a class manages the filter classes
+ */
if (useCacheFilter())
{
- CacheFilterRegistry preCache = new CacheFilterRegistry(realCache);
+ getLogger().info("Enable CacheFilters");
+ CacheFilterRegistry filterCache = new CacheFilterRegistry(objectCache);
String[] filters = getFiltersFromConfiguration();
for (int i = 0; i < filters.length; i++)
{
- preCache.addCacheFilter(
+ getLogger().info("Create CacheFilter: "
+ + (filters[i] != null ? filters[i].getClass().getName() : null));
+ filterCache.addCacheFilter(
(CacheFilter) ClassHelper.newInstance(
filters[i],
new Class[]{PersistenceBroker.class, ObjectCache.class},
- new Object[]{broker, realCache}));
+ new Object[]{broker, objectCache}));
}
- objectCache = preCache;
- }
- else
- {
- objectCache = realCache;
+ objectCache = filterCache;
}
getLogger().debug("Object cache created, using cache:" + objectCache);
+
}
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);
+ " files and the used implementation class - use default cache " + defaultCache + "instead", e);
+ objectCache = defaultCache;
}
+ getLogger().info("New ObjectCache instance was created");
return objectCache;
}
@@ -134,5 +177,24 @@
{
return ((PersistenceBrokerConfiguration) OjbConfigurator.
getInstance().getConfigurationFor(null)).getCacheFilters();
+ }
+
+ protected ObjectCache createNewCacheInstance(Class target, PersistenceBroker broker,
+ Properties prop) throws Exception
+ {
+ ObjectCache newCache = null;
+ try
+ {
+ newCache = (ObjectCache) ClassHelper.newInstance(
+ target,
+ new Class[]{PersistenceBroker.class, Properties.class},
+ new Object[]{broker, prop});
+ }
+ catch (Exception e)
+ {
+ getLogger().error("Can not create ObjectCache instance using class " + target, e);
+ throw e;
+ }
+ return newCache;
}
}
1.9 +26 -83 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.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- ObjectCacheEmptyImpl.java 12 Apr 2003 23:24:41 -0000 1.8
+++ ObjectCacheEmptyImpl.java 17 Sep 2003 23:00:15 -0000 1.9
@@ -57,15 +57,9 @@
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.ojb.broker.Identity;
-import org.apache.ojb.broker.PBLifeCycleEvent;
-import org.apache.ojb.broker.PBLifeCycleListener;
-import org.apache.ojb.broker.PBStateEvent;
-import org.apache.ojb.broker.PBStateListener;
import org.apache.ojb.broker.PersistenceBroker;
-import org.apache.ojb.broker.PersistenceBrokerException;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.Properties;
/**
* This is an 'empty' ObjectCache implementation.
@@ -74,17 +68,33 @@
* use a temporary map while store and delete operation
* (this may change in further versions).
*
+ * <p>
+ * Implementation configuration properties:
+ * </p>
+ *
+ * <table cellspacing="2" cellpadding="2" border="3" frame="box">
+ * <tr>
+ * <td><strong>Property Key</strong></td>
+ * <td><strong>Property Values</strong></td>
+ * </tr>
+ * <tr>
+ * <td> - </td>
+ * <td>
+ * -
+ * </td>
+ * </tr>
+ * </table>
+ *
* @author Thomas Mahler
* @version $Id$
*
*/
-public class ObjectCacheEmptyImpl implements ObjectCache, PBStateListener, PBLifeCycleListener
+public class ObjectCacheEmptyImpl implements ObjectCache
{
- private Map tempMap = new HashMap();
- public ObjectCacheEmptyImpl(PersistenceBroker broker)
+ public ObjectCacheEmptyImpl(PersistenceBroker broker, Properties prop)
{
- broker.addListener(this, true);
+
}
/**
@@ -92,7 +102,7 @@
*/
public void cache(Identity oid, Object obj)
{
- tempMap.put(oid, obj);
+
}
/**
@@ -100,7 +110,7 @@
*/
public Object lookup(Identity oid)
{
- return tempMap.get(oid);
+ return null;
}
/**
@@ -108,7 +118,7 @@
*/
public void remove(Identity oid)
{
- tempMap.remove(oid);
+
}
/**
@@ -116,80 +126,13 @@
*/
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 afterOpen(PBStateEvent event)
- {
- }
-
- public void beforeBegin(PBStateEvent event)
- {
- clear();
- }
-
- public void afterBegin(PBStateEvent event)
- {
- }
-
- public void beforeCommit(PBStateEvent event)
- {
- }
-
- public void afterCommit(PBStateEvent event)
- {
- }
-
- public void beforeRollback(PBStateEvent event)
- {
- }
-
- public void afterRollback(PBStateEvent event)
- {
- }
-
- public void beforeClose(PBStateEvent event)
- {
- clear();
- }
-
- public void beforeInsert(PBLifeCycleEvent event) throws PersistenceBrokerException
- {
- }
-
- public void afterInsert(PBLifeCycleEvent event) throws PersistenceBrokerException
- {
- clear();
- }
-
- public void beforeUpdate(PBLifeCycleEvent event) throws PersistenceBrokerException
- {
- }
-
- public void afterUpdate(PBLifeCycleEvent event) throws PersistenceBrokerException
- {
- clear();
- }
-
- public void beforeDelete(PBLifeCycleEvent event) throws PersistenceBrokerException
- {
- }
-
- public void afterDelete(PBLifeCycleEvent event) throws PersistenceBrokerException
- {
- clear();
- }
-
- public void afterLookup(PBLifeCycleEvent event) throws PersistenceBrokerException
- {
}
}
1.13 +70 -28 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.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- ObjectCacheDefaultImpl.java 12 May 2003 14:01:32 -0000 1.12
+++ ObjectCacheDefaultImpl.java 17 Sep 2003 23:00:15 -0000 1.13
@@ -57,6 +57,7 @@
import java.lang.ref.SoftReference;
import java.util.Hashtable;
import java.util.Map;
+import java.util.Properties;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
@@ -66,21 +67,39 @@
import org.apache.ojb.broker.util.logging.LoggerFactory;
/**
- * This global ObjectCache stores all Objects loaded by the 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.
- *
- * 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.
- *
+ * This global ObjectCache stores all Objects loaded by the <code>PersistenceBroker</code>
+ * from a DB using a static {@link java.util.Map}. This means each {@link ObjectCache}
+ * instance associated with all {@link PersistenceBroker} instances use the same
+ * <code>Map</code> to cache objects.
+ * <br/>
+ * When the PersistenceBroker tries to get an Object by its {@link Identity}.
+ * It first lookups the cache if the object has been already loaded and cached.
+ * <br/>
* The cache uses soft-references which allows objects (softly) referenced by
* the cache to be reclaimed by the Java Garbage Collector when they are not
* longer referenced elsewhere.
*
+ * <p>
+ * Implementation configuration properties:
+ * </p>
+ *
+ * <table cellspacing="2" cellpadding="2" border="3" frame="box">
+ * <tr>
+ * <td><strong>Property Key</strong></td>
+ * <td><strong>Property Values</strong></td>
+ * </tr>
+ * <tr>
+ * <td>timeout</td>
+ * <td>
+ * Lifetime of the cached objects in milliseconds.
+ * If expired the cached object was not returned
+ * on lookup call (and removed from cache).
+ * </td>
+ * </tr>
+ * </table>
+ *
+ * <br/>
+ *
* @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
* @version $Id$
*/
@@ -91,15 +110,21 @@
*/
protected static Map objectTable = new Hashtable();
- private long hitCount = 0;
- private long failCount = 0;
- private long gcCount = 0;
+ private static long hitCount = 0;
+ private static long failCount = 0;
+ private static long gcCount = 0;
+
+ /**
+ * Timeout of the cached objects.
+ */
+ private long timeout = 1000 * 60 * 15;
/**
- * public Default Constructor
+ *
*/
- public ObjectCacheDefaultImpl(PersistenceBroker broker)
+ public ObjectCacheDefaultImpl(PersistenceBroker broker, Properties prop)
{
+ timeout = prop == null ? timeout : new Long(prop.getProperty("timeout", "" + timeout)).longValue();
}
/**
@@ -119,7 +144,7 @@
{
if ((obj != null))
{
- SoftReference ref = new SoftReference(obj);
+ SoftReference ref = new SoftReference(new CacheEntry(obj));
objectTable.put(oid, ref);
}
}
@@ -131,22 +156,24 @@
public Object lookup(Identity oid)
{
hitCount++;
- Object obj = null;
+ CacheEntry entry = null;
SoftReference ref = (SoftReference) objectTable.get(oid);
if (ref != null)
{
- obj = ref.get();
- if (obj == null)
+ entry = (CacheEntry) ref.get();
+ if (entry == null || entry.lifetime < System.currentTimeMillis())
{
gcCount++;
objectTable.remove(oid); // Soft-referenced Object reclaimed by GC
+ // timeout, so set null
+ entry = null;
}
}
else
{
failCount++;
}
- return obj;
+ return entry != null ? entry.object : null;
}
/**
@@ -162,18 +189,33 @@
public String toString()
{
- ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
- buf.append("CACHE STATISTICS");
+ ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
buf.append("Count of cached objects", objectTable.keySet().size());
- buf.append("lookups", hitCount);
- buf.append("failures", failCount);
- buf.append("reclaimed", gcCount);
+ buf.append("Lookup hits", hitCount);
+ buf.append("Failures", failCount);
+ buf.append("Reclaimed", gcCount);
return buf.toString();
}
public void finalize()
{
Logger logger = LoggerFactory.getDefaultLogger();
- logger.debug(this.toString());
+ logger.debug("Finalize ObjectCache: "+this.toString());
}
+
+ //-----------------------------------------------------------
+ // inner class
+ //-----------------------------------------------------------
+ class CacheEntry
+ {
+ long lifetime;
+ Object object;
+
+ public CacheEntry(Object object)
+ {
+ this.object = object;
+ lifetime = System.currentTimeMillis() + timeout;
+ }
+ }
+
}
1.7 +6 -5 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.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ObjectCache.java 5 Mar 2003 23:52:08 -0000 1.6
+++ ObjectCache.java 17 Sep 2003 23:00:15 -0000 1.7
@@ -78,8 +78,10 @@
* 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.
+ * instances. To make the <code>ObjectCache</code> implementation work, a
+ * constructor with {@link org.apache.ojb.broker.PersistenceBroker} and
+ * {@link java.util.Properties} as arguments or only <code>PersistenceBroker</code>
+ * argument is needed.
*
* @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
* @author <a href="mailto:armin@codeaulait.de">Armin Waibel<a>
@@ -100,10 +102,9 @@
*/
public Object lookup(Identity oid);
-
/**
* removes an Object from the cache.
- * @param obj the Object (or the Identity of the object) to be removed.
+ * @param oid Identity of the object to be removed.
*/
public void remove(Identity oid);
1.2 +7 -2 db-ojb/src/java/org/apache/ojb/broker/cache/JCSHelper.java
Index: JCSHelper.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/JCSHelper.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JCSHelper.java 5 Mar 2003 23:52:09 -0000 1.1
+++ JCSHelper.java 17 Sep 2003 23:00:15 -0000 1.2
@@ -4,12 +4,17 @@
import org.apache.jcs.access.exception.CacheException;
/**
- *
+ * Helper class to support <a href="http://jakarta.apache.org/turbine/jcs/index.html">
+ * turbine-JCS</a>.
+ *
* @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
* @version $Id$
*/
public class JCSHelper
{
+ /**
+ * The used default region name.
+ */
public static final String DEFAULT_REGION = "ojbDefault";
public static JCS newInstance(String region)
1.2 +7 -7 db-ojb/src/java/org/apache/ojb/broker/cache/CacheFilter.java
Index: CacheFilter.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/CacheFilter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CacheFilter.java 5 Mar 2003 23:52:09 -0000 1.1
+++ CacheFilter.java 17 Sep 2003 23:00:15 -0000 1.2
@@ -3,12 +3,12 @@
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.
+ * Implementations of this interface can be used do filter
+ * operations, checks or whatever - before the used {@link ObjectCache}
+ * implementation was called.
* <br>
- * <b>Note:</b> Need a constructor with {@link org.apache.ojb.broker.PersistenceBroker}
- * + {@link ObjectCache} parameter.
+ * <b>Note:</b> All implemenation classes 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$
@@ -34,7 +34,7 @@
public boolean beforeRemove(Identity oid);
/**
- * Returns the underlying {@link ObjectCache}.
+ * Returns the underlying {@link ObjectCache} implemenation.
*/
public ObjectCache getObjectCache();
}
1.3 +18 -2 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.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- AbstractMetaCache.java 5 Mar 2003 23:52:09 -0000 1.2
+++ AbstractMetaCache.java 17 Sep 2003 23:00:15 -0000 1.3
@@ -57,6 +57,11 @@
import org.apache.ojb.broker.Identity;
/**
+ * An abstract 'meta' implementation of the {@link ObjectCache}
+ * interace.
+ * <br/>
+ * Implement the abstract {@link #getCache} method in sub-classes.
+ * All base Object/Identity validation is done by this class.
*
* @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
* @version $Id$
@@ -67,6 +72,17 @@
public static final int METHOD_LOOKUP = 2;
public static final int METHOD_REMOVE = 3;
+ /**
+ * This method handle all calls against the {@link ObjectCache} interface.
+ * Note: The parameter <code>obj</code> can be <code>null</code> - e.g. when
+ * lookup or remove method was called.
+ *
+ * @param oid Identity of the target object.
+ * @param obj The target object itself or <code>null</code> if not available.
+ * @param callingMethod Specifies the type of method call against the {@link ObjectCache}
+ * interface. {@link #METHOD_CACHE}, {@link #METHOD_LOOKUP}, {@link #METHOD_REMOVE}.
+ * @return The {@link ObjectCache} implementation.
+ */
public abstract ObjectCache getCache(Identity oid, Object obj, int callingMethod);
/**
@@ -110,7 +126,7 @@
/**
* Removes the given object from the cache
*
- * @param obj The object to remove
+ * @param oid oid of the object to remove
*/
public void remove(Identity oid)
{
1.1 db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheLocalDefaultImpl.java
Index: ObjectCacheLocalDefaultImpl.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.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.PersistenceBroker;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* Simple, flexible local {@link ObjectCache} implementation using a
* {@link java.util.HashMap} to cache given objects.
*
* The cache uses soft-references which allows objects (softly) referenced by
* the cache to be reclaimed by the Java Garbage Collector when they are not
* longer referenced elsewhere.
*
* <p>
* Implementation configuration properties:
* </p>
*
* <table cellspacing="2" cellpadding="2" border="3" frame="box">
* <tr>
* <td><strong>Property Key</strong></td>
* <td><strong>Property Values</strong></td>
* </tr>
* <tr>
* <td>timeout</td>
* <td>
* Lifetime of the cached objects in milliseconds.
* If expired the cached object was not returned
* on lookup call (and removed from cache).
* </td>
* </tr>
* </table>
*
* <br/>
*
* @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
* @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
* @version $Id: ObjectCacheLocalDefaultImpl.java,v 1.1 2003/09/17 23:00:15 arminw Exp $
*/
public class ObjectCacheLocalDefaultImpl implements ObjectCache
{
private static final String TIMEOUT = "timeout";
/**
* the hashtable holding all cached object
*/
protected Map objectTable = new HashMap();
/**
* Timeout of the cached objects.
*/
private long timeout = 1000 * 60 * 15;
/**
*
*/
public ObjectCacheLocalDefaultImpl(PersistenceBroker broker, Properties prop)
{
timeout = prop == null ? timeout : new Long(prop.getProperty(TIMEOUT, "" + timeout)).longValue();
}
/**
* Clear ObjectCache. I.e. remove all entries for classes and objects.
*/
public void clear()
{
objectTable.clear();
}
/**
* Makes object persistent to the Objectcache.
* I'm using soft-references to allow gc reclaim unused objects
* even if they are still cached.
*/
public void cache(Identity oid, Object obj)
{
if ((obj != null))
{
SoftReference ref = new SoftReference(new CacheEntry(obj));
objectTable.put(oid, ref);
}
}
/**
* Lookup object with Identity oid in objectTable.
* Returns null if no matching id is found
*/
public Object lookup(Identity oid)
{
CacheEntry entry = null;
SoftReference ref = (SoftReference) objectTable.get(oid);
if (ref != null)
{
entry = (CacheEntry) ref.get();
if (entry == null || entry.lifetime < System.currentTimeMillis())
{
objectTable.remove(oid); // Soft-referenced Object reclaimed by GC
// timeout, so set null
entry = null;
}
}
return entry != null ? entry.object : null;
}
/**
* Removes an Object from the cache.
*/
public void remove(Identity oid)
{
if (oid != null)
{
objectTable.remove(oid);
}
}
public String toString()
{
ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
buf.append("Count of cached objects", objectTable.keySet().size());
return buf.toString();
}
//-----------------------------------------------------------
// inner class
//-----------------------------------------------------------
class CacheEntry
{
long lifetime;
Object object;
public CacheEntry(Object object)
{
this.object = object;
lifetime = System.currentTimeMillis() + timeout;
}
}
}
1.1 db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheJCSPerClassImpl.java
Index: ObjectCacheJCSPerClassImpl.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 org.apache.ojb.broker.util.logging.LoggerFactory;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
/**
* A global {@link ObjectCache} implementation using a JCS region for
* each class. Each class name was associated with a dedicated
* {@link ObjectCacheJCSImpl} instance to cache given objects.
* This allows to define JCS cache region configuration properties
* for each used class in JCS configuration files.
*
* <br/>
* More info see <a href="http://jakarta.apache.org/turbine/jcs/index.html">
* turbine-JCS</a>.
*
* <p>
* Implementation configuration properties:
* </p>
*
* <table cellspacing="2" cellpadding="2" border="3" frame="box">
* <tr>
* <td><strong>Property Key</strong></td>
* <td><strong>Property Values</strong></td>
* </tr>
* <tr>
* <td> - </td>
* <td>
* -
* </td>
* </tr>
* </table>
*
* @author Matthew Baird (mattbaird@yahoo.com)
* @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
* @version $Id: ObjectCacheJCSPerClassImpl.java,v 1.1 2003/09/17 23:00:15 arminw Exp $
*/
public class ObjectCacheJCSPerClassImpl extends AbstractMetaCache
{
private static Map cachesByClass = new HashMap();
/**
* Constructor for the MetaObjectCachePerClassImpl object
*/
public ObjectCacheJCSPerClassImpl(PersistenceBroker broker, Properties prop)
{
}
public ObjectCache getCache(Identity oid, Object obj, int methodCall)
{
if (oid.getObjectsRealClass() == null)
{
LoggerFactory.getDefaultLogger().info("[" + this.getClass()
+ "] Can't get JCS cache, real class was 'null' for Identity: " + oid);
return null;
}
return getCachePerClass(oid.getObjectsRealClass(), methodCall);
}
/**
* Clears the cache
*/
public void clear()
{
Iterator it = cachesByClass.values().iterator();
while (it.hasNext())
{
ObjectCache cache = (ObjectCache) it.next();
if (cache != null)
{
cache.clear();
}
else
{
it.remove();
}
}
}
/**
* 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, int methodCall)
{
ObjectCache cache = (ObjectCache) cachesByClass.get(objectClass.getName());
if (cache == null && methodCall == AbstractMetaCache.METHOD_CACHE)
{
/**
* 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/CacheDistributor.java
Index: CacheDistributor.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.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.metadata.ClassDescriptor;
import org.apache.ojb.broker.metadata.ObjectCacheDescriptor;
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 java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* A intern used {@link AbstractMetaCache} implementation acting
* as distributor of <code>ObjectCache</code> implementations declared
* in configuration metadata.
* <p>
* Reads the name of the used ObjectCache implementation
* <br/>
* a) from class-descriptor, or if not found
* <br/>
* b) from jdbc-connection-descriptor, or if not found
* <br/>
* use a given standard ObjectCache implementation (given by
* constructor argument).
* </p>
* <p>
*
* </p>
*
* @author Matthew Baird (mattbaird@yahoo.com)
* @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
* @version $Id: CacheDistributor.java,v 1.1 2003/09/17 23:00:15 arminw Exp $
*/
public class CacheDistributor extends AbstractMetaCache
{
private static Logger logger = LoggerFactory.getLogger(CacheDistributor.class);
private static final String DESCRIPTOR_BASED_CACHES = "descriptorBasedCaches";
/**
* global map, represents cache implementations
*/
private static Map caches = new HashMap();
private ObjectCache defaultCache;
private PersistenceBroker broker;
/**
* If <code>true</code> the class name of the object is used
* to find a per class {@link ObjectCache} implementation.
* If set <code>false</code> the {@link ObjectCacheDescriptor}
* instance is used as key to find a per class ObjectCache.
*/
private boolean descriptorBasedCaches;
/**
* public Default Constructor
*/
public CacheDistributor(PersistenceBroker broker, ObjectCache defaultCache)
{
this.broker = broker;
this.defaultCache = defaultCache;
this.descriptorBasedCaches = OjbConfigurator.getInstance().getConfigurationFor(null)
.getBoolean(DESCRIPTOR_BASED_CACHES, false);
logger.info("Use property 'descriptorBasedCaches' is set '"+descriptorBasedCaches+"'");
}
public void clear()
{
/**
* first be nice to the GC and clear each internal cache.
*/
defaultCache.clear();
synchronized (caches)
{
Iterator it = caches.values().iterator();
ObjectCache oc = null;
while (it.hasNext())
{
oc = (ObjectCache) it.next();
oc.clear();
}
/**
* then call clear on the caches hashmap.
*/
caches.clear();
}
}
public ObjectCache getCache(Identity oid, Object obj, int callingMethod)
{
/*
the priorities to find an ObjectCache for a specific object are:
1. try to find a cache defined per class
2. try to find a cache defined per jdbc-connection-descriptor
3. use the default cache implementation
*/
boolean connectionLevel = false;
ObjectCache retval = null;
/*
first search in class-descriptor, then in jdbc-connection-descriptor
for ObjectCacheDescriptor.
*/
ObjectCacheDescriptor ocd = searchInClassDescriptor(oid);
if(ocd == null)
{
ocd = searchInJdbcConnectionDescriptor();
connectionLevel = true;
}
/*
If no ObjectCacheDescriptor was found means there is no specific ObjectCache
defined, thus we use the default ObjectCache
*/
if (ocd == null)
{
retval = defaultCache;
}
else
{
// use a class-descriptor level cache
if (!connectionLevel)
{
if (!descriptorBasedCaches)
{
synchronized (caches)
{
retval = lookupCache(oid.getObjectsRealClass());
if (retval == null && oid.getObjectsRealClass() != null
&& callingMethod == AbstractMetaCache.METHOD_CACHE)
{
retval = createObjectCache(ocd);
caches.put(oid.getObjectsRealClass(), retval);
addCache(oid.getObjectsRealClass(), retval);
}
}
}
else
{
synchronized (caches)
{
retval = lookupCache(ocd);
if (retval == null && callingMethod == AbstractMetaCache.METHOD_CACHE)
{
retval = createObjectCache(ocd);
addCache(ocd, retval);
}
}
}
}
// use a jdbc-connection-descriptor level cache
else
{
String jcdAlias = broker.serviceConnectionManager().getConnectionDescriptor().getJcdAlias();
synchronized (caches)
{
retval = lookupCache(jcdAlias);
if (retval == null && callingMethod == AbstractMetaCache.METHOD_CACHE)
{
retval = createObjectCache(ocd);
addCache(jcdAlias, retval);
}
}
}
}
return retval;
}
private ObjectCache lookupCache(Object key)
{
return (ObjectCache) caches.get(key);
}
private void addCache(Object key, ObjectCache cache)
{
if(caches.containsKey(key))
{
logger.error("Duplicate key '"+key+"' found, can't add ObjectCache implementation");
}
else
{
caches.put(key, cache);
}
}
private void removeCache(Object key)
{
caches.remove(key);
}
/**
* Try to lookup {@link ObjectCacheDescriptor} in {@link ClassDescriptor} or
* in {@link org.apache.ojb.broker.metadata.JdbcConnectionDescriptor}.
*
* @param oid
* @return Returns the found {@link ObjectCacheDescriptor} or <code>null</code>
* if none was found.
*/
protected ObjectCacheDescriptor searchInClassDescriptor(Identity oid)
{
return oid.getObjectsRealClass() != null
? broker.getClassDescriptor(oid.getObjectsRealClass()).getObjectCacheDescriptor()
: null;
}
/**
* Lookup {@link ObjectCacheDescriptor} in
* {@link org.apache.ojb.broker.metadata.JdbcConnectionDescriptor}.
*
* @return Returns the found {@link ObjectCacheDescriptor} or <code>null</code>
* if none was found.
*/
protected ObjectCacheDescriptor searchInJdbcConnectionDescriptor()
{
return broker.serviceConnectionManager().getConnectionDescriptor().getObjectCacheDescriptor();
}
private ObjectCache createObjectCache(ObjectCacheDescriptor ocd)
{
try
{
return ObjectCacheFactory.getInstance()
.createNewCacheInstance(ocd.getObjectCache(), broker, ocd.getConfigurationProperties());
}
catch (Exception e)
{
logger.error("Can not create ObjectCache instance using " + ocd.getObjectCache() +
", use default cache instead", e);
}
return null;
}
public String toString()
{
ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
return buf.append("Associated PB", broker)
.append("Used default cache", defaultCache)
.append("Mapped caches", caches).toString();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org