001: package org.apache.ojb.ejb.odmg;
002:
003: /* Copyright 2004-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 javax.ejb.EJBException;
019: import java.util.Collection;
020: import java.util.Iterator;
021:
022: import org.apache.ojb.broker.PersistenceBroker;
023: import org.apache.ojb.broker.query.QueryByCriteria;
024: import org.apache.ojb.broker.util.logging.Logger;
025: import org.apache.ojb.broker.util.logging.LoggerFactory;
026: import org.apache.ojb.ejb.SessionBeanImpl;
027: import org.apache.ojb.odmg.HasBroker;
028: import org.apache.ojb.odmg.TransactionExt;
029: import org.odmg.Database;
030: import org.odmg.Implementation;
031: import org.odmg.LockNotGrantedException;
032: import org.odmg.OQLQuery;
033: import org.odmg.Transaction;
034:
035: /**
036: * Base class for using OJB-ODMG api within SessionBeans,
037: * subclass this class to implement your own bean
038: * implementations.
039: *
040: * To keep this example as simple as possible, we lookup a static OJB ODMG
041: * implementation instance from an helper class.
042: * But it's recommended to bind an instances of the ODMG main/access classes in JNDI
043: * (at appServer start), open the database and lookup these instances instance via JNDI in
044: * ejbCreate(), instead of lookup a static instance on each bean creation.
045: *
046: * To get the {@link org.odmg.Database} or
047: * {@link org.odmg.Implementation} instance use
048: * the {@link #getDatabase} and {@link #getImplementation}
049: * methods.
050: *
051: * Additionally there are some basic methods for
052: * storing, deleting, counting, get all objects
053: * implemented.
054: *
055: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
056: * @version $Id: ODMGBaseBeanImpl.java,v 1.5.2.4 2005/12/21 22:21:39 tomdz Exp $
057: */
058: public abstract class ODMGBaseBeanImpl extends SessionBeanImpl {
059: private Logger log = LoggerFactory
060: .getLogger(ODMGBaseBeanImpl.class);
061: private Implementation odmg;
062:
063: /**
064: * Lookup the OJB ODMG implementation.
065: * It's recommended to bind an instance of the Implementation class in JNDI
066: * (at appServer start), open the database and lookup this instance via JNDI in
067: * ejbCreate().
068: */
069: public void ejbCreate() {
070: if (log.isDebugEnabled())
071: log.debug("ejbCreate was called");
072: odmg = ODMGHelper.getODMG();
073: }
074:
075: /**
076: * Here we do the OJB cleanup.
077: */
078: public void ejbRemove() {
079: super .ejbRemove();
080: odmg = null;
081: }
082:
083: /**
084: * Return the Database associated with
085: * this bean.
086: */
087: public Database getDatabase() {
088: return odmg.getDatabase(null);
089: }
090:
091: /**
092: * Return the Implementation instance associated
093: * with this bean.
094: */
095: public Implementation getImplementation() {
096: return odmg;
097: }
098:
099: /**
100: * Store an object.
101: */
102: public Object storeObject(Object object) {
103: /* One possibility of storing objects is to use the current transaction
104: associated with the container */
105: try {
106: TransactionExt tx = (TransactionExt) odmg
107: .currentTransaction();
108: tx.lock(object, Transaction.WRITE);
109: tx.markDirty(object);
110: } catch (LockNotGrantedException e) {
111: log.error("Failure while storing object " + object, e);
112: throw new EJBException("Failure while storing object", e);
113: }
114: return object;
115: }
116:
117: /**
118: * Delete an object.
119: */
120: public void deleteObject(Object object) {
121: getDatabase().deletePersistent(object);
122: }
123:
124: /**
125: * Store a collection of objects.
126: */
127: public Collection storeObjects(Collection objects) {
128: try {
129: /* One possibility of storing objects is to use the current transaction
130: associated with the container */
131: Transaction tx = odmg.currentTransaction();
132: for (Iterator iterator = objects.iterator(); iterator
133: .hasNext();) {
134: tx.lock(iterator.next(), Transaction.WRITE);
135: }
136: } catch (LockNotGrantedException e) {
137: log.error("Failure while storing objects " + objects, e);
138: throw new EJBException("Failure while storing objects", e);
139: }
140: return objects;
141: }
142:
143: /**
144: * Delete a Collection of objects.
145: */
146: public void deleteObjects(Collection objects) {
147: for (Iterator iterator = objects.iterator(); iterator.hasNext();) {
148: getDatabase().deletePersistent(iterator.next());
149: }
150: }
151:
152: /**
153: * Return the count of all objects found
154: * for given class, using the PB-api within
155: * ODMG - this may change in further versions.
156: */
157: public int getCount(Class target) {
158: PersistenceBroker broker = ((HasBroker) odmg
159: .currentTransaction()).getBroker();
160: int result = broker.getCount(new QueryByCriteria(target));
161: return result;
162: }
163:
164: public Collection getAllObjects(Class target) {
165: OQLQuery query = odmg.newOQLQuery();
166: try {
167: query.create("select allObjects from " + target.getName());
168: return (Collection) query.execute();
169: } catch (Exception e) {
170: log.error("OQLQuery failed", e);
171: throw new EJBException("OQLQuery failed", e);
172: }
173: }
174: }
|