1 22 package org.jboss.test.txpropiiop.ejb.a; 23 24 import java.rmi.RemoteException ; 25 import java.util.Properties ; 26 import javax.ejb.CreateException ; 27 import javax.ejb.EJBException ; 28 import javax.ejb.SessionBean ; 29 import javax.ejb.SessionContext ; 30 import javax.naming.Context ; 31 import javax.naming.InitialContext ; 32 import javax.rmi.PortableRemoteObject ; 33 34 import org.jboss.logging.Logger; 35 import org.jboss.test.txpropiiop.interfaces.b.SessionB; 36 import org.jboss.test.txpropiiop.interfaces.b.SessionBHome; 37 import org.jboss.tm.iiop.TxClientInterceptor; 38 import org.omg.CORBA.ORB ; 39 import org.omg.CosTransactions.PropagationContext; 40 import org.omg.CosTransactions.TransIdentity; 41 import org.omg.CosTransactions.otid_t; 42 43 49 public class SessionAEJB implements SessionBean 50 { 51 private static final Logger log = Logger.getLogger(SessionAEJB.class); 52 53 private static SessionB sessionB; 54 55 public void invokeSessionB() 56 { 57 try 58 { 59 SessionB session = getSessionB(); 60 61 String hello = "Hello"; 62 String result = session.sayHello(hello); 63 validate(hello, result); 64 } 65 catch (Exception e) 66 { 67 throw new EJBException (e); 68 } 69 } 70 71 public void testTxToNotSupported() 72 { 73 setTransaction(); 74 try 75 { 76 SessionB session = getSessionB(); 77 78 String hello = "Hello"; 79 String result = session.testNotSupported(hello); 80 validate(hello, result); 81 } 82 catch (Exception e) 83 { 84 throw new EJBException (e); 85 } 86 finally 87 { 88 unsetTransaction(); 89 } 90 } 91 92 public void testTxToRequired() 93 { 94 setTransaction(); 95 try 96 { 97 SessionB session = getSessionB(); 98 99 String hello = "Hello"; 100 String result = session.testRequired(hello); 101 throw new EJBException ("Expected RemoteException"); 102 } 103 catch (EJBException e) 104 { 105 throw e; 106 } 107 catch (RemoteException expected) 108 { 109 } 110 catch (Exception e) 111 { 112 throw new EJBException (e); 113 } 114 finally 115 { 116 unsetTransaction(); 117 } 118 } 119 120 public void testTxToSupports() 121 { 122 setTransaction(); 123 try 124 { 125 SessionB session = getSessionB(); 126 127 String hello = "Hello"; 128 String result = session.testSupports(hello); 129 throw new EJBException ("Expected RemoteException"); 130 } 131 catch (EJBException e) 132 { 133 throw e; 134 } 135 catch (RemoteException expected) 136 { 137 } 138 catch (Exception e) 139 { 140 throw new EJBException (e); 141 } 142 finally 143 { 144 unsetTransaction(); 145 } 146 } 147 148 public void testTxToRequiresNew() 149 { 150 setTransaction(); 151 try 152 { 153 SessionB session = getSessionB(); 154 155 String hello = "Hello"; 156 String result = session.testRequiresNew(hello); 157 validate(hello, result); 158 } 159 catch (Exception e) 160 { 161 throw new EJBException (e); 162 } 163 finally 164 { 165 unsetTransaction(); 166 } 167 } 168 169 public void testTxToMandatory() 170 { 171 setTransaction(); 172 try 173 { 174 SessionB session = getSessionB(); 175 176 String hello = "Hello"; 177 String result = session.testMandatory(hello); 178 throw new EJBException ("Expected RemoteException"); 179 } 180 catch (EJBException e) 181 { 182 throw e; 183 } 184 catch (RemoteException expected) 185 { 186 } 187 catch (Exception e) 188 { 189 throw new EJBException (e); 190 } 191 finally 192 { 193 unsetTransaction(); 194 } 195 } 196 197 public void testTxToNever() 198 { 199 setTransaction(); 200 try 201 { 202 SessionB session = getSessionB(); 203 204 String hello = "Hello"; 205 String result = session.testNever(hello); 206 throw new EJBException ("Expected RemoteException"); 207 } 208 catch (EJBException e) 209 { 210 throw e; 211 } 212 catch (RemoteException expected) 213 { 214 } 215 catch (Exception e) 216 { 217 throw new EJBException (e); 218 } 219 finally 220 { 221 unsetTransaction(); 222 } 223 } 224 225 public void testNoTxToNotSupported() 226 { 227 try 228 { 229 SessionB session = getSessionB(); 230 231 String hello = "Hello"; 232 String result = session.testNotSupported(hello); 233 validate(hello, result); 234 } 235 catch (Exception e) 236 { 237 throw new EJBException (e); 238 } 239 } 240 241 public void testNoTxToRequired() 242 { 243 try 244 { 245 SessionB session = getSessionB(); 246 247 String hello = "Hello"; 248 String result = session.testRequired(hello); 249 validate(hello, result); 250 } 251 catch (Exception e) 252 { 253 throw new EJBException (e); 254 } 255 } 256 257 public void testNoTxToSupports() 258 { 259 try 260 { 261 SessionB session = getSessionB(); 262 263 String hello = "Hello"; 264 String result = session.testSupports(hello); 265 validate(hello, result); 266 } 267 catch (Exception e) 268 { 269 throw new EJBException (e); 270 } 271 } 272 273 public void testNoTxToRequiresNew() 274 { 275 try 276 { 277 SessionB session = getSessionB(); 278 279 String hello = "Hello"; 280 String result = session.testRequiresNew(hello); 281 validate(hello, result); 282 } 283 catch (Exception e) 284 { 285 throw new EJBException (e); 286 } 287 } 288 289 public void testNoTxToMandatory() 290 { 291 try 292 { 293 SessionB session = getSessionB(); 294 295 String hello = "Hello"; 296 String result = session.testMandatory(hello); 297 throw new EJBException ("Expected RemoteException"); 298 } 299 catch (EJBException e) 300 { 301 throw e; 302 } 303 catch (RemoteException expected) 304 { 305 } 306 catch (Exception e) 307 { 308 throw new EJBException (e); 309 } 310 } 311 312 public void testNoTxToNever() 313 { 314 try 315 { 316 SessionB session = getSessionB(); 317 318 String hello = "Hello"; 319 String result = session.testNever(hello); 320 validate(hello, result); 321 } 322 catch (Exception e) 323 { 324 throw new EJBException (e); 325 } 326 } 327 328 public void ejbCreate() throws CreateException 329 { 330 } 331 332 public void ejbActivate() 333 { 334 } 335 336 public void ejbPassivate() 337 { 338 } 339 340 public void ejbRemove() 341 { 342 } 343 344 public void setSessionContext(SessionContext ctx) 345 { 346 } 347 348 protected void setTransaction() 349 { 350 PropagationContext pc = new PropagationContext(); 351 pc.parents = new TransIdentity[0]; 352 pc.current = new TransIdentity(); 353 pc.current.otid = new otid_t(); 354 pc.current.otid.formatID = 666; 355 pc.current.otid.bqual_length = 1; 356 pc.current.otid.tid = new byte[] { (byte) 1 }; 357 pc.implementation_specific_data = getORB().create_any(); 358 pc.implementation_specific_data.insert_long(1); 359 TxClientInterceptor.setOutgoingPropagationContext(pc); 360 } 361 362 protected void unsetTransaction() 363 { 364 TxClientInterceptor.unsetOutgoingPropagationContext(); 365 } 366 367 private static void validate(String hello, String result) throws Exception 368 { 369 if (hello == result) 370 throw new EJBException ("Should be pass by value"); 371 if (hello.equals(result) == false) 372 throw new EJBException ("Did not get expected 'Hello'"); 373 } 374 375 private static SessionB getSessionB() throws Exception 376 { 377 if (sessionB == null) 378 { 379 SessionBHome home = (SessionBHome) lookup("SessionB", SessionBHome.class); 380 sessionB = home.create(); 381 } 382 return sessionB; 383 } 384 385 private static Object lookup(String name, Class clazz) throws Exception 386 { 387 Properties jndiProps = new Properties (); 388 jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory"); 389 jndiProps.put(Context.PROVIDER_URL, "corbaloc::localhost:3528/JBoss/Naming/root"); 390 Context ctx = new InitialContext (jndiProps); 391 Object obj = ctx.lookup(name); 392 Object result = PortableRemoteObject.narrow(obj, clazz); 393 return result; 394 } 395 396 private static ORB getORB() 397 { 398 try 399 { 400 return (ORB ) new InitialContext ().lookup("java:comp/ORB"); 401 } 402 catch (Exception e) 403 { 404 throw new EJBException (e); 405 } 406 } 407 } 408 | Popular Tags |