KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > cache > ObjectCachePerClassImpl


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 java.util.Collections JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import org.apache.ojb.broker.Identity;
25 import org.apache.ojb.broker.PersistenceBroker;
26
27 /**
28  * Global {@link ObjectCache} implementation.
29  *
30  * @author matthew.baird
31  * @version $Id: ObjectCachePerClassImpl.java,v 1.6.2.2 2005/12/21 22:24:15 tomdz Exp $
32  */

33 public class ObjectCachePerClassImpl extends AbstractMetaCache
34 {
35     private static Map JavaDoc cachesByClass = Collections.synchronizedMap(new HashMap JavaDoc());
36
37     /**
38      * Constructor for the ObjectCachePerClassImpl object
39      */

40     public ObjectCachePerClassImpl(PersistenceBroker broker, Properties JavaDoc prop)
41     {
42         setClassCache(Object JavaDoc.class, new ObjectCacheDefaultImpl(broker, null));
43     }
44
45     public ObjectCache getCache(Identity oid, Object JavaDoc obj, int methodCall)
46     {
47         if(oid.getObjectsRealClass() == null)
48         {
49             return null;
50         }
51         else
52         {
53             return getCachePerClass(oid.getObjectsRealClass(), methodCall);
54         }
55     }
56
57     /**
58      * Clears the cache
59      */

60     public void clear()
61     {
62         Iterator JavaDoc it = cachesByClass.values().iterator();
63         while (it.hasNext())
64         {
65             ObjectCache cache = (ObjectCache) it.next();
66             if (cache != null)
67             {
68                 cache.clear();
69             }
70         }
71     }
72
73     /**
74      * Sets the ObjectCache implementation to use for objects with the given
75      * type and subclasses
76      *
77      * @param objectClass The object's class, use java.lang.Object to alter
78      * default caching for all objects which have no special
79      * caching defined
80      * @param cache The new ObjectCache implementation to use for this
81      * class and subclasses, null to switch off caching
82      * for the given class
83      */

84     public void setClassCache(Class JavaDoc objectClass, ObjectCache cache)
85
86     {
87         setClassCache(objectClass.getName(), cache);
88     }
89
90     /**
91      * Sets the ObjectCache implementation for the given class name
92      *
93      * @param className The name of the class to cache
94      * @param cache The ObjectCache to use for this class and subclasses
95      */

96     private void setClassCache(String JavaDoc className, ObjectCache cache)
97     {
98         cachesByClass.put(className, cache);
99     }
100
101     /**
102      * Gets the cache for the given class
103      *
104      * @param objectClass The class to look up the cache for
105      * @return The cache
106      */

107     private ObjectCache getCachePerClass(Class JavaDoc objectClass, int methodCall)
108     {
109         ObjectCache cache = (ObjectCache) cachesByClass.get(objectClass.getName());
110         if (cache == null && AbstractMetaCache.METHOD_CACHE == methodCall
111                 && !cachesByClass.containsKey(objectClass.getName()))
112         {
113             cache = new ObjectCacheDefaultImpl(null, null);
114             setClassCache(objectClass.getName(), cache);
115         }
116         return cache;
117     }
118 }
119
Popular Tags