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

TimerWrapper

public class TimerWrapper extends Object implements javax.ejb.Timer, com.sun.ejb.spi.io.IndirectlySerializable

Fields Summary
private TimerPrimaryKey
timerId_
private EJBTimerService
timerService_
Constructors Summary
TimerWrapper(TimerPrimaryKey timerId, EJBTimerService timerService)

        timerId_      = timerId;
        timerService_ = timerService;   //TimerService passed in could be null
    
Methods Summary
public voidcancel()


        checkCallPermission();

        try {
            timerService_.cancelTimer(timerId_);
        } catch(FinderException fe) {
            throw new NoSuchObjectLocalException("timer no longer exists", fe);
        } catch(Exception e) {
            throw new EJBException(e);
        }

    
private static voidcheckCallPermission()
Verify that Timer method access is allowed from this context. This method is static so that TimerHandle can call it even before it has created a TimerWrapper instance.


        boolean allowed = false;

        ContainerFactoryImpl cf = (ContainerFactoryImpl) 
            Switch.getSwitch().getContainerFactory();
        EJBTimerService timerService = cf.getEJBTimerService();
        if( timerService == null ) {
            throw new IllegalStateException 
                ("EJBTimerService is not available");
        }

        try {
            InvocationManager invManager = 
                Switch.getSwitch().getInvocationManager();

            ComponentInvocation inv = invManager.getCurrentInvocation();
            if (inv == null)
                throw new IllegalStateException
                    ("Invocation cannot be null");

            int invType = inv.getInvocationType();
            if( invType == ComponentInvocation.EJB_INVOCATION ) { 
                if ( inv instanceof Invocation ) {
                    ComponentContext context = ((Invocation) inv).context;
                    // Delegate check to EJB context.  Let any 
                    // IllegalStateException bubble up.
                    context.checkTimerServiceMethodAccess();
                    allowed = true;
                } else {
                    // NOTE : There shouldn't be any cases where an EJB
                    // container uses com.sun.enterprise.ComponentInvocation
                    // instead of com.sun.ejb.Invocation and timer method
                    // access is allowed. No EJBContextImpl is available
                    // to perform checks.
                    allowed = false;
                }
            } else if( invType == ComponentInvocation.SERVLET_INVOCATION ) {
                throw new IllegalStateException
                    ("Web tier access to EJB timers through local " +
                     "interfaces is not portable");
            }
        } catch(InvocationException ie) {
            IllegalStateException ise = new IllegalStateException
                ("Operation not allowed");
            ise.initCause(ie);
            throw ise;
        }

        if( !allowed ) {
            throw new IllegalStateException("Operation not allowed");
        }
        
    
public booleanequals(java.lang.Object o)

        boolean equal = false;
        if(o instanceof TimerWrapper) {
            TimerWrapper other = (TimerWrapper) o;
            equal = other.timerId_.equals(this.timerId_);
        }
        return equal;
    
public javax.ejb.TimerHandlegetHandle()


        checkCallPermission();

        if( !timerService_.timerExists(timerId_) ) {
            throw new NoSuchObjectLocalException("timer no longer exists");
        } 

        return new TimerHandleImpl(timerId_);
    
public java.io.SerializablegetInfo()


        checkCallPermission();

        Serializable info;

        try {
            info = timerService_.getInfo(timerId_);
        } catch(FinderException fe) {
            throw new NoSuchObjectLocalException("timer no longer exists", fe);
        } 

        return info;
    
public java.util.DategetNextTimeout()


        checkCallPermission();

        Date nextTimeout;

        try {
            nextTimeout = timerService_.getNextTimeout(timerId_);
        } catch(FinderException fe) {
            throw new NoSuchObjectLocalException("timer no longer exists", fe);
        } 

        return nextTimeout;
    
public com.sun.ejb.spi.io.SerializableObjectFactorygetSerializableObjectFactory()

        return new SerializedTimerWrapper(timerId_);
    
public longgetTimeRemaining()


        Date nextTimeout = getNextTimeout();

        Date now = new Date();

        long timeRemaining = nextTimeout.getTime() - now.getTime();

        return (timeRemaining > 0) ? timeRemaining : 0;
    
public inthashCode()

        return timerId_.hashCode();
    
public java.lang.StringtoString()

        return "Timer " + timerId_;