1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.openjpa.kernel;
20
21 import java.util.Properties;
22
23 import org.apache.openjpa.conf.OpenJPAConfiguration;
24 import org.apache.openjpa.util.RuntimeExceptionTranslator;
25
26 ///////////////////////////////////////////////////////////////
27 // NOTE: when adding a public API method, be sure to add it to
28 // JDO and JPA facades!
29 ///////////////////////////////////////////////////////////////
30
31 /**
32 * Delegating broker factory that can also perform exception translation
33 * for use in facades.
34 *
35 * @since 0.4.0
36 * @author Abe White
37 * @nojavadoc
38 */
39 public class DelegatingBrokerFactory
40 implements BrokerFactory {
41
42 private final BrokerFactory _factory;
43 private final DelegatingBrokerFactory _del;
44 private final RuntimeExceptionTranslator _trans;
45
46 /**
47 * Constructor; supply delegate.
48 */
49 public DelegatingBrokerFactory(BrokerFactory factory) {
50 this(factory, null);
51 }
52
53 /**
54 * Constructor; supply delegate and exception translator.
55 */
56 public DelegatingBrokerFactory(BrokerFactory factory,
57 RuntimeExceptionTranslator trans) {
58 _factory = factory;
59 if (factory instanceof DelegatingBrokerFactory)
60 _del = (DelegatingBrokerFactory) factory;
61 else
62 _del = null;
63 _trans = trans;
64 }
65
66 /**
67 * Return the direct delegate.
68 */
69 public BrokerFactory getDelegate() {
70 return _factory;
71 }
72
73 /**
74 * Return the native delegate.
75 */
76 public BrokerFactory getInnermostDelegate() {
77 return (_del == null) ? _factory : _del.getInnermostDelegate();
78 }
79
80 public int hashCode() {
81 return getInnermostDelegate().hashCode();
82 }
83
84 public boolean equals(Object other) {
85 if (other == this)
86 return true;
87 if (other instanceof DelegatingBrokerFactory)
88 other = ((DelegatingBrokerFactory) other).getInnermostDelegate();
89 return getInnermostDelegate().equals(other);
90 }
91
92 /**
93 * Translate the OpenJPA exception.
94 */
95 protected RuntimeException translate(RuntimeException re) {
96 return (_trans == null) ? re : _trans.translate(re);
97 }
98
99 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, connRetainMode,
143 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 }