KickJava   Java API By Example, From Geeks To Geeks.

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


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.PBStateEvent;
20 import org.apache.ojb.broker.PBStateListener;
21 import org.apache.ojb.broker.PersistenceBroker;
22
23 import java.lang.ref.SoftReference JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 /**
29  * This local {@link ObjectCache} implementation allows to have dedicated caches per broker.
30  * All calls are delegated to the cache associated with the currentBroker.
31  * When the broker was closed (returned to pool) the cache was cleared.
32  *
33  * <p>
34  * Implementation configuration properties:
35  * </p>
36  *
37  * <table cellspacing="2" cellpadding="2" border="3" frame="box">
38  * <tr>
39  * <td><strong>Property Key</strong></td>
40  * <td><strong>Property Values</strong></td>
41  * </tr>
42  * <tr>
43  * <td> - </td>
44  * <td>
45  * -
46  * </td>
47  * </tr>
48  * </table>
49  *
50  * @author <a HREF="mailto:thma@apache.org">Thomas Mahler<a>
51  * @version $Id: ObjectCachePerBrokerImpl.java,v 1.11.2.2 2005/12/21 22:24:15 tomdz Exp $
52  */

53 public class ObjectCachePerBrokerImpl implements ObjectCache, PBStateListener
54 {
55     /**
56      * the hashtable holding all cached object
57      */

58     protected Map JavaDoc objectTable = null;
59
60     /**
61      * public Default Constructor
62      */

63     public ObjectCachePerBrokerImpl(PersistenceBroker broker, Properties JavaDoc prop)
64     {
65         objectTable = new HashMap JavaDoc();
66         // add this cache as permanent listener
67
broker.addListener(this, true);
68     }
69
70     /**
71      * Clear ObjectCache. I.e. remove all entries for classes and objects.
72      */

73     public void clear()
74     {
75         objectTable.clear();
76     }
77
78     /**
79      * Makes object persistent to the Objectcache.
80      * I'm using soft-references to allow gc reclaim unused objects
81      * even if they are still cached.
82      */

83     public void cache(Identity oid, Object JavaDoc obj)
84     {
85         if ((obj != null))
86         {
87             SoftReference JavaDoc ref = new SoftReference JavaDoc(obj);
88             objectTable.put(oid, ref);
89         }
90     }
91
92     public boolean cacheIfNew(Identity oid, Object JavaDoc obj)
93     {
94         if(objectTable.get(oid) == null)
95         {
96             objectTable.put(oid, obj);
97             return true;
98         }
99         return false;
100     }
101
102     /**
103      * Lookup object with Identity oid in objectTable.
104      * Returns null if no matching id is found
105      */

106     public Object JavaDoc lookup(Identity oid)
107     {
108         Object JavaDoc obj = null;
109         SoftReference JavaDoc ref = (SoftReference JavaDoc) objectTable.get(oid);
110         if (ref != null)
111         {
112             obj = ref.get();
113             if (obj == null)
114             {
115                 objectTable.remove(oid); // Soft-referenced Object reclaimed by GC
116
}
117         }
118         return obj;
119     }
120
121     /**
122      * Removes an Object from the cache.
123      */

124     public void remove(Identity oid)
125     {
126         if (oid != null)
127         {
128             objectTable.remove(oid);
129         }
130     }
131
132     /**
133      * We clear the cache
134      */

135     public void beforeClose(PBStateEvent event)
136     {
137         clear();
138     }
139
140     public void afterOpen(PBStateEvent event)
141     {
142         //do nothing
143
}
144
145     public void beforeBegin(PBStateEvent event)
146     {
147         //do nothing
148
}
149
150     public void afterBegin(PBStateEvent event)
151     {
152         //do nothing
153
}
154
155     public void beforeCommit(PBStateEvent event)
156     {
157         //do nothing
158
}
159
160     public void afterCommit(PBStateEvent event)
161     {
162         //do nothing
163
}
164
165     public void beforeRollback(PBStateEvent event)
166     {
167         //do nothing
168
}
169
170     public void afterRollback(PBStateEvent event)
171     {
172         // clear to be in sync with DB
173
clear();
174     }
175 }
176
Popular Tags