1 package org.apache.ojb.broker.cache;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 import org.apache.ojb.broker.Identity;
19 import org.apache.ojb.broker.PersistenceBroker;
20 import org.apache.ojb.broker.util.logging.LoggerFactory;
21
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.Map;
25 import java.util.Properties;
26
27 /**
28 * A global {@link ObjectCache} implementation using a JCS region for
29 * each class. Each class name was associated with a dedicated
30 * {@link ObjectCacheJCSImpl} instance to cache given objects.
31 * This allows to define JCS cache region configuration properties
32 * for each used class in JCS configuration files.
33 *
34 * <br/>
35 * More info see <a href="http://jakarta.apache.org/turbine/jcs/index.html">
36 * turbine-JCS</a>.
37 *
38 * <p>
39 * Implementation configuration properties:
40 * </p>
41 *
42 * <table cellspacing="2" cellpadding="2" border="3" frame="box">
43 * <tr>
44 * <td><strong>Property Key</strong></td>
45 * <td><strong>Property Values</strong></td>
46 * </tr>
47 * <tr>
48 * <td> - </td>
49 * <td>
50 * -
51 * </td>
52 * </tr>
53 * </table>
54 *
55 * @author Matthew Baird (mattbaird@yahoo.com)
56 * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
57 * @version $Id: ObjectCacheJCSPerClassImpl.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $
58 */
59
60 public class ObjectCacheJCSPerClassImpl extends AbstractMetaCache
61 {
62 private static Map cachesByClass = new HashMap();
63
64 /**
65 * Constructor for the MetaObjectCachePerClassImpl object
66 */
67 public ObjectCacheJCSPerClassImpl(PersistenceBroker broker, Properties prop)
68 {
69 }
70
71 public ObjectCache getCache(Identity oid, Object obj, int methodCall)
72 {
73 if (oid.getObjectsRealClass() == null)
74 {
75 LoggerFactory.getDefaultLogger().info("[" + this.getClass()
76 + "] Can't get JCS cache, real class was 'null' for Identity: " + oid);
77 return null;
78 }
79 return getCachePerClass(oid.getObjectsRealClass(), methodCall);
80 }
81
82 /**
83 * Clears the cache
84 */
85 public void clear()
86 {
87 Iterator it = cachesByClass.values().iterator();
88 while (it.hasNext())
89 {
90 ObjectCache cache = (ObjectCache) it.next();
91 if (cache != null)
92 {
93 cache.clear();
94 }
95 else
96 {
97 it.remove();
98 }
99 }
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 }