| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ObjectCachePerBrokerImpl |
|
| 1.4285714285714286;1.429 |
| 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; | |
| 24 | import java.util.HashMap; | |
| 25 | import java.util.Map; | |
| 26 | import java.util.Properties; | |
| 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.1 2007-08-24 22:17:29 ewestfal Exp $ | |
| 52 | */ | |
| 53 | public class ObjectCachePerBrokerImpl implements ObjectCache, PBStateListener | |
| 54 | { | |
| 55 | /** | |
| 56 | * the hashtable holding all cached object | |
| 57 | */ | |
| 58 | protected Map objectTable = null; | |
| 59 | ||
| 60 | /** | |
| 61 | * public Default Constructor | |
| 62 | */ | |
| 63 | public ObjectCachePerBrokerImpl(PersistenceBroker broker, Properties prop) | |
| 64 | { | |
| 65 | objectTable = new HashMap(); | |
| 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 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 | /** | |
| 103 | * Lookup object with Identity oid in objectTable. | |
| 104 | * Returns null if no matching id is found | |
| 105 | */ | |
| 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); // 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 | } |