/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.ejb3.test.timer;

import java.util.Date;
import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;

import org.jboss.logging.Logger;

/**
 * Comment
 *
 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 * @version $Revision: 1.1.2.5 $
 */
@Stateless
@Remote(TimerTester.class)
public class TimerTesterBean implements TimerTester
{
   private static final Logger log = Logger.getLogger(TimerTesterBean.class);
   
   public static boolean timerCalled = false;

   private @Resource TimerService timerService;

   private @Resource SessionContext ctx;
   
   private Timer timer;

   public void startTimer(long pPeriod)
   {
      timerCalled = false;
      System.out.println("************ startTimer");
      timer = timerService.createTimer(new Date(new Date().getTime() + pPeriod), "TimerSLSBean");
      
   }

   public void startTimerViaEJBContext(long pPeriod)
   {
      timerCalled = false;
      System.out.println("************ startTimerViaEJBContext");
      timer = ctx.getTimerService().createTimer(new Date(new Date().getTime() + pPeriod), "TimerSLSBean");
   }

   public void accessTimer()
   {
      //Access timer to make sure we have pushed the AllowedOperationsAssociation
      timer.getTimeRemaining();
      timer.getHandle();
      timer.getInfo();
   }
   
   public boolean isTimerCalled()
   {
      return timerCalled;
   }

   @Timeout
   public void timeoutHandler(Timer timer)
   {
      log.info("EJB TIMEOUT!!!!");
      timerCalled = true;
      timer.cancel();
   }
}
