KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > testbean > bean > 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
23
24 package org.jboss.test.testbean.bean;
25
26 import java.rmi.*;
27 import javax.ejb.*;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import org.jboss.test.testbean.interfaces.TxSession;
31
32
33 public class TxSessionBean implements SessionBean {
34    org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass());
35   private SessionContext sessionContext;
36
37   public void ejbCreate() throws RemoteException, CreateException {
38   log.debug("TxSessionBean.ejbCreate() called");
39   }
40
41   public void ejbActivate() throws RemoteException {
42     log.debug("TxSessionBean.ejbActivate() called");
43   }
44
45   public void ejbPassivate() throws RemoteException {
46       log.debug("TxSessionBean.ejbPassivate() called");
47   }
48
49   public void ejbRemove() throws RemoteException {
50      log.debug("TxSessionBean.ejbRemove() called");
51   }
52
53   public void setSessionContext(SessionContext context) throws RemoteException {
54     sessionContext = context;
55     //Exception e = new Exception("in set Session context");
56
//log.debug("failed", e);
57
}
58
59   /*
60   * This method is defined with "Required"
61   */

62   public String JavaDoc txRequired() {
63  
64       log.debug("TxSessionBean.txRequired() called");
65       
66       try {
67          Object JavaDoc tx =getDaTransaction();
68          if (tx == null)
69            throw new Error JavaDoc("Required sees no transaction");
70          else
71            return ("required sees a transaction "+tx.hashCode());
72       }
73       catch (Exception JavaDoc e) {log.debug("failed", e); return e.getMessage();}
74    }
75   
76   
77   /*
78   * This method is defined with "Requires_new"
79   */

80   public String JavaDoc txRequiresNew() {
81  
82       log.debug("TxSessionBean.txRequiresNew() called");
83       
84       try {
85          Object JavaDoc tx =getDaTransaction();
86          if (tx == null)
87            throw new Error JavaDoc("RequiresNew sees no transaction");
88          else
89            return ("requiresNew sees a transaction "+tx.hashCode());
90       }
91       catch (Exception JavaDoc e) {log.debug("failed", e);return e.getMessage();}
92    }
93   
94    /*
95   * testSupports is defined with Supports
96   */

97    
98   public String JavaDoc txSupports() {
99       
100       log.debug("TxSessionBean.txSupports() called");
101       
102       try {
103           
104         Object JavaDoc tx =getDaTransaction();
105         
106         if (tx == null)
107            return "supports sees no transaction";
108         else
109            return "supports sees a transaction "+tx.hashCode();
110     }
111     catch (Exception JavaDoc e) {log.debug("failed", e);return e.getMessage();}
112  }
113  
114  
115   /*
116   * This method is defined with "Mandatory"
117   */

118   public String JavaDoc txMandatory() {
119  
120       log.debug("TxSessionBean.txMandatory() called");
121       
122       try {
123          Object JavaDoc tx =getDaTransaction();
124          if (tx == null)
125            throw new Error JavaDoc("Mandatory sees no transaction");
126          else
127            return ("mandatory sees a transaction "+tx.hashCode());
128       }
129       catch (Exception JavaDoc e) {log.debug("failed", e);return e.getMessage();}
130    }
131   
132    /*
133   * This method is defined with "Never"
134   */

135   public String JavaDoc txNever() {
136  
137       log.debug("TxSessionBean.txNever() called");
138       
139       try {
140          Object JavaDoc tx =getDaTransaction();
141          if (tx == null)
142            return "never sees no transaction";
143          else
144              throw new Error JavaDoc("txNever sees a transaction");
145       }
146       catch (Exception JavaDoc e) {log.debug("failed", e);return e.getMessage();}
147    }
148     /*
149   * This method is defined with "TxNotSupported"
150   */

151   
152   public String JavaDoc txNotSupported() {
153  
154       log.debug("TxSessionBean.txNotSupported() called");
155       
156       try {
157          Object JavaDoc tx =getDaTransaction();
158          if (tx == null)
159            return "notSupported sees no transaction";
160          else
161              throw new Error JavaDoc("txNotSupported sees a transaction");
162       }
163       catch (Exception JavaDoc e) {log.debug("failed", e);return e.getMessage();}
164    }
165  
166   /*
167   * This method is defined with "Required" and it passes it to a Supports Tx
168   */

169   public String JavaDoc requiredToSupports() throws RemoteException {
170       
171       log.debug("TxSessionBean.requiredToSupports() called");
172       
173       String JavaDoc message;
174       Object JavaDoc tx =getDaTransaction();
175          if (tx == null)
176            throw new Error JavaDoc("Required doesn't see a transaction");
177          else
178            message = "Required sees a transaction "+tx.hashCode()+ " Supports should see the same ";
179      
180       message = message + ((TxSession) sessionContext.getEJBObject()).txSupports();
181   
182        // And after invocation we should have the same transaction
183
tx =getDaTransaction();
184          if (tx == null)
185            throw new Error 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   
191     /*
192   * This method is defined with "Required" and it passes it to a NotSupported Tx
193   */

194   public String JavaDoc requiredToNotSupported() throws RemoteException {
195       
196       log.debug("TxSessionBean.requiredToNotSupported() called");
197       
198       String JavaDoc message;
199       Object JavaDoc tx =getDaTransaction();
200          if (tx == null)
201            throw new Error JavaDoc("Required doesn't see a transaction");
202          else
203            message = "Required sees a transaction "+tx.hashCode()+ " NotSupported should see the same ";
204     
205         
206       message = message + ((TxSession) sessionContext.getEJBObject()).txNotSupported();
207       
208       // And after invocation we should have the same transaction
209
tx =getDaTransaction();
210          if (tx == null)
211            throw new Error JavaDoc("Required doesn't see a transaction COMING BACK");
212          else
213            return message + " on coming back Required sees a transaction "+tx.hashCode() ;
214   }
215   
216   public String JavaDoc requiredToRequiresNew() throws RemoteException{
217       
218       log.debug("TxSessionBean.requiredToRequiresNew() called");
219       
220       String JavaDoc message;
221       Object JavaDoc tx =getDaTransaction();
222          if (tx == null)
223            throw new Error JavaDoc("Required doesn't see a transaction");
224          else
225            message = "Required sees a transaction "+tx.hashCode()+ " Requires new should see a new transaction ";
226     
227     message = message + ((TxSession) sessionContext.getEJBObject()).txRequiresNew();
228       
229      // And after invocation we should have the same transaction
230
tx =getDaTransaction();
231          if (tx == null)
232            throw new Error JavaDoc("Required doesn't see a transaction COMING BACK");
233          else
234            return message + " on coming back Required sees a transaction "+tx.hashCode() ;
235   }
236       
237
238   
239   private Object JavaDoc getDaTransaction() {
240   
241      try {
242         
243         return ((javax.transaction.TransactionManager JavaDoc) new InitialContext JavaDoc().lookup("java:/TransactionManager")).getTransaction();
244     }
245     catch (Exception JavaDoc e) { return null;}
246   }
247              
248 }
249
Popular Tags