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.1 2007-08-24 22:17:35 ewestfal Exp $
059     */
060    public class DelegatingPersistenceBroker implements PersistenceBrokerInternal, PBState
061    {
062            protected PersistenceBrokerInternal m_broker;
063    
064            public DelegatingPersistenceBroker(PersistenceBrokerInternal broker)
065            {
066                    this.m_broker = broker;
067            }
068    
069            /**
070             * All delegated method use this method to
071             * get the wrapped broker.
072             */
073            protected PersistenceBrokerInternal getBroker()
074            {
075                    if (m_broker != null)
076            {
077                            return m_broker;
078            }
079                    else
080            {
081                throw new IllegalStateException(
082                        "This PersistenceBroker instance is already closed and no longer useable." +
083                        " It's not possible to re-use a closed instance.");
084            }
085    
086            }
087    
088            /**
089             * Returns only the wrapped
090             * {@link org.apache.ojb.broker.PersistenceBroker} instance
091             */
092            public PersistenceBrokerInternal getDelegate()
093            {
094                    return this.m_broker;
095            }
096    
097            public void setDelegate(PersistenceBrokerInternal broker)
098            {
099                    this.m_broker = broker;
100            }
101    
102            /**
103             * If my underlying {@link org.apache.ojb.broker.PersistenceBroker}
104             * is not a {@link DelegatingPersistenceBroker}, returns it,
105             * otherwise recursively invokes this method on my delegate.
106             * <p>
107             * Hence this method will return the first
108             * delegate that is not a {@link DelegatingPersistenceBroker},
109             * or <tt>null</tt> when no non-{@link DelegatingPersistenceBroker}
110             * delegate can be found by transversing this chain.
111             * <p>
112             * This method is useful when you may have nested
113             * {@link DelegatingPersistenceBroker}s, and you want to make
114             * sure to obtain a "genuine" {@link org.apache.ojb.broker.PersistenceBroker}
115             * implementaion instance.
116             */
117            public PersistenceBroker getInnermostDelegate()
118            {
119                    PersistenceBroker broker = this.m_broker;
120                    while (broker != null && broker instanceof DelegatingPersistenceBroker)
121                    {
122                            broker = ((DelegatingPersistenceBroker) broker).getDelegate();
123                            if (this == broker)
124                            {
125                                    return null;
126                            }
127                    }
128                    return broker;
129            }
130    
131        public boolean isManaged()
132        {
133            return m_broker.isManaged();
134        }
135    
136        public void setManaged(boolean managed)
137        {
138            m_broker.setManaged(managed);
139        }
140    
141        public QueryReferenceBroker getReferenceBroker()
142        {
143            return m_broker.getReferenceBroker();
144        }
145    
146        public void checkRefreshRelationships(Object obj, Identity oid, ClassDescriptor cld)
147        {
148            m_broker.checkRefreshRelationships(obj, oid, cld);
149        }
150    
151        public RelationshipPrefetcherFactory getRelationshipPrefetcherFactory()
152        {
153            return m_broker.getRelationshipPrefetcherFactory();
154        }
155    
156        public void store(Object obj, Identity oid, ClassDescriptor cld, boolean insert, boolean ignoreReferences)
157        {
158            m_broker.store(obj, oid, cld, insert, ignoreReferences);
159        }
160    
161        public void delete(Object obj, boolean ignoreReferences) throws PersistenceBrokerException
162        {
163            m_broker.delete(obj, ignoreReferences);
164        }
165    
166        public boolean isInTransaction() throws PersistenceBrokerException
167        {
168            return m_broker != null && getBroker().isInTransaction();
169        }
170    
171            public boolean isClosed()
172            {
173                    return m_broker == null || getBroker().isClosed();
174            }
175    
176            public void setClosed(boolean closed)
177            {
178                    ((PBState) getBroker()).setClosed(closed);
179            }
180    
181            public void beginTransaction()
182                            throws TransactionInProgressException, TransactionAbortedException
183            {
184                    getBroker().beginTransaction();
185            }
186    
187            public void commitTransaction()
188                            throws TransactionNotInProgressException, TransactionAbortedException
189            {
190                    getBroker().commitTransaction();
191            }
192    
193            public void abortTransaction() throws TransactionNotInProgressException
194            {
195                    getBroker().abortTransaction();
196            }
197    
198            public boolean close()
199            {
200                    return getBroker().close();
201            }
202    
203            public SqlGenerator serviceSqlGenerator()
204            {
205                    return getBroker().serviceSqlGenerator();
206            }
207    
208            public JdbcAccess serviceJdbcAccess()
209            {
210                    return getBroker().serviceJdbcAccess();
211            }
212    
213            public void delete(Object obj) throws PersistenceBrokerException
214            {
215                    getBroker().delete(obj);
216            }
217    
218            public void store(Object obj) throws PersistenceBrokerException
219            {
220                    getBroker().store(obj);
221            }
222    
223            public void store(Object obj,
224                                              ObjectModification modification) throws PersistenceBrokerException
225            {
226                    getBroker().store(obj, modification);
227            }
228    
229            public PBKey getPBKey()
230            {
231                    return getBroker().getPBKey();
232            }
233    
234            public void removeFromCache(Object obj) throws PersistenceBrokerException
235            {
236                    getBroker().removeFromCache(obj);
237            }
238    
239            public void clearCache() throws PersistenceBrokerException
240            {
241                    getBroker().clearCache();
242            }
243    
244            public DescriptorRepository getDescriptorRepository()
245            {
246                    return getBroker().getDescriptorRepository();
247            }
248    
249            public void removeAllListeners() throws PersistenceBrokerException
250            {
251                    getBroker().removeAllListeners();
252            }
253    
254            public void removeAllListeners(boolean permanent) throws PersistenceBrokerException
255            {
256                    getBroker().removeAllListeners(permanent);
257            }
258    
259            public void retrieveReference(Object pInstance, String pAttributeName) throws PersistenceBrokerException
260            {
261                    getBroker().retrieveReference(pInstance, pAttributeName);
262            }
263    
264            public void retrieveAllReferences(Object pInstance) throws PersistenceBrokerException
265            {
266                    getBroker().retrieveAllReferences(pInstance);
267            }
268    
269            public ConnectionManagerIF serviceConnectionManager()
270            {
271                    return getBroker().serviceConnectionManager();
272            }
273    
274            public StatementManagerIF serviceStatementManager()
275            {
276                    return getBroker().serviceStatementManager();
277            }
278    
279            public SequenceManager serviceSequenceManager()
280            {
281                    return getBroker().serviceSequenceManager();
282            }
283    
284            public BrokerHelper serviceBrokerHelper()
285            {
286                    return getBroker().serviceBrokerHelper();
287            }
288    
289            public ObjectCache serviceObjectCache()
290            {
291                    return getBroker().serviceObjectCache();
292            }
293    
294        public IdentityFactory serviceIdentity()
295        {
296            return getBroker().serviceIdentity();
297        }
298    
299            public void fireBrokerEvent(PersistenceBrokerEvent event)
300            {
301                    getBroker().fireBrokerEvent(event);
302            }
303    
304            public void fireBrokerEvent(PBLifeCycleEvent event)
305            {
306                    getBroker().fireBrokerEvent(event);
307            }
308    
309            public void fireBrokerEvent(PBStateEvent event)
310            {
311                    getBroker().fireBrokerEvent(event);
312            }
313    
314            public void addListener(PBListener listener) throws PersistenceBrokerException
315            {
316                    getBroker().addListener(listener);
317            }
318    
319            public void addListener(PBListener listener, boolean permanent) throws PersistenceBrokerException
320            {
321                    getBroker().addListener(listener, permanent);
322            }
323    
324            public void removeListener(PBListener listener) throws PersistenceBrokerException
325            {
326            //do nothing        
327            }
328    
329            public Class getTopLevelClass(Class clazz) throws PersistenceBrokerException
330            {
331                    return getBroker().getTopLevelClass(clazz);
332            }
333    
334            public boolean hasClassDescriptor(Class clazz)
335            {
336                    return getBroker().hasClassDescriptor(clazz);
337            }
338    
339            public ClassDescriptor getClassDescriptor(Class clazz) throws PersistenceBrokerException
340            {
341                    return getBroker().getClassDescriptor(clazz);
342            }
343    
344            public Enumeration getPKEnumerationByQuery(Class primaryKeyClass,
345                                                                                               Query query) throws PersistenceBrokerException
346            {
347                    return getBroker().getPKEnumerationByQuery(primaryKeyClass, query);
348            }
349    
350            public Object getObjectByQuery(Query query) throws PersistenceBrokerException
351            {
352                    return getBroker().getObjectByQuery(query);
353            }
354    
355            public Object getObjectByIdentity(Identity id) throws PersistenceBrokerException
356            {
357                    return getBroker().getObjectByIdentity(id);
358            }
359    
360            public Iterator getReportQueryIteratorByQuery(Query query) throws PersistenceBrokerException
361            {
362                    return getBroker().getReportQueryIteratorByQuery(query);
363            }
364    
365            public Iterator getIteratorByQuery(Query query) throws PersistenceBrokerException
366            {
367                    return getBroker().getIteratorByQuery(query);
368            }
369    
370            public ManageableCollection getCollectionByQuery(Class collectionClass, Query query)
371                            throws PersistenceBrokerException
372            {
373                    return getBroker().getCollectionByQuery(collectionClass, query);
374            }
375    
376            public int getCount(Query query) throws PersistenceBrokerException
377            {
378                    return getBroker().getCount(query);
379            }
380    
381            public Collection getCollectionByQuery(Query query) throws PersistenceBrokerException
382            {
383                    return getBroker().getCollectionByQuery(query);
384            }
385    
386            public void configure(Configuration pConfig) throws ConfigurationException
387            {
388                    getBroker().configure(pConfig);
389            }
390    
391            public org.odbms.Query query()
392            {
393                    return getBroker().query();
394            }
395    
396            public void deleteByQuery(Query query) throws PersistenceBrokerException
397            {
398                    getBroker().deleteByQuery(query);
399            }
400    
401        /**
402         * @see org.apache.ojb.broker.PersistenceBroker#deleteMtoNImplementor
403         */
404        public void deleteMtoNImplementor(MtoNImplementor m2nImpl) throws PersistenceBrokerException
405        {
406            getBroker().deleteMtoNImplementor(m2nImpl);
407        }
408    
409        /**
410         * @see org.apache.ojb.broker.PersistenceBroker#addMtoNImplementor
411         */
412        public void addMtoNImplementor(MtoNImplementor m2nImpl) throws PersistenceBrokerException
413        {
414            getBroker().addMtoNImplementor(m2nImpl);
415        }
416    
417        public ProxyFactory getProxyFactory() 
418        {
419           return getBroker().getProxyFactory();
420        }
421    
422        public Object createProxy(Class proxyClass, Identity realSubjectsIdentity) {
423            return getBroker().createProxy(proxyClass, realSubjectsIdentity);
424        }
425        
426        
427        
428    
429    }