KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.InvocationTargetException JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26
27 import javax.ejb.CreateException JavaDoc;
28 import javax.ejb.EJBException JavaDoc;
29 import javax.ejb.EJBObject JavaDoc;
30 import javax.ejb.Handle JavaDoc;
31 import javax.ejb.NoSuchObjectLocalException JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35
36 import org.jboss.logging.Logger;
37
38 import org.jboss.test.cts.interfaces.StatefulSessionLocalHome;
39 import org.jboss.test.cts.interfaces.StatefulSessionLocal;
40 import org.jboss.test.cts.interfaces.StatelessSessionHome;
41 import org.jboss.test.cts.interfaces.StatelessSession;
42 import org.jboss.test.cts.interfaces.ClientCallback;
43 import org.jboss.test.cts.interfaces.StatelessSessionLocalHome;
44 import org.jboss.test.cts.interfaces.StatelessSessionLocal;
45 import org.jboss.test.util.ejb.SessionSupport;
46
47 /** The stateless session bean implementation
48  *
49  * @author Scott.Stark@jboss.org
50  * @version $Revision: 58115 $
51  */

52 public class StatelessSessionBean
53       extends SessionSupport
54 {
55    /** The serialVersionUID */
56    private static final long serialVersionUID = 1L;
57
58    private static Logger log = Logger.getLogger(StatelessSessionBean.class);
59
60    private static boolean breakCreate = false;
61    
62    public void ejbCreate()
63          throws CreateException JavaDoc
64    {
65       if (breakCreate)
66          throw new CreateException JavaDoc("broken create");
67    }
68
69    public String JavaDoc method1(String JavaDoc msg)
70    {
71       return msg;
72    }
73
74    /**
75     * This method is actually invalid (a hack)
76     * because it lets the client change the state of the SLSB
77     *
78     * @param action the action
79     */

80    public void breakCreate()
81    {
82       breakCreate = true;
83    }
84
85    public void loopbackTest()
86          throws java.rmi.RemoteException JavaDoc
87    {
88       try
89       {
90          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
91          StatelessSessionHome home = (StatelessSessionHome) ctx.lookup("ejbcts/StatelessSessionBean");
92          StatelessSession sessionBean;
93          try
94          {
95             sessionBean = home.create();
96          }
97          catch (CreateException JavaDoc ex)
98          {
99             log.debug("Loopback CreateException: " + ex);
100             throw new EJBException JavaDoc(ex);
101          }
102          sessionBean.loopbackTest(sessionCtx.getEJBObject());
103       }
104       catch (javax.naming.NamingException JavaDoc nex)
105       {
106          log.debug("Could not locate bean instance");
107       }
108    }
109
110    public void loopbackTest(EJBObject JavaDoc obj)
111          throws java.rmi.RemoteException JavaDoc
112    {
113       // This should throw an exception.
114
StatelessSession bean = (StatelessSession) obj;
115       bean.method1("Hello");
116    }
117
118    public void callbackTest(ClientCallback callback, String JavaDoc data)
119          throws java.rmi.RemoteException JavaDoc
120    {
121       callback.callback(data);
122    }
123
124    public void npeError()
125    {
126       Object JavaDoc obj = null;
127       obj.toString();
128    }
129
130    public void testLocalHome() throws InvocationTargetException JavaDoc
131    {
132       StatelessSessionLocalHome home = (StatelessSessionLocalHome) sessionCtx.getEJBLocalHome();
133       log.debug("Obtained StatelessSessionLocalHome from ctx");
134       try
135       {
136          StatelessSessionLocal local = home.create();
137          log.debug("Created StatelessSessionLocal#1");
138          StatelessSessionLocalHome home2 = (StatelessSessionLocalHome) local.getEJBLocalHome();
139          log.debug("Obtained StatelessSessionLocalHome from StatelessSessionLocal");
140          local = home2.create();
141          log.debug("Created StatelessSessionLocal#2");
142          local.remove();
143       }
144       catch(Exception JavaDoc e)
145       {
146          log.debug("testLocalHome failed", e);
147          throw new InvocationTargetException JavaDoc(e, "testLocalHome failed");
148       }
149    }
150
151    public void testPassivationByTimeLocal()
152    {
153       StatefulSessionLocal sessionBean1 = null;
154       Handle JavaDoc handle = null;
155       try
156       {
157          Context JavaDoc ctx = new InitialContext JavaDoc();
158          log.debug("+++ testPassivationByTime");
159          StatefulSessionLocalHome sessionHome = ( StatefulSessionLocalHome ) ctx.lookup("ejbcts/StatefulSessionLocalBean");
160          sessionBean1 = sessionHome.create("testPassivationByTimeLocal");
161          sessionBean1.ping();
162
163            handle = sessionBean1.getHandle();
164
165          log.debug("Waiting 41 seconds for passivation...");
166          Thread.sleep(41*1000);
167
168          // Validate that sessionBean1 was passivated and activated
169
boolean passivated = sessionBean1.getWasPassivated();
170          if (passivated == false) throw new EJBException JavaDoc("sessionBean1 WasPassivated");
171          boolean activated = sessionBean1.getWasActivated();
172          if (activated == false) throw new EJBException JavaDoc("sessionBean1 WasActivated");
173
174          log.debug("Waiting 90 seconds for removal due to age...");
175          Thread.sleep(90*1000);
176       }
177       catch (CreateException JavaDoc e)
178       {
179          throw new EJBException JavaDoc(e.toString());
180       }
181       catch (NamingException JavaDoc e)
182       {
183          throw new EJBException JavaDoc(e.toString());
184       }
185       catch (InterruptedException JavaDoc e)
186       {
187          throw new EJBException JavaDoc(e.toString());
188       }
189
190       try
191       {
192          sessionBean1.ping();
193          throw new EJBException JavaDoc("Was able to ping for a removed session");
194       }
195       catch (NoSuchObjectLocalException JavaDoc expected)
196       {
197          log.debug("Session access failed as expected", expected);
198       }
199
200       try
201       {
202          handle.getEJBObject();
203          throw new EJBException JavaDoc("Was able to getEJBObject for a removed session");
204       }
205       catch (RemoteException JavaDoc expected)
206       {
207          log.debug("Session access failed as expected", expected);
208       }
209    }
210 }
211
Popular Tags