KickJava   Java API By Example, From Geeks To Geeks.

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


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.CreateException JavaDoc;
20 import javax.ejb.EJBException JavaDoc;
21 import javax.ejb.SessionBean JavaDoc;
22 import javax.naming.Context JavaDoc;
23 import javax.naming.InitialContext JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.ojb.broker.PersistenceBroker;
31 import org.apache.ojb.broker.query.Criteria;
32 import org.apache.ojb.broker.query.Query;
33 import org.apache.ojb.broker.query.QueryByCriteria;
34 import org.apache.ojb.broker.util.logging.Logger;
35 import org.apache.ojb.broker.util.logging.LoggerFactory;
36 import org.apache.ojb.ejb.ArticleVO;
37 import org.apache.ojb.ejb.PersonVO;
38
39 /**
40  * This is an session bean implementation used for testing different "rollback"
41  * scenarios for the PB-api. The most important directive when using the PB-api within
42  * EJB beans is in any case to close the used PB instance after use within the bean method.
43  * Have a look in {@link PBBaseBeanImpl}.
44  *
45  *
46  * @ejb:bean type="Stateless"
47  * name="RollbackBeanPB"
48  * jndi-name="org.apache.ojb.ejb.pb.RollbackBean"
49  * local-jndi-name="org.apache.ojb.ejb.pb.RollbackBeanLocal"
50  * view-type="both"
51  * transaction-type="Container"
52  *
53  * @ejb:interface remote-class="org.apache.ojb.ejb.pb.RollbackRemote"
54  * local-class="org.apache.ojb.ejb.pb.RollbackLocal"
55  * extends="javax.ejb.EJBObject"
56  *
57  * @ejb:home remote-class="org.apache.ojb.ejb.pb.RollbackHome"
58  * local-class="org.apache.ojb.ejb.pb.RollbackLocalHome"
59  * extends="javax.ejb.EJBHome"
60  *
61  * @ejb:ejb-ref
62  * ejb-name="PersonManagerPBBean"
63  * view-type="local"
64  * ref-name="ejb/ojb/pb/PersonManager"
65  *
66  * @ejb:ejb-ref
67  * ejb-name="ArticleManagerPBBean"
68  * view-type="local"
69  * ref-name="ejb/ojb/pb/ArticleManager"
70  *
71  * @ejb:transaction type="Required"
72  *
73  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
74  * @version $Id: RollbackBean.java,v 1.1.2.3 2005/12/21 22:21:38 tomdz Exp $
75  */

76 public class RollbackBean extends PBBaseBeanImpl implements SessionBean JavaDoc
77 {
78     private Logger log = LoggerFactory.getLogger(RollbackBean.class);
79
80     private static final String JavaDoc PERSON_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/pb/PersonManager";
81     private static final String JavaDoc ARTICLE_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/pb/ArticleManager";
82
83     private ArticleManagerPBLocal am;
84     private PersonManagerPBLocal pm;
85
86     public RollbackBean()
87     {
88     }
89
90     /**
91      * First stores all articles, persons form
92      * the lists using ArticleManager and PersonManager
93      * beans after doing that, a Exception will be thrown.
94      *
95      * @ejb:interface-method
96      */

97     public void rollbackOtherBeanUsing(List JavaDoc articles, List JavaDoc persons)
98     {
99         log.info("rollbackOtherBeanUsing method was called");
100         //store all objects
101
ArticleManagerPBLocal am = getArticleManager();
102         PersonManagerPBLocal pm = getPersonManager();
103         am.storeArticles(articles);
104         pm.storePersons(persons);
105
106         // after all is done we throw an exception to activate rollback process
107
throw new EJBException JavaDoc("## Testing of rollback behaviour - rollbackOtherBeanUsing ##");
108     }
109
110     /**
111      * First store a list of persons then we
112      * store the article using a failure store
113      * method in ArticleManager.
114      *
115      * @ejb:interface-method
116      */

117     public void rollbackOtherBeanUsing_2(ArticleVO article, List JavaDoc persons)
118     {
119         log.info("rollbackOtherBeanUsing_2 method was called");
120         ArticleManagerPBLocal am = getArticleManager();
121         PersonManagerPBLocal pm = getPersonManager();
122         pm.storePersons(persons);
123         am.failureStore(article);
124     }
125
126     /**
127      * This test method expect an invalid object in the person list,
128      * so that OJB cause an internal error.
129      *
130      * @ejb:interface-method
131      */

132     public void rollbackClientWrongInput(List JavaDoc articles, List JavaDoc persons)
133     {
134         log.info("rollbackClientWrongInput method was called");
135         ArticleManagerPBLocal am = getArticleManager();
136         PersonManagerPBLocal pm = getPersonManager();
137         am.storeArticles(articles);
138         pm.storePersons(persons);
139     }
140
141     /**
142      * The bean will throw an exception before the method ends.
143      *
144      * @ejb:interface-method
145      */

146     public void rollbackThrowException(List JavaDoc objects)
147     {
148         log.info("rollbackThrowException method was called");
149         storeObjects(objects);
150         // now we throw an exception
151
throw new EJBException JavaDoc("## Testing of rollback behaviour - rollbackThrowException ##");
152     }
153
154     /**
155      * One of the objects passed by the client will cause an exception.
156      *
157      * @ejb:interface-method
158      */

159     public void rollbackPassInvalidObject(List JavaDoc objects)
160     {
161         log.info("rollbackPassInvalidObject method was called");
162         storeObjects(objects);
163     }
164
165     /**
166      * We do call ctx.setRollbackOnly and do odmg-tx.abort() call.
167      *
168      * @ejb:interface-method
169      */

170     public void rollbackSetRollbackOnly(List JavaDoc objects)
171     {
172         log.info("rollbackSetRollbackOnly method was called");
173         storeObjects(objects);
174         /*
175         setRollbackOnly does only rollback the transaction, the client will not be
176         notified by an RemoteException (tested on JBoss)
177         */

178         getSessionContext().setRollbackOnly();
179     }
180
181     /**
182      * We do call ctx.setRollbackOnly and do odmg-tx.abort() call.
183      *
184      * @ejb:interface-method
185      */

186     public void rollbackSetRollbackAndThrowException(List JavaDoc objects)
187     {
188         log.info("rollbackSetRollbackAndThrowException method was called");
189         storeObjects(objects);
190         getSessionContext().setRollbackOnly();
191         // to notify the client about the failure we throw an exception
192
// if we don't throw such an exception the client don't get notified
193
// about the failure
194
throw new EJBException JavaDoc("## Testing of rollback behaviour - rollbackSetRollbackAndThrowException ##");
195     }
196
197     /**
198      * We use several OJB services, start to iterate a query result and do
199      * an odmg-tx.abort call.
200      *
201      * @ejb:interface-method
202      */

203     public void rollbackBreakIteration(List JavaDoc objectsToStore)
204     {
205         // now we mix up different api's and use PB-api too
206
log.info("rollbackBreakIteration");
207         /*
208         store list of objects, then get these objects with Iterator, start
209         iteration, then break
210         */

211         storeObjects(objectsToStore);
212         Class JavaDoc searchClass = objectsToStore.get(0).getClass();
213         PersistenceBroker broker = getBroker();
214         try
215         {
216             Query q = new QueryByCriteria(searchClass);
217             // we get the iterator and step into the first found object
218
Iterator JavaDoc it = broker.getIteratorByQuery(q);
219             it.next();
220         }
221         /*
222         Now we want to break iteration or something wrong. In this case we have to
223         cleanup the used PB instance by a close call
224         */

225         finally
226         {
227             if(broker != null) broker.close();
228         }
229
230         // to notify the client about the failure we throw an exception
231
// if we don't throw such an exception the client don't get notified
232
// about the failure
233
throw new EJBException JavaDoc("## Testing of rollback behaviour - rollbackBreakIteration ##");
234     }
235
236     /**
237      * @ejb:interface-method
238      */

239     public List JavaDoc storeObjects(List JavaDoc objects)
240     {
241         return new ArrayList JavaDoc(super.storeObjects(objects));
242     }
243
244     /**
245      * @ejb:interface-method
246      */

247     public void deleteObjects(List JavaDoc objects)
248     {
249         log.info("deleteObjects");
250         super.deleteObjects(objects);
251     }
252
253     /**
254      * @ejb:interface-method
255      */

256     public int getArticleCount()
257     {
258         log.info("getArticleCount was called");
259         return getCount(ArticleVO.class);
260     }
261
262     /**
263      * @ejb:interface-method
264      */

265     public int getPersonCount()
266     {
267         log.info("getPersonCount was called");
268         return getCount(PersonVO.class);
269     }
270
271     /**
272      * @ejb:interface-method
273      */

274     public Collection JavaDoc getAllObjects(Class JavaDoc target)
275     {
276         if(log.isDebugEnabled()) log.debug("getAllObjects was called");
277         return super.getAllObjects(target);
278     }
279
280     private ArticleManagerPBLocal getArticleManager()
281     {
282         if (am == null)
283         {
284             Context JavaDoc context = null;
285             try
286             {
287                 context = new InitialContext JavaDoc();
288                 am = ((ArticleManagerPBLocalHome) context.lookup(ARTICLE_MANAGER_EJB_REF_NAME)).create();
289                 log.info("** Found bean: " + am);
290                 return am;
291             }
292             catch (NamingException JavaDoc e)
293             {
294                 log.error("Lookup using ejb-ref " + ARTICLE_MANAGER_EJB_REF_NAME + " failed", e);
295                 throw new EJBException JavaDoc(e);
296             }
297             catch (CreateException JavaDoc e)
298             {
299                 log.error("Creation of ArticleManager failed", e);
300                 throw new EJBException JavaDoc(e);
301             }
302         }
303         return am;
304     }
305
306     private PersonManagerPBLocal getPersonManager()
307     {
308         if (pm == null)
309         {
310             Context JavaDoc context = null;
311             try
312             {
313                 context = new InitialContext JavaDoc();
314                 pm = ((PersonManagerPBLocalHome) context.lookup(PERSON_MANAGER_EJB_REF_NAME)).create();
315                 log.info("** Found bean: " + pm);
316                 return pm;
317             }
318             catch (NamingException JavaDoc e)
319             {
320                 log.error("Lookup using ejb-ref " + PERSON_MANAGER_EJB_REF_NAME + " failed", e);
321                 throw new EJBException JavaDoc(e);
322             }
323             catch (CreateException JavaDoc e)
324             {
325                 log.error("Creation of PersonManager failed", e);
326                 throw new EJBException JavaDoc(e);
327             }
328         }
329         return pm;
330     }
331 }
332
Popular Tags