1 22 package org.jboss.ejb; 23 24 import java.io.Serializable ; 25 import java.rmi.RemoteException ; 26 import java.security.Principal ; 27 import java.util.Collection ; 28 import java.util.Date ; 29 30 import javax.ejb.EJBContext ; 31 import javax.ejb.EJBException ; 32 import javax.ejb.EJBHome ; 33 import javax.ejb.EJBLocalHome ; 34 import javax.ejb.EJBLocalObject ; 35 import javax.ejb.EJBObject ; 36 import javax.ejb.EntityBean ; 37 import javax.ejb.EntityContext ; 38 import javax.ejb.Timer ; 39 import javax.ejb.TimerService ; 40 import javax.transaction.UserTransaction ; 41 42 import org.jboss.ejb.plugins.lock.NonReentrantLock; 43 44 45 56 public class EntityEnterpriseContext extends EnterpriseContext 57 { 58 private EJBObject ejbObject; 59 private EJBLocalObject ejbLocalObject; 60 private EntityContext ctx; 61 62 66 private boolean hasTxSynchronization = false; 67 68 71 private GlobalTxEntityMap.TxAssociation txAssociation = GlobalTxEntityMap.NONE; 72 73 78 private boolean valid = false; 79 80 83 private boolean readOnly = false; 84 85 89 private Object persistenceCtx; 90 91 92 private Object key; 93 94 private NonReentrantLock methodLock = new NonReentrantLock(); 95 96 97 private boolean passivateAfterCommit; 98 99 public EntityEnterpriseContext(Object instance, Container con) 100 throws RemoteException 101 { 102 super(instance, con); 103 ctx = new EntityContextImpl(); 104 try 105 { 106 AllowedOperationsAssociation.pushInMethodFlag(IN_SET_ENTITY_CONTEXT); 107 ((EntityBean )instance).setEntityContext(ctx); 108 } 109 finally 110 { 111 AllowedOperationsAssociation.popInMethodFlag(); 112 } 113 } 114 115 120 public NonReentrantLock getMethodLock() 121 { 122 return methodLock; 123 } 124 125 public void clear() 126 { 127 super.clear(); 128 129 hasTxSynchronization = false; 130 valid = false; 131 readOnly = false; 132 key = null; 133 persistenceCtx = null; 134 ejbObject = null; 135 ejbLocalObject = null; 136 txAssociation = GlobalTxEntityMap.NONE; 137 passivateAfterCommit = false; 138 } 139 140 public void discard() throws RemoteException 141 { 142 ((EntityBean )instance).unsetEntityContext(); 143 } 144 145 public EJBContext getEJBContext() 146 { 147 return ctx; 148 } 149 150 public void setEJBObject(EJBObject eo) 151 { 152 ejbObject = eo; 153 } 154 155 public EJBObject getEJBObject() 156 { 157 if(ejbObject == null && con.getProxyFactory() != null) 160 { 161 ejbObject = (EJBObject )con.getProxyFactory().getEntityEJBObject(id); 162 } 163 return ejbObject; 164 } 165 166 public void setEJBLocalObject(EJBLocalObject eo) 167 { 168 ejbLocalObject = eo; 169 } 170 171 public EJBLocalObject getEJBLocalObject() 172 { 173 if(ejbLocalObject == null && con.getLocalHomeClass() != null) 174 { 175 ejbLocalObject = ((EntityContainer)con).getLocalProxyFactory().getEntityEJBLocalObject(id); 176 } 177 return ejbLocalObject; 178 } 179 180 public void setCacheKey(Object key) 181 { 182 this.key = key; 183 } 184 185 public Object getCacheKey() 186 { 187 return key; 188 } 189 190 public void setPersistenceContext(Object ctx) 191 { 192 this.persistenceCtx = ctx; 193 } 194 195 public Object getPersistenceContext() 196 { 197 return persistenceCtx; 198 } 199 200 public void hasTxSynchronization(boolean value) 201 { 202 hasTxSynchronization = value; 203 } 204 205 public boolean hasTxSynchronization() 206 { 207 return hasTxSynchronization; 208 } 209 210 public GlobalTxEntityMap.TxAssociation getTxAssociation() 211 { 212 return txAssociation; 213 } 214 215 public void setTxAssociation(GlobalTxEntityMap.TxAssociation txAssociation) 216 { 217 this.txAssociation = txAssociation; 218 } 219 220 public void setValid(boolean valid) 221 { 222 this.valid = valid; 223 } 224 225 public boolean isValid() 226 { 227 return valid; 228 } 229 230 public void setReadOnly(boolean readOnly) 231 { 232 this.readOnly = readOnly; 233 } 234 235 public boolean isReadOnly() 236 { 237 return readOnly; 238 } 239 240 public boolean isPassivateAfterCommit() 241 { 242 return passivateAfterCommit; 243 } 244 245 public void setPassivateAfterCommit(boolean passivateAfterCommit) 246 { 247 this.passivateAfterCommit = passivateAfterCommit; 248 } 249 250 public String toString() 251 { 252 return getContainer().getBeanMetaData().getEjbName() + '#' + getId(); 253 } 254 255 protected class EntityContextImpl 256 extends EJBContextImpl 257 implements EntityContext 258 { 259 public EJBHome getEJBHome() 260 { 261 AllowedOperationsAssociation.assertAllowedIn("getEJBHome", 262 IN_SET_ENTITY_CONTEXT | IN_UNSET_ENTITY_CONTEXT | 263 IN_EJB_CREATE | IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_FIND | IN_EJB_HOME | 264 IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD | 265 IN_EJB_TIMEOUT); 266 267 return super.getEJBHome(); 268 } 269 270 public EJBLocalHome getEJBLocalHome() 271 { 272 AllowedOperationsAssociation.assertAllowedIn("getEJBLocalHome", 273 IN_SET_ENTITY_CONTEXT | IN_UNSET_ENTITY_CONTEXT | 274 IN_EJB_CREATE | IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_FIND | IN_EJB_HOME | 275 IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD | 276 IN_EJB_TIMEOUT); 277 278 return super.getEJBLocalHome(); 279 } 280 281 public Principal getCallerPrincipal() 282 { 283 AllowedOperationsAssociation.assertAllowedIn("getCallerPrincipal", 284 IN_EJB_CREATE | IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_FIND | IN_EJB_HOME | 285 IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD | 286 IN_EJB_TIMEOUT); 287 288 return super.getCallerPrincipal(); 289 } 290 291 public boolean getRollbackOnly() 292 { 293 AllowedOperationsAssociation.assertAllowedIn("getRollbackOnly", 294 IN_EJB_CREATE | IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_FIND | IN_EJB_HOME | 295 IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD | 296 IN_EJB_TIMEOUT); 297 298 return super.getRollbackOnly(); 299 } 300 301 public void setRollbackOnly() 302 { 303 AllowedOperationsAssociation.assertAllowedIn("setRollbackOnly", 304 IN_EJB_CREATE | IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_FIND | IN_EJB_HOME | 305 IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD | 306 IN_EJB_TIMEOUT); 307 308 super.setRollbackOnly(); 309 } 310 311 public boolean isCallerInRole(String id) 312 { 313 AllowedOperationsAssociation.assertAllowedIn("getCallerInRole", 314 IN_EJB_CREATE | IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_FIND | IN_EJB_HOME | 315 IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD | 316 IN_EJB_TIMEOUT); 317 return super.isCallerInRole(id); 318 } 319 320 public UserTransaction getUserTransaction() 321 { 322 AllowedOperationsAssociation.assertAllowedIn("getUserTransaction", 323 NOT_ALLOWED); 324 return super.getUserTransaction(); 325 } 326 327 public EJBObject getEJBObject() 328 { 329 AllowedOperationsAssociation.assertAllowedIn("getEJBObject", 330 IN_EJB_POST_CREATE | IN_EJB_REMOVE | 331 IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD | 332 IN_EJB_TIMEOUT); 333 334 if(((EntityContainer)con).getRemoteClass() == null) 335 { 336 throw new IllegalStateException ( "No remote interface defined." ); 337 } 338 339 if (ejbObject == null) 340 { 341 Object cacheKey = ((EntityCache)((EntityContainer)con).getInstanceCache()).createCacheKey(id); 343 EJBProxyFactory proxyFactory = con.getProxyFactory(); 344 if(proxyFactory == null) 345 { 346 String defaultInvokerName = con.getBeanMetaData(). 347 getContainerConfiguration().getDefaultInvokerName(); 348 proxyFactory = con.lookupProxyFactory(defaultInvokerName); 349 } 350 ejbObject = (EJBObject )proxyFactory.getEntityEJBObject(cacheKey); 351 } 352 353 return ejbObject; 354 } 355 356 public EJBLocalObject getEJBLocalObject() 357 { 358 AllowedOperationsAssociation.assertAllowedIn("getEJBLocalObject", 359 IN_EJB_POST_CREATE | IN_EJB_REMOVE | 360 IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD | 361 IN_EJB_TIMEOUT); 362 363 if (con.getLocalHomeClass()==null) 364 throw new IllegalStateException ( "No local interface for bean." ); 365 366 if (ejbLocalObject == null) 367 { 368 Object cacheKey = ((EntityCache)((EntityContainer)con).getInstanceCache()).createCacheKey(id); 369 ejbLocalObject = ((EntityContainer)con).getLocalProxyFactory().getEntityEJBLocalObject(cacheKey); 370 } 371 return ejbLocalObject; 372 } 373 374 public Object getPrimaryKey() 375 { 376 AllowedOperationsAssociation.assertAllowedIn("getPrimaryKey", 377 IN_EJB_POST_CREATE | IN_EJB_REMOVE | 378 IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD | 379 IN_EJB_TIMEOUT); 380 381 return id; 382 } 383 384 public TimerService getTimerService() throws IllegalStateException 385 { 386 AllowedOperationsAssociation.assertAllowedIn("getTimerService", 387 IN_EJB_CREATE | IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_HOME | 388 IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD | 389 IN_EJB_TIMEOUT); 390 391 return new TimerServiceWrapper(this, getContainer().getTimerService(id)); 392 } 393 } 394 395 398 public class TimerServiceWrapper implements TimerService 399 { 400 401 private TimerService timerService; 403 404 public TimerServiceWrapper(EnterpriseContext.EJBContextImpl ctx, TimerService timerService) 405 { 406 this.timerService = timerService; 408 } 409 410 public Timer createTimer(long duration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 411 { 412 assertAllowedIn("TimerService.createTimer"); 413 return timerService.createTimer(duration, info); 414 } 415 416 public Timer createTimer(long initialDuration, long intervalDuration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 417 { 418 assertAllowedIn("TimerService.createTimer"); 419 return timerService.createTimer(initialDuration, intervalDuration, info); 420 } 421 422 public Timer createTimer(Date expiration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 423 { 424 assertAllowedIn("TimerService.createTimer"); 425 return timerService.createTimer(expiration, info); 426 } 427 428 public Timer createTimer(Date initialExpiration, long intervalDuration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 429 { 430 assertAllowedIn("TimerService.createTimer"); 431 return timerService.createTimer(initialExpiration, intervalDuration, info); 432 } 433 434 public Collection getTimers() throws IllegalStateException , EJBException 435 { 436 assertAllowedIn("TimerService.getTimers"); 437 return timerService.getTimers(); 438 } 439 440 private void assertAllowedIn(String timerMethod) 441 { 442 AllowedOperationsAssociation.assertAllowedIn(timerMethod, 443 IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_LOAD | IN_EJB_STORE | 444 IN_BUSINESS_METHOD | IN_EJB_TIMEOUT); 445 } 446 } 447 } | Popular Tags |