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.resource.spi.endpoint.MessageEndpoint ; 41 import javax.transaction.Status ; 42 import javax.transaction.SystemException ; 43 import javax.transaction.UserTransaction ; 44 import javax.transaction.xa.XAResource ; 45 46 import org.objectweb.jonas_ejb.deployment.api.MethodDesc; 47 48 import org.objectweb.security.context.SecurityContext; 49 import org.objectweb.security.context.SecurityCurrent; 50 51 import org.objectweb.transaction.jta.TransactionManager; 52 import org.objectweb.util.monolog.api.BasicLevel; 53 54 61 public class JMessageEndpoint implements MessageDrivenContext { 62 63 protected JMdbEndpointFactory bf = null; 64 65 protected MessageDrivenBean mdb = null; 66 67 protected MessageEndpoint mep = null; 68 69 protected int txattr; 72 protected TransactionManager tm = null; 73 74 protected XAResource xar = null; 75 76 protected boolean released = false; 77 78 private static final int MAX_NB_RETRY = 2; 79 80 85 public JMessageEndpoint(JMdbEndpointFactory bf, MessageDrivenBean mdb) { 86 this.bf = bf; 87 this.mdb = mdb; 88 txattr = bf.getTransactionAttribute(); 90 tm = bf.getTransactionManager(); 91 } 92 93 public void setProxy(MessageEndpoint mep) { 94 this.mep = mep; 95 } 96 97 public XAResource getXAResource() { 98 return xar; 99 } 100 101 public void setXAResource(XAResource xa) { 102 xar = xa; 103 } 105 106 public boolean getReleasedState() { 107 return released; 108 } 109 110 public void setReleasedState(boolean state) { 111 released = state; 112 } 113 114 118 124 public TimerService getTimerService() throws IllegalStateException { 125 if (TraceEjb.isDebugIc()) { 126 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 127 } 128 return bf.getTimerService(); 129 } 130 131 135 private static final String DISALLOWED_MSG = " is disallowed in a message driven bean"; 136 137 142 public Identity getCallerIdentity() { 143 TraceEjb.logger.log(BasicLevel.ERROR, DISALLOWED_MSG); 144 throw new IllegalStateException ("getCallerIdentity()" + DISALLOWED_MSG); 145 } 146 147 151 public Principal getCallerPrincipal() { 152 boolean inRunAs = false; 153 if (bf.dd.getRunAsRole() != null) { 154 inRunAs = true; 155 } 156 SecurityCurrent current = SecurityCurrent.getCurrent(); 158 if (current != null) { 159 SecurityContext sctx = current.getSecurityContext(); 160 if (sctx == null) { 161 if (TraceEjb.isDebugSecurity()) { 162 TraceEjb.security.log(BasicLevel.DEBUG, "runas : Security context is null, create a new one"); 163 } 164 sctx = new SecurityContext(); 165 current.setSecurityContext(sctx); 166 } 167 } 168 169 Principal principal = bf.getContainer().getPrincipalFactory().getCallerPrincipal(inRunAs); 170 if (principal == null) { 171 throw new IllegalStateException ("No principal exists in security context"); 172 } 173 return principal; 174 } 175 176 181 public boolean isCallerInRole(Identity role) { 182 TraceEjb.logger.log(BasicLevel.ERROR, DISALLOWED_MSG); 183 throw new IllegalStateException ("isCallerInRole()" + DISALLOWED_MSG); 184 } 185 186 191 public boolean isCallerInRole(java.lang.String roleLink) { 192 TraceEjb.logger.log(BasicLevel.ERROR, DISALLOWED_MSG); 193 throw new IllegalStateException ("isCallerInRole()" + DISALLOWED_MSG); 194 } 195 196 202 public void setRollbackOnly() { 203 204 if (TraceEjb.isDebugJms()) { 205 TraceEjb.mdb.log(BasicLevel.DEBUG, ""); 206 } 207 208 if(bf.isTxBeanManaged()) 209 throw new IllegalStateException ("Bean-managed transaction, not use this method."); 210 try { 211 tm.setRollbackOnly(); 212 } catch (IllegalStateException e) { 213 TraceEjb.logger.log(BasicLevel.ERROR, "current thread not associated with transaction"); 214 throw e; 215 } catch (SystemException e) { 216 TraceEjb.logger.log(BasicLevel.ERROR, "unexpected exception:", e); 217 } 218 } 219 220 224 public boolean getRollbackOnly() { 225 if (TraceEjb.isDebugJms()) { 226 TraceEjb.mdb.log(BasicLevel.DEBUG, ""); 227 } 228 if(bf.isTxBeanManaged()) 229 throw new IllegalStateException ("Bean-managed transaction, not use this method."); 230 try { 231 if (tm.getTransaction() != null) { 232 switch (tm.getStatus()) { 233 case Status.STATUS_MARKED_ROLLBACK: 234 case Status.STATUS_ROLLEDBACK: 235 case Status.STATUS_ROLLING_BACK: 236 return true; 237 case Status.STATUS_NO_TRANSACTION: 238 throw new IllegalStateException ("No transaction"); 239 default: 240 return false; 241 } 242 } else { 243 TraceEjb.logger.log(BasicLevel.ERROR, "the bean is not associated in a transaction"); 244 throw new IllegalStateException ("the message driven bean is not associated in a transaction"); 245 } 246 } catch (SystemException e) { 247 TraceEjb.logger.log(BasicLevel.ERROR, "cannot get status:", e); 248 return false; 249 } 250 } 251 252 256 public EJBHome getEJBHome() { 257 TraceEjb.logger.log(BasicLevel.ERROR, DISALLOWED_MSG); 258 throw new IllegalStateException ("getEJBHome()" + DISALLOWED_MSG); 259 } 260 261 265 public EJBLocalHome getEJBLocalHome() { 266 TraceEjb.logger.log(BasicLevel.ERROR, DISALLOWED_MSG); 267 throw new IllegalStateException ("getEJBLocalHome()" + DISALLOWED_MSG); 268 } 269 270 274 public Properties getEnvironment() { 275 TraceEjb.logger.log(BasicLevel.ERROR, "deprecated use : Use the JNDI naming context java:comp/env"); 276 return new java.util.Properties (); 277 } 278 279 287 public UserTransaction getUserTransaction() throws IllegalStateException { 288 289 if (TraceEjb.isDebugJms()) { 290 TraceEjb.mdb.log(BasicLevel.DEBUG, ""); 291 } 292 293 if (!bf.isTxBeanManaged()) { 294 throw new IllegalStateException ("This bean is not allowed to use UserTransaction interface"); 295 } 296 return (UserTransaction ) tm; 297 } 298 299 303 307 public void deliverTimeout(Timer timer) { 308 if (TraceEjb.isDebugJms()) { 309 TraceEjb.mdb.log(BasicLevel.DEBUG, ""); 310 } 311 312 boolean committed = false; 313 for (int nbretry = 0; ! committed && nbretry < MAX_NB_RETRY; nbretry++) { 314 RequestCtx rctx = null; 315 try { 316 rctx = bf.preInvoke(MethodDesc.TX_REQUIRED); 318 } catch (Exception e) { 319 TraceEjb.logger.log(BasicLevel.ERROR, "preInvoke failed: ", e); 320 return; 321 } 322 try { 323 bf.checkSecurity(null); 324 if (mdb instanceof TimedObject ) { 325 ((TimedObject ) mdb).ejbTimeout(timer); 326 } else { 327 throw new EJBException ("The bean does not implement the `TimedObject` interface"); 328 } 329 committed = ! getRollbackOnly(); 330 } catch (EJBException e) { 331 rctx.sysExc = e; 332 TraceEjb.logger.log(BasicLevel.ERROR, "EJB exception thrown by an enterprise Bean", e); 333 } catch (RuntimeException e) { 334 rctx.sysExc = e; 335 TraceEjb.logger.log(BasicLevel.ERROR, "runtime exception thrown by an enterprise Bean", e); 336 } catch (Error e) { 337 rctx.sysExc = e; 338 TraceEjb.logger.log(BasicLevel.ERROR, "error thrown by an enterprise Bean", e); 339 } finally { 340 try { 341 bf.postInvoke(rctx); 342 } catch (Exception e) { 343 TraceEjb.logger.log(BasicLevel.ERROR, "exception on postInvoke: ", e); 344 } 345 } 346 } 347 } 348 } 349 | Popular Tags |