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.PBStateEvent;
020: import org.apache.ojb.broker.PBStateListener;
021: import org.apache.ojb.broker.PersistenceBroker;
022:
023: import java.lang.ref.SoftReference;
024: import java.util.HashMap;
025: import java.util.Map;
026: import java.util.Properties;
027:
028: /**
029: * This local {@link ObjectCache} implementation allows to have dedicated caches per broker.
030: * All calls are delegated to the cache associated with the currentBroker.
031: * When the broker was closed (returned to pool) the cache was cleared.
032: *
033: * <p>
034: * Implementation configuration properties:
035: * </p>
036: *
037: * <table cellspacing="2" cellpadding="2" border="3" frame="box">
038: * <tr>
039: * <td><strong>Property Key</strong></td>
040: * <td><strong>Property Values</strong></td>
041: * </tr>
042: * <tr>
043: * <td> - </td>
044: * <td>
045: * -
046: * </td>
047: * </tr>
048: * </table>
049: *
050: * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
051: * @version $Id: ObjectCachePerBrokerImpl.java,v 1.11.2.2 2005/12/21 22:24:15 tomdz Exp $
052: */
053: public class ObjectCachePerBrokerImpl implements ObjectCache,
054: PBStateListener {
055: /**
056: * the hashtable holding all cached object
057: */
058: protected Map objectTable = null;
059:
060: /**
061: * public Default Constructor
062: */
063: public ObjectCachePerBrokerImpl(PersistenceBroker broker,
064: Properties prop) {
065: objectTable = new HashMap();
066: // add this cache as permanent listener
067: broker.addListener(this , true);
068: }
069:
070: /**
071: * Clear ObjectCache. I.e. remove all entries for classes and objects.
072: */
073: public void clear() {
074: objectTable.clear();
075: }
076:
077: /**
078: * Makes object persistent to the Objectcache.
079: * I'm using soft-references to allow gc reclaim unused objects
080: * even if they are still cached.
081: */
082: public void cache(Identity oid, Object obj) {
083: if ((obj != null)) {
084: SoftReference ref = new SoftReference(obj);
085: objectTable.put(oid, ref);
086: }
087: }
088:
089: public boolean cacheIfNew(Identity oid, Object obj) {
090: if (objectTable.get(oid) == null) {
091: objectTable.put(oid, obj);
092: return true;
093: }
094: return false;
095: }
096:
097: /**
098: * Lookup object with Identity oid in objectTable.
099: * Returns null if no matching id is found
100: */
101: public Object lookup(Identity oid) {
102: Object obj = null;
103: SoftReference ref = (SoftReference) objectTable.get(oid);
104: if (ref != null) {
105: obj = ref.get();
106: if (obj == null) {
107: objectTable.remove(oid); // Soft-referenced Object reclaimed by GC
108: }
109: }
110: return obj;
111: }
112:
113: /**
114: * Removes an Object from the cache.
115: */
116: public void remove(Identity oid) {
117: if (oid != null) {
118: objectTable.remove(oid);
119: }
120: }
121:
122: /**
123: * We clear the cache
124: */
125: public void beforeClose(PBStateEvent event) {
126: clear();
127: }
128:
129: public void afterOpen(PBStateEvent event) {
130: //do nothing
131: }
132:
133: public void beforeBegin(PBStateEvent event) {
134: //do nothing
135: }
136:
137: public void afterBegin(PBStateEvent event) {
138: //do nothing
139: }
140:
141: public void beforeCommit(PBStateEvent event) {
142: //do nothing
143: }
144:
145: public void afterCommit(PBStateEvent event) {
146: //do nothing
147: }
148:
149: public void beforeRollback(PBStateEvent event) {
150: //do nothing
151: }
152:
153: public void afterRollback(PBStateEvent event) {
154: // clear to be in sync with DB
155: clear();
156: }
157: }
|