001: package org.apache.ojb.broker.cache;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.Collections;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Properties;
023:
024: import org.apache.ojb.broker.Identity;
025: import org.apache.ojb.broker.PersistenceBroker;
026:
027: /**
028: * Global {@link ObjectCache} implementation.
029: *
030: * @author matthew.baird
031: * @version $Id: ObjectCachePerClassImpl.java,v 1.6.2.2 2005/12/21 22:24:15 tomdz Exp $
032: */
033: public class ObjectCachePerClassImpl extends AbstractMetaCache {
034: private static Map cachesByClass = Collections
035: .synchronizedMap(new HashMap());
036:
037: /**
038: * Constructor for the ObjectCachePerClassImpl object
039: */
040: public ObjectCachePerClassImpl(PersistenceBroker broker,
041: Properties prop) {
042: setClassCache(Object.class, new ObjectCacheDefaultImpl(broker,
043: null));
044: }
045:
046: public ObjectCache getCache(Identity oid, Object obj, int methodCall) {
047: if (oid.getObjectsRealClass() == null) {
048: return null;
049: } else {
050: return getCachePerClass(oid.getObjectsRealClass(),
051: methodCall);
052: }
053: }
054:
055: /**
056: * Clears the cache
057: */
058: public void clear() {
059: Iterator it = cachesByClass.values().iterator();
060: while (it.hasNext()) {
061: ObjectCache cache = (ObjectCache) it.next();
062: if (cache != null) {
063: cache.clear();
064: }
065: }
066: }
067:
068: /**
069: * Sets the ObjectCache implementation to use for objects with the given
070: * type and subclasses
071: *
072: * @param objectClass The object's class, use java.lang.Object to alter
073: * default caching for all objects which have no special
074: * caching defined
075: * @param cache The new ObjectCache implementation to use for this
076: * class and subclasses, null to switch off caching
077: * for the given class
078: */
079: public void setClassCache(Class objectClass, ObjectCache cache)
080:
081: {
082: setClassCache(objectClass.getName(), cache);
083: }
084:
085: /**
086: * Sets the ObjectCache implementation for the given class name
087: *
088: * @param className The name of the class to cache
089: * @param cache The ObjectCache to use for this class and subclasses
090: */
091: private void setClassCache(String className, ObjectCache cache) {
092: cachesByClass.put(className, cache);
093: }
094:
095: /**
096: * Gets the cache for the given class
097: *
098: * @param objectClass The class to look up the cache for
099: * @return The cache
100: */
101: private ObjectCache getCachePerClass(Class objectClass,
102: int methodCall) {
103: ObjectCache cache = (ObjectCache) cachesByClass.get(objectClass
104: .getName());
105: if (cache == null
106: && AbstractMetaCache.METHOD_CACHE == methodCall
107: && !cachesByClass.containsKey(objectClass.getName())) {
108: cache = new ObjectCacheDefaultImpl(null, null);
109: setClassCache(objectClass.getName(), cache);
110: }
111: return cache;
112: }
113: }
|