| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ObjectCacheOSCacheImpl |
|
| 2.125;2.125 |
| 1 | /* | |
| 2 | * Copyright 2005-2007 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.opensource.org/licenses/ecl2.php | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package org.kuali.rice.krad.util.cache; | |
| 17 | ||
| 18 | import java.util.Properties; | |
| 19 | ||
| 20 | import org.apache.log4j.Logger; | |
| 21 | import org.apache.ojb.broker.Identity; | |
| 22 | import org.apache.ojb.broker.PersistenceBroker; | |
| 23 | import org.apache.ojb.broker.cache.ObjectCacheInternal; | |
| 24 | import org.apache.ojb.broker.cache.ObjectCacheTwoLevelImpl; | |
| 25 | import org.apache.ojb.broker.cache.RuntimeCacheException; | |
| 26 | import org.kuali.rice.krad.service.KRADServiceLocatorInternal; | |
| 27 | ||
| 28 | import com.opensymphony.oscache.base.Cache; | |
| 29 | import com.opensymphony.oscache.base.NeedsRefreshException; | |
| 30 | import com.opensymphony.oscache.general.GeneralCacheAdministrator; | |
| 31 | ||
| 32 | /* | |
| 33 | * Copyright 2002-2004 The Apache Software Foundation | |
| 34 | * | |
| 35 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | |
| 36 | * You may obtain a copy of the License at | |
| 37 | * | |
| 38 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 39 | * | |
| 40 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" | |
| 41 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language | |
| 42 | * governing permissions and limitations under the License. | |
| 43 | */ | |
| 44 | ||
| 45 | /** | |
| 46 | * ObjectCacheOSCacheImpl | |
| 47 | * | |
| 48 | * Original source http://db.apache.org/ojb/docu/guides/objectcache.html#oscache | |
| 49 | * | |
| 50 | * We use the extended caching interface {@link ObjectCacheInternal}to allow usage of this implementation in | |
| 51 | * {@link ObjectCacheTwoLevelImpl}. | |
| 52 | */ | |
| 53 | public class ObjectCacheOSCacheImpl implements ObjectCacheInternal { | |
| 54 | 0 | private static final Logger LOG = Logger.getLogger(ObjectCacheOSCacheImpl.class); |
| 55 | private static final int REFRESH_PERIOD = com.opensymphony.oscache.base.CacheEntry.INDEFINITE_EXPIRY; | |
| 56 | ||
| 57 | // using SpringServiceLocator to get the admin, since there's no obvious way to let Spring inject | |
| 58 | // create/manage the actual cache instance that OJB will use | |
| 59 | private final GeneralCacheAdministrator admin; | |
| 60 | ||
| 61 | /** | |
| 62 | * Required default constructor. | |
| 63 | */ | |
| 64 | 0 | public ObjectCacheOSCacheImpl() { |
| 65 | 0 | admin = KRADServiceLocatorInternal.getPersistenceCacheAdministrator(); |
| 66 | 0 | } |
| 67 | ||
| 68 | /** | |
| 69 | * Required constructor. | |
| 70 | * | |
| 71 | * @param broker | |
| 72 | * @param prop | |
| 73 | */ | |
| 74 | public ObjectCacheOSCacheImpl(PersistenceBroker broker, Properties prop) { | |
| 75 | 0 | this(); |
| 76 | 0 | } |
| 77 | ||
| 78 | ||
| 79 | /** | |
| 80 | * @see org.apache.ojb.broker.cache.ObjectCache#cache(org.apache.ojb.broker.Identity, java.lang.Object) | |
| 81 | */ | |
| 82 | public void cache(Identity oid, Object obj) { | |
| 83 | 0 | Cache cache = admin.getCache(); |
| 84 | ||
| 85 | try { | |
| 86 | /* | |
| 87 | * Actually, OSCache sends notifications (Events) only on flush events. The putInCache method do not flush the cache, so | |
| 88 | * no event is sent. The ObjectCacheOSCacheInternalImpl should force OSCache to flush the entry in order to generate an | |
| 89 | * event. This guarantee that other nodes always in sync with the DB. Alternative a non-indefinite refresh-period could | |
| 90 | * be used in conjunction with optimistic-locking for persistent objects. | |
| 91 | */ | |
| 92 | 0 | remove(oid); |
| 93 | 0 | admin.putInCache(oid.toString(), obj); |
| 94 | } | |
| 95 | 0 | catch (Exception e) { |
| 96 | 0 | LOG.error("error caching object: " + oid, e); |
| 97 | 0 | } |
| 98 | ||
| 99 | ||
| 100 | 0 | } |
| 101 | ||
| 102 | /** | |
| 103 | * @see org.apache.ojb.broker.cache.ObjectCacheInternal#doInternalCache(org.apache.ojb.broker.Identity, java.lang.Object, int) | |
| 104 | */ | |
| 105 | public void doInternalCache(Identity oid, Object obj, int type) { | |
| 106 | 0 | cache(oid, obj); |
| 107 | 0 | } |
| 108 | ||
| 109 | /** | |
| 110 | * @see org.apache.ojb.broker.cache.ObjectCacheInternal#cacheIfNew(org.apache.ojb.broker.Identity, java.lang.Object) | |
| 111 | */ | |
| 112 | public boolean cacheIfNew(Identity oid, Object obj) { | |
| 113 | 0 | boolean result = false; |
| 114 | ||
| 115 | 0 | Cache cache = admin.getCache(); |
| 116 | try { | |
| 117 | 0 | cache.getFromCache(oid.toString()); |
| 118 | } | |
| 119 | 0 | catch (NeedsRefreshException e) { |
| 120 | try { | |
| 121 | 0 | cache.putInCache(oid.toString(), obj); |
| 122 | 0 | result = true; |
| 123 | } | |
| 124 | 0 | catch (Exception e1) { |
| 125 | 0 | cache.cancelUpdate(oid.toString()); |
| 126 | 0 | LOG.error("error caching object: " + oid, e); |
| 127 | 0 | } |
| 128 | 0 | } |
| 129 | ||
| 130 | 0 | return result; |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * @see org.apache.ojb.broker.cache.ObjectCache#lookup(org.apache.ojb.broker.Identity) | |
| 135 | */ | |
| 136 | public Object lookup(Identity oid) { | |
| 137 | 0 | Object cachedObject = null; |
| 138 | 0 | Cache cache = admin.getCache(); |
| 139 | try { | |
| 140 | 0 | cachedObject = cache.getFromCache(oid.toString(), REFRESH_PERIOD); |
| 141 | } | |
| 142 | 0 | catch (NeedsRefreshException e) { |
| 143 | 0 | cache.cancelUpdate(oid.toString()); |
| 144 | } | |
| 145 | 0 | catch (Exception e) { |
| 146 | 0 | cache.cancelUpdate(oid.toString()); |
| 147 | 0 | LOG.error("error retrieving object from cache: " + oid, e); |
| 148 | 0 | } |
| 149 | ||
| 150 | 0 | return cachedObject; |
| 151 | } | |
| 152 | ||
| 153 | /** | |
| 154 | * @see org.apache.ojb.broker.cache.ObjectCache#remove(org.apache.ojb.broker.Identity) | |
| 155 | */ | |
| 156 | public void remove(Identity oid) { | |
| 157 | try { | |
| 158 | 0 | admin.flushEntry(oid.toString()); |
| 159 | } | |
| 160 | 0 | catch (Exception e) { |
| 161 | 0 | throw new RuntimeCacheException("error removing object from cache: " + oid, e); |
| 162 | 0 | } |
| 163 | 0 | } |
| 164 | ||
| 165 | /** | |
| 166 | * @see org.apache.ojb.broker.cache.ObjectCache#clear() | |
| 167 | */ | |
| 168 | public void clear() { | |
| 169 | try { | |
| 170 | 0 | admin.flushAll(); |
| 171 | } | |
| 172 | 0 | catch (Exception e) { |
| 173 | 0 | throw new RuntimeCacheException("error clearing cache", e); |
| 174 | 0 | } |
| 175 | 0 | } |
| 176 | } |