001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.datacache;
020:
021: import java.util.BitSet;
022: import java.util.Collection;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.apache.commons.lang.ObjectUtils;
027: import org.apache.openjpa.util.RuntimeExceptionTranslator;
028:
029: /**
030: * Delegating data cache that can also perform exception translation for
031: * use in facades. This cache allows its delegate to be null, in which
032: * case it returns default values for all operations.
033: *
034: * @author Abe White
035: * @nojavadoc
036: */
037: public class DelegatingDataCache implements DataCache {
038:
039: private static final BitSet EMPTY_BITSET = new BitSet(0);
040:
041: private final DataCache _cache;
042: private final DelegatingDataCache _del;
043: private final RuntimeExceptionTranslator _trans;
044:
045: /**
046: * Constructor. Supply delegate.
047: */
048: public DelegatingDataCache(DataCache cache) {
049: this (cache, null);
050: }
051:
052: public DelegatingDataCache(DataCache cache,
053: RuntimeExceptionTranslator trans) {
054: _cache = cache;
055: _trans = trans;
056: if (cache instanceof DelegatingDataCache)
057: _del = (DelegatingDataCache) _cache;
058: else
059: _del = null;
060: }
061:
062: /**
063: * Return the direct delegate.
064: */
065: public DataCache getDelegate() {
066: return _cache;
067: }
068:
069: /**
070: * Return the native delegate.
071: */
072: public DataCache getInnermostDelegate() {
073: return (_del == null) ? _cache : _del.getInnermostDelegate();
074: }
075:
076: public int hashCode() {
077: if (_cache == null)
078: return super .hashCode();
079: return getInnermostDelegate().hashCode();
080: }
081:
082: public boolean equals(Object other) {
083: if (other == this )
084: return true;
085: if (other instanceof DelegatingDataCache)
086: other = ((DelegatingDataCache) other)
087: .getInnermostDelegate();
088: return ObjectUtils.equals(getInnermostDelegate(), other);
089: }
090:
091: /**
092: * Translate the OpenJPA exception.
093: */
094: protected RuntimeException translate(RuntimeException re) {
095: return (_trans == null) ? re : _trans.translate(re);
096: }
097:
098: public String getName() {
099: if (_cache == null)
100: return null;
101: try {
102: return _cache.getName();
103: } catch (RuntimeException re) {
104: throw translate(re);
105: }
106: }
107:
108: public void setName(String name) {
109: if (_cache == null)
110: return;
111: try {
112: _cache.setName(name);
113: } catch (RuntimeException re) {
114: throw translate(re);
115: }
116: }
117:
118: public void initialize(DataCacheManager manager) {
119: if (_cache == null)
120: return;
121: try {
122: _cache.initialize(manager);
123: } catch (RuntimeException re) {
124: throw translate(re);
125: }
126: }
127:
128: public void commit(Collection additions, Collection newUpdates,
129: Collection existingUpdates, Collection deletes) {
130: if (_cache == null)
131: return;
132: try {
133: _cache.commit(additions, newUpdates, existingUpdates,
134: deletes);
135: } catch (RuntimeException re) {
136: throw translate(re);
137: }
138: }
139:
140: public boolean contains(Object oid) {
141: if (_cache == null)
142: return false;
143: try {
144: return _cache.contains(oid);
145: } catch (RuntimeException re) {
146: throw translate(re);
147: }
148: }
149:
150: public BitSet containsAll(Collection oids) {
151: if (_cache == null)
152: return EMPTY_BITSET;
153: try {
154: return _cache.containsAll(oids);
155: } catch (RuntimeException re) {
156: throw translate(re);
157: }
158: }
159:
160: public DataCachePCData get(Object oid) {
161: if (_cache == null)
162: return null;
163: try {
164: return _cache.get(oid);
165: } catch (RuntimeException re) {
166: throw translate(re);
167: }
168: }
169:
170: public DataCachePCData put(DataCachePCData value) {
171: if (_cache == null)
172: return null;
173: try {
174: return _cache.put(value);
175: } catch (RuntimeException re) {
176: throw translate(re);
177: }
178: }
179:
180: public void update(DataCachePCData value) {
181: if (_cache == null)
182: return;
183: try {
184: _cache.update(value);
185: } catch (RuntimeException re) {
186: throw translate(re);
187: }
188: }
189:
190: public DataCachePCData remove(Object oid) {
191: if (_cache == null)
192: return null;
193: try {
194: return _cache.remove(oid);
195: } catch (RuntimeException re) {
196: throw translate(re);
197: }
198: }
199:
200: public BitSet removeAll(Collection oids) {
201: if (_cache == null)
202: return EMPTY_BITSET;
203: try {
204: return _cache.removeAll(oids);
205: } catch (RuntimeException re) {
206: throw translate(re);
207: }
208: }
209:
210: public void removeAll(Class cls, boolean subclasses) {
211: if (_cache == null)
212: return;
213: try {
214: _cache.removeAll(cls, subclasses);
215: } catch (RuntimeException re) {
216: throw translate(re);
217: }
218: }
219:
220: public void clear() {
221: if (_cache == null)
222: return;
223: try {
224: _cache.clear();
225: } catch (RuntimeException re) {
226: throw translate(re);
227: }
228: }
229:
230: public boolean pin(Object oid) {
231: if (_cache == null)
232: return false;
233: try {
234: return _cache.pin(oid);
235: } catch (RuntimeException re) {
236: throw translate(re);
237: }
238: }
239:
240: public BitSet pinAll(Collection oids) {
241: if (_cache == null)
242: return EMPTY_BITSET;
243: try {
244: return _cache.pinAll(oids);
245: } catch (RuntimeException re) {
246: throw translate(re);
247: }
248: }
249:
250: public void pinAll(Class cls, boolean subs) {
251: if (_cache == null)
252: return;
253: try {
254: _cache.pinAll(cls, subs);
255: } catch (RuntimeException re) {
256: throw translate(re);
257: }
258: }
259:
260: public boolean unpin(Object oid) {
261: if (_cache == null)
262: return false;
263: try {
264: return _cache.unpin(oid);
265: } catch (RuntimeException re) {
266: throw translate(re);
267: }
268: }
269:
270: public BitSet unpinAll(Collection oids) {
271: if (_cache == null)
272: return EMPTY_BITSET;
273: try {
274: return _cache.unpinAll(oids);
275: } catch (RuntimeException re) {
276: throw translate(re);
277: }
278: }
279:
280: public void unpinAll(Class cls, boolean subs) {
281: if (_cache == null)
282: return;
283: try {
284: _cache.unpinAll(cls, subs);
285: } catch (RuntimeException re) {
286: throw translate(re);
287: }
288: }
289:
290: public void writeLock() {
291: if (_cache == null)
292: return;
293: try {
294: _cache.writeLock();
295: } catch (RuntimeException re) {
296: throw translate(re);
297: }
298: }
299:
300: public void writeUnlock() {
301: if (_cache == null)
302: return;
303: try {
304: _cache.writeUnlock();
305: } catch (RuntimeException re) {
306: throw translate(re);
307: }
308: }
309:
310: public void addExpirationListener(ExpirationListener listen) {
311: if (_cache == null)
312: return;
313: try {
314: _cache.addExpirationListener(listen);
315: } catch (RuntimeException re) {
316: throw translate(re);
317: }
318: }
319:
320: public boolean removeExpirationListener(ExpirationListener listen) {
321: if (_cache == null)
322: return false;
323: try {
324: return _cache.removeExpirationListener(listen);
325: } catch (RuntimeException re) {
326: throw translate(re);
327: }
328: }
329:
330: public void close() {
331: if (_cache == null)
332: return;
333: try {
334: _cache.close();
335: } catch (RuntimeException re) {
336: throw translate(re);
337: }
338: }
339:
340: public Map getAll(List keys) {
341: if (_cache == null)
342: return null;
343: try {
344: return _cache.getAll(keys);
345: } catch (RuntimeException re) {
346: throw translate(re);
347: }
348: }
349: }
|