1 package org.apache.ojb.broker.cache; 2 3 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 ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 28 53 public class ObjectCachePerBrokerImpl implements ObjectCache, PBStateListener 54 { 55 58 protected Map objectTable = null; 59 60 63 public ObjectCachePerBrokerImpl(PersistenceBroker broker, Properties prop) 64 { 65 objectTable = new HashMap (); 66 broker.addListener(this, true); 68 } 69 70 73 public void clear() 74 { 75 objectTable.clear(); 76 } 77 78 83 public void cache(Identity oid, Object obj) 84 { 85 if ((obj != null)) 86 { 87 SoftReference ref = new SoftReference (obj); 88 objectTable.put(oid, ref); 89 } 90 } 91 92 public boolean cacheIfNew(Identity oid, Object obj) 93 { 94 if(objectTable.get(oid) == null) 95 { 96 objectTable.put(oid, obj); 97 return true; 98 } 99 return false; 100 } 101 102 106 public Object lookup(Identity oid) 107 { 108 Object obj = null; 109 SoftReference ref = (SoftReference ) objectTable.get(oid); 110 if (ref != null) 111 { 112 obj = ref.get(); 113 if (obj == null) 114 { 115 objectTable.remove(oid); } 117 } 118 return obj; 119 } 120 121 124 public void remove(Identity oid) 125 { 126 if (oid != null) 127 { 128 objectTable.remove(oid); 129 } 130 } 131 132 135 public void beforeClose(PBStateEvent event) 136 { 137 clear(); 138 } 139 140 public void afterOpen(PBStateEvent event) 141 { 142 } 144 145 public void beforeBegin(PBStateEvent event) 146 { 147 } 149 150 public void afterBegin(PBStateEvent event) 151 { 152 } 154 155 public void beforeCommit(PBStateEvent event) 156 { 157 } 159 160 public void afterCommit(PBStateEvent event) 161 { 162 } 164 165 public void beforeRollback(PBStateEvent event) 166 { 167 } 169 170 public void afterRollback(PBStateEvent event) 171 { 172 clear(); 174 } 175 } 176 | Popular Tags |