1 25 26 package org.objectweb.jonas_ejb.container; 27 28 import java.security.Identity ; 29 import java.security.Principal ; 30 import java.util.Properties ; 31 32 import javax.ejb.EJBException ; 33 import javax.ejb.EJBHome ; 34 import javax.ejb.EJBLocalHome ; 35 import javax.ejb.MessageDrivenBean ; 36 import javax.ejb.MessageDrivenContext ; 37 import javax.ejb.TimedObject ; 38 import javax.ejb.Timer ; 39 import javax.ejb.TimerService ; 40 import javax.jms.JMSException ; 41 import javax.jms.Message ; 42 import javax.jms.MessageListener ; 43 import javax.jms.ServerSession ; 44 import javax.jms.Session ; 45 import javax.jms.XASession ; 46 import javax.resource.spi.work.Work ; 47 import javax.resource.spi.work.WorkException ; 48 import javax.resource.spi.work.WorkManager ; 49 import javax.transaction.Status ; 50 import javax.transaction.SystemException ; 51 import javax.transaction.UserTransaction ; 52 import javax.transaction.xa.XAResource ; 53 54 import org.objectweb.transaction.jta.TransactionManager; 55 56 import org.objectweb.util.monolog.api.BasicLevel; 57 58 67 public class JMessageDrivenBean implements MessageListener , ServerSession , Work , MessageDrivenContext { 68 69 protected Session sess = null; 70 71 protected JMdbFactory bf = null; 72 73 protected MessageDrivenBean mdb = null; 74 75 79 protected int txattr; 80 81 85 protected int timerTxAttr; 86 87 protected TransactionManager tm = null; 88 89 protected WorkManager wm = null; 90 91 98 public JMessageDrivenBean(JMdbFactory bf, Session sess, MessageDrivenBean mdb, WorkManager wm) { 99 this.bf = bf; 100 this.sess = sess; 101 this.mdb = mdb; 102 this.wm = wm; 103 txattr = bf.getTransactionAttribute(); 105 timerTxAttr = bf.getTimerTxAttribute(); 106 tm = bf.getTransactionManager(); 107 } 108 109 113 119 public TimerService getTimerService() throws IllegalStateException { 120 if (TraceEjb.isDebugIc()) { 121 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 122 } 123 return bf.getTimerService(); 124 } 125 126 130 136 public synchronized void onMessage(Message message) { 137 if (TraceEjb.isDebugJms()) { 138 TraceEjb.mdb.log(BasicLevel.DEBUG, ""); 139 } 140 141 RequestCtx rctx = null; 142 try { 143 144 if (tm.getTransaction() != null) { 145 TraceEjb.logger.log(BasicLevel.ERROR, "Transaction already OPENED"); 147 TraceEjb.logger.log(BasicLevel.ERROR, "Transaction = " + tm.getTransaction()); 148 Thread.dumpStack(); 149 return; 150 } 151 152 rctx = bf.preInvoke(txattr); 153 bf.checkSecurity(null); 154 if (rctx.mustCommit) { 155 if (TraceEjb.isDebugTx()) { 156 TraceEjb.tx.log(BasicLevel.DEBUG, "enlistResource"); 157 } 158 rctx.currTx.enlistResource(((XASession ) sess).getXAResource()); 159 } 160 } catch (Exception e) { 161 TraceEjb.logger.log(BasicLevel.ERROR, "preInvoke failed: ", e); 162 return; 163 } 164 try { 165 if (TraceEjb.isDebugJms()) { 166 TraceEjb.mdb.log(BasicLevel.DEBUG, "Call MDB"); 167 } 168 ((MessageListener ) mdb).onMessage(message); 169 if (TraceEjb.isDebugJms()) { 170 TraceEjb.mdb.log(BasicLevel.DEBUG, "Return from MDB"); 171 } 172 } catch (RuntimeException e) { 173 rctx.sysExc = e; 174 TraceEjb.logger.log(BasicLevel.ERROR, "runtime exception thrown by an enterprise Bean", e); 175 } catch (Error e) { 176 rctx.sysExc = e; 177 TraceEjb.logger.log(BasicLevel.ERROR, "error thrown by an enterprise Bean", e); 178 } finally { 179 try { 180 if (rctx.mustCommit) { 181 if (TraceEjb.isDebugTx()) { 182 TraceEjb.tx.log(BasicLevel.DEBUG, "delistResource"); 183 } 184 rctx.currTx.delistResource(((XASession ) sess).getXAResource(), XAResource.TMSUCCESS); 185 } 186 bf.postInvoke(rctx); 187 } catch (Exception e) { 188 TraceEjb.logger.log(BasicLevel.ERROR, "exception on postInvoke: ", e); 189 } 190 } 191 } 192 193 197 206 public Session getSession() throws JMSException { 207 if (TraceEjb.isDebugJms()) { 208 TraceEjb.mdb.log(BasicLevel.DEBUG, ""); 209 } 210 return sess; 211 } 212 213 219 public void start() throws JMSException { 220 if (TraceEjb.isDebugJms()) { 221 TraceEjb.mdb.log(BasicLevel.DEBUG, ""); 222 } 223 try { 224 wm.scheduleWork(this); 225 } catch (WorkException e) { 226 JMSException jmsE = new JMSException ("Cannot schedule work"); 227 jmsE.initCause(e); 228 throw jmsE; 229 } 230 } 231 232 236 240 public void run() { 241 if (TraceEjb.isDebugJms()) { 242 TraceEjb.mdb.log(BasicLevel.DEBUG, ""); 243 } 244 245 Thread.currentThread().setContextClassLoader(bf.myClassLoader()); 248 249 sess.run(); 250 bf.releaseServerSession(this); 251 } 252 253 public void release() { 254 TraceEjb.mdb.log(BasicLevel.WARN, "Ignored"); 255 } 256 257 261 private static final String DISALLOWED_MSG = " is disallowed in a message driven bean"; 262 263 268 public Identity getCallerIdentity() { 269 TraceEjb.logger.log(BasicLevel.ERROR, DISALLOWED_MSG); 270 throw new IllegalStateException ("getCallerIdentity()" + DISALLOWED_MSG); 271 } 272 273 279 public Principal getCallerPrincipal() { 280 TraceEjb.logger.log(BasicLevel.ERROR, DISALLOWED_MSG); 281 throw new IllegalStateException ("getCallerPrincipal()" + DISALLOWED_MSG); 282 } 283 284 289 public boolean isCallerInRole(Identity role) { 290 TraceEjb.logger.log(BasicLevel.ERROR, DISALLOWED_MSG); 291 throw new IllegalStateException ("isCallerInRole()" + DISALLOWED_MSG); 292 } 293 294 299 public boolean isCallerInRole(java.lang.String roleLink) { 300 TraceEjb.logger.log(BasicLevel.ERROR, DISALLOWED_MSG); 301 throw new IllegalStateException ("isCallerInRole()" + DISALLOWED_MSG); 302 } 303 304 310 public void setRollbackOnly() { 311 312 if (TraceEjb.isDebugJms()) { 313 TraceEjb.mdb.log(BasicLevel.DEBUG, ""); 314 } 315 try { 316 tm.setRollbackOnly(); 317 } catch (IllegalStateException e) { 318 TraceEjb.logger.log(BasicLevel.ERROR, "current thread not associated with transaction"); 319 throw e; 320 } catch (SystemException e) { 321 TraceEjb.logger.log(BasicLevel.ERROR, "unexpected exception:", e); 322 } 323 } 324 325 329 public boolean getRollbackOnly() { 330 if (TraceEjb.isDebugJms()) { 331 TraceEjb.mdb.log(BasicLevel.DEBUG, ""); 332 } 333 334 try { 335 if (tm.getTransaction() != null) { 336 switch (tm.getStatus()) { 337 case Status.STATUS_MARKED_ROLLBACK: 338 case Status.STATUS_ROLLEDBACK: 339 case Status.STATUS_ROLLING_BACK: 340 return true; 341 case Status.STATUS_NO_TRANSACTION: 342 throw new IllegalStateException ("No transaction"); 343 default: 344 return false; 345 } 346 } else { 347 TraceEjb.logger.log(BasicLevel.ERROR, "the bean is not associated in a transaction"); 348 throw new IllegalStateException ("the message driven bean is not associated in a transaction"); 349 } 350 } catch (SystemException e) { 351 TraceEjb.logger.log(BasicLevel.ERROR, "cannot get status:", e); 352 return false; 353 } 354 } 355 356 360 public EJBHome getEJBHome() { 361 TraceEjb.logger.log(BasicLevel.ERROR, DISALLOWED_MSG); 362 throw new IllegalStateException ("getEJBHome()" + DISALLOWED_MSG); 363 } 364 365 369 public EJBLocalHome getEJBLocalHome() { 370 TraceEjb.logger.log(BasicLevel.ERROR, DISALLOWED_MSG); 371 throw new IllegalStateException ("getEJBLocalHome()" + DISALLOWED_MSG); 372 } 373 374 378 public Properties getEnvironment() { 379 TraceEjb.logger.log(BasicLevel.ERROR, "deprecated use : Use the JNDI naming context java:comp/env"); 380 return new java.util.Properties (); 381 } 382 383 391 public UserTransaction getUserTransaction() throws IllegalStateException { 392 393 if (TraceEjb.isDebugJms()) { 394 TraceEjb.mdb.log(BasicLevel.DEBUG, ""); 395 } 396 397 if (!bf.isTxBeanManaged()) { 398 throw new IllegalStateException ("This bean is not allowed to use UserTransaction interface"); 399 } 400 return (UserTransaction ) tm; 401 } 402 403 407 411 public void deliverTimeout(Timer timer) { 412 if (TraceEjb.isDebugJms()) { 413 TraceEjb.mdb.log(BasicLevel.DEBUG, ""); 414 } 415 416 RequestCtx rctx = null; 417 try { 418 rctx = bf.preInvoke(timerTxAttr); 419 } catch (Exception e) { 420 TraceEjb.logger.log(BasicLevel.ERROR, "preInvoke failed: ", e); 421 return; 422 } 423 try { 424 bf.checkSecurity(null); 425 if (mdb instanceof TimedObject ) { 426 ((TimedObject ) mdb).ejbTimeout(timer); 427 } else { 428 throw new EJBException ("The bean does not implement the `TimedObject` interface"); 429 } 430 } catch (EJBException e) { 431 rctx.sysExc = e; 432 TraceEjb.logger.log(BasicLevel.ERROR, "EJB exception thrown by an enterprise Bean", e); 433 } catch (RuntimeException e) { 434 rctx.sysExc = e; 435 TraceEjb.logger.log(BasicLevel.ERROR, "runtime exception thrown by an enterprise Bean", e); 436 } catch (Error e) { 437 rctx.sysExc = e; 438 TraceEjb.logger.log(BasicLevel.ERROR, "error thrown by an enterprise Bean", e); 439 } finally { 440 try { 441 bf.postInvoke(rctx); 442 } catch (Exception e) { 443 TraceEjb.logger.log(BasicLevel.ERROR, "exception on postInvoke: ", e); 444 } 445 } 446 } 447 448 } 449 | Popular Tags |