KickJava   Java API By Example, From Geeks To Geeks.

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


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.ejb.SessionSynchronization JavaDoc;
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38
39 import org.jboss.logging.Logger;
40 import org.jboss.ejb.AllowedOperationsAssociation;
41 import org.jboss.test.cts.interfaces.BeanContextInfo;
42 import org.jboss.test.cts.interfaces.CtsCmpLocal;
43 import org.jboss.test.cts.interfaces.CtsCmpLocalHome;
44 import org.jboss.test.cts.interfaces.StatefulSession;
45 import org.jboss.test.cts.interfaces.StatefulSessionHome;
46 import org.jboss.test.cts.interfaces.StatelessSession;
47 import org.jboss.test.cts.interfaces.StatelessSessionHome;
48 import org.jboss.test.cts.keys.AccountPK;
49 import org.jboss.test.util.ejb.SessionSupport;
50
51
52 /**
53  * The stateful session ejb implementation
54  *
55  * @author Scott.Stark@jboss.org
56  * @author Dimitris.Andreadis@jboss.org
57  * @version $Revision: 58115 $
58  */

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