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.naming.Context;
036: import javax.naming.InitialContext;
037: import javax.transaction.UserTransaction;
038:
039: import org.jboss.logging.Logger;
040: import org.jboss.test.cts.interfaces.BeanContextInfo;
041: import org.jboss.test.cts.interfaces.CtsCmpLocal;
042: import org.jboss.test.cts.interfaces.CtsCmpLocalHome;
043: import org.jboss.test.cts.interfaces.StatefulSession;
044: import org.jboss.test.cts.interfaces.StatefulSessionHome;
045: import org.jboss.test.cts.interfaces.StatelessSession;
046: import org.jboss.test.cts.interfaces.StatelessSessionHome;
047: import org.jboss.test.cts.keys.AccountPK;
048: import org.jboss.test.util.ejb.SessionSupport;
049:
050: /** The stateful session ejb implementation
051: *
052: * @author Scott.Stark@jboss.org
053: * @version $Revision: 57211 $
054: */
055: public class BMTStatefulSessionBean extends SessionSupport {
056: /** The serialVersionUID */
057: private static final long serialVersionUID = 1L;
058: private Logger log;
059: private String testName;
060: private int counter;
061: private CtsCmpLocal entityBean;
062: private Context enc;
063: private Handle sessionHandle;
064: private SessionRef sessionRef;
065: private byte[] statefulHandle;
066: private boolean wasActivated;
067: private boolean wasPassivated;
068:
069: public void ejbCreate(String testName) {
070: this .testName = testName;
071: log = Logger.getLogger(BMTStatefulSessionBean.class.getName()
072: + "#" + testName);
073: log.debug("ejbCreate(" + testName + "), ctx=" + sessionCtx);
074: }
075:
076: public void ejbCreateAlt(String testName) {
077: this .testName = testName + "Alt";
078: log = Logger.getLogger(BMTStatefulSessionBean.class.getName()
079: + "#" + testName);
080: log.debug("ejbCreateAlt(" + testName + "), ctx=" + sessionCtx);
081: }
082:
083: public void ejbActivate() {
084: log = Logger.getLogger(BMTStatefulSessionBean.class.getName()
085: + "#" + testName);
086: log.debug("ejbActivate( )...");
087: wasActivated = true;
088: }
089:
090: public void ejbPassivate() {
091: log.debug("ejbPassivate( )...");
092: wasPassivated = true;
093: }
094:
095: public String getTestName() {
096: return testName;
097: }
098:
099: public String method1(String msg) {
100: log.debug("method1( ), msg=" + msg);
101: return msg;
102: }
103:
104: public void incCounter() {
105: counter++;
106: }
107:
108: public void decCounter() {
109: counter--;
110: }
111:
112: public int getCounter() {
113: return counter;
114: }
115:
116: public void setCounter(int value) {
117: counter = value;
118: }
119:
120: public BeanContextInfo getBeanContextInfo() throws RemoteException {
121: BeanContextInfo ctx = new BeanContextInfo();
122:
123: log.debug("Getting EJBObject..");
124: Class remoteInterface = sessionCtx.getEJBObject().getClass();
125: ctx.remoteInterface = remoteInterface.getName();
126:
127: log.debug("Getting EJBHome...");
128: Class homeInterface = sessionCtx.getEJBHome().getClass();
129: ctx.homeInterface = homeInterface.getName();
130:
131: log.debug("calling setRollbackOnly( ) on context");
132: sessionCtx.setRollbackOnly();
133: ctx.isRollbackOnly = new Boolean(sessionCtx.getRollbackOnly());
134:
135: return ctx;
136: }
137:
138: public void loopbackTest() throws RemoteException {
139: try {
140: Context ctx = new InitialContext();
141: StatefulSessionHome home = (StatefulSessionHome) ctx
142: .lookup("ejbcts/StatefulSessionBean");
143: StatefulSession sessionBean;
144: try {
145: sessionBean = home.create(testName);
146: } catch (CreateException crex) {
147: log.debug("Loopback CreateException: " + crex);
148: throw new EJBException(crex);
149: }
150: sessionBean.loopbackTest(sessionCtx.getEJBObject());
151:
152: } catch (javax.naming.NamingException nex) {
153: log.debug("Could not locate bean instance");
154: throw new EJBException(nex);
155: }
156: }
157:
158: public void loopbackTest(EJBObject obj) throws RemoteException {
159: // This should throw an exception.
160: StatefulSession bean = (StatefulSession) obj;
161:
162: bean.method1("Hello");
163: }
164:
165: public void ping() {
166: }
167:
168: public void sleep(long wait) {
169: try {
170: Thread.sleep(wait);
171: } catch (InterruptedException e) {
172: throw new EJBException("Interrupted", e);
173: }
174: }
175:
176: public boolean getWasActivated() {
177: log.debug("getWasActivated( ), wasActivated=" + wasActivated);
178: return wasActivated;
179: }
180:
181: public boolean getWasPassivated() {
182: log
183: .debug("getWasPassivated( ), wasPassivated="
184: + wasPassivated);
185: return wasPassivated;
186: }
187:
188: public void createLocalEntity(AccountPK pk, String personsName)
189: throws CreateException {
190: try {
191: InitialContext ctx = new InitialContext();
192: enc = (Context) ctx.lookup("java:comp/env");
193: CtsCmpLocalHome home = (CtsCmpLocalHome) enc
194: .lookup("ejb/CMPBeanLocalHome");
195: entityBean = home.create(pk, personsName);
196: } catch (Exception e) {
197: log.error("CtsCmpLocal create failed", e);
198: throw new EJBException("CtsCmpLocal create failed", e);
199: }
200: }
201:
202: public String readAndRemoveEntity() throws RemoveException {
203: String name = entityBean.getPersonsName();
204: entityBean.remove();
205: return name;
206: }
207:
208: public void createSessionHandle() {
209: log.info("createSessionHandle");
210: try {
211: InitialContext ctx = new InitialContext();
212: enc = (Context) ctx.lookup("java:comp/env");
213: StatelessSessionHome home = (StatelessSessionHome) enc
214: .lookup("ejb/StatelessSessionHome");
215: StatelessSession bean = home.create();
216: sessionHandle = bean.getHandle();
217: } catch (Exception e) {
218: log.error("StatelessSessionHome create failed", e);
219: throw new EJBException(
220: "StatelessSessionHome create failed", e);
221: }
222: }
223:
224: public String useSessionHandle(String arg) {
225: log.info("useSessionHandle");
226: try {
227: StatelessSession bean = (StatelessSession) sessionHandle
228: .getEJBObject();
229: arg = bean.method1(arg);
230: } catch (Exception e) {
231: log.error("StatelessSession handle failed", e);
232: throw new EJBException("StatelessSession handle failed", e);
233: }
234: return arg;
235: }
236:
237: public void createStatefulSessionHandle(String testName) {
238: log.info("createStatefulSessionHandle");
239: try {
240: StatefulSessionHome home = (StatefulSessionHome) sessionCtx
241: .getEJBHome();
242: StatefulSession bean = home.create(testName);
243: bean.incCounter();
244: Handle handle = bean.getHandle();
245: ByteArrayOutputStream baos = new ByteArrayOutputStream();
246: ObjectOutputStream oos = new ObjectOutputStream(baos);
247: oos.writeObject(handle);
248: this .statefulHandle = baos.toByteArray();
249: } catch (Exception e) {
250: log.error("Failed to serialize session handle", e);
251: throw new EJBException(
252: "Failed to serialize session handle", e);
253: }
254: }
255:
256: public void useStatefulSessionHandle() {
257: log.info("useStatefulSessionHandle");
258: try {
259: ByteArrayInputStream bais = new ByteArrayInputStream(
260: statefulHandle);
261: ObjectInputStream ois = new ObjectInputStream(bais);
262: Handle handle = (Handle) ois.readObject();
263: StatefulSession bean = (StatefulSession) handle
264: .getEJBObject();
265: bean.incCounter();
266: int count = bean.getCounter();
267: log.info("useStatefulSessionHandle, count=" + count);
268: } catch (Exception e) {
269: log.error("Failed to read session from handle", e);
270: throw new EJBException(
271: "SFailed to read session from handle", e);
272: }
273: }
274:
275: public void createSessionRef() throws RemoteException {
276: log.info("createSessionRef");
277: Handle handle = super .sessionCtx.getEJBObject().getHandle();
278: this .sessionRef = new SessionRef(handle);
279: }
280:
281: public String useSessionRef() throws RemoteException {
282: log.info("useSessionRef");
283: Handle handle = sessionRef.getHandle();
284: return handle.toString();
285: }
286:
287: public Handle getHandle() {
288: try {
289: return sessionCtx.getEJBObject().getHandle();
290: } catch (RemoteException e) {
291: throw new EJBException(e);
292: }
293: }
294:
295: public void testBadUserTx() {
296: UserTransaction tx = sessionCtx.getUserTransaction();
297: try {
298: tx.begin();
299: } catch (Exception e) {
300: log.error("This should work", e);
301: }
302: throw new Error(
303: "Force a rollback without committing/rollingback");
304: }
305: }
|