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 java.rmi.RemoteException;
019: import java.util.Collection;
020: import java.util.Enumeration;
021: import java.util.Iterator;
022:
023: import javax.ejb.EJBException;
024: import javax.ejb.SessionBean;
025: import javax.ejb.SessionContext;
026: import javax.naming.Context;
027: import javax.naming.InitialContext;
028: import javax.naming.NamingException;
029:
030: import org.apache.ojb.broker.Identity;
031: import org.apache.ojb.broker.IdentityFactory;
032: import org.apache.ojb.broker.ManageableCollection;
033: import org.apache.ojb.broker.MtoNImplementor;
034: import org.apache.ojb.broker.PBKey;
035: import org.apache.ojb.broker.PBLifeCycleEvent;
036: import org.apache.ojb.broker.PBListener;
037: import org.apache.ojb.broker.PBStateEvent;
038: import org.apache.ojb.broker.PersistenceBroker;
039: import org.apache.ojb.broker.PersistenceBrokerEvent;
040: import org.apache.ojb.broker.PersistenceBrokerException;
041: import org.apache.ojb.broker.PersistenceBrokerInternal;
042: import org.apache.ojb.broker.TransactionAbortedException;
043: import org.apache.ojb.broker.TransactionInProgressException;
044: import org.apache.ojb.broker.TransactionNotInProgressException;
045: import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
046: import org.apache.ojb.broker.accesslayer.JdbcAccess;
047: import org.apache.ojb.broker.accesslayer.StatementManagerIF;
048: import org.apache.ojb.broker.accesslayer.sql.SqlGenerator;
049: import org.apache.ojb.broker.cache.ObjectCache;
050: import org.apache.ojb.broker.core.proxy.ProxyFactory;
051: import org.apache.ojb.broker.metadata.ClassDescriptor;
052: import org.apache.ojb.broker.metadata.DescriptorRepository;
053: import org.apache.ojb.broker.query.Query;
054: import org.apache.ojb.broker.util.BrokerHelper;
055: import org.apache.ojb.broker.util.ObjectModification;
056: import org.apache.ojb.broker.util.configuration.Configuration;
057: import org.apache.ojb.broker.util.configuration.ConfigurationException;
058: import org.apache.ojb.broker.util.logging.Logger;
059: import org.apache.ojb.broker.util.logging.LoggerFactory;
060: import org.apache.ojb.broker.util.sequence.SequenceManager;
061:
062: /**
063: * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
064: *
065: * The PersistenceBrokerBean wraps a persistenceBroker implementation and allows
066: * PersistenceBroker server operations with communication happening over RMI.
067: * Useful if you are going to use OJB in a J2EE environment.
068: *
069: * Allows for OJB objects with proxies to be taken outside of the VM, say to an
070: * instance of a servlet container, and the proxies will call back on the bean
071: * to materialize proxies via standard RMI bean calls, instead of the custom
072: * protocol.
073: *
074: * Container will be responsible for pooling of bean instances.
075: *
076: * Can be used by normal EJB clients, not just the PersistenceBrokerClient
077: *
078: * @ejb:bean
079: * type="Stateless"
080: * name="PersistenceBrokerBean"
081: * jndi-name="org.apache.ojb.broker.core.PersistenceBrokerBean"
082: * local-jndi-name="ojb.PersistenceBrokerBean"
083: * view-type="both"
084: * transaction-type="Container"
085: *
086: * @ejb:interface
087: * remote-class="org.apache.ojb.broker.server.PersistenceBrokerRemote"
088: * local-class="org.apache.ojb.broker.server.PersistenceBrokerLocal"
089: * extends="javax.ejb.EJBObject, org.apache.ojb.broker.PersistenceBroker"
090: *
091: * @ejb:home
092: * remote-class="org.apache.ojb.broker.server.PersistenceBrokerHome"
093: * local-class="org.apache.ojb.broker.server.PersistenceBrokerLocalHome"
094: * extends="javax.ejb.EJBHome"
095: *
096: * @ejb:transaction
097: * type="Required"
098: *
099: * @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,
107: SessionBean {
108: private Logger m_log;
109: private SessionContext m_ctx;
110: private String 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: return getBroker().getDescriptorRepository();
120: }
121:
122: /**
123: * @ejb:interface-method
124: * @see org.apache.ojb.broker.PersistenceBroker#getPBKey()
125: */
126: public PBKey getPBKey() {
127: return getBroker().getPBKey();
128: }
129:
130: /**
131: * @ejb:interface-method
132: * @see org.apache.ojb.broker.PersistenceBroker#delete(Object)
133: */
134: public void delete(Object obj) throws PersistenceBrokerException {
135: getBroker().delete(obj);
136: }
137:
138: /**
139: * @ejb:interface-method
140: * @see org.apache.ojb.broker.PersistenceBroker#deleteByQuery(Query)
141: */
142: public void deleteByQuery(Query query)
143: throws PersistenceBrokerException {
144: getBroker().deleteByQuery(query);
145: }
146:
147: /**
148: * @ejb:interface-method
149: * @see org.apache.ojb.broker.PersistenceBroker#removeFromCache(Object)
150: */
151: public void removeFromCache(Object obj)
152: throws PersistenceBrokerException {
153: getBroker().removeFromCache(obj);
154: }
155:
156: /**
157: * @ejb:interface-method
158: * @see org.apache.ojb.broker.PersistenceBroker#clearCache()
159: */
160: public void clearCache() throws PersistenceBrokerException {
161: getBroker().clearCache();
162: }
163:
164: /**
165: * @ejb:interface-method
166: * @see org.apache.ojb.broker.PersistenceBroker#store(Object)
167: */
168: public void store(Object obj) throws PersistenceBrokerException {
169: getBroker().store(obj);
170: }
171:
172: /**
173: * @ejb:interface-method
174: * @see org.apache.ojb.broker.PersistenceBroker#abortTransaction()
175: */
176: public void abortTransaction()
177: throws TransactionNotInProgressException {
178: getBroker().abortTransaction();
179: }
180:
181: /**
182: * @ejb:interface-method
183: * @see org.apache.ojb.broker.PersistenceBroker#beginTransaction()
184: */
185: public void beginTransaction()
186: throws TransactionInProgressException,
187: TransactionAbortedException {
188: getBroker().beginTransaction();
189: }
190:
191: /**
192: * @ejb:interface-method
193: * @see org.apache.ojb.broker.PersistenceBroker#commitTransaction()
194: */
195: public void commitTransaction()
196: throws TransactionNotInProgressException,
197: TransactionAbortedException {
198: getBroker().commitTransaction();
199: }
200:
201: /**
202: * @ejb:interface-method
203: * @see org.apache.ojb.broker.PersistenceBroker#isInTransaction()
204: */
205: public boolean isInTransaction() throws PersistenceBrokerException {
206: return getBroker().isInTransaction();
207: }
208:
209: /**
210: * @ejb:interface-method
211: * @see org.apache.ojb.broker.PersistenceBroker#close()
212: */
213: public boolean close() {
214: return getBroker().close();
215: }
216:
217: public boolean isClosed() {
218: return getBroker().isClosed();
219: }
220:
221: /**
222: * @ejb:interface-method
223: * @see org.apache.ojb.broker.PersistenceBroker#getCollectionByQuery(Query)
224: */
225: public Collection getCollectionByQuery(Query query)
226: throws PersistenceBrokerException {
227: return getBroker().getCollectionByQuery(query);
228: }
229:
230: /**
231: * @ejb:interface-method
232: * @see org.apache.ojb.broker.PersistenceBroker#getCount(Query)
233: */
234: public int getCount(Query query) throws PersistenceBrokerException {
235: return getBroker().getCount(query);
236: }
237:
238: /**
239: * @ejb:interface-method
240: * @see org.apache.ojb.broker.PersistenceBroker#getCollectionByQuery(Class, Query)
241: */
242: public ManageableCollection getCollectionByQuery(
243: Class collectionClass, Query query)
244: throws PersistenceBrokerException {
245: return getBroker().getCollectionByQuery(collectionClass, query);
246: }
247:
248: /**
249: * @ejb:interface-method
250: * @see org.apache.ojb.broker.PersistenceBroker#getIteratorByQuery(Query)
251: */
252: public Iterator getIteratorByQuery(Query query)
253: throws PersistenceBrokerException {
254: return getBroker().getIteratorByQuery(query);
255: }
256:
257: /**
258: * @ejb:interface-method
259: * @see org.apache.ojb.broker.PersistenceBroker#getReportQueryIteratorByQuery(Query)
260: */
261: public Iterator getReportQueryIteratorByQuery(Query query)
262: throws PersistenceBrokerException {
263: return getBroker().getReportQueryIteratorByQuery(query);
264: }
265:
266: /**
267: * @ejb:interface-method
268: * @see org.apache.ojb.broker.PersistenceBroker#getObjectByIdentity(Identity)
269: */
270: public Object getObjectByIdentity(Identity id)
271: throws PersistenceBrokerException {
272: return getBroker().getObjectByIdentity(id);
273: }
274:
275: /**
276: * @ejb:interface-method
277: * @see org.apache.ojb.broker.PersistenceBroker#getObjectByQuery(Query)
278: */
279: public Object getObjectByQuery(Query query)
280: throws PersistenceBrokerException {
281: return getBroker().getObjectByQuery(query);
282: }
283:
284: /**
285: * @ejb:interface-method
286: * @see org.apache.ojb.broker.PersistenceBroker#getPKEnumerationByQuery(Class, Query)
287: */
288: public Enumeration getPKEnumerationByQuery(Class PrimaryKeyClass,
289: Query query) throws PersistenceBrokerException {
290: return getBroker().getPKEnumerationByQuery(PrimaryKeyClass,
291: query);
292: }
293:
294: /**
295: * @ejb:interface-method
296: * @see org.apache.ojb.broker.PersistenceBroker#store(Object, ObjectModification)
297: */
298: public void store(Object obj, ObjectModification modification)
299: throws PersistenceBrokerException {
300: getBroker().store(obj, modification);
301: }
302:
303: /**
304: * @ejb:interface-method
305: * @see org.apache.ojb.broker.PersistenceBroker#getClassDescriptor(Class)
306: */
307: public ClassDescriptor getClassDescriptor(Class clazz)
308: throws PersistenceBrokerException {
309: return getBroker().getClassDescriptor(clazz);
310: }
311:
312: /**
313: * @ejb:interface-method
314: * @see org.apache.ojb.broker.PersistenceBroker#hasClassDescriptor(Class)
315: */
316: public boolean hasClassDescriptor(Class clazz) {
317: return getBroker().hasClassDescriptor(clazz);
318: }
319:
320: /**
321: * @ejb:interface-method
322: * @see org.apache.ojb.broker.PersistenceBroker#getTopLevelClass(Class)
323: */
324: public Class getTopLevelClass(Class clazz)
325: throws PersistenceBrokerException {
326: return getBroker().getTopLevelClass(clazz);
327: }
328:
329: /**
330: * @ejb:interface-method
331: * @see org.apache.ojb.broker.PersistenceBroker#serviceStatementManager()
332: */
333: public StatementManagerIF serviceStatementManager() {
334: return getBroker().serviceStatementManager();
335: }
336:
337: /**
338: * @ejb:interface-method
339: * @see org.apache.ojb.broker.PersistenceBroker#serviceConnectionManager()
340: */
341: public ConnectionManagerIF serviceConnectionManager() {
342: return getBroker().serviceConnectionManager();
343: }
344:
345: public JdbcAccess serviceJdbcAccess() {
346: return getBroker().serviceJdbcAccess();
347: }
348:
349: /**
350: * @ejb:interface-method
351: * @see org.apache.ojb.broker.PersistenceBroker#serviceSqlGenerator()
352: */
353: public SqlGenerator serviceSqlGenerator() {
354: return getBroker().serviceSqlGenerator();
355: }
356:
357: public SequenceManager serviceSequenceManager() {
358: return getBroker().serviceSequenceManager();
359: }
360:
361: public BrokerHelper serviceBrokerHelper() {
362: return getBroker().serviceBrokerHelper();
363: }
364:
365: public ObjectCache serviceObjectCache() {
366: return getBroker().serviceObjectCache();
367: }
368:
369: public IdentityFactory serviceIdentity() {
370: return getBroker().serviceIdentity();
371: }
372:
373: public void fireBrokerEvent(PersistenceBrokerEvent event) {
374: getBroker().fireBrokerEvent(event);
375: }
376:
377: public void fireBrokerEvent(PBLifeCycleEvent event) {
378: getBroker().fireBrokerEvent(event);
379: }
380:
381: public void fireBrokerEvent(PBStateEvent event) {
382: getBroker().fireBrokerEvent(event);
383: }
384:
385: public ProxyFactory getProxyFactory() {
386: return getBroker().getProxyFactory();
387: }
388:
389: public Object createProxy(Class proxyClass,
390: Identity realSubjectsIdentity) {
391: return getBroker()
392: .createProxy(proxyClass, realSubjectsIdentity);
393: }
394:
395: /**
396: * @ejb:interface-method
397: * @see org.apache.ojb.broker.PersistenceBroker#addListener(PBListener)
398: */
399: public void addListener(PBListener listener)
400: throws PersistenceBrokerException {
401: getBroker().addListener(listener);
402: }
403:
404: /**
405: * @ejb:interface-method
406: * @see org.apache.ojb.broker.PersistenceBroker#addListener(PBListener, boolean)
407: */
408: public void addListener(PBListener listener, boolean permanent)
409: throws PersistenceBrokerException {
410: getBroker().addListener(listener, permanent);
411: }
412:
413: /**
414: * @ejb:interface-method
415: * @see org.apache.ojb.broker.PersistenceBroker#removeListener(PBListener)
416: */
417: public void removeListener(PBListener listener)
418: throws PersistenceBrokerException {
419: getBroker().removeListener(listener);
420: }
421:
422: /**
423: * @ejb:interface-method
424: * @see org.apache.ojb.broker.PersistenceBroker#retrieveAllReferences(Object)
425: */
426: public void retrieveAllReferences(Object pInstance)
427: throws PersistenceBrokerException {
428: getBroker().retrieveAllReferences(pInstance);
429: }
430:
431: /**
432: * @ejb:interface-method
433: * @see org.apache.ojb.broker.PersistenceBroker#retrieveReference(Object, String)
434: */
435: public void retrieveReference(Object pInstance,
436: String pAttributeName) throws PersistenceBrokerException {
437: getBroker().retrieveReference(pInstance, pAttributeName);
438: }
439:
440: public void removeAllListeners(boolean permanent)
441: throws PersistenceBrokerException {
442: getBroker().removeAllListeners(permanent);
443: }
444:
445: /**
446: * @ejb:interface-method
447: * @see org.apache.ojb.broker.PersistenceBroker#removeAllListeners()
448: */
449: public void removeAllListeners() throws PersistenceBrokerException {
450: getBroker().removeAllListeners();
451: }
452:
453: /**
454: * @ejb:interface-method
455: * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
456: */
457: public void configure(Configuration pConfig)
458: throws ConfigurationException {
459: getBroker().configure(pConfig);
460: }
461:
462: /**
463: * @ejb:interface-method
464: * @see org.odbms.ObjectContainer#query()
465: */
466: public org.odbms.Query query() {
467: return getBroker().query();
468: }
469:
470: private void ojbPrepare() {
471: if (m_log.isDebugEnabled())
472: m_log.info("PersistenceBrokerBean: ejbActivate was called");
473: Context context = null;
474: // Lookup if a environment entry for repository exists
475: String ojbRepository = null;
476: try {
477: context = new InitialContext();
478: ojbRepository = (String) context
479: .lookup("java:comp/env/ojb.repository");
480: } catch (NamingException e) {
481: m_log.error("Lookup for ojb repository failed", e);
482: }
483: // no repository found in environment, use default one
484: if (ojbRepository == null || ojbRepository.equals("")) {
485: m_log
486: .info("No enviroment entry was found, use default repository");
487: ojbRepository = "repository.xml";
488: }
489: m_log.info("Use OJB repository file: " + ojbRepository);
490: m_pbKey = new PBKey(ojbRepository);
491:
492: // Lookup the PBF implementation
493: try {
494: context = new InitialContext();
495: m_pbf = ((PBFactoryIF) context
496: .lookup(PBFactoryIF.PBFACTORY_JNDI_NAME))
497: .getInstance();
498: } catch (NamingException e) {
499: m_log
500: .error(
501: "Lookup for PersistenceBrokerFactory failed",
502: e);
503: throw new PersistenceBrokerException(e);
504: }
505: }
506:
507: public void ejbActivate() throws EJBException, RemoteException {
508: m_log = LoggerFactory.getLogger(PersistenceBrokerBean.class);
509: ojbPrepare();
510: }
511:
512: public void ejbPassivate() throws EJBException, RemoteException {
513: m_log = null;
514: m_pbf = null;
515: }
516:
517: public void ejbRemove() throws EJBException, RemoteException {
518: m_ctx = null;
519: }
520:
521: public void setSessionContext(SessionContext sessionContext)
522: throws EJBException, RemoteException {
523: m_ctx = sessionContext;
524: }
525:
526: private PersistenceBrokerInternal getBroker() {
527: return m_pbf.createPersistenceBroker(m_pbKey);
528: }
529:
530: /**
531: * @see org.apache.ojb.broker.PersistenceBroker#deleteMtoNImplementor()
532: */
533: public void deleteMtoNImplementor(MtoNImplementor m2nImpl)
534: throws PersistenceBrokerException {
535: throw new UnsupportedOperationException();
536: }
537:
538: /**
539: * @see org.apache.ojb.broker.PersistenceBroker#addMtoNImplementor()
540: */
541: public void addMtoNImplementor(MtoNImplementor m2nImpl)
542: throws PersistenceBrokerException {
543: throw new UnsupportedOperationException();
544: }
545:
546: }
|