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.1 2007-08-24 22:17:29 ewestfal Exp $
058 */
059
060 public class ObjectCacheJCSPerClassImpl extends AbstractMetaCache
061 {
062 private static Map cachesByClass = new HashMap();
063
064 /**
065 * Constructor for the MetaObjectCachePerClassImpl object
066 */
067 public ObjectCacheJCSPerClassImpl(PersistenceBroker broker, Properties prop)
068 {
069 }
070
071 public ObjectCache getCache(Identity oid, Object obj, int methodCall)
072 {
073 if (oid.getObjectsRealClass() == null)
074 {
075 LoggerFactory.getDefaultLogger().info("[" + this.getClass()
076 + "] Can't get JCS cache, real class was 'null' for Identity: " + oid);
077 return null;
078 }
079 return getCachePerClass(oid.getObjectsRealClass(), methodCall);
080 }
081
082 /**
083 * Clears the cache
084 */
085 public void clear()
086 {
087 Iterator it = cachesByClass.values().iterator();
088 while (it.hasNext())
089 {
090 ObjectCache cache = (ObjectCache) it.next();
091 if (cache != null)
092 {
093 cache.clear();
094 }
095 else
096 {
097 it.remove();
098 }
099 }
100 }
101
102 /**
103 * Gets the cache for the given class
104 *
105 * @param objectClass The class to look up the cache for
106 * @return The cache
107 */
108 private ObjectCache getCachePerClass(Class objectClass, int methodCall)
109 {
110 ObjectCache cache = (ObjectCache) cachesByClass.get(objectClass.getName());
111 if (cache == null && methodCall == AbstractMetaCache.METHOD_CACHE)
112 {
113 /**
114 * the cache wasn't found, and the cachesByClass didn't contain the key with a
115 * null value, so create a new cache for this classtype
116 */
117 cache = new ObjectCacheJCSImpl(objectClass.getName());
118 cachesByClass.put(objectClass.getName(), cache);
119 }
120 return cache;
121 }
122 }