KickJava   Java API By Example, From Geeks To Geeks.

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


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.rmi.RemoteException JavaDoc;
25 import java.rmi.ServerException JavaDoc;
26 import java.util.Properties JavaDoc;
27 import javax.ejb.CreateException JavaDoc;
28 import javax.ejb.EJBException JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32
33 import org.jboss.logging.Logger;
34 import org.jboss.test.cts.interfaces.CallerSession;
35 import org.jboss.test.cts.interfaces.CallerSessionHome;
36 import org.jboss.test.cts.interfaces.ReferenceTest;
37 import org.jboss.test.cts.interfaces.CalleeData;
38 import org.jboss.test.cts.interfaces.CalleeException;
39 import org.jboss.test.util.ejb.SessionSupport;
40 import org.jboss.util.Classes;
41 import org.jboss.mx.loading.ClassLoaderUtils;
42
43 /** The stateless session bean implementation
44  *
45  * @author Scott.Stark@jboss.org
46  * @version $Revision: 58115 $
47  */

48 public class CallerSessionBean
49       extends SessionSupport
50 {
51    private static Logger log = Logger.getLogger(CallerSessionBean.class);
52
53    private CallerSessionHome cachedHome;
54
55    public void ejbCreate() throws CreateException JavaDoc
56    {
57    }
58
59    public CalleeData simpleCall2(boolean isCaller) throws RemoteException JavaDoc
60    {
61       StringBuffer JavaDoc info = new StringBuffer JavaDoc("simpleCall2, isCaller: "+isCaller);
62       info.append(" CalleeData, ");
63       try
64       {
65          Classes.displayClassInfo(CalleeData.class, info);
66          log.info(info.toString());
67       }
68       catch(Exception JavaDoc e)
69       {
70          // Can happen due to no permissions to get TCL
71
e.printStackTrace();
72       }
73
74       // If this is the callee just return
75
if( isCaller == false )
76          return new CalleeData();
77
78       // Call the second deployment instance
79
CallerSessionHome home = null;
80       CallerSession callee = null;
81
82       try
83       {
84          home = lookupHome("ejbcts2/CalleeSessionHome");
85          callee = home.create();
86       }
87       catch(NamingException JavaDoc e)
88       {
89          throw new ServerException JavaDoc("Failed to lookup CalleeHome", e);
90       }
91       catch(CreateException JavaDoc e)
92       {
93          throw new ServerException JavaDoc("Failed to create Callee", e);
94       }
95
96       CalleeData data = callee.simpleCall(false);
97       return data;
98    }
99
100    public CalleeData simpleCall(boolean isCaller) throws RemoteException JavaDoc
101    {
102       StringBuffer JavaDoc info = new StringBuffer JavaDoc("simpleCall, isCaller: "+isCaller);
103       info.append(" CalleeData, ");
104       try
105       {
106          Classes.displayClassInfo(CalleeData.class, info);
107          log.info(info.toString());
108       }
109       catch(Exception JavaDoc e)
110       {
111          // Can happen due to no permissions to get TCL
112
log.debug("displayClassInfo failure", e);
113       }
114       // If this is the callee just return
115
if( isCaller == false )
116          return new CalleeData();
117
118       // Call the second deployment instance
119
CallerSession callee = null;
120       try
121       {
122          cachedHome = lookupHome("ejbcts2/CalleeSessionHome");
123          callee = cachedHome.create();
124       }
125       catch(NamingException JavaDoc e)
126       {
127          throw new ServerException JavaDoc("Failed to lookup CalleeHome", e);
128       }
129       catch(CreateException JavaDoc e)
130       {
131          throw new ServerException JavaDoc("Failed to create Callee", e);
132       }
133       catch(Throwable JavaDoc e)
134       {
135          log.error("Unexpected error", e);
136          throw new ServerException JavaDoc("Unexpected error"+e.getMessage());
137       }
138
139       CalleeData data = callee.simpleCall2(false);
140       return data;
141    }
142
143    /** Lookup the cts.jar/CalleeHome binding and invoke
144     *
145     * @throws RemoteException
146     */

147    public void callByValueInSameJar() throws RemoteException JavaDoc
148    {
149       // Call the second deployment instance
150
CallerSession callee = null;
151       try
152       {
153          cachedHome = lookupHome("ejbcts/CalleeSessionHome");
154          callee = cachedHome.create();
155       }
156       catch(NamingException JavaDoc e)
157       {
158          throw new ServerException JavaDoc("Failed to lookup CalleeHome", e);
159       }
160       catch(CreateException JavaDoc e)
161       {
162          throw new ServerException JavaDoc("Failed to create Callee", e);
163       }
164       catch(Throwable JavaDoc e)
165       {
166          log.error("Unexpected error", e);
167          throw new ServerException JavaDoc("Unexpected error"+e.getMessage());
168       }
169
170       ReferenceTest test = new ReferenceTest();
171       callee.validateValueMarshalling(test);
172    }
173
174    public void callAppEx() throws CalleeException, RemoteException JavaDoc
175    {
176       StringBuffer JavaDoc info = new StringBuffer JavaDoc("appEx, CalleeException, ");
177       try
178       {
179          Classes.displayClassInfo(CalleeException.class, info);
180          log.info(info.toString());
181       }
182       catch(Exception JavaDoc e)
183       {
184          // Can happen due to no permissions to get TCL
185
log.debug("displayClassInfo failure", e);
186       }
187       // Call the second deployment instance
188
CallerSessionHome home = null;
189       CallerSession callee = null;
190
191       try
192       {
193          home = lookupHome("ejbcts2/CalleeSessionHome");
194          callee = home.create();
195          callee.appEx();
196       }
197       catch(NamingException JavaDoc e)
198       {
199          throw new ServerException JavaDoc("Failed to lookup CalleeHome", e);
200       }
201       catch(CreateException JavaDoc e)
202       {
203          throw new ServerException JavaDoc("Failed to create Callee", e);
204       }
205       catch(CalleeException e)
206       {
207          throw e;
208       }
209    }
210
211    public void appEx() throws CalleeException
212    {
213       StringBuffer JavaDoc info = new StringBuffer JavaDoc("appEx, CalleeException, ");
214       try
215       {
216          Classes.displayClassInfo(CalleeException.class, info);
217          log.info(info.toString());
218       }
219       catch(Exception JavaDoc e)
220       {
221          // Can happen due to no permissions to get TCL
222
log.debug("displayClassInfo failure", e);
223       }
224       throw new CalleeException();
225    }
226
227    public void validateValueMarshalling(ReferenceTest test)
228    {
229       boolean wasSerialized = test.getWasSerialized();
230       log.info("validateValueMarshalling, testWasSerialized: "+wasSerialized);
231       if( wasSerialized == false )
232          throw new EJBException JavaDoc("ReferenceTest was not serialized");
233    }
234
235    private CallerSessionHome lookupHome(String JavaDoc ejbName) throws NamingException JavaDoc
236    {
237       CallerSessionHome home = null;
238       Properties JavaDoc env = new Properties JavaDoc();
239       env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
240       env.setProperty(Context.OBJECT_FACTORIES, "org.jboss.naming:org.jnp.interfaces");
241       env.setProperty(Context.PROVIDER_URL, "localhost:1099");
242
243       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
244       log.info("looking up: "+ejbName);
245       Object JavaDoc ref = ctx.lookup(ejbName);
246       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("JNDI CallerSessionHome.class: ");
247       try
248       {
249          Classes.displayClassInfo(ref.getClass(), buffer);
250          log.info(buffer.toString());
251       }
252       catch(Exception JavaDoc e)
253       {
254          // Can happen due to no permissions to get TCL
255
log.debug("displayClassInfo failure", e);
256       }
257       buffer.setLength(0);
258       buffer.append("Session CallerSessionHome.class: ");
259       try
260       {
261          Classes.displayClassInfo(CallerSessionHome.class, buffer);
262          log.info(buffer.toString());
263       }
264       catch(Exception JavaDoc e)
265       {
266          // Can happen due to no permissions to get TCL
267
log.debug("displayClassInfo failure", e);
268       }
269
270       home = (CallerSessionHome) ref;
271       return home;
272    }
273 }
274
Popular Tags