KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > perf > ejb > TxSessionBean


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.perf.ejb;
23
24 import java.rmi.RemoteException JavaDoc;
25 import javax.ejb.CreateException JavaDoc;
26 import javax.ejb.EJBException JavaDoc;
27 import javax.ejb.SessionBean JavaDoc;
28 import javax.ejb.SessionContext JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import javax.transaction.Transaction JavaDoc;
33 import javax.transaction.TransactionManager JavaDoc;
34
35 import org.jboss.test.perf.interfaces.TxSession;
36
37 public class TxSessionBean implements SessionBean
38 {
39    private SessionContext JavaDoc sessionContext;
40    private InitialContext JavaDoc iniCtx;
41    
42    public void ejbCreate() throws CreateException JavaDoc
43    {
44    }
45    
46    public void ejbActivate()
47    {
48    }
49    
50    public void ejbPassivate()
51    {
52    }
53    
54    public void ejbRemove()
55    {
56    }
57
58    public void setSessionContext(SessionContext JavaDoc context)
59    {
60       sessionContext = context;
61       try
62       {
63          iniCtx = new InitialContext JavaDoc();
64       }
65       catch(NamingException JavaDoc e)
66       {
67          throw new EJBException JavaDoc(e);
68       }
69    }
70    
71   /*
72    * This method is defined with "Required"
73    */

74    public String JavaDoc txRequired()
75    {
76       Transaction JavaDoc tx = getDaTransaction();
77       if (tx == null)
78          throw new EJBException JavaDoc("Required sees no transaction");
79       else
80          return ("required sees a transaction "+tx.hashCode());
81    }
82    
83    
84   /*
85    * This method is defined with "Requires_new"
86    */

87    public String JavaDoc txRequiresNew()
88    {
89       Object JavaDoc tx =getDaTransaction();
90       if (tx == null)
91          throw new EJBException JavaDoc("RequiresNew sees no transaction");
92       else
93          return ("requiresNew sees a transaction "+tx.hashCode());
94    }
95    
96    /*
97     * testSupports is defined with Supports
98     */

99    public String JavaDoc txSupports()
100    {
101       Object JavaDoc tx =getDaTransaction();
102       if (tx == null)
103          return "supports sees no transaction";
104       else
105          return "supports sees a transaction "+tx.hashCode();
106    }
107
108   /*
109    * This method is defined with "Mandatory"
110    */

111    public String JavaDoc txMandatory()
112    {
113       Object JavaDoc tx =getDaTransaction();
114       if (tx == null)
115          throw new EJBException JavaDoc("Mandatory sees no transaction");
116       else
117          return ("mandatory sees a transaction "+tx.hashCode());
118    }
119    
120    /*
121     * This method is defined with "Never"
122     */

123    public String JavaDoc txNever()
124    {
125       Object JavaDoc tx =getDaTransaction();
126       if (tx == null)
127          return "never sees no transaction";
128       else
129          throw new EJBException JavaDoc("txNever sees a transaction");
130    }
131    
132     /*
133      * This method is defined with "TxNotSupported"
134      */

135    public String JavaDoc txNotSupported()
136    {
137       Object JavaDoc tx =getDaTransaction();
138       if (tx == null)
139          return "notSupported sees no transaction";
140       else
141          throw new EJBException JavaDoc("txNotSupported sees a transaction");
142    }
143    
144   /*
145    * This method is defined with "Required" and it passes it to a Supports Tx
146    */

147    public String JavaDoc requiredToSupports() throws RemoteException JavaDoc
148    {
149       String JavaDoc message;
150       Object JavaDoc tx =getDaTransaction();
151       if (tx == null)
152          throw new EJBException JavaDoc("Required doesn't see a transaction");
153       else
154          message = "Required sees a transaction "+tx.hashCode()+ " Supports should see the same ";
155       
156       message = message + ((TxSession) sessionContext.getEJBObject()).txSupports();
157       
158       // And after invocation we should have the same transaction
159
tx =getDaTransaction();
160       if (tx == null)
161          throw new EJBException JavaDoc("Required doesn't see a transaction COMING BACK");
162       else
163          return message + " on coming back Required sees a transaction "+tx.hashCode() ;
164       
165    }
166
167     /*
168      * This method is defined with "Required" and it passes it to a Mandatory Tx
169      */

170    public String JavaDoc requiredToMandatory() throws RemoteException JavaDoc
171    {
172       String JavaDoc message;
173       Object JavaDoc tx =getDaTransaction();
174       if (tx == null)
175          throw new EJBException JavaDoc("Required doesn't see a transaction");
176       else
177          message = "Required sees a transaction "+tx.hashCode()+ " NotSupported should see the same ";
178       
179       
180       message = message + ((TxSession) sessionContext.getEJBObject()).txMandatory();
181       
182       // And after invocation we should have the same transaction
183
tx =getDaTransaction();
184       if (tx == null)
185          throw new EJBException JavaDoc("Required doesn't see a transaction COMING BACK");
186       else
187          return message + " on coming back Required sees a transaction "+tx.hashCode() ;
188    }
189    
190    public String JavaDoc requiredToRequiresNew() throws RemoteException JavaDoc
191    {
192       String JavaDoc message;
193       Object JavaDoc tx =getDaTransaction();
194       if (tx == null)
195          throw new EJBException JavaDoc("Required doesn't see a transaction");
196       else
197          message = "Required sees a transaction "+tx.hashCode()+ " Requires new should see a new transaction ";
198       
199       message = message + ((TxSession) sessionContext.getEJBObject()).txRequiresNew();
200       
201       // And after invocation we should have the same transaction
202
tx = getDaTransaction();
203       if (tx == null)
204          throw new EJBException JavaDoc("Required doesn't see a transaction COMING BACK");
205       else
206          return message + " on coming back Required sees a transaction "+tx.hashCode() ;
207    }
208
209    private Transaction JavaDoc getDaTransaction()
210    {
211       Transaction JavaDoc t = null;
212       try
213       {
214          TransactionManager JavaDoc tm = (TransactionManager JavaDoc) iniCtx.lookup("java:/TransactionManager");
215          t = tm.getTransaction();
216       }
217       catch(Exception JavaDoc e)
218       {
219       }
220       return t;
221    }
222    
223 }
224
Popular Tags