1 22 package org.jboss.ejb; 23 24 import java.io.Serializable ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.Method ; 27 import java.rmi.RemoteException ; 28 import java.security.Principal ; 29 import java.util.Collection ; 30 import java.util.Date ; 31 import javax.ejb.EJBContext ; 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.Timer ; 38 import javax.ejb.TimerService ; 39 import javax.transaction.UserTransaction ; 40 41 import org.jboss.metadata.MessageDrivenMetaData; 42 import org.jboss.metadata.MetaData; 43 44 53 public class MessageDrivenEnterpriseContext 54 extends EnterpriseContext 55 { 56 private MessageDrivenContext ctx; 57 58 70 public MessageDrivenEnterpriseContext(Object instance, Container con) 71 throws Exception 72 { 73 super(instance, con); 74 75 ctx = new MessageDrivenContextImpl(); 76 ((MessageDrivenBean )instance).setMessageDrivenContext(ctx); 77 78 try 79 { 80 AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_CREATE); 81 Method ejbCreate = instance.getClass().getMethod("ejbCreate", new Class [0]); 82 ejbCreate.invoke(instance, new Object [0]); 83 } 84 catch (InvocationTargetException e) 85 { 86 Throwable t = e.getTargetException(); 87 88 if (t instanceof RuntimeException ) { 89 if (t instanceof EJBException ) { 90 throw (EJBException )t; 91 } 92 else { 93 throw new EJBException ((RuntimeException )t); 95 } 96 } 97 else if (t instanceof Exception ) { 98 throw (Exception )t; 99 } 100 else if (t instanceof Error ) { 101 throw (Error )t; 102 } 103 else { 104 throw new org.jboss.util.NestedError("Unexpected Throwable", t); 105 } 106 } 107 } 108 109 public MessageDrivenContext getMessageDrivenContext() 110 { 111 return ctx; 112 } 113 114 116 119 public void discard() throws RemoteException 120 { 121 ((MessageDrivenBean )instance).ejbRemove(); 122 } 123 124 public EJBContext getEJBContext() 125 { 126 return ctx; 127 } 128 129 132 protected class MessageDrivenContextImpl 133 extends EJBContextImpl 134 implements MessageDrivenContext 135 { 136 137 public EJBHome getEJBHome() 138 { 139 throw new IllegalStateException ("getEJBHome should not be access from a message driven bean"); 140 } 141 142 public EJBLocalHome getEJBLocalHome() 143 { 144 throw new IllegalStateException ("getEJBHome should not be access from a message driven bean"); 145 } 146 147 public TimerService getTimerService() throws IllegalStateException 148 { 149 AllowedOperationsAssociation.assertAllowedIn("getTimerService", 150 IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT); 151 return new TimerServiceWrapper(this, super.getTimerService()); 152 } 153 154 public Principal getCallerPrincipal() 155 { 156 AllowedOperationsAssociation.assertAllowedIn("getCallerPrincipal", 157 IN_BUSINESS_METHOD | IN_EJB_TIMEOUT); 158 return super.getCallerPrincipal(); 159 } 160 161 public boolean isCallerInRole(String id) 162 { 163 throw new IllegalStateException ("isCallerInRole should not be access from a message driven bean"); 164 } 165 166 public UserTransaction getUserTransaction() 167 { 168 if (isContainerManagedTx()) 169 throw new IllegalStateException ("getUserTransaction should not be access for container managed Tx"); 170 171 AllowedOperationsAssociation.assertAllowedIn("getUserTransaction", 172 IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT); 173 174 return super.getUserTransaction(); 175 } 176 177 184 public boolean getRollbackOnly() 185 { 186 if (isUserManagedTx()) 187 throw new IllegalStateException ("getRollbackOnly should not be access for user managed Tx"); 188 189 AllowedOperationsAssociation.assertAllowedIn("getRollbackOnly", 190 IN_BUSINESS_METHOD | IN_EJB_TIMEOUT); 191 192 198 if (!isTxRequired()) { 199 throw new IllegalStateException 200 ("getRollbackOnly must only be called in the context of a transaction (EJB 2.0 - 15.5.1)"); 201 } 202 203 return super.getRollbackOnly(); 204 } 205 206 213 public void setRollbackOnly() 214 { 215 if (isUserManagedTx()) 216 throw new IllegalStateException ("setRollbackOnly should not be access for user managed Tx"); 217 218 AllowedOperationsAssociation.assertAllowedIn("getRollbackOnly", 219 IN_BUSINESS_METHOD | IN_EJB_TIMEOUT); 220 221 if (!isTxRequired()) { 222 throw new IllegalStateException 223 ("setRollbackOnly must only be called in the context of a transaction (EJB 2.0 - 15.5.1)"); 224 } 225 226 super.setRollbackOnly(); 227 } 228 229 230 private boolean isTxRequired() 231 { 232 MessageDrivenMetaData md = (MessageDrivenMetaData)con.getBeanMetaData(); 233 return md.getMethodTransactionType() == MetaData.TX_REQUIRED; 234 } 235 } 236 237 240 public class TimerServiceWrapper implements TimerService 241 { 242 243 private EnterpriseContext.EJBContextImpl context; 244 private TimerService timerService; 245 246 public TimerServiceWrapper(EnterpriseContext.EJBContextImpl ctx, TimerService timerService) 247 { 248 this.context = ctx; 249 this.timerService = timerService; 250 } 251 252 public Timer createTimer(long duration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 253 { 254 assertAllowedIn("TimerService.createTimer"); 255 return timerService.createTimer(duration, info); 256 } 257 258 public Timer createTimer(long initialDuration, long intervalDuration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 259 { 260 assertAllowedIn("TimerService.createTimer"); 261 return timerService.createTimer(initialDuration, intervalDuration, info); 262 } 263 264 public Timer createTimer(Date expiration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 265 { 266 assertAllowedIn("TimerService.createTimer"); 267 return timerService.createTimer(expiration, info); 268 } 269 270 public Timer createTimer(Date initialExpiration, long intervalDuration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 271 { 272 assertAllowedIn("TimerService.createTimer"); 273 return timerService.createTimer(initialExpiration, intervalDuration, info); 274 } 275 276 public Collection getTimers() throws IllegalStateException , EJBException 277 { 278 assertAllowedIn("TimerService.getTimers"); 279 return timerService.getTimers(); 280 } 281 282 private void assertAllowedIn(String timerMethod) 283 { 284 AllowedOperationsAssociation.assertAllowedIn(timerMethod, 285 IN_BUSINESS_METHOD | IN_EJB_TIMEOUT); 286 } 287 } 288 } | Popular Tags |