KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > ejb > pb > PBBaseBeanImpl


1 package org.apache.ojb.ejb.pb;
2
3 /* Copyright 2004-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.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.apache.ojb.broker.PBKey;
22 import org.apache.ojb.broker.PersistenceBroker;
23 import org.apache.ojb.broker.core.PersistenceBrokerFactoryFactory;
24 import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF;
25 import org.apache.ojb.broker.query.Query;
26 import org.apache.ojb.broker.query.QueryByCriteria;
27 import org.apache.ojb.broker.util.logging.Logger;
28 import org.apache.ojb.broker.util.logging.LoggerFactory;
29 import org.apache.ojb.ejb.SessionBeanImpl;
30
31 /**
32  * Base class for using OJB-PB api within SessionBeans,
33  * subclass this class to implement your own bean
34  * implementations.
35  *
36  * <br>
37  * Use the {@link #getBroker} method to obtain a
38  * PersistenceBroker instance, do PB.close() after
39  * using.
40  *
41  * <br>
42  * Additionally there are some basic methods for
43  * storing, deleting, counting, get all objects
44  * implemented.
45  *
46  *
47  *
48  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
49  * @version $Id: PBBaseBeanImpl.java,v 1.4.2.2 2005/12/21 22:21:38 tomdz Exp $
50  */

51 public abstract class PBBaseBeanImpl extends SessionBeanImpl
52 {
53     private Logger log = LoggerFactory.getLogger(PBBaseBeanImpl.class);
54     private PersistenceBrokerFactoryIF pbf;
55
56     public void ejbRemove()
57     {
58         super.ejbRemove();
59         // we do explicit cleanup (not necessary)
60
pbf = null;
61         log = null;
62     }
63
64     public void ejbCreate()
65     {
66         if (log.isDebugEnabled()) log.info("ejbCreate was called");
67         pbf = PersistenceBrokerFactoryFactory.instance();
68     }
69
70     /**
71      * Return a PersistenceBroker instance.
72      */

73     public PersistenceBroker getBroker()
74     {
75         return pbf.defaultPersistenceBroker();
76     }
77
78     /**
79      * Return a PersistenceBroker instance for
80      * the given PBKey.
81      */

82     public PersistenceBroker getBroker(PBKey key)
83     {
84         return pbf.createPersistenceBroker(key);
85     }
86
87     /**
88      * Return the count of all objects found
89      * for given class.
90      */

91     public int getCount(Class JavaDoc target)
92     {
93         PersistenceBroker broker = getBroker();
94         int result;
95         try
96         {
97             result = broker.getCount(new QueryByCriteria(target));
98         }
99         finally
100         {
101             if (broker != null) broker.close();
102         }
103         return result;
104     }
105
106     /**
107      * Return all objects for the given class.
108      */

109     public Collection JavaDoc getAllObjects(Class JavaDoc target)
110     {
111         PersistenceBroker broker = getBroker();
112         Collection JavaDoc result;
113         try
114         {
115             Query q = new QueryByCriteria(target);
116             result = broker.getCollectionByQuery(q);
117         }
118         finally
119         {
120             if (broker != null) broker.close();
121         }
122         return result;
123     }
124
125     /**
126      * Store an object.
127      */

128     public Object JavaDoc storeObject(Object JavaDoc object)
129     {
130         PersistenceBroker broker = getBroker();
131         try
132         {
133             broker.store(object);
134         }
135         finally
136         {
137             if (broker != null) broker.close();
138         }
139         return object;
140     }
141
142     /**
143      * Delete an object.
144      */

145     public void deleteObject(Object JavaDoc object)
146     {
147         PersistenceBroker broker = null;
148         try
149         {
150             broker = getBroker();
151             broker.delete(object);
152         }
153         finally
154         {
155             if (broker != null) broker.close();
156         }
157     }
158
159     /**
160      * Store a collection of objects.
161      */

162     public Collection JavaDoc storeObjects(Collection JavaDoc objects)
163     {
164         PersistenceBroker broker = null;
165         Collection JavaDoc stored;
166         try
167         {
168             broker = getBroker();
169             stored = this.storeObjects(broker, objects);
170         }
171         finally
172         {
173             if (broker != null) broker.close();
174         }
175         return stored;
176     }
177
178     /**
179      * Delete a Collection of objects.
180      */

181     public void deleteObjects(Collection JavaDoc objects)
182     {
183         PersistenceBroker broker = null;
184         try
185         {
186             broker = getBroker();
187             this.deleteObjects(broker, objects);
188         }
189         finally
190         {
191             if (broker != null) broker.close();
192         }
193     }
194
195     protected Collection JavaDoc storeObjects(PersistenceBroker broker, Collection JavaDoc objects)
196     {
197         for (Iterator JavaDoc it = objects.iterator(); it.hasNext();)
198         {
199             broker.store(it.next());
200         }
201         return objects;
202     }
203
204     protected void deleteObjects(PersistenceBroker broker, Collection JavaDoc objects)
205     {
206         for (Iterator JavaDoc it = objects.iterator(); it.hasNext();)
207         {
208             broker.delete(it.next());
209         }
210     }
211 }
212
Popular Tags