1 22 package org.jboss.test.cts.ejb; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.rmi.RemoteException ; 29 30 import javax.ejb.CreateException ; 31 import javax.ejb.EJBException ; 32 import javax.ejb.EJBObject ; 33 import javax.ejb.Handle ; 34 import javax.ejb.RemoveException ; 35 import javax.naming.Context ; 36 import javax.naming.InitialContext ; 37 import javax.transaction.UserTransaction ; 38 39 import org.jboss.logging.Logger; 40 import org.jboss.test.cts.interfaces.BeanContextInfo; 41 import org.jboss.test.cts.interfaces.CtsCmpLocal; 42 import org.jboss.test.cts.interfaces.CtsCmpLocalHome; 43 import org.jboss.test.cts.interfaces.StatefulSession; 44 import org.jboss.test.cts.interfaces.StatefulSessionHome; 45 import org.jboss.test.cts.interfaces.StatelessSession; 46 import org.jboss.test.cts.interfaces.StatelessSessionHome; 47 import org.jboss.test.cts.keys.AccountPK; 48 import org.jboss.test.util.ejb.SessionSupport; 49 50 51 56 public class BMTStatefulSessionBean 57 extends SessionSupport 58 { 59 60 private static final long serialVersionUID = 1L; 61 private Logger log; 62 private String testName; 63 private int counter; 64 private CtsCmpLocal entityBean; 65 private Context enc; 66 private Handle sessionHandle; 67 private SessionRef sessionRef; 68 private byte[] statefulHandle; 69 private boolean wasActivated; 70 private boolean wasPassivated; 71 72 public void ejbCreate(String testName) 73 { 74 this.testName = testName; 75 log = Logger.getLogger(BMTStatefulSessionBean.class.getName()+"#"+testName); 76 log.debug("ejbCreate("+testName+"), ctx="+sessionCtx); 77 } 78 public void ejbCreateAlt(String testName) 79 { 80 this.testName = testName + "Alt"; 81 log = Logger.getLogger(BMTStatefulSessionBean.class.getName()+"#"+testName); 82 log.debug("ejbCreateAlt("+testName+"), ctx="+sessionCtx); 83 } 84 85 public void ejbActivate() 86 { 87 log = Logger.getLogger(BMTStatefulSessionBean.class.getName()+"#"+testName); 88 log.debug("ejbActivate( )..."); 89 wasActivated = true; 90 } 91 public void ejbPassivate() 92 { 93 log.debug("ejbPassivate( )..."); 94 wasPassivated = true; 95 } 96 97 public String getTestName() 98 { 99 return testName; 100 } 101 102 public String method1(String msg) 103 { 104 log.debug("method1( ), msg="+msg); 105 return msg; 106 } 107 108 public void incCounter () 109 { 110 counter++; 111 } 112 113 public void decCounter () 114 { 115 counter--; 116 } 117 118 public int getCounter () 119 { 120 return counter; 121 } 122 123 public void setCounter (int value) 124 { 125 counter = value; 126 } 127 128 public BeanContextInfo getBeanContextInfo () 129 throws RemoteException 130 { 131 BeanContextInfo ctx = new BeanContextInfo(); 132 133 log.debug("Getting EJBObject.."); 134 Class remoteInterface = sessionCtx.getEJBObject().getClass(); 135 ctx.remoteInterface = remoteInterface.getName(); 136 137 log.debug("Getting EJBHome..."); 138 Class homeInterface = sessionCtx.getEJBHome().getClass(); 139 ctx.homeInterface = homeInterface.getName(); 140 141 log.debug("calling setRollbackOnly( ) on context"); 142 sessionCtx.setRollbackOnly(); 143 ctx.isRollbackOnly = new Boolean (sessionCtx.getRollbackOnly()); 144 145 return ctx; 146 } 147 148 public void loopbackTest () 149 throws RemoteException 150 { 151 try 152 { 153 Context ctx = new InitialContext (); 154 StatefulSessionHome home = (StatefulSessionHome) ctx.lookup("ejbcts/StatefulSessionBean"); 155 StatefulSession sessionBean; 156 try 157 { 158 sessionBean = home.create(testName); 159 } 160 catch (CreateException crex) 161 { 162 log.debug("Loopback CreateException: " + crex); 163 throw new EJBException (crex); 164 } 165 sessionBean.loopbackTest(sessionCtx.getEJBObject()); 166 167 } 168 catch (javax.naming.NamingException nex) 169 { 170 log.debug("Could not locate bean instance"); 171 throw new EJBException (nex); 172 } 173 } 174 175 public void loopbackTest (EJBObject obj) 176 throws RemoteException 177 { 178 StatefulSession bean = ( StatefulSession ) obj; 180 181 bean.method1("Hello"); 182 } 183 184 public void ping() 185 { 186 } 187 188 public void sleep(long wait) 189 { 190 try 191 { 192 Thread.sleep(wait); 193 } 194 catch (InterruptedException e) 195 { 196 throw new EJBException ("Interrupted", e); 197 } 198 } 199 200 public boolean getWasActivated() 201 { 202 log.debug("getWasActivated( ), wasActivated="+wasActivated); 203 return wasActivated; 204 } 205 206 public boolean getWasPassivated() 207 { 208 log.debug("getWasPassivated( ), wasPassivated="+wasPassivated); 209 return wasPassivated; 210 } 211 212 public void createLocalEntity(AccountPK pk, String personsName) 213 throws CreateException 214 { 215 try 216 { 217 InitialContext ctx = new InitialContext (); 218 enc = (Context ) ctx.lookup("java:comp/env"); 219 CtsCmpLocalHome home = (CtsCmpLocalHome) enc.lookup("ejb/CMPBeanLocalHome"); 220 entityBean = home.create(pk, personsName); 221 } 222 catch(Exception e) 223 { 224 log.error("CtsCmpLocal create failed", e); 225 throw new EJBException ("CtsCmpLocal create failed", e); 226 } 227 } 228 229 public String readAndRemoveEntity() 230 throws RemoveException 231 { 232 String name = entityBean.getPersonsName(); 233 entityBean.remove(); 234 return name; 235 } 236 237 public void createSessionHandle() 238 { 239 log.info("createSessionHandle"); 240 try 241 { 242 InitialContext ctx = new InitialContext (); 243 enc = (Context ) ctx.lookup("java:comp/env"); 244 StatelessSessionHome home = (StatelessSessionHome) enc.lookup("ejb/StatelessSessionHome"); 245 StatelessSession bean = home.create(); 246 sessionHandle = bean.getHandle(); 247 } 248 catch(Exception e) 249 { 250 log.error("StatelessSessionHome create failed", e); 251 throw new EJBException ("StatelessSessionHome create failed", e); 252 } 253 } 254 255 public String useSessionHandle(String arg) 256 { 257 log.info("useSessionHandle"); 258 try 259 { 260 StatelessSession bean = (StatelessSession) sessionHandle.getEJBObject(); 261 arg = bean.method1(arg); 262 } 263 catch(Exception e) 264 { 265 log.error("StatelessSession handle failed", e); 266 throw new EJBException ("StatelessSession handle failed", e); 267 } 268 return arg; 269 } 270 271 public void createStatefulSessionHandle(String testName) 272 { 273 log.info("createStatefulSessionHandle"); 274 try 275 { 276 StatefulSessionHome home = (StatefulSessionHome) sessionCtx.getEJBHome(); 277 StatefulSession bean = home.create(testName); 278 bean.incCounter(); 279 Handle handle = bean.getHandle(); 280 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 281 ObjectOutputStream oos = new ObjectOutputStream (baos); 282 oos.writeObject(handle); 283 this.statefulHandle = baos.toByteArray(); 284 } 285 catch(Exception e) 286 { 287 log.error("Failed to serialize session handle", e); 288 throw new EJBException ("Failed to serialize session handle", e); 289 } 290 } 291 292 public void useStatefulSessionHandle() 293 { 294 log.info("useStatefulSessionHandle"); 295 try 296 { 297 ByteArrayInputStream bais = new ByteArrayInputStream (statefulHandle); 298 ObjectInputStream ois = new ObjectInputStream (bais); 299 Handle handle = (Handle ) ois.readObject(); 300 StatefulSession bean = (StatefulSession) handle.getEJBObject(); 301 bean.incCounter(); 302 int count = bean.getCounter(); 303 log.info("useStatefulSessionHandle, count="+count); 304 } 305 catch(Exception e) 306 { 307 log.error("Failed to read session from handle", e); 308 throw new EJBException ("SFailed to read session from handle", e); 309 } 310 } 311 312 public void createSessionRef() 313 throws RemoteException 314 { 315 log.info("createSessionRef"); 316 Handle handle = super.sessionCtx.getEJBObject().getHandle(); 317 this.sessionRef = new SessionRef(handle); 318 } 319 public String useSessionRef() 320 throws RemoteException 321 { 322 log.info("useSessionRef"); 323 Handle handle = sessionRef.getHandle(); 324 return handle.toString(); 325 } 326 327 public Handle getHandle() 328 { 329 try 330 { 331 return sessionCtx.getEJBObject().getHandle(); 332 } 333 catch (RemoteException e) 334 { 335 throw new EJBException (e); 336 } 337 } 338 339 public void testBadUserTx() 340 { 341 UserTransaction tx = sessionCtx.getUserTransaction(); 342 try 343 { 344 tx.begin(); 345 } 346 catch (Exception e) 347 { 348 log.error("This should work", e); 349 } 350 throw new Error ("Force a rollback without committing/rollingback"); 351 } 352 } 353 | Popular Tags |