001: package org.apache.ojb.ejb.pb;
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 java.util.Collection;
019: import java.util.Iterator;
020:
021: import org.apache.ojb.broker.PBKey;
022: import org.apache.ojb.broker.PersistenceBroker;
023: import org.apache.ojb.broker.core.PersistenceBrokerFactoryFactory;
024: import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF;
025: import org.apache.ojb.broker.query.Query;
026: import org.apache.ojb.broker.query.QueryByCriteria;
027: import org.apache.ojb.broker.util.logging.Logger;
028: import org.apache.ojb.broker.util.logging.LoggerFactory;
029: import org.apache.ojb.ejb.SessionBeanImpl;
030:
031: /**
032: * Base class for using OJB-PB api within SessionBeans,
033: * subclass this class to implement your own bean
034: * implementations.
035: *
036: * <br>
037: * Use the {@link #getBroker} method to obtain a
038: * PersistenceBroker instance, do PB.close() after
039: * using.
040: *
041: * <br>
042: * Additionally there are some basic methods for
043: * storing, deleting, counting, get all objects
044: * implemented.
045: *
046: *
047: *
048: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
049: * @version $Id: PBBaseBeanImpl.java,v 1.4.2.2 2005/12/21 22:21:38 tomdz Exp $
050: */
051: public abstract class PBBaseBeanImpl extends SessionBeanImpl {
052: private Logger log = LoggerFactory.getLogger(PBBaseBeanImpl.class);
053: private PersistenceBrokerFactoryIF pbf;
054:
055: public void ejbRemove() {
056: super .ejbRemove();
057: // we do explicit cleanup (not necessary)
058: pbf = null;
059: log = null;
060: }
061:
062: public void ejbCreate() {
063: if (log.isDebugEnabled())
064: log.info("ejbCreate was called");
065: pbf = PersistenceBrokerFactoryFactory.instance();
066: }
067:
068: /**
069: * Return a PersistenceBroker instance.
070: */
071: public PersistenceBroker getBroker() {
072: return pbf.defaultPersistenceBroker();
073: }
074:
075: /**
076: * Return a PersistenceBroker instance for
077: * the given PBKey.
078: */
079: public PersistenceBroker getBroker(PBKey key) {
080: return pbf.createPersistenceBroker(key);
081: }
082:
083: /**
084: * Return the count of all objects found
085: * for given class.
086: */
087: public int getCount(Class target) {
088: PersistenceBroker broker = getBroker();
089: int result;
090: try {
091: result = broker.getCount(new QueryByCriteria(target));
092: } finally {
093: if (broker != null)
094: broker.close();
095: }
096: return result;
097: }
098:
099: /**
100: * Return all objects for the given class.
101: */
102: public Collection getAllObjects(Class target) {
103: PersistenceBroker broker = getBroker();
104: Collection result;
105: try {
106: Query q = new QueryByCriteria(target);
107: result = broker.getCollectionByQuery(q);
108: } finally {
109: if (broker != null)
110: broker.close();
111: }
112: return result;
113: }
114:
115: /**
116: * Store an object.
117: */
118: public Object storeObject(Object object) {
119: PersistenceBroker broker = getBroker();
120: try {
121: broker.store(object);
122: } finally {
123: if (broker != null)
124: broker.close();
125: }
126: return object;
127: }
128:
129: /**
130: * Delete an object.
131: */
132: public void deleteObject(Object object) {
133: PersistenceBroker broker = null;
134: try {
135: broker = getBroker();
136: broker.delete(object);
137: } finally {
138: if (broker != null)
139: broker.close();
140: }
141: }
142:
143: /**
144: * Store a collection of objects.
145: */
146: public Collection storeObjects(Collection objects) {
147: PersistenceBroker broker = null;
148: Collection stored;
149: try {
150: broker = getBroker();
151: stored = this .storeObjects(broker, objects);
152: } finally {
153: if (broker != null)
154: broker.close();
155: }
156: return stored;
157: }
158:
159: /**
160: * Delete a Collection of objects.
161: */
162: public void deleteObjects(Collection objects) {
163: PersistenceBroker broker = null;
164: try {
165: broker = getBroker();
166: this .deleteObjects(broker, objects);
167: } finally {
168: if (broker != null)
169: broker.close();
170: }
171: }
172:
173: protected Collection storeObjects(PersistenceBroker broker,
174: Collection objects) {
175: for (Iterator it = objects.iterator(); it.hasNext();) {
176: broker.store(it.next());
177: }
178: return objects;
179: }
180:
181: protected void deleteObjects(PersistenceBroker broker,
182: Collection objects) {
183: for (Iterator it = objects.iterator(); it.hasNext();) {
184: broker.delete(it.next());
185: }
186: }
187: }
|