FileDocCategorySizeDatePackage
EJBContextImpl.javaAPI DocGlassfish v2 API18351Fri May 04 22:32:58 BST 2007com.sun.ejb.containers

EJBContextImpl

public abstract class EJBContextImpl extends Object implements EJBContext, ComponentContext, Serializable
Implementation of javax.ejb.EJBContext for the J2EE Reference Implementation.

Fields Summary
private static final Logger
_logger
private static com.sun.enterprise.util.LocalStringManagerImpl
localStrings
protected static final int
NOT_INITIALIZED
private Object
ejb
protected transient BaseContainer
container
protected transient Transaction
transaction
private transient Context
initialContext
private transient ArrayList
resources
private transient int
concInvokeCount
protected transient EJBObject
ejbStub
protected transient EJBObjectImpl
ejbObjectImpl
protected transient EJBObjectImpl
ejbRemoteBusinessObjectImpl
protected transient EJBLocalObjectImpl
ejbLocalObjectImpl
protected transient EJBLocalObjectImpl
ejbLocalBusinessObjectImpl
private transient long
lastTimeUsed
protected transient int
state
protected final boolean
isRemoteInterfaceSupported
protected final boolean
isLocalInterfaceSupported
protected transient boolean
inEjbRemove
private Object[]
interceptorInstances
Constructors Summary
EJBContextImpl(Object ejb, BaseContainer container)

    
        
        this.ejb = ejb;
        this.container = container;
        state = NOT_INITIALIZED;
        inEjbRemove = false;

        isRemoteInterfaceSupported = container.isRemoteInterfaceSupported();
        isLocalInterfaceSupported  = container.isLocalInterfaceSupported();
    
Methods Summary
protected voidcheckAccessToCallerSecurity()
Overridden in containers that allow access to isCallerInRole() and getCallerPrincipal()

        throw new IllegalStateException("Operation not allowed");
    
protected voidcheckActivatePassivate()

        if( inActivatePassivate() ) {
            throw new IllegalStateException("Operation not allowed.");
        }
        
    
public voidcheckTimerServiceMethodAccess()
The EJB spec makes a distinction between access to the TimerService object itself (via EJBContext.getTimerService) and access to the methods on TimerService, Timer, and TimerHandle. The latter case is covered by this check. It is overridden in the applicable concrete context impl subclasses.

        throw new IllegalStateException("EJB Timer Service method calls " +
        "cannot be called in this context");
    
public synchronized voiddecrementConcurrentInvokeCount()
Decrement the number of concurrent invocations on this bean (could happen with re-entrant bean). Used by TM.

        concInvokeCount--;
    
voiddeleteAllReferences()
Called after this context is freed up.

        ejb = null;
        container = null;
        transaction = null;
        resources = null;
        ejbStub = null;
        ejbObjectImpl = null;
        ejbRemoteBusinessObjectImpl = null;
        ejbLocalObjectImpl = null;
        ejbLocalBusinessObjectImpl = null;
    
public java.security.IdentitygetCallerIdentity()

deprecated

        // This method is deprecated.
        // see EJB2.0 section 21.2.5
        throw new RuntimeException(
        "getCallerIdentity() is deprecated, please use getCallerPrincipal().");
    
public java.security.PrincipalgetCallerPrincipal()


        checkAccessToCallerSecurity();

        com.sun.enterprise.SecurityManager sm = container.getSecurityManager();
        return sm.getCallerPrincipal();
    
public intgetConcurrentInvokeCount()
Get the number of concurrent invocations on this bean (could happen with re-entrant bean). Used by TM.

        return concInvokeCount;
    
public ContainergetContainer()

        return container;
    
public java.lang.ObjectgetEJB()

        return ejb;
    
public EJBHomegetEJBHome()

        if (! isRemoteInterfaceSupported) {
            throw new IllegalStateException("EJBHome not available");
        }

        return container.getEJBHomeStub();
    
EJBLocalObjectImplgetEJBLocalBusinessObjectImpl()

        return ejbLocalBusinessObjectImpl;
    
public EJBLocalHomegetEJBLocalHome()

        if (! isLocalInterfaceSupported) {
            throw new IllegalStateException("EJBLocalHome not available");
        }

        return container.getEJBLocalHome();
    
public EJBLocalObjectgetEJBLocalObject()
This is a SessionContext/EntityContext method.

        if ( ejbLocalObjectImpl == null ) {
            throw new IllegalStateException("EJBLocalObject not available");
        }
        
        // Have to convert EJBLocalObjectImpl to the client-view of 
        // EJBLocalObject
        return (EJBLocalObject) ejbLocalObjectImpl.getClientObject();
    
EJBLocalObjectImplgetEJBLocalObjectImpl()

        return ejbLocalObjectImpl;
    
public EJBObjectgetEJBObject()
This is a SessionContext/EntityContext method.

        if (ejbStub == null) {
            throw new IllegalStateException("EJBObject not available");
        }

        return ejbStub;
    
EJBObjectImplgetEJBObjectImpl()

        return ejbObjectImpl;
    
EJBObjectImplgetEJBRemoteBusinessObjectImpl()

        return this.ejbRemoteBusinessObjectImpl;
    
public java.util.PropertiesgetEnvironment()

        // This is deprecated, see EJB2.0 section 20.6.
        return container.getEnvironmentProperties();
    
public java.lang.Object[]getInterceptorInstances()

        return this.interceptorInstances;
    
public longgetLastTimeUsed()

        return lastTimeUsed;
    
public java.util.ListgetResourceList()
Get all the resources associated with the context

        if (resources == null)
            resources = new ArrayList(0);
        return resources;
    
public booleangetRollbackOnly()

        if ( state == NOT_INITIALIZED )
            throw new IllegalStateException("EJB not in READY state");
        
        // EJB2.0 section 7.5.2: only EJBs with container managed transactions
        // can use this method.
        if ( container.isBeanManagedTx() )
            throw new IllegalStateException(
                "Illegal operation for bean-managed transactions");
        
        J2EETransactionManager tm = Switch.getSwitch().getTransactionManager();
        
        try {
            int status = tm.getStatus();
            if ( status == Status.STATUS_NO_TRANSACTION ) {
                // EJB which was invoked without a global transaction.
                throw new IllegalStateException("No transaction context.");
            }
            
            checkActivatePassivate();
            
            if ( status == Status.STATUS_MARKED_ROLLBACK
            || status == Status.STATUS_ROLLEDBACK
            || status == Status.STATUS_ROLLING_BACK )
                return true;
            else
                return false;
        } catch (Exception ex) {
            _logger.log(Level.FINE, "Exception in method getRollbackOnly()", 
                ex);
            IllegalStateException illEx = new IllegalStateException(ex.toString());
            illEx.initCause(ex);
            throw illEx;
        }
    
intgetState()

        return state;
    
public javax.transaction.TransactiongetTransaction()

        return transaction;
    
public javax.transaction.UserTransactiongetUserTransaction()

        throw new IllegalStateException("Operation not allowed");
    
protected booleaninActivatePassivate()

        return inActivatePassivate(
            container.invocationManager.getCurrentInvocation());
    
protected booleaninActivatePassivate(ComponentInvocation inv)

        boolean inActivatePassivate = false;
        if ( inv instanceof Invocation ) {
            Method currentMethod = ((Invocation)inv).method;
            inActivatePassivate  = (currentMethod != null)
                ? (currentMethod.getName().equals("ejbActivate") ||
                   currentMethod.getName().equals("ejbPassivate")
                  )
                : false;
        }
        return inActivatePassivate;
    
public synchronized voidincrementConcurrentInvokeCount()
Increment the number of concurrent invocations on this bean (could happen with re-entrant bean). Used by TM.

        concInvokeCount++;
    
public booleanisCallerInRole(java.security.Identity identity)

deprecated

        // THis method is deprecated.
        // This implementation is as in EJB2.0 section 21.2.5
        return isCallerInRole(identity.getName());
    
public booleanisCallerInRole(java.lang.String roleRef)

        if ( roleRef == null )
            throw new IllegalArgumentException("Argument is null");

        checkAccessToCallerSecurity();
        
        EjbDescriptor ejbd = container.getEjbDescriptor();
        RoleReference rr = ejbd.getRoleReferenceByName(roleRef);
        
        if ( rr == null ) {
            throw new IllegalArgumentException(
                "No mapping available for role reference " + roleRef);
        }
        
        com.sun.enterprise.SecurityManager sm = container.getSecurityManager();
	return sm.isCallerInRole(roleRef);
    
booleanisInEjbRemove()

        return inEjbRemove;
    
booleanisTimedObject()

        return container.isTimedObject();
    
booleanisUnitialized()
Returns true if this context has NOT progressed past its initial state. The point at which this happens is container-specific.

        return (state == NOT_INITIALIZED);
    
public java.lang.Objectlookup(java.lang.String name)

        Object o = null;

        if( name == null ) {
            throw new IllegalArgumentException("Argument is null");
        }
        try {
            if( initialContext == null ) {
                initialContext = new InitialContext();
            }
            // name is relative to the private component namespace
            o = initialContext.lookup("java:comp/env/" + name);
        } catch(Exception e) {
            throw new IllegalArgumentException(e);
        }
        return o;
    
public voidregisterResource(com.sun.enterprise.resource.ResourceHandle h)
Register a resource opened by the EJB instance associated with this Context.

        if ( resources == null )
            resources = new ArrayList();
        resources.add(h);
    
voidsetContainer(BaseContainer container)

        this.container = container;
    
voidsetEJBLocalBusinessObjectImpl(EJBLocalObjectImpl localBusObjectImpl)

        this.ejbLocalBusinessObjectImpl = localBusObjectImpl;
    
voidsetEJBLocalObjectImpl(EJBLocalObjectImpl localObjectImpl)

        this.ejbLocalObjectImpl = localObjectImpl;
    
voidsetEJBObjectImpl(EJBObjectImpl ejbo)

        this.ejbObjectImpl = ejbo;
    
voidsetEJBRemoteBusinessObjectImpl(EJBObjectImpl ejbo)

        this.ejbRemoteBusinessObjectImpl = ejbo;
    
voidsetEJBStub(EJBObject ejbStub)

        this.ejbStub = ejbStub;
    
voidsetHardRef(java.lang.Object referent)

    
voidsetInEjbRemove(boolean beingRemoved)

        inEjbRemove = beingRemoved;
    
voidsetInterceptorInstances(java.lang.Object[] instances)
The following are EJBContextImpl-specific methods.

        this.interceptorInstances = instances;
    
public voidsetRollbackOnly()

        if ( state == NOT_INITIALIZED )
            throw new IllegalStateException("EJB not in READY state");
        
        // EJB2.0 section 7.5.2: only EJBs with container managed transactions
        // can use this method.
        if ( container.isBeanManagedTx() )
            throw new IllegalStateException(
                "Illegal operation for bean-managed transactions");
        
        J2EETransactionManager tm = Switch.getSwitch().getTransactionManager();
        
        try {
            if ( tm.getStatus() == Status.STATUS_NO_TRANSACTION ) {
                // EJB might be in a non-business method (for SessionBeans)
                // or afterCompletion.
                // OR this was a NotSupported/Never/Supports
                // EJB which was invoked without a global transaction.
                // In that case the JDBC connection would have autoCommit=true
                // so the container doesnt have to do anything.
                throw new IllegalStateException("No transaction context.");
            }
            
            checkActivatePassivate();
            
            tm.setRollbackOnly();
            
        } catch (Exception ex) {
            IllegalStateException illEx = new IllegalStateException(ex.toString());
            illEx.initCause(ex);
            throw illEx;
        }
    
voidsetSoftRef(java.lang.ref.SoftReference softRef)

    
voidsetState(int s)

        state = s;
    
public voidsetTransaction(javax.transaction.Transaction tr)

        transaction = tr;
    
voidtouch()

        lastTimeUsed = System.currentTimeMillis();
    
public voidunregisterResource(com.sun.enterprise.resource.ResourceHandle h)
Unregister a resource from this Context.

        if ( resources == null )
            resources = new ArrayList();
        resources.remove(h);