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 org.apache.ojb.broker.Identity;
019: import org.apache.ojb.broker.PersistenceBroker;
020: import org.apache.ojb.broker.util.logging.LoggerFactory;
021:
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Properties;
026:
027: /**
028: * A global {@link ObjectCache} implementation using a JCS region for
029: * each class. Each class name was associated with a dedicated
030: * {@link ObjectCacheJCSImpl} instance to cache given objects.
031: * This allows to define JCS cache region configuration properties
032: * for each used class in JCS configuration files.
033: *
034: * <br/>
035: * More info see <a href="http://jakarta.apache.org/turbine/jcs/index.html">
036: * turbine-JCS</a>.
037: *
038: * <p>
039: * Implementation configuration properties:
040: * </p>
041: *
042: * <table cellspacing="2" cellpadding="2" border="3" frame="box">
043: * <tr>
044: * <td><strong>Property Key</strong></td>
045: * <td><strong>Property Values</strong></td>
046: * </tr>
047: * <tr>
048: * <td> - </td>
049: * <td>
050: * -
051: * </td>
052: * </tr>
053: * </table>
054: *
055: * @author Matthew Baird (mattbaird@yahoo.com)
056: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
057: * @version $Id: ObjectCacheJCSPerClassImpl.java,v 1.3.2.1 2005/12/21 22:24:15 tomdz Exp $
058: */
059:
060: public class ObjectCacheJCSPerClassImpl extends AbstractMetaCache {
061: private static Map cachesByClass = new HashMap();
062:
063: /**
064: * Constructor for the MetaObjectCachePerClassImpl object
065: */
066: public ObjectCacheJCSPerClassImpl(PersistenceBroker broker,
067: Properties prop) {
068: }
069:
070: public ObjectCache getCache(Identity oid, Object obj, int methodCall) {
071: if (oid.getObjectsRealClass() == null) {
072: LoggerFactory
073: .getDefaultLogger()
074: .info(
075: "["
076: + this .getClass()
077: + "] Can't get JCS cache, real class was 'null' for Identity: "
078: + oid);
079: return null;
080: }
081: return getCachePerClass(oid.getObjectsRealClass(), methodCall);
082: }
083:
084: /**
085: * Clears the cache
086: */
087: public void clear() {
088: Iterator it = cachesByClass.values().iterator();
089: while (it.hasNext()) {
090: ObjectCache cache = (ObjectCache) it.next();
091: if (cache != null) {
092: cache.clear();
093: } else {
094: it.remove();
095: }
096: }
097: }
098:
099: /**
100: * Gets the cache for the given class
101: *
102: * @param objectClass The class to look up the cache for
103: * @return The cache
104: */
105: private ObjectCache getCachePerClass(Class objectClass,
106: int methodCall) {
107: ObjectCache cache = (ObjectCache) cachesByClass.get(objectClass
108: .getName());
109: if (cache == null
110: && methodCall == AbstractMetaCache.METHOD_CACHE) {
111: /**
112: * the cache wasn't found, and the cachesByClass didn't contain the key with a
113: * null value, so create a new cache for this classtype
114: */
115: cache = new ObjectCacheJCSImpl(objectClass.getName());
116: cachesByClass.put(objectClass.getName(), cache);
117: }
118: return cache;
119: }
120: }
|