1 4 package org.objectweb.jonas.jtests.beans.jca15; 5 6 import java.rmi.RemoteException ; 7 import javax.ejb.CreateException ; 8 import javax.ejb.EJBException ; 9 import javax.ejb.RemoveException ; 10 import javax.ejb.EJBObject ; 11 import javax.ejb.SessionBean ; 12 import javax.ejb.SessionContext ; 13 import javax.naming.Context ; 14 import javax.naming.InitialContext ; 15 import javax.naming.NamingException ; 16 import javax.sql.DataSource ; 17 import ersatz.resourceadapter.*; 18 import javax.resource.cci.Connection ; 19 import javax.resource.cci.LocalTransaction ; 20 import javax.resource.spi.ManagedConnection ; 21 import javax.resource.spi.ConnectionEvent ; 22 import javax.resource.spi.ConnectionManager ; 23 import javax.transaction.xa.XAResource ; 24 import javax.transaction.xa.Xid ; 25 import javax.transaction.SystemException ; 26 import javax.transaction.NotSupportedException ; 27 import javax.transaction.RollbackException ; 28 import javax.transaction.HeuristicRollbackException ; 29 import javax.transaction.HeuristicMixedException ; 30 31 32 35 public class TransactedCASLR implements SessionBean { 36 37 private SessionContext ejbContext; 38 private ManagedConnectionImpl mcf = null; private ConnectionSpecImpl csp = null; private ConnectionFactoryImpl cccf = null; private ConnectionImpl conn = null; 42 private InitialContext ic=null; 43 private String 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 xidTest; 50 51 55 56 public void setSessionContext(SessionContext 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 { 68 Utility.log(""); 69 } 70 71 public void ejbPassivate() { 72 Utility.log(""); 73 } 74 75 public void ejbActivate() { 76 Utility.log(""); 77 } 78 81 public void closeUp(int closeType) { 82 Utility.log(cName+ 83 ".closeUp (enter) closeType="+closeType); 84 try { 85 if (closeType==CLOSE_PHYSICAL) { 86 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 conn.close(); 97 Utility.log(cName+ 98 ".closeUp (exit) : closed connection closeType="+closeType); 99 } 100 } catch (Exception e) { 101 Utility.log(cName+".closeUp (exit) error: close "+ 102 "handle/physical connection failed closeType="+closeType); 103 } 104 } 105 106 110 113 public void method1(String rar_jndi_name, String testName) 114 throws Exception 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 (); 127 } catch (Exception 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 e2) { 135 Utility.log(cName+".method1 error: lookup failed for "+rar_jndi_name); 136 throw e2; 137 } 138 139 try { 140 csp = new ConnectionSpecImpl(); 142 } catch (Exception 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 (""); 152 } 153 } catch (Exception 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 e) { throw e; } 162 } 163 if (testName.equals("testIsXAend2")) { 164 try { 165 closeUp(CLOSE_PHYSICAL); 166 xidTest = doXid(); 167 } catch (Exception e) { throw e; } 168 } 169 } 170 public String getXid() 171 { 172 return xidTest; } 174 175 public String doXid() 176 throws Exception 177 { 178 String 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(); 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 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 ise) { 208 String s=cName+".doXid error: failed IllegalStateException="+ise; 209 Utility.log(s); 210 throw new Exception (s); 211 } catch (Exception e) { 212 String s=cName+".doXid error: failed "+e; 213 Utility.log(s); 214 throw new Exception (s); 215 } 216 } 217 222 public boolean getCMInstance() 223 throws Exception 224 { 225 ConnectionManager cm; 226 cm=cccf.getCM(); 227 if (cm==null) { 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 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 e) { 246 Utility.log(cName+".beginLoTransaction (exit) error:" 247 +" "+e.toString()); 248 249 } 250 } 251 public void commitLoTransaction() throws Exception 252 { 253 Utility.log(cName+".commitLoTransaction (enter)"); 254 try { 255 if (lt==null) { 256 Exception e = new Exception ("Undefined LocalTransaction"); 257 throw e; 258 } 259 lt.commit(); 260 int s=lt.getTxState(); 261 Utility.log(cName+".commitLoTransaction (exit) ="+s); 262 } catch (Exception 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 e = new Exception ("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 e) { 280 Utility.log(cName+".rollbackLoTransaction (exit) error: State error" 281 +" "+e.getMessage()); 282 283 } 284 lt=null; 285 } 286 } 287 288 | Popular Tags |