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 org.apache.commons.lang.ObjectUtils;
022: import org.apache.openjpa.util.RuntimeExceptionTranslator;
023:
024: /**
025: * Delegating query cache that can also perform exception translation for
026: * use in facades. This cache allows its delegate to be null, in which case
027: * it returns default values or all methods.
028: *
029: * @author Abe White
030: * @since 0.4.0
031: * @nojavadoc
032: */
033: public class DelegatingQueryCache implements QueryCache {
034:
035: private final QueryCache _cache;
036: private final DelegatingQueryCache _del;
037: private final RuntimeExceptionTranslator _trans;
038:
039: /**
040: * Constructor. Supply delegate.
041: */
042: public DelegatingQueryCache(QueryCache cache) {
043: this (cache, null);
044: }
045:
046: public DelegatingQueryCache(QueryCache cache,
047: RuntimeExceptionTranslator trans) {
048: _cache = cache;
049: _trans = trans;
050: if (cache instanceof DelegatingQueryCache)
051: _del = (DelegatingQueryCache) _cache;
052: else
053: _del = null;
054: }
055:
056: /**
057: * Return the direct delegate.
058: */
059: public QueryCache getDelegate() {
060: return _cache;
061: }
062:
063: /**
064: * Return the native delegate.
065: */
066: public QueryCache getInnermostDelegate() {
067: return (_del == null) ? _cache : _del.getInnermostDelegate();
068: }
069:
070: public int hashCode() {
071: return getInnermostDelegate().hashCode();
072: }
073:
074: public boolean equals(Object other) {
075: if (other == this )
076: return true;
077: if (other instanceof DelegatingQueryCache)
078: other = ((DelegatingQueryCache) other)
079: .getInnermostDelegate();
080: return ObjectUtils.equals(getInnermostDelegate(), other);
081: }
082:
083: /**
084: * Translate the OpenJPA exception.
085: */
086: protected RuntimeException translate(RuntimeException re) {
087: return (_trans == null) ? re : _trans.translate(re);
088: }
089:
090: public void initialize(DataCacheManager mgr) {
091: if (_cache == null)
092: return;
093: try {
094: _cache.initialize(mgr);
095: } catch (RuntimeException re) {
096: throw translate(re);
097: }
098: }
099:
100: public void onTypesChanged(TypesChangedEvent e) {
101: if (_cache == null)
102: return;
103: try {
104: _cache.onTypesChanged(e);
105: } catch (RuntimeException re) {
106: throw translate(re);
107: }
108: }
109:
110: public QueryResult get(QueryKey qk) {
111: if (_cache == null)
112: return null;
113: try {
114: return _cache.get(qk);
115: } catch (RuntimeException re) {
116: throw translate(re);
117: }
118: }
119:
120: public QueryResult put(QueryKey qk, QueryResult oids) {
121: if (_cache == null)
122: return null;
123: try {
124: return _cache.put(qk, oids);
125: } catch (RuntimeException re) {
126: throw translate(re);
127: }
128: }
129:
130: public QueryResult remove(QueryKey qk) {
131: if (_cache == null)
132: return null;
133: try {
134: return _cache.remove(qk);
135: } catch (RuntimeException re) {
136: throw translate(re);
137: }
138: }
139:
140: public void clear() {
141: if (_cache == null)
142: return;
143: try {
144: _cache.clear();
145: } catch (RuntimeException re) {
146: throw translate(re);
147: }
148: }
149:
150: public boolean pin(QueryKey qk) {
151: if (_cache == null)
152: return false;
153: try {
154: return _cache.pin(qk);
155: } catch (RuntimeException re) {
156: throw translate(re);
157: }
158: }
159:
160: public boolean unpin(QueryKey qk) {
161: if (_cache == null)
162: return false;
163: try {
164: return _cache.unpin(qk);
165: } catch (RuntimeException re) {
166: throw translate(re);
167: }
168: }
169:
170: public void writeLock() {
171: if (_cache == null)
172: return;
173: try {
174: _cache.writeLock();
175: } catch (RuntimeException re) {
176: throw translate(re);
177: }
178: }
179:
180: public void writeUnlock() {
181: if (_cache == null)
182: return;
183: try {
184: _cache.writeUnlock();
185: } catch (RuntimeException re) {
186: throw translate(re);
187: }
188: }
189:
190: public void addTypesChangedListener(TypesChangedListener listen) {
191: if (_cache == null)
192: return;
193: try {
194: _cache.addTypesChangedListener(listen);
195: } catch (RuntimeException re) {
196: throw translate(re);
197: }
198: }
199:
200: public boolean removeTypesChangedListener(
201: TypesChangedListener listen) {
202: if (_cache == null)
203: return false;
204: try {
205: return _cache.removeTypesChangedListener(listen);
206: } catch (RuntimeException re) {
207: throw translate(re);
208: }
209: }
210:
211: public void close() {
212: if (_cache == null)
213: return;
214: try {
215: _cache.close();
216: } catch (RuntimeException re) {
217: throw translate(re);
218: }
219: }
220: }
|