KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cts > ejb > BMTStatefulSessionBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cts.ejb;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.rmi.RemoteException JavaDoc;
29
30 import javax.ejb.CreateException JavaDoc;
31 import javax.ejb.EJBException JavaDoc;
32 import javax.ejb.EJBObject JavaDoc;
33 import javax.ejb.Handle JavaDoc;
34 import javax.ejb.RemoveException JavaDoc;
35 import javax.naming.Context JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.transaction.UserTransaction JavaDoc;
38
39 import org.jboss.logging.Logger;
40 import org.jboss.test.cts.interfaces.BeanContextInfo;
41 import org.jboss.test.cts.interfaces.CtsCmpLocal;
42 import org.jboss.test.cts.interfaces.CtsCmpLocalHome;
43 import org.jboss.test.cts.interfaces.StatefulSession;
44 import org.jboss.test.cts.interfaces.StatefulSessionHome;
45 import org.jboss.test.cts.interfaces.StatelessSession;
46 import org.jboss.test.cts.interfaces.StatelessSessionHome;
47 import org.jboss.test.cts.keys.AccountPK;
48 import org.jboss.test.util.ejb.SessionSupport;
49
50
51 /** The stateful session ejb implementation
52  *
53  * @author Scott.Stark@jboss.org
54  * @version $Revision: 37406 $
55  */

56 public class BMTStatefulSessionBean
57    extends SessionSupport
58 {
59    /** The serialVersionUID */
60    private static final long serialVersionUID = 1L;
61    private Logger log;
62    private String JavaDoc testName;
63    private int counter;
64    private CtsCmpLocal entityBean;
65    private Context JavaDoc enc;
66    private Handle JavaDoc sessionHandle;
67    private SessionRef sessionRef;
68    private byte[] statefulHandle;
69    private boolean wasActivated;
70    private boolean wasPassivated;
71
72    public void ejbCreate(String JavaDoc testName)
73    {
74       this.testName = testName;
75       log = Logger.getLogger(BMTStatefulSessionBean.class.getName()+"#"+testName);
76       log.debug("ejbCreate("+testName+"), ctx="+sessionCtx);
77    }
78    public void ejbCreateAlt(String JavaDoc testName)
79    {
80       this.testName = testName + "Alt";
81       log = Logger.getLogger(BMTStatefulSessionBean.class.getName()+"#"+testName);
82       log.debug("ejbCreateAlt("+testName+"), ctx="+sessionCtx);
83    }
84
85    public void ejbActivate()
86    {
87       log = Logger.getLogger(BMTStatefulSessionBean.class.getName()+"#"+testName);
88       log.debug("ejbActivate( )...");
89       wasActivated = true;
90    }
91    public void ejbPassivate()
92    {
93       log.debug("ejbPassivate( )...");
94       wasPassivated = true;
95    }
96
97    public String JavaDoc getTestName()
98    {
99       return testName;
100    }
101
102    public String JavaDoc method1(String JavaDoc msg)
103    {
104       log.debug("method1( ), msg="+msg);
105       return msg;
106    }
107
108    public void incCounter ()
109    {
110       counter++;
111    }
112
113    public void decCounter ()
114    {
115       counter--;
116    }
117
118    public int getCounter ()
119    {
120       return counter;
121    }
122
123    public void setCounter (int value)
124    {
125       counter = value;
126    }
127
128    public BeanContextInfo getBeanContextInfo ()
129       throws RemoteException JavaDoc
130    {
131       BeanContextInfo ctx = new BeanContextInfo();
132
133       log.debug("Getting EJBObject..");
134       Class JavaDoc remoteInterface = sessionCtx.getEJBObject().getClass();
135       ctx.remoteInterface = remoteInterface.getName();
136
137       log.debug("Getting EJBHome...");
138       Class JavaDoc homeInterface = sessionCtx.getEJBHome().getClass();
139       ctx.homeInterface = homeInterface.getName();
140
141       log.debug("calling setRollbackOnly( ) on context");
142       sessionCtx.setRollbackOnly();
143       ctx.isRollbackOnly = new Boolean JavaDoc(sessionCtx.getRollbackOnly());
144
145       return ctx;
146    }
147
148    public void loopbackTest ()
149       throws RemoteException JavaDoc
150    {
151       try
152       {
153         Context JavaDoc ctx = new InitialContext JavaDoc();
154         StatefulSessionHome home = (StatefulSessionHome) ctx.lookup("ejbcts/StatefulSessionBean");
155         StatefulSession sessionBean;
156         try
157         {
158            sessionBean = home.create(testName);
159         }
160         catch (CreateException JavaDoc crex)
161         {
162            log.debug("Loopback CreateException: " + crex);
163            throw new EJBException JavaDoc(crex);
164         }
165         sessionBean.loopbackTest(sessionCtx.getEJBObject());
166
167       }
168       catch (javax.naming.NamingException JavaDoc nex)
169       {
170          log.debug("Could not locate bean instance");
171          throw new EJBException JavaDoc(nex);
172       }
173    }
174
175    public void loopbackTest (EJBObject JavaDoc obj)
176       throws RemoteException JavaDoc
177    {
178       // This should throw an exception.
179
StatefulSession bean = ( StatefulSession ) obj;
180
181       bean.method1("Hello");
182    }
183
184    public void ping()
185    {
186    }
187
188    public void sleep(long wait)
189    {
190       try
191       {
192          Thread.sleep(wait);
193       }
194       catch (InterruptedException JavaDoc e)
195       {
196          throw new EJBException JavaDoc("Interrupted", e);
197       }
198    }
199
200    public boolean getWasActivated()
201    {
202       log.debug("getWasActivated( ), wasActivated="+wasActivated);
203       return wasActivated;
204    }
205
206    public boolean getWasPassivated()
207    {
208       log.debug("getWasPassivated( ), wasPassivated="+wasPassivated);
209       return wasPassivated;
210    }
211
212    public void createLocalEntity(AccountPK pk, String JavaDoc personsName)
213          throws CreateException JavaDoc
214    {
215       try
216       {
217          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
218          enc = (Context JavaDoc) ctx.lookup("java:comp/env");
219          CtsCmpLocalHome home = (CtsCmpLocalHome) enc.lookup("ejb/CMPBeanLocalHome");
220          entityBean = home.create(pk, personsName);
221       }
222       catch(Exception JavaDoc e)
223       {
224          log.error("CtsCmpLocal create failed", e);
225          throw new EJBException JavaDoc("CtsCmpLocal create failed", e);
226       }
227    }
228
229    public String JavaDoc readAndRemoveEntity()
230       throws RemoveException JavaDoc
231    {
232       String JavaDoc name = entityBean.getPersonsName();
233       entityBean.remove();
234       return name;
235    }
236
237    public void createSessionHandle()
238    {
239       log.info("createSessionHandle");
240       try
241       {
242          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
243          enc = (Context JavaDoc) ctx.lookup("java:comp/env");
244          StatelessSessionHome home = (StatelessSessionHome) enc.lookup("ejb/StatelessSessionHome");
245          StatelessSession bean = home.create();
246          sessionHandle = bean.getHandle();
247       }
248       catch(Exception JavaDoc e)
249       {
250          log.error("StatelessSessionHome create failed", e);
251          throw new EJBException JavaDoc("StatelessSessionHome create failed", e);
252       }
253    }
254
255    public String JavaDoc useSessionHandle(String JavaDoc arg)
256    {
257       log.info("useSessionHandle");
258       try
259       {
260          StatelessSession bean = (StatelessSession) sessionHandle.getEJBObject();
261          arg = bean.method1(arg);
262       }
263       catch(Exception JavaDoc e)
264       {
265          log.error("StatelessSession handle failed", e);
266          throw new EJBException JavaDoc("StatelessSession handle failed", e);
267       }
268       return arg;
269    }
270
271    public void createStatefulSessionHandle(String JavaDoc testName)
272    {
273       log.info("createStatefulSessionHandle");
274       try
275       {
276          StatefulSessionHome home = (StatefulSessionHome) sessionCtx.getEJBHome();
277          StatefulSession bean = home.create(testName);
278          bean.incCounter();
279          Handle JavaDoc handle = bean.getHandle();
280          ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
281          ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
282          oos.writeObject(handle);
283          this.statefulHandle = baos.toByteArray();
284       }
285       catch(Exception JavaDoc e)
286       {
287          log.error("Failed to serialize session handle", e);
288          throw new EJBException JavaDoc("Failed to serialize session handle", e);
289       }
290    }
291
292    public void useStatefulSessionHandle()
293    {
294       log.info("useStatefulSessionHandle");
295       try
296       {
297          ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(statefulHandle);
298          ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
299          Handle JavaDoc handle = (Handle JavaDoc) ois.readObject();
300          StatefulSession bean = (StatefulSession) handle.getEJBObject();
301          bean.incCounter();
302          int count = bean.getCounter();
303          log.info("useStatefulSessionHandle, count="+count);
304       }
305       catch(Exception JavaDoc e)
306       {
307          log.error("Failed to read session from handle", e);
308          throw new EJBException JavaDoc("SFailed to read session from handle", e);
309       }
310    }
311
312    public void createSessionRef()
313       throws RemoteException JavaDoc
314    {
315       log.info("createSessionRef");
316       Handle JavaDoc handle = super.sessionCtx.getEJBObject().getHandle();
317       this.sessionRef = new SessionRef(handle);
318    }
319    public String JavaDoc useSessionRef()
320       throws RemoteException JavaDoc
321    {
322       log.info("useSessionRef");
323       Handle JavaDoc handle = sessionRef.getHandle();
324       return handle.toString();
325    }
326
327    public Handle JavaDoc getHandle()
328    {
329       try
330       {
331          return sessionCtx.getEJBObject().getHandle();
332       }
333       catch (RemoteException JavaDoc e)
334       {
335          throw new EJBException JavaDoc(e);
336       }
337    }
338    
339    public void testBadUserTx()
340    {
341       UserTransaction JavaDoc tx = sessionCtx.getUserTransaction();
342       try
343       {
344          tx.begin();
345       }
346       catch (Exception JavaDoc e)
347       {
348          log.error("This should work", e);
349       }
350       throw new Error JavaDoc("Force a rollback without committing/rollingback");
351    }
352 }
353
Popular Tags