KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > j2eeca > transactedCASLR


1 // transactedCASLR.java
2
// Stateless Session bean
3

4 package org.objectweb.jonas.jtests.beans.j2eeca;
5
6 import javax.ejb.CreateException JavaDoc;
7 import javax.ejb.SessionBean JavaDoc;
8 import javax.ejb.SessionContext JavaDoc;
9 import javax.naming.InitialContext JavaDoc;
10 import javax.resource.cci.LocalTransaction JavaDoc;
11 import javax.resource.spi.ConnectionEvent JavaDoc;
12 import javax.transaction.xa.Xid JavaDoc;
13
14 import org.objectweb.jonas.common.Log;
15 import org.objectweb.util.monolog.api.BasicLevel;
16 import org.objectweb.util.monolog.api.Logger;
17
18 import fictional.resourceadapter.CommonClient;
19 import fictional.resourceadapter.ConnectionImpl;
20 import fictional.resourceadapter.JtestResourceAdapter;
21 import fictional.resourceadapter.LocalTransactionImpl;
22 import fictional.resourceadapter.XAResourceImpl;
23
24
25 /**
26  *
27  */

28 public class transactedCASLR implements SessionBean JavaDoc {
29
30     static private Logger logger = null;
31     SessionContext JavaDoc ejbContext;
32     private JtestResourceAdapter mcf = null; //Managed Connection Factory
33
private CommonClient csp = null; //ConnectionSpec
34
private CommonClient cccf = null; //Common Client Connection Factory
35
private ConnectionImpl conn = null;
36     InitialContext JavaDoc ic=null;
37     String JavaDoc cName = "transactedCASLR";
38     final public int NoTransaction = 0;
39     final public int LoTransaction = 1;
40     final public int XATransaction = 2;
41     final public int CLOSE_HANDLE = 0;
42     final public int CLOSE_PHYSICAL = 1;
43     public int TransactionType = 0;
44
45     // ------------------------------------------------------------------
46
// SessionBean implementation
47
// ------------------------------------------------------------------
48

49
50     public void setSessionContext(SessionContext JavaDoc ctx) {
51         if (logger == null) {
52             logger = Log.getLogger("org.objectweb.jonas.jtests.j2eeca");
53         }
54         logger.log(BasicLevel.DEBUG, cName+".setSessionContext");
55         ejbContext = ctx;
56     }
57         
58
59     public void ejbRemove() {
60         logger.log(BasicLevel.DEBUG, "");
61     }
62         
63
64     public void ejbCreate() throws CreateException JavaDoc {
65         logger.log(BasicLevel.DEBUG, "");
66     }
67
68     public void ejbPassivate() {
69         logger.log(BasicLevel.DEBUG, "");
70     }
71
72     public void ejbActivate() {
73         logger.log(BasicLevel.DEBUG, "");
74     }
75     /**
76      * closeUp
77      */

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

107     /**
108      * method1
109      */

110     public void method1(String JavaDoc rar_jndi_name, String JavaDoc testName)
111             throws Exception JavaDoc
112     {
113         logger.log(BasicLevel.DEBUG, "============================ "+testName);
114
115         if ("FictionalXATransaction".equals(rar_jndi_name))
116             TransactionType = XATransaction;
117         else if ("FictionalLoTransaction".equals(rar_jndi_name))
118             TransactionType = LoTransaction;
119         else
120             TransactionType = NoTransaction;
121
122         try {
123             ic = new InitialContext JavaDoc();
124         } catch (Exception JavaDoc e1) {
125             logger.log(BasicLevel.DEBUG, cName+".method1 error: InitialContext failed");
126             throw e1;
127         }
128         try {
129             cccf = (CommonClient)ic.lookup(rar_jndi_name);
130             logger.log(BasicLevel.DEBUG, cName+".method1 : found "+rar_jndi_name);
131         } catch (Exception JavaDoc e2) {
132             logger.log(BasicLevel.DEBUG, cName+".method1 error: lookup failed for "+rar_jndi_name);
133             throw e2;
134         }
135         
136         try {
137             csp = new CommonClient(); // get a new ConnectionSpec
138

139         } catch (Exception JavaDoc e3) {
140             logger.log(BasicLevel.DEBUG, cName+".method1 : new connection spec failed");
141             throw e3;
142         }
143         try {
144             conn = (ConnectionImpl)cccf.getConnection();
145             logger.log(BasicLevel.DEBUG, cName+".method1 : getConnection conn="+conn);
146             if (conn==null) {
147                 logger.log(BasicLevel.DEBUG, cName+".method1 error: getConnection returned null connection.");
148                 throw new Exception JavaDoc("");
149             }
150         } catch (Exception JavaDoc e4) {
151             logger.log(BasicLevel.DEBUG, cName+".method1 error: getConnection failed "
152                     +e4.toString());
153             throw e4;
154         }
155     }
156     public String JavaDoc getXid() {
157         ConnectionImpl conni = (ConnectionImpl)conn;
158         try {
159             JtestResourceAdapter mc = (JtestResourceAdapter)conni.getMC(); //get ManagedConnection
160
XAResourceImpl xar = mc.getCurrentXar();
161             if (xar==null) {
162                 logger.log(BasicLevel.DEBUG, cName+".getXid error: failed xar==null");
163                 return "FAIL";
164             } else {
165                 Xid JavaDoc xid = xar.getCurrentXid();
166                 logger.log(BasicLevel.DEBUG, cName+".getXid ");
167                 return "OK";
168             }
169         } catch (Exception JavaDoc e) {
170             logger.log(BasicLevel.DEBUG, cName+".getXid error: failed");
171             return "FAIL";
172         }
173     }
174     /**
175      * After the newly created ManagedConnectionFactory instance has been configured with
176      * its property set, the application server creates a new ConnectionManager instance.
177      * true returned if ConnectionManager is valid
178      */

179     public boolean getCMInstance()
180     {
181         mcf = (JtestResourceAdapter) cccf.getMcf(); // ManagedConnectionFactory
182
if (mcf.getCM()==null) { // ConnectionManager not null
183
logger.log(BasicLevel.DEBUG, cName+".getCMInstance error: ConnectionManager is null");
184             return false;
185         }
186         else {
187             logger.log(BasicLevel.DEBUG, cName+".getCMInstance ConnectionManager is o.k.");
188             return true;
189         }
190     }
191     LocalTransactionImpl lt=null;
192     public void beginLoTransaction() {
193         logger.log(BasicLevel.DEBUG, cName+".beginLoTransaction (enter)");
194         try {
195             LocalTransaction JavaDoc l = conn.getLocalTransaction();
196             lt = (LocalTransactionImpl) l;
197             lt.begin();
198             int s=lt.getTxState();
199             logger.log(BasicLevel.DEBUG, cName+".beginLoTransaction (exit) State="+s);
200         } catch (Exception JavaDoc e) {
201             logger.log(BasicLevel.DEBUG, cName+".beginLoTransaction (exit) error:"
202                   +" "+e.toString());
203                     
204         }
205     }
206     public void commitLoTransaction() throws Exception JavaDoc
207     {
208         logger.log(BasicLevel.DEBUG, cName+".commitLoTransaction (enter)");
209         try {
210             if (lt==null) {
211                 Exception JavaDoc e = new Exception JavaDoc("Undefined LocalTransaction");
212                 throw e;
213             }
214             lt.commit();
215             int s=lt.getTxState();
216             logger.log(BasicLevel.DEBUG, cName+".commitLoTransaction (exit) ="+s);
217         } catch (Exception JavaDoc e) {
218             logger.log(BasicLevel.DEBUG, cName+".commitLoTransaction (exit) error: State error."
219                   +" "+e.getMessage());
220             throw e;
221         }
222         lt=null;
223     }
224     public void rollbackLoTransaction() {
225         logger.log(BasicLevel.DEBUG, cName+".rollbackLoTransaction (enter)");
226         try {
227             if (lt==null) {
228                 Exception JavaDoc e = new Exception JavaDoc("Undefined LocalTransaction");
229                 throw e;
230             }
231             lt.rollback();
232             int s=lt.getTxState();
233             logger.log(BasicLevel.DEBUG, cName+".rollbackLoTransaction (exit) State="+s);
234         } catch (Exception JavaDoc e) {
235             logger.log(BasicLevel.DEBUG, cName+".rollbackLoTransaction (exit) error: State error"
236                   +" "+e.getMessage());
237                     
238         }
239         lt=null;
240     }
241 }
242
243
Popular Tags