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.Properties;
022:
023: import org.apache.openjpa.conf.OpenJPAConfiguration;
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 broker factory that can also perform exception translation
033: * for use in facades.
034: *
035: * @since 0.4.0
036: * @author Abe White
037: * @nojavadoc
038: */
039: public class DelegatingBrokerFactory implements BrokerFactory {
040:
041: private final BrokerFactory _factory;
042: private final DelegatingBrokerFactory _del;
043: private final RuntimeExceptionTranslator _trans;
044:
045: /**
046: * Constructor; supply delegate.
047: */
048: public DelegatingBrokerFactory(BrokerFactory factory) {
049: this (factory, null);
050: }
051:
052: /**
053: * Constructor; supply delegate and exception translator.
054: */
055: public DelegatingBrokerFactory(BrokerFactory factory,
056: RuntimeExceptionTranslator trans) {
057: _factory = factory;
058: if (factory instanceof DelegatingBrokerFactory)
059: _del = (DelegatingBrokerFactory) factory;
060: else
061: _del = null;
062: _trans = trans;
063: }
064:
065: /**
066: * Return the direct delegate.
067: */
068: public BrokerFactory getDelegate() {
069: return _factory;
070: }
071:
072: /**
073: * Return the native delegate.
074: */
075: public BrokerFactory getInnermostDelegate() {
076: return (_del == null) ? _factory : _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 DelegatingBrokerFactory)
087: other = ((DelegatingBrokerFactory) other)
088: .getInnermostDelegate();
089: return getInnermostDelegate().equals(other);
090: }
091:
092: /**
093: * Translate the OpenJPA exception.
094: */
095: protected RuntimeException translate(RuntimeException re) {
096: return (_trans == null) ? re : _trans.translate(re);
097: }
098:
099: public OpenJPAConfiguration getConfiguration() {
100: try {
101: return _factory.getConfiguration();
102: } catch (RuntimeException re) {
103: throw translate(re);
104: }
105: }
106:
107: public Properties getProperties() {
108: try {
109: return _factory.getProperties();
110: } catch (RuntimeException re) {
111: throw translate(re);
112: }
113: }
114:
115: public Object putUserObject(Object key, Object val) {
116: try {
117: return _factory.putUserObject(key, val);
118: } catch (RuntimeException re) {
119: throw translate(re);
120: }
121: }
122:
123: public Object getUserObject(Object key) {
124: try {
125: return _factory.getUserObject(key);
126: } catch (RuntimeException re) {
127: throw translate(re);
128: }
129: }
130:
131: public Broker newBroker() {
132: try {
133: return _factory.newBroker();
134: } catch (RuntimeException re) {
135: throw translate(re);
136: }
137: }
138:
139: public Broker newBroker(String user, String pass, boolean managed,
140: int connRetainMode, boolean findExisting) {
141: try {
142: return _factory.newBroker(user, pass, managed,
143: connRetainMode, findExisting);
144: } catch (RuntimeException re) {
145: throw translate(re);
146: }
147: }
148:
149: public void addLifecycleListener(Object listener, Class[] classes) {
150: try {
151: _factory.addLifecycleListener(listener, classes);
152: } catch (RuntimeException re) {
153: throw translate(re);
154: }
155: }
156:
157: public void removeLifecycleListener(Object listener) {
158: try {
159: _factory.removeLifecycleListener(listener);
160: } catch (RuntimeException re) {
161: throw translate(re);
162: }
163: }
164:
165: public void addTransactionListener(Object listener) {
166: try {
167: _factory.addTransactionListener(listener);
168: } catch (RuntimeException re) {
169: throw translate(re);
170: }
171: }
172:
173: public void removeTransactionListener(Object listener) {
174: try {
175: _factory.removeTransactionListener(listener);
176: } catch (RuntimeException re) {
177: throw translate(re);
178: }
179: }
180:
181: public void close() {
182: try {
183: _factory.close();
184: } catch (RuntimeException re) {
185: throw translate(re);
186: }
187: }
188:
189: public boolean isClosed() {
190: try {
191: return _factory.isClosed();
192: } catch (RuntimeException re) {
193: throw translate(re);
194: }
195: }
196:
197: public void lock() {
198: try {
199: _factory.lock();
200: } catch (RuntimeException re) {
201: throw translate(re);
202: }
203: }
204:
205: public void unlock() {
206: try {
207: _factory.unlock();
208: } catch (RuntimeException re) {
209: throw translate(re);
210: }
211: }
212: }
|