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