KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > core > PersistenceBrokerBean


1 package org.apache.ojb.broker.core;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.rmi.RemoteException JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import javax.ejb.EJBException JavaDoc;
24 import javax.ejb.SessionBean JavaDoc;
25 import javax.ejb.SessionContext JavaDoc;
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29
30 import org.apache.ojb.broker.Identity;
31 import org.apache.ojb.broker.IdentityFactory;
32 import org.apache.ojb.broker.ManageableCollection;
33 import org.apache.ojb.broker.MtoNImplementor;
34 import org.apache.ojb.broker.PBKey;
35 import org.apache.ojb.broker.PBLifeCycleEvent;
36 import org.apache.ojb.broker.PBListener;
37 import org.apache.ojb.broker.PBStateEvent;
38 import org.apache.ojb.broker.PersistenceBroker;
39 import org.apache.ojb.broker.PersistenceBrokerEvent;
40 import org.apache.ojb.broker.PersistenceBrokerException;
41 import org.apache.ojb.broker.PersistenceBrokerInternal;
42 import org.apache.ojb.broker.TransactionAbortedException;
43 import org.apache.ojb.broker.TransactionInProgressException;
44 import org.apache.ojb.broker.TransactionNotInProgressException;
45 import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
46 import org.apache.ojb.broker.accesslayer.JdbcAccess;
47 import org.apache.ojb.broker.accesslayer.StatementManagerIF;
48 import org.apache.ojb.broker.accesslayer.sql.SqlGenerator;
49 import org.apache.ojb.broker.cache.ObjectCache;
50 import org.apache.ojb.broker.core.proxy.ProxyFactory;
51 import org.apache.ojb.broker.metadata.ClassDescriptor;
52 import org.apache.ojb.broker.metadata.DescriptorRepository;
53 import org.apache.ojb.broker.query.Query;
54 import org.apache.ojb.broker.util.BrokerHelper;
55 import org.apache.ojb.broker.util.ObjectModification;
56 import org.apache.ojb.broker.util.configuration.Configuration;
57 import org.apache.ojb.broker.util.configuration.ConfigurationException;
58 import org.apache.ojb.broker.util.logging.Logger;
59 import org.apache.ojb.broker.util.logging.LoggerFactory;
60 import org.apache.ojb.broker.util.sequence.SequenceManager;
61
62 /**
63  * @author <a HREF="mailto:mattbaird@yahoo.com">Matthew Baird</a>
64  *
65  * The PersistenceBrokerBean wraps a persistenceBroker implementation and allows
66  * PersistenceBroker server operations with communication happening over RMI.
67  * Useful if you are going to use OJB in a J2EE environment.
68  *
69  * Allows for OJB objects with proxies to be taken outside of the VM, say to an
70  * instance of a servlet container, and the proxies will call back on the bean
71  * to materialize proxies via standard RMI bean calls, instead of the custom
72  * protocol.
73  *
74  * Container will be responsible for pooling of bean instances.
75  *
76  * Can be used by normal EJB clients, not just the PersistenceBrokerClient
77  *
78  * @ejb:bean
79  * type="Stateless"
80  * name="PersistenceBrokerBean"
81  * jndi-name="org.apache.ojb.broker.core.PersistenceBrokerBean"
82  * local-jndi-name="ojb.PersistenceBrokerBean"
83  * view-type="both"
84  * transaction-type="Container"
85  *
86  * @ejb:interface
87  * remote-class="org.apache.ojb.broker.server.PersistenceBrokerRemote"
88  * local-class="org.apache.ojb.broker.server.PersistenceBrokerLocal"
89  * extends="javax.ejb.EJBObject, org.apache.ojb.broker.PersistenceBroker"
90  *
91  * @ejb:home
92  * remote-class="org.apache.ojb.broker.server.PersistenceBrokerHome"
93  * local-class="org.apache.ojb.broker.server.PersistenceBrokerLocalHome"
94  * extends="javax.ejb.EJBHome"
95  *
96  * @ejb:transaction
97  * type="Required"
98  *
99  * @ejb:env-entry
100  * name="ojb.repository"
101  * type="java.lang.String"
102  * value="repository.xml"
103  *
104  */

105
106 public class PersistenceBrokerBean implements PersistenceBroker, SessionBean JavaDoc
107 {
108     private Logger m_log;
109     private SessionContext JavaDoc m_ctx;
110     private String JavaDoc m_ojbRepository;
111     private PBKey m_pbKey;
112     private PersistenceBrokerFactoryIF m_pbf;
113
114     /**
115      * @ejb:interface-method
116      * @see org.apache.ojb.broker.PersistenceBroker#getDescriptorRepository()
117      */

118     public DescriptorRepository getDescriptorRepository()
119     {
120         return getBroker().getDescriptorRepository();
121     }
122
123     /**
124      * @ejb:interface-method
125      * @see org.apache.ojb.broker.PersistenceBroker#getPBKey()
126      */

127     public PBKey getPBKey()
128     {
129         return getBroker().getPBKey();
130     }
131
132     /**
133      * @ejb:interface-method
134      * @see org.apache.ojb.broker.PersistenceBroker#delete(Object)
135      */

136     public void delete(Object JavaDoc obj) throws PersistenceBrokerException
137     {
138         getBroker().delete(obj);
139     }
140
141     /**
142      * @ejb:interface-method
143      * @see org.apache.ojb.broker.PersistenceBroker#deleteByQuery(Query)
144      */

145     public void deleteByQuery(Query query) throws PersistenceBrokerException
146     {
147         getBroker().deleteByQuery(query);
148     }
149
150     /**
151      * @ejb:interface-method
152      * @see org.apache.ojb.broker.PersistenceBroker#removeFromCache(Object)
153      */

154     public void removeFromCache(Object JavaDoc obj) throws PersistenceBrokerException
155     {
156         getBroker().removeFromCache(obj);
157     }
158
159     /**
160      * @ejb:interface-method
161      * @see org.apache.ojb.broker.PersistenceBroker#clearCache()
162      */

163     public void clearCache() throws PersistenceBrokerException
164     {
165         getBroker().clearCache();
166     }
167
168     /**
169      * @ejb:interface-method
170      * @see org.apache.ojb.broker.PersistenceBroker#store(Object)
171      */

172     public void store(Object JavaDoc obj) throws PersistenceBrokerException
173     {
174         getBroker().store(obj);
175     }
176
177     /**
178      * @ejb:interface-method
179      * @see org.apache.ojb.broker.PersistenceBroker#abortTransaction()
180      */

181     public void abortTransaction() throws TransactionNotInProgressException
182     {
183         getBroker().abortTransaction();
184     }
185
186     /**
187      * @ejb:interface-method
188      * @see org.apache.ojb.broker.PersistenceBroker#beginTransaction()
189      */

190     public void beginTransaction()
191             throws TransactionInProgressException, TransactionAbortedException
192     {
193         getBroker().beginTransaction();
194     }
195
196     /**
197      * @ejb:interface-method
198      * @see org.apache.ojb.broker.PersistenceBroker#commitTransaction()
199      */

200     public void commitTransaction()
201             throws TransactionNotInProgressException, TransactionAbortedException
202     {
203         getBroker().commitTransaction();
204     }
205
206     /**
207      * @ejb:interface-method
208      * @see org.apache.ojb.broker.PersistenceBroker#isInTransaction()
209      */

210     public boolean isInTransaction() throws PersistenceBrokerException
211     {
212         return getBroker().isInTransaction();
213     }
214
215     /**
216      * @ejb:interface-method
217      * @see org.apache.ojb.broker.PersistenceBroker#close()
218      */

219     public boolean close()
220     {
221         return getBroker().close();
222     }
223
224     public boolean isClosed()
225     {
226         return getBroker().isClosed();
227     }
228
229     /**
230      * @ejb:interface-method
231      * @see org.apache.ojb.broker.PersistenceBroker#getCollectionByQuery(Query)
232      */

233     public Collection JavaDoc getCollectionByQuery(Query query)
234             throws PersistenceBrokerException
235     {
236         return getBroker().getCollectionByQuery(query);
237     }
238
239     /**
240      * @ejb:interface-method
241      * @see org.apache.ojb.broker.PersistenceBroker#getCount(Query)
242      */

243     public int getCount(Query query) throws PersistenceBrokerException
244     {
245         return getBroker().getCount(query);
246     }
247
248     /**
249      * @ejb:interface-method
250      * @see org.apache.ojb.broker.PersistenceBroker#getCollectionByQuery(Class, Query)
251      */

252     public ManageableCollection getCollectionByQuery(
253             Class JavaDoc collectionClass,
254             Query query)
255             throws PersistenceBrokerException
256     {
257         return getBroker().getCollectionByQuery(collectionClass, query);
258     }
259
260     /**
261      * @ejb:interface-method
262      * @see org.apache.ojb.broker.PersistenceBroker#getIteratorByQuery(Query)
263      */

264     public Iterator JavaDoc getIteratorByQuery(Query query)
265             throws PersistenceBrokerException
266     {
267         return getBroker().getIteratorByQuery(query);
268     }
269
270     /**
271      * @ejb:interface-method
272      * @see org.apache.ojb.broker.PersistenceBroker#getReportQueryIteratorByQuery(Query)
273      */

274     public Iterator JavaDoc getReportQueryIteratorByQuery(Query query)
275             throws PersistenceBrokerException
276     {
277         return getBroker().getReportQueryIteratorByQuery(query);
278     }
279
280     /**
281      * @ejb:interface-method
282      * @see org.apache.ojb.broker.PersistenceBroker#getObjectByIdentity(Identity)
283      */

284     public Object JavaDoc getObjectByIdentity(Identity id)
285             throws PersistenceBrokerException
286     {
287         return getBroker().getObjectByIdentity(id);
288     }
289
290     /**
291      * @ejb:interface-method
292      * @see org.apache.ojb.broker.PersistenceBroker#getObjectByQuery(Query)
293      */

294     public Object JavaDoc getObjectByQuery(Query query)
295             throws PersistenceBrokerException
296     {
297         return getBroker().getObjectByQuery(query);
298     }
299
300     /**
301      * @ejb:interface-method
302      * @see org.apache.ojb.broker.PersistenceBroker#getPKEnumerationByQuery(Class, Query)
303      */

304     public Enumeration JavaDoc getPKEnumerationByQuery(
305             Class JavaDoc PrimaryKeyClass,
306             Query query)
307             throws PersistenceBrokerException
308     {
309         return getBroker().getPKEnumerationByQuery(PrimaryKeyClass, query);
310     }
311
312     /**
313      * @ejb:interface-method
314      * @see org.apache.ojb.broker.PersistenceBroker#store(Object, ObjectModification)
315      */

316     public void store(Object JavaDoc obj, ObjectModification modification)
317             throws PersistenceBrokerException
318     {
319         getBroker().store(obj, modification);
320     }
321
322
323     /**
324      * @ejb:interface-method
325      * @see org.apache.ojb.broker.PersistenceBroker#getClassDescriptor(Class)
326      */

327     public ClassDescriptor getClassDescriptor(Class JavaDoc clazz)
328             throws PersistenceBrokerException
329     {
330         return getBroker().getClassDescriptor(clazz);
331     }
332
333     /**
334      * @ejb:interface-method
335      * @see org.apache.ojb.broker.PersistenceBroker#hasClassDescriptor(Class)
336      */

337     public boolean hasClassDescriptor(Class JavaDoc clazz)
338     {
339         return getBroker().hasClassDescriptor(clazz);
340     }
341
342
343     /**
344      * @ejb:interface-method
345      * @see org.apache.ojb.broker.PersistenceBroker#getTopLevelClass(Class)
346      */

347     public Class JavaDoc getTopLevelClass(Class JavaDoc clazz) throws PersistenceBrokerException
348     {
349         return getBroker().getTopLevelClass(clazz);
350     }
351
352     /**
353      * @ejb:interface-method
354      * @see org.apache.ojb.broker.PersistenceBroker#serviceStatementManager()
355      */

356     public StatementManagerIF serviceStatementManager()
357     {
358         return getBroker().serviceStatementManager();
359     }
360
361     /**
362      * @ejb:interface-method
363      * @see org.apache.ojb.broker.PersistenceBroker#serviceConnectionManager()
364      */

365     public ConnectionManagerIF serviceConnectionManager()
366     {
367         return getBroker().serviceConnectionManager();
368     }
369
370     public JdbcAccess serviceJdbcAccess()
371     {
372         return getBroker().serviceJdbcAccess();
373     }
374
375     /**
376      * @ejb:interface-method
377      * @see org.apache.ojb.broker.PersistenceBroker#serviceSqlGenerator()
378      */

379     public SqlGenerator serviceSqlGenerator()
380     {
381         return getBroker().serviceSqlGenerator();
382     }
383
384     public SequenceManager serviceSequenceManager()
385     {
386         return getBroker().serviceSequenceManager();
387     }
388
389     public BrokerHelper serviceBrokerHelper()
390     {
391         return getBroker().serviceBrokerHelper();
392     }
393
394     public ObjectCache serviceObjectCache()
395     {
396         return getBroker().serviceObjectCache();
397     }
398
399     public IdentityFactory serviceIdentity()
400     {
401         return getBroker().serviceIdentity();
402     }
403
404     public void fireBrokerEvent(PersistenceBrokerEvent event)
405     {
406         getBroker().fireBrokerEvent(event);
407     }
408
409     public void fireBrokerEvent(PBLifeCycleEvent event)
410     {
411         getBroker().fireBrokerEvent(event);
412     }
413
414     public void fireBrokerEvent(PBStateEvent event)
415     {
416         getBroker().fireBrokerEvent(event);
417     }
418     
419     
420
421     public ProxyFactory getProxyFactory() {
422         return getBroker().getProxyFactory();
423     }
424     
425     public Object JavaDoc createProxy(Class JavaDoc proxyClass, Identity realSubjectsIdentity) {
426         return getBroker().createProxy(proxyClass, realSubjectsIdentity);
427     }
428
429     /**
430      * @ejb:interface-method
431      * @see org.apache.ojb.broker.PersistenceBroker#addListener(PBListener)
432      */

433     public void addListener(PBListener listener) throws PersistenceBrokerException
434     {
435         getBroker().addListener(listener);
436     }
437
438     /**
439      * @ejb:interface-method
440      * @see org.apache.ojb.broker.PersistenceBroker#addListener(PBListener, boolean)
441      */

442     public void addListener(PBListener listener, boolean permanent) throws PersistenceBrokerException
443     {
444         getBroker().addListener(listener, permanent);
445     }
446
447     /**
448      * @ejb:interface-method
449      * @see org.apache.ojb.broker.PersistenceBroker#removeListener(PBListener)
450      */

451     public void removeListener(PBListener listener) throws PersistenceBrokerException
452     {
453         getBroker().removeListener(listener);
454     }
455
456     /**
457      * @ejb:interface-method
458      * @see org.apache.ojb.broker.PersistenceBroker#retrieveAllReferences(Object)
459      */

460     public void retrieveAllReferences(Object JavaDoc pInstance)
461             throws PersistenceBrokerException
462     {
463         getBroker().retrieveAllReferences(pInstance);
464     }
465
466     /**
467      * @ejb:interface-method
468      * @see org.apache.ojb.broker.PersistenceBroker#retrieveReference(Object, String)
469      */

470     public void retrieveReference(Object JavaDoc pInstance, String JavaDoc pAttributeName)
471             throws PersistenceBrokerException
472     {
473         getBroker().retrieveReference(pInstance, pAttributeName);
474     }
475
476     public void removeAllListeners(boolean permanent) throws PersistenceBrokerException
477     {
478         getBroker().removeAllListeners(permanent);
479     }
480
481     /**
482      * @ejb:interface-method
483      * @see org.apache.ojb.broker.PersistenceBroker#removeAllListeners()
484      */

485     public void removeAllListeners() throws PersistenceBrokerException
486     {
487         getBroker().removeAllListeners();
488     }
489
490     /**
491      * @ejb:interface-method
492      * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
493      */

494     public void configure(Configuration pConfig)
495             throws ConfigurationException
496     {
497         getBroker().configure(pConfig);
498     }
499
500     /**
501      * @ejb:interface-method
502      * @see org.odbms.ObjectContainer#query()
503      */

504     public org.odbms.Query query()
505     {
506         return getBroker().query();
507     }
508
509     private void ojbPrepare()
510     {
511         if (m_log.isDebugEnabled()) m_log.info("PersistenceBrokerBean: ejbActivate was called");
512         Context JavaDoc context = null;
513         // Lookup if a environment entry for repository exists
514
String JavaDoc ojbRepository = null;
515         try
516         {
517             context = new InitialContext JavaDoc();
518             ojbRepository = (String JavaDoc) context.lookup("java:comp/env/ojb.repository");
519         }
520         catch (NamingException JavaDoc e)
521         {
522             m_log.error("Lookup for ojb repository failed", e);
523         }
524         // no repository found in environment, use default one
525
if (ojbRepository == null || ojbRepository.equals(""))
526         {
527             m_log.info("No enviroment entry was found, use default repository");
528             ojbRepository = "repository.xml";
529         }
530         m_log.info("Use OJB repository file: " + ojbRepository);
531         m_pbKey = new PBKey(ojbRepository);
532
533         // Lookup the PBF implementation
534
try
535         {
536             context = new InitialContext JavaDoc();
537             m_pbf = ((PBFactoryIF) context.lookup(PBFactoryIF.PBFACTORY_JNDI_NAME)).getInstance();
538         }
539         catch (NamingException JavaDoc e)
540         {
541             m_log.error("Lookup for PersistenceBrokerFactory failed", e);
542             throw new PersistenceBrokerException(e);
543         }
544     }
545
546     public void ejbActivate() throws EJBException JavaDoc, RemoteException JavaDoc
547     {
548         m_log = LoggerFactory.getLogger(PersistenceBrokerBean.class);
549         ojbPrepare();
550     }
551
552     public void ejbPassivate() throws EJBException JavaDoc, RemoteException JavaDoc
553     {
554         m_log = null;
555         m_pbf = null;
556     }
557
558     public void ejbRemove() throws EJBException JavaDoc, RemoteException JavaDoc
559     {
560         m_ctx = null;
561     }
562
563     public void setSessionContext(SessionContext JavaDoc sessionContext) throws EJBException JavaDoc, RemoteException JavaDoc
564     {
565         m_ctx = sessionContext;
566     }
567
568     private PersistenceBrokerInternal getBroker()
569     {
570         return m_pbf.createPersistenceBroker(m_pbKey);
571     }
572
573     /**
574      * @see org.apache.ojb.broker.PersistenceBroker#deleteMtoNImplementor()
575      */

576     public void deleteMtoNImplementor(MtoNImplementor m2nImpl) throws PersistenceBrokerException
577     {
578         throw new UnsupportedOperationException JavaDoc();
579     }
580
581     /**
582      * @see org.apache.ojb.broker.PersistenceBroker#addMtoNImplementor()
583      */

584     public void addMtoNImplementor(MtoNImplementor m2nImpl) throws PersistenceBrokerException
585     {
586         throw new UnsupportedOperationException JavaDoc();
587     }
588     
589     
590 }
591
Popular Tags