001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cts.ejb;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.ObjectInputStream;
027: import java.io.ObjectOutputStream;
028: import java.rmi.RemoteException;
029:
030: import javax.ejb.CreateException;
031: import javax.ejb.EJBException;
032: import javax.ejb.EJBObject;
033: import javax.ejb.Handle;
034: import javax.ejb.RemoveException;
035: import javax.ejb.SessionSynchronization;
036: import javax.naming.Context;
037: import javax.naming.InitialContext;
038:
039: import org.apache.log4j.Category;
040: import org.jboss.ejb.AllowedOperationsAssociation;
041: import org.jboss.test.cts.interfaces.BeanContextInfo;
042: import org.jboss.test.cts.interfaces.CtsCmpLocal;
043: import org.jboss.test.cts.interfaces.CtsCmpLocalHome;
044: import org.jboss.test.cts.interfaces.StatefulSession;
045: import org.jboss.test.cts.interfaces.StatefulSessionHome;
046: import org.jboss.test.cts.interfaces.StatelessSession;
047: import org.jboss.test.cts.interfaces.StatelessSessionHome;
048: import org.jboss.test.cts.keys.AccountPK;
049: import org.jboss.test.util.ejb.SessionSupport;
050:
051: /**
052: * The stateful session ejb implementation
053: *
054: * @author Scott.Stark@jboss.org
055: * @author Dimitris.Andreadis@jboss.org
056: * @version $Revision: 57211 $
057: */
058: public class StatefulSessionBean extends SessionSupport implements
059: SessionSynchronization {
060: /** The serialVersionUID */
061: private static final long serialVersionUID = 1L;
062: private static transient Category log = Category
063: .getInstance(StatefulSessionBean.class);
064: private transient int counterAtTxStart;
065: private String testName;
066: private int counter;
067: private CtsCmpLocal entityBean;
068: private Context enc;
069: private Handle sessionHandle;
070: private SessionRef sessionRef;
071: private byte[] statefulHandle;
072: private boolean wasActivated;
073: private boolean wasPassivated;
074:
075: public void ejbCreate(String testName) {
076: this .testName = testName;
077: log = Category.getInstance(StatefulSessionBean.class.getName()
078: + "#" + testName);
079: log.debug("ejbCreate(" + testName + "), ctx=" + sessionCtx);
080: }
081:
082: public void ejbCreateAlt(String testName) {
083: this .testName = testName + "Alt";
084: log = Category.getInstance(StatefulSessionBean.class.getName()
085: + "#" + testName);
086: log.debug("ejbCreateAlt(" + testName + "), ctx=" + sessionCtx);
087: }
088:
089: public void ejbActivate() {
090: log = Category.getInstance(StatefulSessionBean.class.getName()
091: + "#" + testName);
092:
093: // Expecting : AllowedOperationsFlags.IN_EJB_ACTIVATE
094: log.info("ejbActivate( ) - inMethodFlag : "
095: + AllowedOperationsAssociation
096: .peekInMethodFlagAsString());
097:
098: // JBAS-2660, should be able to call these
099: super .sessionCtx.getEJBObject();
100: super .sessionCtx.getEJBLocalObject();
101:
102: wasActivated = true;
103: }
104:
105: public void ejbPassivate() {
106: // Expecting : AllowedOperationsFlags.IN_EJB_PASSIVATE
107: log.info("ejbPassivate( ) - inMethodFlag : "
108: + AllowedOperationsAssociation
109: .peekInMethodFlagAsString());
110:
111: // JBAS-2660, should be able to call these
112: super .sessionCtx.getEJBObject();
113: super .sessionCtx.getEJBLocalObject();
114:
115: wasPassivated = true;
116: }
117:
118: public void afterBegin() {
119: log.debug("afterBegin()..., counter=" + counter);
120: counterAtTxStart = counter;
121: }
122:
123: public void afterCompletion(boolean isCommited) {
124: log.debug("afterCompletion(), isCommited=" + isCommited
125: + ", counter=" + counter + ", counterAtTxStart="
126: + counterAtTxStart);
127: if (isCommited == false) {
128: counter = counterAtTxStart;
129: log.debug("Rolling counter back to: " + counter);
130: } else {
131: log.debug("Committed updated counter: " + counter);
132: }
133: }
134:
135: public void beforeCompletion() {
136: log.debug("beforeCompletion(), counter=" + counter
137: + ", counterAtTxStart=" + counterAtTxStart);
138: }
139:
140: public String getTestName() {
141: return testName;
142: }
143:
144: public String method1(String msg) {
145: log.debug("method1( ), msg=" + msg);
146: return msg;
147: }
148:
149: public void incCounter() {
150: counter++;
151: }
152:
153: public void decCounter() {
154: counter--;
155: }
156:
157: public int getCounter() {
158: return counter;
159: }
160:
161: public void setCounter(int value) {
162: counter = value;
163: }
164:
165: public BeanContextInfo getBeanContextInfo()
166: throws java.rmi.RemoteException {
167: BeanContextInfo ctx = new BeanContextInfo();
168:
169: log.debug("Getting EJBObject..");
170: Class remoteInterface = sessionCtx.getEJBObject().getClass();
171: ctx.remoteInterface = remoteInterface.getName();
172:
173: log.debug("Getting EJBHome...");
174: Class homeInterface = sessionCtx.getEJBHome().getClass();
175: ctx.homeInterface = homeInterface.getName();
176:
177: log.debug("calling setRollbackOnly( ) on context");
178: sessionCtx.setRollbackOnly();
179: ctx.isRollbackOnly = new Boolean(sessionCtx.getRollbackOnly());
180:
181: return ctx;
182: }
183:
184: public void loopbackTest() throws java.rmi.RemoteException {
185: try {
186: Context ctx = new InitialContext();
187: StatefulSessionHome home = (StatefulSessionHome) ctx
188: .lookup("ejbcts/StatefulSessionBean");
189: StatefulSession sessionBean;
190: try {
191: sessionBean = home.create(testName);
192: } catch (CreateException crex) {
193: log.debug("Loopback CreateException: " + crex);
194: throw new EJBException(crex);
195: }
196: sessionBean.loopbackTest(sessionCtx.getEJBObject());
197:
198: } catch (javax.naming.NamingException nex) {
199: log.debug("Could not locate bean instance");
200: throw new EJBException(nex);
201: }
202: }
203:
204: public void loopbackTest(EJBObject obj)
205: throws java.rmi.RemoteException {
206: // This should throw an exception.
207: StatefulSession bean = (StatefulSession) obj;
208:
209: bean.method1("Hello");
210: }
211:
212: public void ping() {
213: }
214:
215: public void sleep(long wait) {
216: try {
217: Thread.sleep(wait);
218: } catch (InterruptedException e) {
219: throw new EJBException("Interrupted", e);
220: }
221: }
222:
223: public boolean getWasActivated() {
224: log.debug("getWasActivated( ), wasActivated=" + wasActivated);
225: return wasActivated;
226: }
227:
228: public boolean getWasPassivated() {
229: log
230: .debug("getWasPassivated( ), wasPassivated="
231: + wasPassivated);
232: return wasPassivated;
233: }
234:
235: public void createLocalEntity(AccountPK pk, String personsName)
236: throws CreateException {
237: try {
238: InitialContext ctx = new InitialContext();
239: enc = (Context) ctx.lookup("java:comp/env");
240: CtsCmpLocalHome home = (CtsCmpLocalHome) enc
241: .lookup("ejb/CMPBeanLocalHome");
242: entityBean = home.create(pk, personsName);
243: } catch (Exception e) {
244: log.error("CtsCmpLocal create failed", e);
245: throw new EJBException("CtsCmpLocal create failed", e);
246: }
247: }
248:
249: public String readAndRemoveEntity() throws RemoveException {
250: String name = entityBean.getPersonsName();
251: entityBean.remove();
252: return name;
253: }
254:
255: public void createSessionHandle() {
256: log.info("createSessionHandle");
257: try {
258: InitialContext ctx = new InitialContext();
259: enc = (Context) ctx.lookup("java:comp/env");
260: StatelessSessionHome home = (StatelessSessionHome) enc
261: .lookup("ejb/StatelessSessionHome");
262: StatelessSession bean = home.create();
263: sessionHandle = bean.getHandle();
264: } catch (Exception e) {
265: log.error("StatelessSessionHome create failed", e);
266: throw new EJBException(
267: "StatelessSessionHome create failed", e);
268: }
269: }
270:
271: public String useSessionHandle(String arg) {
272: log.info("useSessionHandle");
273: try {
274: StatelessSession bean = (StatelessSession) sessionHandle
275: .getEJBObject();
276: arg = bean.method1(arg);
277: } catch (Exception e) {
278: log.error("StatelessSession handle failed", e);
279: throw new EJBException("StatelessSession handle failed", e);
280: }
281: return arg;
282: }
283:
284: public void createStatefulSessionHandle(String testName) {
285: log.info("createStatefulSessionHandle");
286: try {
287: StatefulSessionHome home = (StatefulSessionHome) sessionCtx
288: .getEJBHome();
289: StatefulSession bean = home.create(testName);
290: bean.incCounter();
291: Handle handle = bean.getHandle();
292: ByteArrayOutputStream baos = new ByteArrayOutputStream();
293: ObjectOutputStream oos = new ObjectOutputStream(baos);
294: oos.writeObject(handle);
295: this .statefulHandle = baos.toByteArray();
296: } catch (Exception e) {
297: log.error("Failed to serialize session handle", e);
298: throw new EJBException(
299: "Failed to serialize session handle", e);
300: }
301: }
302:
303: public void useStatefulSessionHandle() {
304: log.info("useStatefulSessionHandle");
305: try {
306: ByteArrayInputStream bais = new ByteArrayInputStream(
307: statefulHandle);
308: ObjectInputStream ois = new ObjectInputStream(bais);
309: Handle handle = (Handle) ois.readObject();
310: StatefulSession bean = (StatefulSession) handle
311: .getEJBObject();
312: bean.incCounter();
313: int count = bean.getCounter();
314: log.info("useStatefulSessionHandle, count=" + count);
315: } catch (Exception e) {
316: log.error("Failed to read session from handle", e);
317: throw new EJBException(
318: "SFailed to read session from handle", e);
319: }
320: }
321:
322: public void createSessionRef() throws RemoteException {
323: log.info("createSessionRef");
324: Handle handle = super .sessionCtx.getEJBObject().getHandle();
325: this .sessionRef = new SessionRef(handle);
326: }
327:
328: public String useSessionRef() throws RemoteException {
329: log.info("useSessionRef");
330: Handle handle = sessionRef.getHandle();
331: return handle.toString();
332: }
333:
334: public Handle getHandle() {
335: try {
336: return sessionCtx.getEJBObject().getHandle();
337: } catch (RemoteException e) {
338: throw new EJBException(e);
339: }
340: }
341:
342: public void testBadUserTx() {
343: throw new Error("Not Applicable");
344: }
345: }
|