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.kernel;
020:
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.openjpa.util.RuntimeExceptionTranslator;
025:
026: ///////////////////////////////////////////////////////////////
027: // NOTE: when adding a public API method, be sure to add it to
028: // JDO and JPA facades!
029: ///////////////////////////////////////////////////////////////
030:
031: /**
032: * Delegating extent that also can perform exception translation for use
033: * in facades.
034: *
035: * @since 0.4.0
036: * @author Abe White
037: * @nojavadoc
038: */
039: public class DelegatingExtent implements Extent {
040:
041: private final Extent _extent;
042: private final DelegatingExtent _del;
043: private final RuntimeExceptionTranslator _trans;
044:
045: /**
046: * Constructor; supply delegate.
047: */
048: public DelegatingExtent(Extent extent) {
049: this (extent, null);
050: }
051:
052: /**
053: * Constructor; supply delegate and exception translator.
054: */
055: public DelegatingExtent(Extent extent,
056: RuntimeExceptionTranslator trans) {
057: _extent = extent;
058: if (extent instanceof DelegatingExtent)
059: _del = (DelegatingExtent) extent;
060: else
061: _del = null;
062: _trans = trans;
063: }
064:
065: /**
066: * Return the direct delegate.
067: */
068: public Extent getDelegate() {
069: return _extent;
070: }
071:
072: /**
073: * Return the native delegate.
074: */
075: public Extent getInnermostDelegate() {
076: return (_del == null) ? _extent : _del.getInnermostDelegate();
077: }
078:
079: public int hashCode() {
080: return getInnermostDelegate().hashCode();
081: }
082:
083: public boolean equals(Object other) {
084: if (other == this )
085: return true;
086: if (other instanceof DelegatingExtent)
087: other = ((DelegatingExtent) other).getInnermostDelegate();
088: return getInnermostDelegate().equals(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 Class getElementType() {
099: try {
100: return _extent.getElementType();
101: } catch (RuntimeException re) {
102: throw translate(re);
103: }
104: }
105:
106: public boolean hasSubclasses() {
107: try {
108: return _extent.hasSubclasses();
109: } catch (RuntimeException re) {
110: throw translate(re);
111: }
112: }
113:
114: public Broker getBroker() {
115: try {
116: return _extent.getBroker();
117: } catch (RuntimeException re) {
118: throw translate(re);
119: }
120: }
121:
122: public FetchConfiguration getFetchConfiguration() {
123: try {
124: return _extent.getFetchConfiguration();
125: } catch (RuntimeException re) {
126: throw translate(re);
127: }
128: }
129:
130: public boolean getIgnoreChanges() {
131: try {
132: return _extent.getIgnoreChanges();
133: } catch (RuntimeException re) {
134: throw translate(re);
135: }
136: }
137:
138: public void setIgnoreChanges(boolean ignoreCache) {
139: try {
140: _extent.setIgnoreChanges(ignoreCache);
141: } catch (RuntimeException re) {
142: throw translate(re);
143: }
144: }
145:
146: public List list() {
147: try {
148: return _extent.list();
149: } catch (RuntimeException re) {
150: throw translate(re);
151: }
152: }
153:
154: public Iterator iterator() {
155: try {
156: return _extent.iterator();
157: } catch (RuntimeException re) {
158: throw translate(re);
159: }
160: }
161:
162: public void closeAll() {
163: try {
164: _extent.closeAll();
165: } catch (RuntimeException re) {
166: throw translate(re);
167: }
168: }
169:
170: public void lock() {
171: try {
172: _extent.lock();
173: } catch (RuntimeException re) {
174: throw translate(re);
175: }
176: }
177:
178: public void unlock() {
179: try {
180: _extent.unlock();
181: } catch (RuntimeException re) {
182: throw translate(re);
183: }
184: }
185: }
|