1 23 package com.sun.ejb.containers; 24 25 import java.util.*; 26 import java.security.Identity ; 27 import java.security.Principal ; 28 import java.lang.reflect.Method ; 29 30 import java.lang.ref.*; 31 32 import javax.ejb.*; 33 import javax.transaction.*; 34 35 import com.sun.ejb.*; 36 import com.sun.enterprise.*; 37 import com.sun.enterprise.util.LocalStringManagerImpl; 38 import com.sun.enterprise.resource.ResourceHandle; 39 import com.sun.enterprise.deployment.EjbDescriptor; 40 import com.sun.enterprise.deployment.RoleReference; 41 42 import com.sun.ejb.containers.util.cache.CacheEntry; 43 44 import java.util.logging.*; 45 import com.sun.logging.*; 46 import javax.naming.Context ; 47 import javax.naming.InitialContext ; 48 49 53 54 public abstract class EJBContextImpl 55 implements EJBContext, ComponentContext, java.io.Serializable 56 { 57 private static Logger _logger; 58 static { 59 _logger=LogDomains.getLogger(LogDomains.EJB_LOGGER); 60 } 61 62 private static LocalStringManagerImpl localStrings = 63 new LocalStringManagerImpl(EJBContextImpl.class); 64 protected static int NOT_INITIALIZED = -999; 66 private Object ejb; 67 68 transient protected BaseContainer container; 72 73 transient protected Transaction transaction = null; 74 transient private Context initialContext = null; 75 transient private ArrayList resources; 76 transient private int concInvokeCount = 0; 77 78 transient protected EJBObject ejbStub=null; 80 transient protected EJBObjectImpl ejbObjectImpl; 81 82 transient protected EJBObjectImpl ejbRemoteBusinessObjectImpl; 83 84 transient protected EJBLocalObjectImpl ejbLocalObjectImpl; 85 transient protected EJBLocalObjectImpl ejbLocalBusinessObjectImpl; 86 87 transient private long lastTimeUsed; 88 transient protected int state; 89 90 protected final boolean isRemoteInterfaceSupported; 93 94 protected final boolean isLocalInterfaceSupported; 97 98 transient protected boolean inEjbRemove; 103 104 private Object [] interceptorInstances; 105 106 EJBContextImpl(Object ejb, BaseContainer container) { 107 this.ejb = ejb; 108 this.container = container; 109 state = NOT_INITIALIZED; 110 inEjbRemove = false; 111 112 isRemoteInterfaceSupported = container.isRemoteInterfaceSupported(); 113 isLocalInterfaceSupported = container.isLocalInterfaceSupported(); 114 } 115 116 public Transaction getTransaction() { 117 return transaction; 118 } 119 120 public void setTransaction(Transaction tr) { 121 transaction = tr; 122 } 123 124 void setEJBStub(EJBObject ejbStub) { 125 this.ejbStub = ejbStub; 126 } 127 128 void setEJBLocalObjectImpl(EJBLocalObjectImpl localObjectImpl) { 129 this.ejbLocalObjectImpl = localObjectImpl; 130 } 131 132 void setEJBLocalBusinessObjectImpl(EJBLocalObjectImpl localBusObjectImpl) { 133 this.ejbLocalBusinessObjectImpl = localBusObjectImpl; 134 } 135 136 137 void setEJBObjectImpl(EJBObjectImpl ejbo) { 138 this.ejbObjectImpl = ejbo; 139 } 140 141 EJBObjectImpl getEJBObjectImpl() { 142 return ejbObjectImpl; 143 } 144 145 void setEJBRemoteBusinessObjectImpl(EJBObjectImpl ejbo) { 146 this.ejbRemoteBusinessObjectImpl = ejbo; 147 } 148 149 EJBObjectImpl getEJBRemoteBusinessObjectImpl() { 150 return this.ejbRemoteBusinessObjectImpl; 151 } 152 153 EJBLocalObjectImpl getEJBLocalObjectImpl() { 154 return ejbLocalObjectImpl; 155 } 156 157 EJBLocalObjectImpl getEJBLocalBusinessObjectImpl() { 158 return ejbLocalBusinessObjectImpl; 159 } 160 161 void setContainer(BaseContainer container) { 162 this.container = container; 163 } 164 165 void setState(int s) { 166 state = s; 167 } 168 169 boolean isTimedObject() { 170 return container.isTimedObject(); 171 } 172 173 int getState() { 174 return state; 175 } 176 177 void setInEjbRemove(boolean beingRemoved) { 178 inEjbRemove = beingRemoved; 179 } 180 181 boolean isInEjbRemove() { 182 return inEjbRemove; 183 } 184 185 189 boolean isUnitialized() { 190 return (state == NOT_INITIALIZED); 191 } 192 193 public long getLastTimeUsed() { 194 return lastTimeUsed; 195 } 196 197 void setSoftRef(SoftReference softRef) { 198 } 199 200 void setHardRef(Object referent) { 201 } 202 203 void touch() { 204 lastTimeUsed = System.currentTimeMillis(); 205 } 206 207 208 209 212 213 216 public Object getEJB() { 217 return ejb; 218 } 219 220 221 public Container getContainer() { 222 return container; 223 } 224 225 229 public void registerResource(ResourceHandle h) { 230 if ( resources == null ) 231 resources = new ArrayList(); 232 resources.add(h); 233 } 234 235 238 public void unregisterResource(ResourceHandle h) { 239 if ( resources == null ) 240 resources = new ArrayList(); 241 resources.remove(h); 242 } 243 244 247 public List getResourceList() { 248 if (resources == null) 249 resources = new ArrayList(0); 250 return resources; 251 } 252 253 254 259 public int getConcurrentInvokeCount() { 260 return concInvokeCount; 261 } 262 263 268 public synchronized void incrementConcurrentInvokeCount() { 269 concInvokeCount++; 270 } 271 272 277 public synchronized void decrementConcurrentInvokeCount() { 278 concInvokeCount--; 279 } 280 281 284 285 288 public EJBObject getEJBObject() 289 throws IllegalStateException 290 { 291 if (ejbStub == null) { 292 throw new IllegalStateException ("EJBObject not available"); 293 } 294 295 return ejbStub; 296 } 297 298 301 public EJBLocalObject getEJBLocalObject() 302 throws IllegalStateException 303 { 304 if ( ejbLocalObjectImpl == null ) { 305 throw new IllegalStateException ("EJBLocalObject not available"); 306 } 307 308 return (EJBLocalObject) ejbLocalObjectImpl.getClientObject(); 311 } 312 313 316 public EJBHome getEJBHome() { 317 if (! isRemoteInterfaceSupported) { 318 throw new IllegalStateException ("EJBHome not available"); 319 } 320 321 return container.getEJBHomeStub(); 322 } 323 324 325 328 public EJBLocalHome getEJBLocalHome() { 329 if (! isLocalInterfaceSupported) { 330 throw new IllegalStateException ("EJBLocalHome not available"); 331 } 332 333 return container.getEJBLocalHome(); 334 } 335 336 337 340 public Properties getEnvironment() { 341 return container.getEnvironmentProperties(); 343 } 344 345 348 public Identity getCallerIdentity() { 349 throw new RuntimeException ( 352 "getCallerIdentity() is deprecated, please use getCallerPrincipal()."); 353 } 354 355 356 public Object lookup(String name) { 357 Object o = null; 358 359 if( name == null ) { 360 throw new IllegalArgumentException ("Argument is null"); 361 } 362 try { 363 if( initialContext == null ) { 364 initialContext = new InitialContext (); 365 } 366 o = initialContext.lookup("java:comp/env/" + name); 368 } catch(Exception e) { 369 throw new IllegalArgumentException (e); 370 } 371 return o; 372 } 373 374 377 public Principal getCallerPrincipal() { 378 379 checkAccessToCallerSecurity(); 380 381 com.sun.enterprise.SecurityManager sm = container.getSecurityManager(); 382 return sm.getCallerPrincipal(); 383 } 384 385 386 389 public boolean isCallerInRole(Identity identity) { 390 return isCallerInRole(identity.getName()); 393 } 394 395 396 399 public boolean isCallerInRole(String roleRef) { 400 if ( roleRef == null ) 401 throw new IllegalArgumentException ("Argument is null"); 402 403 checkAccessToCallerSecurity(); 404 405 EjbDescriptor ejbd = container.getEjbDescriptor(); 406 RoleReference rr = ejbd.getRoleReferenceByName(roleRef); 407 408 if ( rr == null ) { 409 throw new IllegalArgumentException ( 410 "No mapping available for role reference " + roleRef); 411 } 412 413 com.sun.enterprise.SecurityManager sm = container.getSecurityManager(); 414 return sm.isCallerInRole(roleRef); 415 } 416 417 421 protected void checkAccessToCallerSecurity() 422 throws IllegalStateException 423 { 424 throw new IllegalStateException ("Operation not allowed"); 425 } 426 427 430 public UserTransaction getUserTransaction() 431 throws IllegalStateException 432 { 433 throw new IllegalStateException ("Operation not allowed"); 434 } 435 436 439 public void setRollbackOnly() 440 throws IllegalStateException 441 { 442 if ( state == NOT_INITIALIZED ) 443 throw new IllegalStateException ("EJB not in READY state"); 444 445 if ( container.isBeanManagedTx() ) 448 throw new IllegalStateException ( 449 "Illegal operation for bean-managed transactions"); 450 451 J2EETransactionManager tm = Switch.getSwitch().getTransactionManager(); 452 453 try { 454 if ( tm.getStatus() == Status.STATUS_NO_TRANSACTION ) { 455 throw new IllegalStateException ("No transaction context."); 462 } 463 464 checkActivatePassivate(); 465 466 tm.setRollbackOnly(); 467 468 } catch (Exception ex) { 469 IllegalStateException illEx = new IllegalStateException (ex.toString()); 470 illEx.initCause(ex); 471 throw illEx; 472 } 473 } 474 475 478 public boolean getRollbackOnly() 479 throws IllegalStateException 480 { 481 if ( state == NOT_INITIALIZED ) 482 throw new IllegalStateException ("EJB not in READY state"); 483 484 if ( container.isBeanManagedTx() ) 487 throw new IllegalStateException ( 488 "Illegal operation for bean-managed transactions"); 489 490 J2EETransactionManager tm = Switch.getSwitch().getTransactionManager(); 491 492 try { 493 int status = tm.getStatus(); 494 if ( status == Status.STATUS_NO_TRANSACTION ) { 495 throw new IllegalStateException ("No transaction context."); 497 } 498 499 checkActivatePassivate(); 500 501 if ( status == Status.STATUS_MARKED_ROLLBACK 502 || status == Status.STATUS_ROLLEDBACK 503 || status == Status.STATUS_ROLLING_BACK ) 504 return true; 505 else 506 return false; 507 } catch (Exception ex) { 508 _logger.log(Level.FINE, "Exception in method getRollbackOnly()", 509 ex); 510 IllegalStateException illEx = new IllegalStateException (ex.toString()); 511 illEx.initCause(ex); 512 throw illEx; 513 } 514 } 515 516 519 void setInterceptorInstances(Object [] instances) { 520 this.interceptorInstances = instances; 521 } 522 523 public Object [] getInterceptorInstances() { 524 return this.interceptorInstances; 525 } 526 527 534 public void checkTimerServiceMethodAccess() 535 throws IllegalStateException 536 { 537 throw new IllegalStateException ("EJB Timer Service method calls " + 538 "cannot be called in this context"); 539 } 540 541 protected void checkActivatePassivate() 543 throws IllegalStateException 544 { 545 if( inActivatePassivate() ) { 546 throw new IllegalStateException ("Operation not allowed."); 547 } 548 549 } 550 551 protected boolean inActivatePassivate() { 552 return inActivatePassivate( 553 container.invocationManager.getCurrentInvocation()); 554 } 555 556 protected boolean inActivatePassivate(ComponentInvocation inv) { 557 boolean inActivatePassivate = false; 558 if ( inv instanceof Invocation ) { 559 Method currentMethod = ((Invocation)inv).method; 560 inActivatePassivate = (currentMethod != null) 561 ? (currentMethod.getName().equals("ejbActivate") || 562 currentMethod.getName().equals("ejbPassivate") 563 ) 564 : false; 565 } 566 return inActivatePassivate; 567 } 568 569 572 void deleteAllReferences() { 573 ejb = null; 574 container = null; 575 transaction = null; 576 resources = null; 577 ejbStub = null; 578 ejbObjectImpl = null; 579 ejbRemoteBusinessObjectImpl = null; 580 ejbLocalObjectImpl = null; 581 ejbLocalBusinessObjectImpl = null; 582 } 583 584 } 585 | Popular Tags |