001: package org.apache.ojb.broker.core;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.broker.Identity;
019: import org.apache.ojb.broker.MtoNImplementor;
020: import org.apache.ojb.broker.ManageableCollection;
021: import org.apache.ojb.broker.PBKey;
022: import org.apache.ojb.broker.PBListener;
023: import org.apache.ojb.broker.PBState;
024: import org.apache.ojb.broker.PersistenceBroker;
025: import org.apache.ojb.broker.PersistenceBrokerEvent;
026: import org.apache.ojb.broker.PBLifeCycleEvent;
027: import org.apache.ojb.broker.PBStateEvent;
028: import org.apache.ojb.broker.PersistenceBrokerException;
029: import org.apache.ojb.broker.TransactionAbortedException;
030: import org.apache.ojb.broker.TransactionInProgressException;
031: import org.apache.ojb.broker.TransactionNotInProgressException;
032: import org.apache.ojb.broker.IdentityFactory;
033: import org.apache.ojb.broker.PersistenceBrokerInternal;
034: import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
035: import org.apache.ojb.broker.accesslayer.JdbcAccess;
036: import org.apache.ojb.broker.accesslayer.StatementManagerIF;
037: import org.apache.ojb.broker.accesslayer.RelationshipPrefetcherFactory;
038: import org.apache.ojb.broker.accesslayer.sql.SqlGenerator;
039: import org.apache.ojb.broker.cache.ObjectCache;
040: import org.apache.ojb.broker.core.proxy.ProxyFactory;
041: import org.apache.ojb.broker.metadata.ClassDescriptor;
042: import org.apache.ojb.broker.metadata.DescriptorRepository;
043: import org.apache.ojb.broker.query.Query;
044: import org.apache.ojb.broker.util.BrokerHelper;
045: import org.apache.ojb.broker.util.ObjectModification;
046: import org.apache.ojb.broker.util.configuration.Configuration;
047: import org.apache.ojb.broker.util.configuration.ConfigurationException;
048: import org.apache.ojb.broker.util.sequence.SequenceManager;
049:
050: import java.util.Collection;
051: import java.util.Enumeration;
052: import java.util.Iterator;
053:
054: /**
055: * Delegating implementation of a PersistenceBroker.
056: *
057: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
058: * @version $Id: DelegatingPersistenceBroker.java,v 1.11.2.7 2005/09/05 23:37:26 arminw Exp $
059: */
060: public class DelegatingPersistenceBroker implements
061: PersistenceBrokerInternal, PBState {
062: protected PersistenceBrokerInternal m_broker;
063:
064: public DelegatingPersistenceBroker(PersistenceBrokerInternal broker) {
065: this .m_broker = broker;
066: }
067:
068: /**
069: * All delegated method use this method to
070: * get the wrapped broker.
071: */
072: protected PersistenceBrokerInternal getBroker() {
073: if (m_broker != null) {
074: return m_broker;
075: } else {
076: throw new IllegalStateException(
077: "This PersistenceBroker instance is already closed and no longer useable."
078: + " It's not possible to re-use a closed instance.");
079: }
080:
081: }
082:
083: /**
084: * Returns only the wrapped
085: * {@link org.apache.ojb.broker.PersistenceBroker} instance
086: */
087: public PersistenceBrokerInternal getDelegate() {
088: return this .m_broker;
089: }
090:
091: public void setDelegate(PersistenceBrokerInternal broker) {
092: this .m_broker = broker;
093: }
094:
095: /**
096: * If my underlying {@link org.apache.ojb.broker.PersistenceBroker}
097: * is not a {@link DelegatingPersistenceBroker}, returns it,
098: * otherwise recursively invokes this method on my delegate.
099: * <p>
100: * Hence this method will return the first
101: * delegate that is not a {@link DelegatingPersistenceBroker},
102: * or <tt>null</tt> when no non-{@link DelegatingPersistenceBroker}
103: * delegate can be found by transversing this chain.
104: * <p>
105: * This method is useful when you may have nested
106: * {@link DelegatingPersistenceBroker}s, and you want to make
107: * sure to obtain a "genuine" {@link org.apache.ojb.broker.PersistenceBroker}
108: * implementaion instance.
109: */
110: public PersistenceBroker getInnermostDelegate() {
111: PersistenceBroker broker = this .m_broker;
112: while (broker != null
113: && broker instanceof DelegatingPersistenceBroker) {
114: broker = ((DelegatingPersistenceBroker) broker)
115: .getDelegate();
116: if (this == broker) {
117: return null;
118: }
119: }
120: return broker;
121: }
122:
123: public boolean isManaged() {
124: return m_broker.isManaged();
125: }
126:
127: public void setManaged(boolean managed) {
128: m_broker.setManaged(managed);
129: }
130:
131: public QueryReferenceBroker getReferenceBroker() {
132: return m_broker.getReferenceBroker();
133: }
134:
135: public void checkRefreshRelationships(Object obj, Identity oid,
136: ClassDescriptor cld) {
137: m_broker.checkRefreshRelationships(obj, oid, cld);
138: }
139:
140: public RelationshipPrefetcherFactory getRelationshipPrefetcherFactory() {
141: return m_broker.getRelationshipPrefetcherFactory();
142: }
143:
144: public void store(Object obj, Identity oid, ClassDescriptor cld,
145: boolean insert, boolean ignoreReferences) {
146: m_broker.store(obj, oid, cld, insert, ignoreReferences);
147: }
148:
149: public void delete(Object obj, boolean ignoreReferences)
150: throws PersistenceBrokerException {
151: m_broker.delete(obj, ignoreReferences);
152: }
153:
154: public boolean isInTransaction() throws PersistenceBrokerException {
155: return m_broker != null && getBroker().isInTransaction();
156: }
157:
158: public boolean isClosed() {
159: return m_broker == null || getBroker().isClosed();
160: }
161:
162: public void setClosed(boolean closed) {
163: ((PBState) getBroker()).setClosed(closed);
164: }
165:
166: public void beginTransaction()
167: throws TransactionInProgressException,
168: TransactionAbortedException {
169: getBroker().beginTransaction();
170: }
171:
172: public void commitTransaction()
173: throws TransactionNotInProgressException,
174: TransactionAbortedException {
175: getBroker().commitTransaction();
176: }
177:
178: public void abortTransaction()
179: throws TransactionNotInProgressException {
180: getBroker().abortTransaction();
181: }
182:
183: public boolean close() {
184: return getBroker().close();
185: }
186:
187: public SqlGenerator serviceSqlGenerator() {
188: return getBroker().serviceSqlGenerator();
189: }
190:
191: public JdbcAccess serviceJdbcAccess() {
192: return getBroker().serviceJdbcAccess();
193: }
194:
195: public void delete(Object obj) throws PersistenceBrokerException {
196: getBroker().delete(obj);
197: }
198:
199: public void store(Object obj) throws PersistenceBrokerException {
200: getBroker().store(obj);
201: }
202:
203: public void store(Object obj, ObjectModification modification)
204: throws PersistenceBrokerException {
205: getBroker().store(obj, modification);
206: }
207:
208: public PBKey getPBKey() {
209: return getBroker().getPBKey();
210: }
211:
212: public void removeFromCache(Object obj)
213: throws PersistenceBrokerException {
214: getBroker().removeFromCache(obj);
215: }
216:
217: public void clearCache() throws PersistenceBrokerException {
218: getBroker().clearCache();
219: }
220:
221: public DescriptorRepository getDescriptorRepository() {
222: return getBroker().getDescriptorRepository();
223: }
224:
225: public void removeAllListeners() throws PersistenceBrokerException {
226: getBroker().removeAllListeners();
227: }
228:
229: public void removeAllListeners(boolean permanent)
230: throws PersistenceBrokerException {
231: getBroker().removeAllListeners(permanent);
232: }
233:
234: public void retrieveReference(Object pInstance,
235: String pAttributeName) throws PersistenceBrokerException {
236: getBroker().retrieveReference(pInstance, pAttributeName);
237: }
238:
239: public void retrieveAllReferences(Object pInstance)
240: throws PersistenceBrokerException {
241: getBroker().retrieveAllReferences(pInstance);
242: }
243:
244: public ConnectionManagerIF serviceConnectionManager() {
245: return getBroker().serviceConnectionManager();
246: }
247:
248: public StatementManagerIF serviceStatementManager() {
249: return getBroker().serviceStatementManager();
250: }
251:
252: public SequenceManager serviceSequenceManager() {
253: return getBroker().serviceSequenceManager();
254: }
255:
256: public BrokerHelper serviceBrokerHelper() {
257: return getBroker().serviceBrokerHelper();
258: }
259:
260: public ObjectCache serviceObjectCache() {
261: return getBroker().serviceObjectCache();
262: }
263:
264: public IdentityFactory serviceIdentity() {
265: return getBroker().serviceIdentity();
266: }
267:
268: public void fireBrokerEvent(PersistenceBrokerEvent event) {
269: getBroker().fireBrokerEvent(event);
270: }
271:
272: public void fireBrokerEvent(PBLifeCycleEvent event) {
273: getBroker().fireBrokerEvent(event);
274: }
275:
276: public void fireBrokerEvent(PBStateEvent event) {
277: getBroker().fireBrokerEvent(event);
278: }
279:
280: public void addListener(PBListener listener)
281: throws PersistenceBrokerException {
282: getBroker().addListener(listener);
283: }
284:
285: public void addListener(PBListener listener, boolean permanent)
286: throws PersistenceBrokerException {
287: getBroker().addListener(listener, permanent);
288: }
289:
290: public void removeListener(PBListener listener)
291: throws PersistenceBrokerException {
292: //do nothing
293: }
294:
295: public Class getTopLevelClass(Class clazz)
296: throws PersistenceBrokerException {
297: return getBroker().getTopLevelClass(clazz);
298: }
299:
300: public boolean hasClassDescriptor(Class clazz) {
301: return getBroker().hasClassDescriptor(clazz);
302: }
303:
304: public ClassDescriptor getClassDescriptor(Class clazz)
305: throws PersistenceBrokerException {
306: return getBroker().getClassDescriptor(clazz);
307: }
308:
309: public Enumeration getPKEnumerationByQuery(Class primaryKeyClass,
310: Query query) throws PersistenceBrokerException {
311: return getBroker().getPKEnumerationByQuery(primaryKeyClass,
312: query);
313: }
314:
315: public Object getObjectByQuery(Query query)
316: throws PersistenceBrokerException {
317: return getBroker().getObjectByQuery(query);
318: }
319:
320: public Object getObjectByIdentity(Identity id)
321: throws PersistenceBrokerException {
322: return getBroker().getObjectByIdentity(id);
323: }
324:
325: public Iterator getReportQueryIteratorByQuery(Query query)
326: throws PersistenceBrokerException {
327: return getBroker().getReportQueryIteratorByQuery(query);
328: }
329:
330: public Iterator getIteratorByQuery(Query query)
331: throws PersistenceBrokerException {
332: return getBroker().getIteratorByQuery(query);
333: }
334:
335: public ManageableCollection getCollectionByQuery(
336: Class collectionClass, Query query)
337: throws PersistenceBrokerException {
338: return getBroker().getCollectionByQuery(collectionClass, query);
339: }
340:
341: public int getCount(Query query) throws PersistenceBrokerException {
342: return getBroker().getCount(query);
343: }
344:
345: public Collection getCollectionByQuery(Query query)
346: throws PersistenceBrokerException {
347: return getBroker().getCollectionByQuery(query);
348: }
349:
350: public void configure(Configuration pConfig)
351: throws ConfigurationException {
352: getBroker().configure(pConfig);
353: }
354:
355: public org.odbms.Query query() {
356: return getBroker().query();
357: }
358:
359: public void deleteByQuery(Query query)
360: throws PersistenceBrokerException {
361: getBroker().deleteByQuery(query);
362: }
363:
364: /**
365: * @see org.apache.ojb.broker.PersistenceBroker#deleteMtoNImplementor
366: */
367: public void deleteMtoNImplementor(MtoNImplementor m2nImpl)
368: throws PersistenceBrokerException {
369: getBroker().deleteMtoNImplementor(m2nImpl);
370: }
371:
372: /**
373: * @see org.apache.ojb.broker.PersistenceBroker#addMtoNImplementor
374: */
375: public void addMtoNImplementor(MtoNImplementor m2nImpl)
376: throws PersistenceBrokerException {
377: getBroker().addMtoNImplementor(m2nImpl);
378: }
379:
380: public ProxyFactory getProxyFactory() {
381: return getBroker().getProxyFactory();
382: }
383:
384: public Object createProxy(Class proxyClass,
385: Identity realSubjectsIdentity) {
386: return getBroker()
387: .createProxy(proxyClass, realSubjectsIdentity);
388: }
389:
390: }
|