KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > jca15 > TransactedCASLR


1 // TransactedCASLR.java
2
// Stateful Session bean
3

4 package org.objectweb.jonas.jtests.beans.jca15;
5
6 import java.rmi.RemoteException JavaDoc;
7 import javax.ejb.CreateException JavaDoc;
8 import javax.ejb.EJBException JavaDoc;
9 import javax.ejb.RemoveException JavaDoc;
10 import javax.ejb.EJBObject JavaDoc;
11 import javax.ejb.SessionBean JavaDoc;
12 import javax.ejb.SessionContext JavaDoc;
13 import javax.naming.Context JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.naming.NamingException JavaDoc;
16 import javax.sql.DataSource JavaDoc;
17 import ersatz.resourceadapter.*;
18 import javax.resource.cci.Connection JavaDoc;
19 import javax.resource.cci.LocalTransaction JavaDoc;
20 import javax.resource.spi.ManagedConnection JavaDoc;
21 import javax.resource.spi.ConnectionEvent JavaDoc;
22 import javax.resource.spi.ConnectionManager JavaDoc;
23 import javax.transaction.xa.XAResource JavaDoc;
24 import javax.transaction.xa.Xid JavaDoc;
25 import javax.transaction.SystemException JavaDoc;
26 import javax.transaction.NotSupportedException JavaDoc;
27 import javax.transaction.RollbackException JavaDoc;
28 import javax.transaction.HeuristicRollbackException JavaDoc;
29 import javax.transaction.HeuristicMixedException JavaDoc;
30
31
32 /**
33  *
34  */

35 public class TransactedCASLR implements SessionBean JavaDoc {
36
37     private SessionContext JavaDoc ejbContext;
38     private ManagedConnectionImpl mcf = null; //Managed Connection Factory
39
private ConnectionSpecImpl csp = null; //ConnectionSpec
40
private ConnectionFactoryImpl cccf = null; //Common Client Connection Factory
41
private ConnectionImpl conn = null;
42     private InitialContext JavaDoc ic=null;
43     private String JavaDoc cName = "TransactedCASLR";
44     public boolean NoTransaction = false;
45     public boolean LoTransaction = false;
46     public boolean XATransaction = false;
47     final public int CLOSE_HANDLE = 0;
48     final public int CLOSE_PHYSICAL = 1;
49     String JavaDoc xidTest;
50
51     // ------------------------------------------------------------------
52
// SessionBean implementation
53
// ------------------------------------------------------------------
54

55
56     public void setSessionContext(SessionContext JavaDoc ctx) {
57         Utility.log(cName+".setSessionContext");
58         ejbContext = ctx;
59     }
60         
61
62     public void ejbRemove() {
63         Utility.log("");
64     }
65         
66
67     public void ejbCreate() throws CreateException JavaDoc {
68         Utility.log("");
69     }
70
71     public void ejbPassivate() {
72         Utility.log("");
73     }
74
75     public void ejbActivate() {
76         Utility.log("");
77     }
78     /**
79      * closeUp
80      */

81     public void closeUp(int closeType) {
82         Utility.log(cName+
83                              ".closeUp (enter) closeType="+closeType);
84         try {
85             if (closeType==CLOSE_PHYSICAL) {
86                 // The CONNECTION_ERROR_OCCURRED indicates that the associated
87
// ManagedConnectionImpl instance is now invalid and unusable.
88
conn.close(ConnectionEvent.CONNECTION_ERROR_OCCURRED);
89                 Utility.log(cName+
90                     ".closeUp (exit) : closed physical connection closeType="+closeType+
91                     " ConnectionEvent.CONNECTION_ERROR_OCCURRED="+
92                       ConnectionEvent.CONNECTION_ERROR_OCCURRED);
93             } else {
94                 // The CONNECTION_CLOSED indicates that connection handle
95
// is closed, but physical connection still exists
96
conn.close();
97                 Utility.log(cName+
98                              ".closeUp (exit) : closed connection closeType="+closeType);
99             }
100         } catch (Exception JavaDoc e) {
101             Utility.log(cName+".closeUp (exit) error: close "+
102                         "handle/physical connection failed closeType="+closeType);
103         }
104     }
105     
106     // ------------------------------------------------------------------
107
// transacted implementation
108
// ------------------------------------------------------------------
109

110     /**
111      * method1
112      */

113     public void method1(String JavaDoc rar_jndi_name, String JavaDoc testName)
114             throws Exception JavaDoc
115     {
116         Utility.log("============================ "+testName);
117
118         if ("eis/ErsatzXATransaction".equals(rar_jndi_name))
119             XATransaction = true;
120         else if ("eis/ErsatzLoTransaction".equals(rar_jndi_name))
121             LoTransaction = true;
122         else
123             NoTransaction = true;
124
125         try {
126             ic = new InitialContext JavaDoc();
127         } catch (Exception JavaDoc e1) {
128             Utility.log(cName+".method1 error: InitialContext failed");
129             throw e1;
130         }
131         try {
132             cccf = (ConnectionFactoryImpl)ic.lookup(rar_jndi_name);
133             Utility.log(cName+".method1 : found "+rar_jndi_name);
134         } catch (Exception JavaDoc e2) {
135             Utility.log(cName+".method1 error: lookup failed for "+rar_jndi_name);
136             throw e2;
137         }
138         
139         try {
140             csp = new ConnectionSpecImpl(); // get a new ConnectionSpecImpl
141

142         } catch (Exception JavaDoc e3) {
143             Utility.log(cName+".method1 : new connection spec failed");
144             throw e3;
145         }
146         try {
147             conn = (ConnectionImpl)cccf.getConnection();
148             Utility.log(cName+".method1 : getConnection conn="+conn);
149             if (conn==null) {
150                 Utility.log(cName+".method1 error: getConnection returned null connection.");
151                 throw new Exception JavaDoc("");
152             }
153         } catch (Exception JavaDoc e4) {
154             Utility.log(cName+".method1 error: getConnection failed "
155                     +e4.toString());
156             throw e4;
157         }
158         if (testName.equals("testIsXaresource")) {
159             try {
160                 xidTest = doXid();
161             } catch (Exception JavaDoc e) { throw e; }
162         }
163         if (testName.equals("testIsXAend2")) {
164             try {
165                 closeUp(CLOSE_PHYSICAL);
166                 xidTest = doXid();
167             } catch (Exception JavaDoc e) { throw e; }
168         }
169     }
170     public String JavaDoc getXid()
171     {
172         return xidTest; // xidTest == OK | FAIL
173
}
174     
175     public String JavaDoc doXid()
176                 throws Exception JavaDoc
177     {
178         String JavaDoc ret;
179         Utility.log(cName+".doXid [enter]");
180         if (conn==null) {
181             Utility.log(cName+".doXid [exit] error: Connection=null");
182             return "FAIL";
183         }
184         ConnectionImpl conni = (ConnectionImpl)conn;
185         try {
186             ManagedConnectionImpl mc = (ManagedConnectionImpl)conni.getMC(); //get ManagedConnectionImpl
187
if (mc==null) {
188                 Utility.log(cName+".doXid mc==null Should be null in testIsXAend2");
189                 ret = "FAIL";
190             } else {
191                 XAResourceImpl xar = mc.getCurrentXar();
192                 if (xar==null) {
193                     Utility.log(cName+".doXid error: failed xar==null");
194                     ret = "FAIL";
195                 } else {
196                     Xid JavaDoc xid = xar.getCurrentXid();
197                     if (xid==null) {
198                        Utility.log(cName+".doXid error: xid="+xid);
199                        ret = "FAIL";
200                     } else {
201                        ret = "OK";
202                     }
203                 }
204             }
205             Utility.log(cName+".doXid [exit] with "+ret);
206             return ret;
207         } catch (IllegalStateException JavaDoc ise) {
208             String JavaDoc s=cName+".doXid error: failed IllegalStateException="+ise;
209             Utility.log(s);
210             throw new Exception JavaDoc(s);
211         } catch (Exception JavaDoc e) {
212             String JavaDoc s=cName+".doXid error: failed "+e;
213             Utility.log(s);
214             throw new Exception JavaDoc(s);
215         }
216     }
217     /**
218      * After the newly created ManagedConnectionFactory instance has been configured with
219      * its property set, the application server creates a new ConnectionManager instance.
220      * true returned if ConnectionManager is valid
221      */

222     public boolean getCMInstance()
223             throws Exception JavaDoc
224     {
225         ConnectionManager JavaDoc cm;
226         cm=cccf.getCM();
227         if (cm==null) { // ConnectionManager not null
228
Utility.log(cName+".getCMInstance error: ConnectionManager is null");
229             return false;
230         }
231         else {
232             Utility.log(cName+".getCMInstance ConnectionManager is o.k.");
233             return true;
234         }
235     }
236     LocalTransactionImpl lt=null;
237     public void beginLoTransaction() {
238         Utility.log(cName+".beginLoTransaction (enter)");
239         try {
240             LocalTransaction JavaDoc l = conn.getLocalTransaction();
241             lt = (LocalTransactionImpl) l;
242             lt.begin();
243             int s=lt.getTxState();
244             Utility.log(cName+".beginLoTransaction (exit) State="+s);
245         } catch (Exception JavaDoc e) {
246             Utility.log(cName+".beginLoTransaction (exit) error:"
247                   +" "+e.toString());
248                     
249         }
250     }
251     public void commitLoTransaction() throws Exception JavaDoc
252     {
253         Utility.log(cName+".commitLoTransaction (enter)");
254         try {
255             if (lt==null) {
256                 Exception JavaDoc e = new Exception JavaDoc("Undefined LocalTransaction");
257                 throw e;
258             }
259             lt.commit();
260             int s=lt.getTxState();
261             Utility.log(cName+".commitLoTransaction (exit) ="+s);
262         } catch (Exception JavaDoc e) {
263             Utility.log(cName+".commitLoTransaction (exit) error: State error."
264                   +" "+e.getMessage());
265             throw e;
266         }
267         lt=null;
268     }
269     public void rollbackLoTransaction() {
270         Utility.log(cName+".rollbackLoTransaction (enter)");
271         try {
272             if (lt==null) {
273                 Exception JavaDoc e = new Exception JavaDoc("Undefined LocalTransaction");
274                 throw e;
275             }
276             lt.rollback();
277             int s=lt.getTxState();
278             Utility.log(cName+".rollbackLoTransaction (exit) State="+s);
279         } catch (Exception JavaDoc e) {
280             Utility.log(cName+".rollbackLoTransaction (exit) error: State error"
281                   +" "+e.getMessage());
282                     
283         }
284         lt=null;
285     }
286 }
287
288
Popular Tags