001: // TransactedCASLR.java
002: // Stateful Session bean
003:
004: package org.objectweb.jonas.jtests.beans.jca15;
005:
006: import java.rmi.RemoteException;
007: import javax.ejb.CreateException;
008: import javax.ejb.EJBException;
009: import javax.ejb.RemoveException;
010: import javax.ejb.EJBObject;
011: import javax.ejb.SessionBean;
012: import javax.ejb.SessionContext;
013: import javax.naming.Context;
014: import javax.naming.InitialContext;
015: import javax.naming.NamingException;
016: import javax.sql.DataSource;
017: import ersatz.resourceadapter.*;
018: import javax.resource.cci.Connection;
019: import javax.resource.cci.LocalTransaction;
020: import javax.resource.spi.ManagedConnection;
021: import javax.resource.spi.ConnectionEvent;
022: import javax.resource.spi.ConnectionManager;
023: import javax.transaction.xa.XAResource;
024: import javax.transaction.xa.Xid;
025: import javax.transaction.SystemException;
026: import javax.transaction.NotSupportedException;
027: import javax.transaction.RollbackException;
028: import javax.transaction.HeuristicRollbackException;
029: import javax.transaction.HeuristicMixedException;
030:
031: /**
032: *
033: */
034: public class TransactedCASLR implements SessionBean {
035:
036: private SessionContext ejbContext;
037: private ManagedConnectionImpl mcf = null; //Managed Connection Factory
038: private ConnectionSpecImpl csp = null; //ConnectionSpec
039: private ConnectionFactoryImpl cccf = null; //Common Client Connection Factory
040: private ConnectionImpl conn = null;
041: private InitialContext ic = null;
042: private String cName = "TransactedCASLR";
043: public boolean NoTransaction = false;
044: public boolean LoTransaction = false;
045: public boolean XATransaction = false;
046: final public int CLOSE_HANDLE = 0;
047: final public int CLOSE_PHYSICAL = 1;
048: String xidTest;
049:
050: // ------------------------------------------------------------------
051: // SessionBean implementation
052: // ------------------------------------------------------------------
053:
054: public void setSessionContext(SessionContext ctx) {
055: Utility.log(cName + ".setSessionContext");
056: ejbContext = ctx;
057: }
058:
059: public void ejbRemove() {
060: Utility.log("");
061: }
062:
063: public void ejbCreate() throws CreateException {
064: Utility.log("");
065: }
066:
067: public void ejbPassivate() {
068: Utility.log("");
069: }
070:
071: public void ejbActivate() {
072: Utility.log("");
073: }
074:
075: /**
076: * closeUp
077: */
078: public void closeUp(int closeType) {
079: Utility.log(cName + ".closeUp (enter) closeType=" + closeType);
080: try {
081: if (closeType == CLOSE_PHYSICAL) {
082: // The CONNECTION_ERROR_OCCURRED indicates that the associated
083: // ManagedConnectionImpl instance is now invalid and unusable.
084: conn.close(ConnectionEvent.CONNECTION_ERROR_OCCURRED);
085: Utility
086: .log(cName
087: + ".closeUp (exit) : closed physical connection closeType="
088: + closeType
089: + " ConnectionEvent.CONNECTION_ERROR_OCCURRED="
090: + ConnectionEvent.CONNECTION_ERROR_OCCURRED);
091: } else {
092: // The CONNECTION_CLOSED indicates that connection handle
093: // is closed, but physical connection still exists
094: conn.close();
095: Utility
096: .log(cName
097: + ".closeUp (exit) : closed connection closeType="
098: + closeType);
099: }
100: } catch (Exception e) {
101: Utility.log(cName + ".closeUp (exit) error: close "
102: + "handle/physical connection failed closeType="
103: + closeType);
104: }
105: }
106:
107: // ------------------------------------------------------------------
108: // transacted implementation
109: // ------------------------------------------------------------------
110:
111: /**
112: * method1
113: */
114: public void method1(String rar_jndi_name, String testName)
115: throws Exception {
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();
127: } catch (Exception e1) {
128: Utility
129: .log(cName
130: + ".method1 error: InitialContext failed");
131: throw e1;
132: }
133: try {
134: cccf = (ConnectionFactoryImpl) ic.lookup(rar_jndi_name);
135: Utility.log(cName + ".method1 : found " + rar_jndi_name);
136: } catch (Exception e2) {
137: Utility.log(cName + ".method1 error: lookup failed for "
138: + rar_jndi_name);
139: throw e2;
140: }
141:
142: try {
143: csp = new ConnectionSpecImpl(); // get a new ConnectionSpecImpl
144:
145: } catch (Exception e3) {
146: Utility
147: .log(cName
148: + ".method1 : new connection spec failed");
149: throw e3;
150: }
151: try {
152: conn = (ConnectionImpl) cccf.getConnection();
153: Utility
154: .log(cName + ".method1 : getConnection conn="
155: + conn);
156: if (conn == null) {
157: Utility
158: .log(cName
159: + ".method1 error: getConnection returned null connection.");
160: throw new Exception("");
161: }
162: } catch (Exception e4) {
163: Utility.log(cName + ".method1 error: getConnection failed "
164: + e4.toString());
165: throw e4;
166: }
167: if (testName.equals("testIsXaresource")) {
168: try {
169: xidTest = doXid();
170: } catch (Exception e) {
171: throw e;
172: }
173: }
174: if (testName.equals("testIsXAend2")) {
175: try {
176: closeUp(CLOSE_PHYSICAL);
177: xidTest = doXid();
178: } catch (Exception e) {
179: throw e;
180: }
181: }
182: }
183:
184: public String getXid() {
185: return xidTest; // xidTest == OK | FAIL
186: }
187:
188: public String doXid() throws Exception {
189: String ret;
190: Utility.log(cName + ".doXid [enter]");
191: if (conn == null) {
192: Utility.log(cName + ".doXid [exit] error: Connection=null");
193: return "FAIL";
194: }
195: ConnectionImpl conni = (ConnectionImpl) conn;
196: try {
197: ManagedConnectionImpl mc = (ManagedConnectionImpl) conni
198: .getMC(); //get ManagedConnectionImpl
199: if (mc == null) {
200: Utility
201: .log(cName
202: + ".doXid mc==null Should be null in testIsXAend2");
203: ret = "FAIL";
204: } else {
205: XAResourceImpl xar = mc.getCurrentXar();
206: if (xar == null) {
207: Utility.log(cName
208: + ".doXid error: failed xar==null");
209: ret = "FAIL";
210: } else {
211: Xid xid = xar.getCurrentXid();
212: if (xid == null) {
213: Utility.log(cName + ".doXid error: xid=" + xid);
214: ret = "FAIL";
215: } else {
216: ret = "OK";
217: }
218: }
219: }
220: Utility.log(cName + ".doXid [exit] with " + ret);
221: return ret;
222: } catch (IllegalStateException ise) {
223: String s = cName
224: + ".doXid error: failed IllegalStateException="
225: + ise;
226: Utility.log(s);
227: throw new Exception(s);
228: } catch (Exception e) {
229: String s = cName + ".doXid error: failed " + e;
230: Utility.log(s);
231: throw new Exception(s);
232: }
233: }
234:
235: /**
236: * After the newly created ManagedConnectionFactory instance has been configured with
237: * its property set, the application server creates a new ConnectionManager instance.
238: * true returned if ConnectionManager is valid
239: */
240: public boolean getCMInstance() throws Exception {
241: ConnectionManager cm;
242: cm = cccf.getCM();
243: if (cm == null) { // ConnectionManager not null
244: Utility
245: .log(cName
246: + ".getCMInstance error: ConnectionManager is null");
247: return false;
248: } else {
249: Utility.log(cName
250: + ".getCMInstance ConnectionManager is o.k.");
251: return true;
252: }
253: }
254:
255: LocalTransactionImpl lt = null;
256:
257: public void beginLoTransaction() {
258: Utility.log(cName + ".beginLoTransaction (enter)");
259: try {
260: LocalTransaction l = conn.getLocalTransaction();
261: lt = (LocalTransactionImpl) l;
262: lt.begin();
263: int s = lt.getTxState();
264: Utility
265: .log(cName + ".beginLoTransaction (exit) State="
266: + s);
267: } catch (Exception e) {
268: Utility.log(cName + ".beginLoTransaction (exit) error:"
269: + " " + e.toString());
270:
271: }
272: }
273:
274: public void commitLoTransaction() throws Exception {
275: Utility.log(cName + ".commitLoTransaction (enter)");
276: try {
277: if (lt == null) {
278: Exception e = new Exception(
279: "Undefined LocalTransaction");
280: throw e;
281: }
282: lt.commit();
283: int s = lt.getTxState();
284: Utility.log(cName + ".commitLoTransaction (exit) =" + s);
285: } catch (Exception e) {
286: Utility.log(cName
287: + ".commitLoTransaction (exit) error: State error."
288: + " " + e.getMessage());
289: throw e;
290: }
291: lt = null;
292: }
293:
294: public void rollbackLoTransaction() {
295: Utility.log(cName + ".rollbackLoTransaction (enter)");
296: try {
297: if (lt == null) {
298: Exception e = new Exception(
299: "Undefined LocalTransaction");
300: throw e;
301: }
302: lt.rollback();
303: int s = lt.getTxState();
304: Utility.log(cName + ".rollbackLoTransaction (exit) State="
305: + s);
306: } catch (Exception e) {
307: Utility
308: .log(cName
309: + ".rollbackLoTransaction (exit) error: State error"
310: + " " + e.getMessage());
311:
312: }
313: lt = null;
314: }
315: }
|