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 javax.ejb.CreateException;
019: import javax.ejb.EJBException;
020: import javax.ejb.SessionBean;
021: import javax.naming.Context;
022: import javax.naming.InitialContext;
023: import javax.naming.NamingException;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: import org.apache.ojb.broker.PersistenceBroker;
030: import org.apache.ojb.broker.query.Criteria;
031: import org.apache.ojb.broker.query.Query;
032: import org.apache.ojb.broker.query.QueryByCriteria;
033: import org.apache.ojb.broker.util.logging.Logger;
034: import org.apache.ojb.broker.util.logging.LoggerFactory;
035: import org.apache.ojb.ejb.ArticleVO;
036: import org.apache.ojb.ejb.PersonVO;
037:
038: /**
039: * This is an session bean implementation used for testing different "rollback"
040: * scenarios for the PB-api. The most important directive when using the PB-api within
041: * EJB beans is in any case to close the used PB instance after use within the bean method.
042: * Have a look in {@link PBBaseBeanImpl}.
043: *
044: *
045: * @ejb:bean type="Stateless"
046: * name="RollbackBeanPB"
047: * jndi-name="org.apache.ojb.ejb.pb.RollbackBean"
048: * local-jndi-name="org.apache.ojb.ejb.pb.RollbackBeanLocal"
049: * view-type="both"
050: * transaction-type="Container"
051: *
052: * @ejb:interface remote-class="org.apache.ojb.ejb.pb.RollbackRemote"
053: * local-class="org.apache.ojb.ejb.pb.RollbackLocal"
054: * extends="javax.ejb.EJBObject"
055: *
056: * @ejb:home remote-class="org.apache.ojb.ejb.pb.RollbackHome"
057: * local-class="org.apache.ojb.ejb.pb.RollbackLocalHome"
058: * extends="javax.ejb.EJBHome"
059: *
060: * @ejb:ejb-ref
061: * ejb-name="PersonManagerPBBean"
062: * view-type="local"
063: * ref-name="ejb/ojb/pb/PersonManager"
064: *
065: * @ejb:ejb-ref
066: * ejb-name="ArticleManagerPBBean"
067: * view-type="local"
068: * ref-name="ejb/ojb/pb/ArticleManager"
069: *
070: * @ejb:transaction type="Required"
071: *
072: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
073: * @version $Id: RollbackBean.java,v 1.1.2.3 2005/12/21 22:21:38 tomdz Exp $
074: */
075: public class RollbackBean extends PBBaseBeanImpl implements SessionBean {
076: private Logger log = LoggerFactory.getLogger(RollbackBean.class);
077:
078: private static final String PERSON_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/pb/PersonManager";
079: private static final String ARTICLE_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/pb/ArticleManager";
080:
081: private ArticleManagerPBLocal am;
082: private PersonManagerPBLocal pm;
083:
084: public RollbackBean() {
085: }
086:
087: /**
088: * First stores all articles, persons form
089: * the lists using ArticleManager and PersonManager
090: * beans after doing that, a Exception will be thrown.
091: *
092: * @ejb:interface-method
093: */
094: public void rollbackOtherBeanUsing(List articles, List persons) {
095: log.info("rollbackOtherBeanUsing method was called");
096: //store all objects
097: ArticleManagerPBLocal am = getArticleManager();
098: PersonManagerPBLocal pm = getPersonManager();
099: am.storeArticles(articles);
100: pm.storePersons(persons);
101:
102: // after all is done we throw an exception to activate rollback process
103: throw new EJBException(
104: "## Testing of rollback behaviour - rollbackOtherBeanUsing ##");
105: }
106:
107: /**
108: * First store a list of persons then we
109: * store the article using a failure store
110: * method in ArticleManager.
111: *
112: * @ejb:interface-method
113: */
114: public void rollbackOtherBeanUsing_2(ArticleVO article, List persons) {
115: log.info("rollbackOtherBeanUsing_2 method was called");
116: ArticleManagerPBLocal am = getArticleManager();
117: PersonManagerPBLocal pm = getPersonManager();
118: pm.storePersons(persons);
119: am.failureStore(article);
120: }
121:
122: /**
123: * This test method expect an invalid object in the person list,
124: * so that OJB cause an internal error.
125: *
126: * @ejb:interface-method
127: */
128: public void rollbackClientWrongInput(List articles, List persons) {
129: log.info("rollbackClientWrongInput method was called");
130: ArticleManagerPBLocal am = getArticleManager();
131: PersonManagerPBLocal pm = getPersonManager();
132: am.storeArticles(articles);
133: pm.storePersons(persons);
134: }
135:
136: /**
137: * The bean will throw an exception before the method ends.
138: *
139: * @ejb:interface-method
140: */
141: public void rollbackThrowException(List objects) {
142: log.info("rollbackThrowException method was called");
143: storeObjects(objects);
144: // now we throw an exception
145: throw new EJBException(
146: "## Testing of rollback behaviour - rollbackThrowException ##");
147: }
148:
149: /**
150: * One of the objects passed by the client will cause an exception.
151: *
152: * @ejb:interface-method
153: */
154: public void rollbackPassInvalidObject(List objects) {
155: log.info("rollbackPassInvalidObject method was called");
156: storeObjects(objects);
157: }
158:
159: /**
160: * We do call ctx.setRollbackOnly and do odmg-tx.abort() call.
161: *
162: * @ejb:interface-method
163: */
164: public void rollbackSetRollbackOnly(List objects) {
165: log.info("rollbackSetRollbackOnly method was called");
166: storeObjects(objects);
167: /*
168: setRollbackOnly does only rollback the transaction, the client will not be
169: notified by an RemoteException (tested on JBoss)
170: */
171: getSessionContext().setRollbackOnly();
172: }
173:
174: /**
175: * We do call ctx.setRollbackOnly and do odmg-tx.abort() call.
176: *
177: * @ejb:interface-method
178: */
179: public void rollbackSetRollbackAndThrowException(List objects) {
180: log
181: .info("rollbackSetRollbackAndThrowException method was called");
182: storeObjects(objects);
183: getSessionContext().setRollbackOnly();
184: // to notify the client about the failure we throw an exception
185: // if we don't throw such an exception the client don't get notified
186: // about the failure
187: throw new EJBException(
188: "## Testing of rollback behaviour - rollbackSetRollbackAndThrowException ##");
189: }
190:
191: /**
192: * We use several OJB services, start to iterate a query result and do
193: * an odmg-tx.abort call.
194: *
195: * @ejb:interface-method
196: */
197: public void rollbackBreakIteration(List objectsToStore) {
198: // now we mix up different api's and use PB-api too
199: log.info("rollbackBreakIteration");
200: /*
201: store list of objects, then get these objects with Iterator, start
202: iteration, then break
203: */
204: storeObjects(objectsToStore);
205: Class searchClass = objectsToStore.get(0).getClass();
206: PersistenceBroker broker = getBroker();
207: try {
208: Query q = new QueryByCriteria(searchClass);
209: // we get the iterator and step into the first found object
210: Iterator it = broker.getIteratorByQuery(q);
211: it.next();
212: }
213: /*
214: Now we want to break iteration or something wrong. In this case we have to
215: cleanup the used PB instance by a close call
216: */
217: finally {
218: if (broker != null)
219: broker.close();
220: }
221:
222: // to notify the client about the failure we throw an exception
223: // if we don't throw such an exception the client don't get notified
224: // about the failure
225: throw new EJBException(
226: "## Testing of rollback behaviour - rollbackBreakIteration ##");
227: }
228:
229: /**
230: * @ejb:interface-method
231: */
232: public List storeObjects(List objects) {
233: return new ArrayList(super .storeObjects(objects));
234: }
235:
236: /**
237: * @ejb:interface-method
238: */
239: public void deleteObjects(List objects) {
240: log.info("deleteObjects");
241: super .deleteObjects(objects);
242: }
243:
244: /**
245: * @ejb:interface-method
246: */
247: public int getArticleCount() {
248: log.info("getArticleCount was called");
249: return getCount(ArticleVO.class);
250: }
251:
252: /**
253: * @ejb:interface-method
254: */
255: public int getPersonCount() {
256: log.info("getPersonCount was called");
257: return getCount(PersonVO.class);
258: }
259:
260: /**
261: * @ejb:interface-method
262: */
263: public Collection getAllObjects(Class target) {
264: if (log.isDebugEnabled())
265: log.debug("getAllObjects was called");
266: return super .getAllObjects(target);
267: }
268:
269: private ArticleManagerPBLocal getArticleManager() {
270: if (am == null) {
271: Context context = null;
272: try {
273: context = new InitialContext();
274: am = ((ArticleManagerPBLocalHome) context
275: .lookup(ARTICLE_MANAGER_EJB_REF_NAME)).create();
276: log.info("** Found bean: " + am);
277: return am;
278: } catch (NamingException e) {
279: log.error("Lookup using ejb-ref "
280: + ARTICLE_MANAGER_EJB_REF_NAME + " failed", e);
281: throw new EJBException(e);
282: } catch (CreateException e) {
283: log.error("Creation of ArticleManager failed", e);
284: throw new EJBException(e);
285: }
286: }
287: return am;
288: }
289:
290: private PersonManagerPBLocal getPersonManager() {
291: if (pm == null) {
292: Context context = null;
293: try {
294: context = new InitialContext();
295: pm = ((PersonManagerPBLocalHome) context
296: .lookup(PERSON_MANAGER_EJB_REF_NAME)).create();
297: log.info("** Found bean: " + pm);
298: return pm;
299: } catch (NamingException e) {
300: log.error("Lookup using ejb-ref "
301: + PERSON_MANAGER_EJB_REF_NAME + " failed", e);
302: throw new EJBException(e);
303: } catch (CreateException e) {
304: log.error("Creation of PersonManager failed", e);
305: throw new EJBException(e);
306: }
307: }
308: return pm;
309: }
310: }
|