KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > txpropiiop > ejb > a > SessionAEJB


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.txpropiiop.ejb.a;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.util.Properties JavaDoc;
26 import javax.ejb.CreateException JavaDoc;
27 import javax.ejb.EJBException JavaDoc;
28 import javax.ejb.SessionBean JavaDoc;
29 import javax.ejb.SessionContext JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.rmi.PortableRemoteObject JavaDoc;
33
34 import org.jboss.logging.Logger;
35 import org.jboss.test.txpropiiop.interfaces.b.SessionB;
36 import org.jboss.test.txpropiiop.interfaces.b.SessionBHome;
37 import org.jboss.tm.iiop.TxClientInterceptor;
38 import org.omg.CORBA.ORB JavaDoc;
39 import org.omg.CosTransactions.PropagationContext;
40 import org.omg.CosTransactions.TransIdentity;
41 import org.omg.CosTransactions.otid_t;
42
43 /**
44  * A SessionA.
45  *
46  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
47  * @version $Revision: 37406 $
48  */

49 public class SessionAEJB implements SessionBean JavaDoc
50 {
51    private static final Logger log = Logger.getLogger(SessionAEJB.class);
52    
53    private static SessionB sessionB;
54    
55    public void invokeSessionB()
56    {
57       try
58       {
59          SessionB session = getSessionB();
60          
61          String JavaDoc hello = "Hello";
62          String JavaDoc result = session.sayHello(hello);
63          validate(hello, result);
64       }
65       catch (Exception JavaDoc e)
66       {
67          throw new EJBException JavaDoc(e);
68       }
69    }
70    
71    public void testTxToNotSupported()
72    {
73       setTransaction();
74       try
75       {
76          SessionB session = getSessionB();
77          
78          String JavaDoc hello = "Hello";
79          String JavaDoc result = session.testNotSupported(hello);
80          validate(hello, result);
81       }
82       catch (Exception JavaDoc e)
83       {
84          throw new EJBException JavaDoc(e);
85       }
86       finally
87       {
88          unsetTransaction();
89       }
90    }
91    
92    public void testTxToRequired()
93    {
94       setTransaction();
95       try
96       {
97          SessionB session = getSessionB();
98          
99          String JavaDoc hello = "Hello";
100          String JavaDoc result = session.testRequired(hello);
101          throw new EJBException JavaDoc("Expected RemoteException");
102       }
103       catch (EJBException JavaDoc e)
104       {
105          throw e;
106       }
107       catch (RemoteException JavaDoc expected)
108       {
109       }
110       catch (Exception JavaDoc e)
111       {
112          throw new EJBException JavaDoc(e);
113       }
114       finally
115       {
116          unsetTransaction();
117       }
118    }
119    
120    public void testTxToSupports()
121    {
122       setTransaction();
123       try
124       {
125          SessionB session = getSessionB();
126          
127          String JavaDoc hello = "Hello";
128          String JavaDoc result = session.testSupports(hello);
129          throw new EJBException JavaDoc("Expected RemoteException");
130       }
131       catch (EJBException JavaDoc e)
132       {
133          throw e;
134       }
135       catch (RemoteException JavaDoc expected)
136       {
137       }
138       catch (Exception JavaDoc e)
139       {
140          throw new EJBException JavaDoc(e);
141       }
142       finally
143       {
144          unsetTransaction();
145       }
146    }
147    
148    public void testTxToRequiresNew()
149    {
150       setTransaction();
151       try
152       {
153          SessionB session = getSessionB();
154          
155          String JavaDoc hello = "Hello";
156          String JavaDoc result = session.testRequiresNew(hello);
157          validate(hello, result);
158       }
159       catch (Exception JavaDoc e)
160       {
161          throw new EJBException JavaDoc(e);
162       }
163       finally
164       {
165          unsetTransaction();
166       }
167    }
168    
169    public void testTxToMandatory()
170    {
171       setTransaction();
172       try
173       {
174          SessionB session = getSessionB();
175          
176          String JavaDoc hello = "Hello";
177          String JavaDoc result = session.testMandatory(hello);
178          throw new EJBException JavaDoc("Expected RemoteException");
179       }
180       catch (EJBException JavaDoc e)
181       {
182          throw e;
183       }
184       catch (RemoteException JavaDoc expected)
185       {
186       }
187       catch (Exception JavaDoc e)
188       {
189          throw new EJBException JavaDoc(e);
190       }
191       finally
192       {
193          unsetTransaction();
194       }
195    }
196    
197    public void testTxToNever()
198    {
199       setTransaction();
200       try
201       {
202          SessionB session = getSessionB();
203          
204          String JavaDoc hello = "Hello";
205          String JavaDoc result = session.testNever(hello);
206          throw new EJBException JavaDoc("Expected RemoteException");
207       }
208       catch (EJBException JavaDoc e)
209       {
210          throw e;
211       }
212       catch (RemoteException JavaDoc expected)
213       {
214       }
215       catch (Exception JavaDoc e)
216       {
217          throw new EJBException JavaDoc(e);
218       }
219       finally
220       {
221          unsetTransaction();
222       }
223    }
224    
225    public void testNoTxToNotSupported()
226    {
227       try
228       {
229          SessionB session = getSessionB();
230          
231          String JavaDoc hello = "Hello";
232          String JavaDoc result = session.testNotSupported(hello);
233          validate(hello, result);
234       }
235       catch (Exception JavaDoc e)
236       {
237          throw new EJBException JavaDoc(e);
238       }
239    }
240    
241    public void testNoTxToRequired()
242    {
243       try
244       {
245          SessionB session = getSessionB();
246          
247          String JavaDoc hello = "Hello";
248          String JavaDoc result = session.testRequired(hello);
249          validate(hello, result);
250       }
251       catch (Exception JavaDoc e)
252       {
253          throw new EJBException JavaDoc(e);
254       }
255    }
256    
257    public void testNoTxToSupports()
258    {
259       try
260       {
261          SessionB session = getSessionB();
262          
263          String JavaDoc hello = "Hello";
264          String JavaDoc result = session.testSupports(hello);
265          validate(hello, result);
266       }
267       catch (Exception JavaDoc e)
268       {
269          throw new EJBException JavaDoc(e);
270       }
271    }
272    
273    public void testNoTxToRequiresNew()
274    {
275       try
276       {
277          SessionB session = getSessionB();
278          
279          String JavaDoc hello = "Hello";
280          String JavaDoc result = session.testRequiresNew(hello);
281          validate(hello, result);
282       }
283       catch (Exception JavaDoc e)
284       {
285          throw new EJBException JavaDoc(e);
286       }
287    }
288    
289    public void testNoTxToMandatory()
290    {
291       try
292       {
293          SessionB session = getSessionB();
294          
295          String JavaDoc hello = "Hello";
296          String JavaDoc result = session.testMandatory(hello);
297          throw new EJBException JavaDoc("Expected RemoteException");
298       }
299       catch (EJBException JavaDoc e)
300       {
301          throw e;
302       }
303       catch (RemoteException JavaDoc expected)
304       {
305       }
306       catch (Exception JavaDoc e)
307       {
308          throw new EJBException JavaDoc(e);
309       }
310    }
311    
312    public void testNoTxToNever()
313    {
314       try
315       {
316          SessionB session = getSessionB();
317          
318          String JavaDoc hello = "Hello";
319          String JavaDoc result = session.testNever(hello);
320          validate(hello, result);
321       }
322       catch (Exception JavaDoc e)
323       {
324          throw new EJBException JavaDoc(e);
325       }
326    }
327    
328    public void ejbCreate() throws CreateException JavaDoc
329    {
330    }
331    
332    public void ejbActivate()
333    {
334    }
335
336    public void ejbPassivate()
337    {
338    }
339    
340    public void ejbRemove()
341    {
342    }
343    
344    public void setSessionContext(SessionContext JavaDoc ctx)
345    {
346    }
347
348    protected void setTransaction()
349    {
350       PropagationContext pc = new PropagationContext();
351       pc.parents = new TransIdentity[0];
352       pc.current = new TransIdentity();
353       pc.current.otid = new otid_t();
354       pc.current.otid.formatID = 666;
355       pc.current.otid.bqual_length = 1;
356       pc.current.otid.tid = new byte[] { (byte) 1 };
357       pc.implementation_specific_data = getORB().create_any();
358       pc.implementation_specific_data.insert_long(1);
359       TxClientInterceptor.setOutgoingPropagationContext(pc);
360    }
361    
362    protected void unsetTransaction()
363    {
364       TxClientInterceptor.unsetOutgoingPropagationContext();
365    }
366    
367    private static void validate(String JavaDoc hello, String JavaDoc result) throws Exception JavaDoc
368    {
369       if (hello == result)
370          throw new EJBException JavaDoc("Should be pass by value");
371       if (hello.equals(result) == false)
372          throw new EJBException JavaDoc("Did not get expected 'Hello'");
373    }
374    
375    private static SessionB getSessionB() throws Exception JavaDoc
376    {
377       if (sessionB == null)
378       {
379          SessionBHome home = (SessionBHome) lookup("SessionB", SessionBHome.class);
380          sessionB = home.create();
381       }
382       return sessionB;
383    }
384
385    private static Object JavaDoc lookup(String JavaDoc name, Class JavaDoc clazz) throws Exception JavaDoc
386    {
387       Properties JavaDoc jndiProps = new Properties JavaDoc();
388       jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
389       jndiProps.put(Context.PROVIDER_URL, "corbaloc::localhost:3528/JBoss/Naming/root");
390       Context JavaDoc ctx = new InitialContext JavaDoc(jndiProps);
391       Object JavaDoc obj = ctx.lookup(name);
392       Object JavaDoc result = PortableRemoteObject.narrow(obj, clazz);
393       return result;
394    }
395    
396    private static ORB JavaDoc getORB()
397    {
398       try
399       {
400          return (ORB JavaDoc) new InitialContext JavaDoc().lookup("java:comp/ORB");
401       }
402       catch (Exception JavaDoc e)
403       {
404          throw new EJBException JavaDoc(e);
405       }
406    }
407 }
408
Popular Tags