KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 import javax.ejb.SessionBean JavaDoc;
20 import javax.ejb.SessionContext JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.ojb.broker.PersistenceBroker;
26 import org.apache.ojb.broker.core.PersistenceBrokerFactoryFactory;
27 import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF;
28 import org.apache.ojb.broker.query.Criteria;
29 import org.apache.ojb.broker.query.Query;
30 import org.apache.ojb.broker.query.QueryByCriteria;
31 import org.apache.ojb.broker.util.logging.Logger;
32 import org.apache.ojb.broker.util.logging.LoggerFactory;
33 import org.apache.ojb.ejb.ArticleVO;
34 import org.apache.ojb.ejb.PersonVO;
35
36 /**
37  * This is an session bean implementation using PB-api.
38  * <br>
39  * For more structured implementations take a look at<br/>
40  * {@link org.apache.ojb.ejb.SessionBeanImpl}<br/>
41  * {@link org.apache.ojb.ejb.odmg.ODMGBaseBeanImpl}<br/>
42  * {@link org.apache.ojb.ejb.pb.PBBaseBeanImpl}<br/>
43  *
44  * @ejb:bean
45  * type="Stateless"
46  * name="PBSessionBean"
47  * jndi-name="org.apache.ojb.ejb.pb.PBSessionBean"
48  * local-jndi-name="org.apache.ojb.ejb.pb.PBSessionBeanLocal"
49  * view-type="both"
50  * transaction-type="Container"
51  *
52  * @ejb:interface
53  * remote-class="org.apache.ojb.ejb.pb.PBSessionRemote"
54  * local-class="org.apache.ojb.ejb.pb.PBSessionLocal"
55  * extends="javax.ejb.EJBObject"
56  *
57  * @ejb:home
58  * remote-class="org.apache.ojb.ejb.pb.PBSessionHome"
59  * local-class="org.apache.ojb.ejb.pb.PBSessionLocalHome"
60  * extends="javax.ejb.EJBHome"
61  *
62  * @ejb:transaction
63  * type="Required"
64  *
65  * @jonas.bean
66  * ejb-name="org.apache.ojb.ejb.pb.PBSessionBean"
67  * jndi-name="org.apache.ojb.ejb.pb.PBSessionBean"
68  *
69  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
70  * @version $Id: PBSessionBean.java,v 1.5.2.3 2005/12/21 22:21:38 tomdz Exp $
71  */

72 public class PBSessionBean implements SessionBean JavaDoc
73 {
74     private Logger log = LoggerFactory.getLogger(PBSessionBean.class);
75     private SessionContext JavaDoc ctx;
76     private PersistenceBrokerFactoryIF pbf;
77
78     public PBSessionBean()
79     {
80     }
81
82     public void ejbActivate()
83     {/* unused */
84     }
85
86     public void ejbPassivate()
87     {/* unused */
88     }
89
90     public void setSessionContext(SessionContext JavaDoc ctx)
91     {
92         this.ctx = ctx;
93     }
94
95     public SessionContext JavaDoc getSessionContext()
96     {
97         return ctx;
98     }
99
100     public void ejbRemove()
101     {
102         ctx = null;
103     }
104
105     public void ejbCreate()
106     {
107         log.info("ejbCreate was called");
108         pbf = PersistenceBrokerFactoryFactory.instance();
109     }
110
111     protected PersistenceBroker getBroker()
112     {
113         if(log.isDebugEnabled()) log.debug("getBroker was called");
114         return pbf.defaultPersistenceBroker();
115     }
116
117     protected List JavaDoc storeObjects(PersistenceBroker broker, List JavaDoc objects)
118     {
119         for (Iterator JavaDoc it = objects.iterator(); it.hasNext();)
120         {
121             broker.store(it.next());
122         }
123         return objects;
124     }
125
126     protected void deleteObjects(PersistenceBroker broker, List JavaDoc objects)
127     {
128         for (Iterator JavaDoc it = objects.iterator(); it.hasNext();)
129         {
130             broker.delete(it.next());
131         }
132     }
133
134     protected int getCount(Class JavaDoc target)
135     {
136         PersistenceBroker broker = getBroker();
137         int result = broker.getCount(new QueryByCriteria(target));
138         broker.close();
139         return result;
140     }
141
142     /**
143      * @ejb:interface-method
144      */

145     public Collection JavaDoc getAllObjects(Class JavaDoc target)
146     {
147         if(log.isDebugEnabled()) log.debug("getAllObjects was called");
148         PersistenceBroker broker = getBroker();
149         Query q = new QueryByCriteria(target);
150         Collection JavaDoc result = broker.getCollectionByQuery(q);
151         broker.close();
152         return result;
153     }
154
155     /**
156      * @ejb:interface-method
157      */

158     public Iterator JavaDoc iterateAllObjects(Class JavaDoc target)
159     {
160         if(log.isDebugEnabled()) log.debug("getAllObjects was called");
161         PersistenceBroker broker = getBroker();
162         Query q = new QueryByCriteria(target);
163         Iterator JavaDoc result = broker.getIteratorByQuery(q);
164         broker.close();
165         return result;
166     }
167
168     /**
169      * @ejb:interface-method
170      */

171     public int getArticleCount()
172     {
173         if(log.isDebugEnabled()) log.debug("getArticleCount was called");
174         return getCount(ArticleVO.class);
175     }
176
177     /**
178      * @ejb:interface-method
179      */

180     public Collection JavaDoc getArticlesByName(String JavaDoc articleName)
181     {
182         if(log.isDebugEnabled()) log.debug("getArticlesByName was called");
183         PersistenceBroker broker = getBroker();
184         Criteria crit = new Criteria();
185         crit.addLike("name", articleName);
186         Query q = new QueryByCriteria(ArticleVO.class, crit);
187         Collection JavaDoc result = broker.getCollectionByQuery(q);
188         broker.close();
189         return result;
190     }
191
192     /**
193      * @ejb:interface-method
194      */

195     public int getPersonCount()
196     {
197         if(log.isDebugEnabled()) log.debug("getPersonCount was called");
198         return getCount(PersonVO.class);
199     }
200
201     /**
202      * @ejb:interface-method
203      */

204     public List JavaDoc storeObjects(List JavaDoc objects)
205     {
206         if(log.isDebugEnabled()) log.debug("storeObjects was called");
207         PersistenceBroker broker = getBroker();
208         List JavaDoc stored = this.storeObjects(broker, objects);
209         broker.close();
210         return stored;
211     }
212
213     /**
214      * @ejb:interface-method
215      */

216     public void deleteObjects(List JavaDoc objects)
217     {
218         if(log.isDebugEnabled()) log.debug("deleteObjects was called");
219         PersistenceBroker broker = getBroker();
220         this.deleteObjects(broker, objects);
221         broker.close();
222     }
223
224     /**
225      * @ejb:interface-method
226      */

227     public boolean allInOne(List JavaDoc articles, List JavaDoc persons)
228     {
229         if(log.isDebugEnabled()) log.debug("allInOne was called");
230         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
231         boolean passedWell = true;
232         String JavaDoc sep = System.getProperty("line.separator");
233
234         int personsBefore = getPersonCount();
235         int articlesBefore = getArticleCount();
236         buf.append(sep + "# Start with " + personsBefore + " persons");
237         buf.append(sep + "# Start with " + articlesBefore + " articles");
238         storeObjects(articles);
239         storeObjects(persons);
240         int personsAfterStore = getPersonCount();
241         int articlesAfterStore = getArticleCount();
242         buf.append(sep + "# After store: " + personsAfterStore + " persons");
243         buf.append(sep + "# After store: " + articlesAfterStore + " articles");
244         deleteObjects(articles);
245         deleteObjects(persons);
246         int personsAfterDelete = getPersonCount();
247         int articlesAfterDelete = getArticleCount();
248         buf.append(sep + "# After delete: " + personsAfterDelete + " persons");
249         buf.append(sep + "# After delete: " + articlesAfterDelete + " articles");
250         log.info("## allInOne-Method call: " + buf.toString());
251         passedWell = (personsBefore + persons.size()) == personsAfterStore &&
252                 (articlesBefore + articles.size()) == articlesAfterStore &&
253                 (personsBefore) == personsAfterDelete &&
254                 (personsBefore) == personsAfterDelete;
255         return passedWell;
256     }
257 }
258
Popular Tags