1 22 package org.jboss.ejb; 23 24 import javax.ejb.*; 25 import javax.transaction.UserTransaction ; 26 import javax.xml.rpc.handler.MessageContext ; 27 import java.io.Serializable ; 28 import java.lang.reflect.InvocationTargetException ; 29 import java.lang.reflect.Method ; 30 import java.rmi.RemoteException ; 31 import java.security.Principal ; 32 import java.util.Collection ; 33 import java.util.Date ; 34 35 42 public class StatelessSessionEnterpriseContext 43 extends EnterpriseContext 44 { 45 47 49 EJBObject ejbObject; 50 EJBLocalObject ejbLocalObject; 51 MessageContext soapMessageContext; 52 SessionContext ctx; 53 54 56 58 public StatelessSessionEnterpriseContext(Object instance, Container con) 59 throws Exception 60 { 61 super(instance, con); 62 ctx = new SessionContextImpl(); 63 64 try 65 { 66 AllowedOperationsAssociation.pushInMethodFlag(IN_SET_SESSION_CONTEXT); 67 ((SessionBean)instance).setSessionContext(ctx); 68 } 69 finally 70 { 71 AllowedOperationsAssociation.popInMethodFlag(); 72 } 73 74 try 75 { 76 AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_CREATE); 77 Method ejbCreate = instance.getClass().getMethod("ejbCreate", new Class [0]); 78 ejbCreate.invoke(instance, new Object [0]); 79 } catch (InvocationTargetException e) 80 { 81 Throwable ex = e.getTargetException(); 82 if (ex instanceof EJBException) 83 throw (Exception )ex; 84 else if (ex instanceof RuntimeException ) 85 throw new EJBException((Exception )ex); else if (ex instanceof Exception ) 87 throw (Exception )ex; 88 else 89 throw (Error )ex; 90 } 91 finally 92 { 93 AllowedOperationsAssociation.popInMethodFlag(); 94 } 95 } 96 97 99 public void setEJBObject(EJBObject eo) { 100 ejbObject = eo; 101 } 102 103 public EJBObject getEJBObject() { 104 return ejbObject; 105 } 106 107 public void setEJBLocalObject(EJBLocalObject eo) { 108 ejbLocalObject = eo; 109 } 110 111 public EJBLocalObject getEJBLocalObject() { 112 return ejbLocalObject; 113 } 114 115 public SessionContext getSessionContext() { 116 return ctx; 117 } 118 119 public void setMessageContext(MessageContext msgContext) 120 { 121 this.soapMessageContext = msgContext; 122 } 123 124 126 public void discard() throws RemoteException 127 { 128 ((SessionBean)instance).ejbRemove(); 129 } 130 131 public EJBContext getEJBContext() 132 { 133 return ctx; 134 } 135 136 138 140 142 144 protected class SessionContextImpl 145 extends EJBContextImpl 146 implements SessionContext 147 { 148 public EJBHome getEJBHome() 149 { 150 AllowedOperationsAssociation.assertAllowedIn("getEJBHome", 151 IN_SET_SESSION_CONTEXT | IN_EJB_CREATE | IN_EJB_REMOVE | 152 IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD); 153 154 return super.getEJBHome(); 155 } 156 157 public EJBLocalHome getEJBLocalHome() 158 { 159 AllowedOperationsAssociation.assertAllowedIn("getEJBLocalHome", 160 IN_SET_SESSION_CONTEXT | IN_EJB_CREATE | IN_EJB_REMOVE | 161 IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD); 162 163 return super.getEJBLocalHome(); 164 } 165 166 public EJBObject getEJBObject() 167 { 168 AllowedOperationsAssociation.assertAllowedIn("getEJBObject", 169 IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD); 170 171 if (((StatelessSessionContainer)con).getProxyFactory()==null) 172 throw new IllegalStateException ( "No remote interface defined." ); 173 174 if (ejbObject == null) 175 { 176 EJBProxyFactory proxyFactory = con.getProxyFactory(); 177 if(proxyFactory == null) 178 { 179 String defaultInvokerName = con.getBeanMetaData(). 180 getContainerConfiguration().getDefaultInvokerName(); 181 proxyFactory = con.lookupProxyFactory(defaultInvokerName); 182 } 183 ejbObject = (EJBObject) proxyFactory.getStatelessSessionEJBObject(); 184 } 185 186 return ejbObject; 187 } 188 189 public Object getBusinessObject(Class businessInterface) throws IllegalStateException 190 { 191 throw new RuntimeException ("NOT IMPLEMENTED"); 192 } 193 194 public Class getInvokedBusinessInterface() throws IllegalStateException 195 { 196 throw new RuntimeException ("NOT IMPLEMENTED"); 197 } 198 199 public EJBLocalObject getEJBLocalObject() 200 { 201 AllowedOperationsAssociation.assertAllowedIn("getEJBLocalObject", 202 IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD); 203 204 if (con.getLocalHomeClass()==null) 205 throw new IllegalStateException ( "No local interface for bean." ); 206 if (ejbLocalObject == null) { 207 ejbLocalObject = ((StatelessSessionContainer)con).getLocalProxyFactory().getStatelessSessionEJBLocalObject(); 208 } 209 return ejbLocalObject; 210 } 211 212 public TimerService getTimerService() throws IllegalStateException 213 { 214 AllowedOperationsAssociation.assertAllowedIn("getTimerService", 215 IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD); 216 return new TimerServiceWrapper(this, super.getTimerService()); 217 } 218 219 public Principal getCallerPrincipal() 220 { 221 AllowedOperationsAssociation.assertAllowedIn("getCallerPrincipal", 222 IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD); 223 return super.getCallerPrincipal(); 224 } 225 226 public boolean getRollbackOnly() 227 { 228 AllowedOperationsAssociation.assertAllowedIn("getRollbackOnly", 229 IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD); 230 return super.getRollbackOnly(); 231 } 232 233 public void setRollbackOnly() 234 { 235 AllowedOperationsAssociation.assertAllowedIn("setRollbackOnly", 236 IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD); 237 super.setRollbackOnly(); 238 } 239 240 public boolean isCallerInRole(String id) 241 { 242 AllowedOperationsAssociation.assertAllowedIn("isCallerInRole", 243 IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD); 244 return super.isCallerInRole(id); 245 } 246 247 public UserTransaction getUserTransaction() 248 { 249 AllowedOperationsAssociation.assertAllowedIn("getUserTransaction", 250 IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD); 251 return super.getUserTransaction(); 252 } 253 254 public MessageContext getMessageContext() throws IllegalStateException 255 { 256 AllowedOperationsAssociation.assertAllowedIn("getMessageContext", 257 IN_SERVICE_ENDPOINT_METHOD); 258 return soapMessageContext; 259 } 260 } 261 262 265 public class TimerServiceWrapper implements TimerService 266 { 267 268 private EnterpriseContext.EJBContextImpl context; 269 private TimerService timerService; 270 271 public TimerServiceWrapper(EnterpriseContext.EJBContextImpl ctx, TimerService timerService) 272 { 273 this.context = ctx; 274 this.timerService = timerService; 275 } 276 277 public Timer createTimer(long duration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 278 { 279 assertAllowedIn("TimerService.createTimer"); 280 return timerService.createTimer(duration, info); 281 } 282 283 public Timer createTimer(long initialDuration, long intervalDuration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 284 { 285 assertAllowedIn("TimerService.createTimer"); 286 return timerService.createTimer(initialDuration, intervalDuration, info); 287 } 288 289 public Timer createTimer(Date expiration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 290 { 291 assertAllowedIn("TimerService.createTimer"); 292 return timerService.createTimer(expiration, info); 293 } 294 295 public Timer createTimer(Date initialExpiration, long intervalDuration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 296 { 297 assertAllowedIn("TimerService.createTimer"); 298 return timerService.createTimer(initialExpiration, intervalDuration, info); 299 } 300 301 public Collection getTimers() throws IllegalStateException , EJBException 302 { 303 assertAllowedIn("TimerService.getTimers"); 304 return timerService.getTimers(); 305 } 306 307 private void assertAllowedIn(String timerMethod) 308 { 309 AllowedOperationsAssociation.assertAllowedIn(timerMethod, 310 IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD); 311 } 312 } 313 } | Popular Tags |