package org.jboss.test.txtimer.ejb;
import org.apache.log4j.Logger;
import javax.ejb.*;
import java.rmi.RemoteException;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Properties;
import java.security.Principal;
import java.io.Serializable;
public class TimerEntityBean
        implements EntityBean, TimedObject
{
   private static Logger log = Logger.getLogger(TimerEntityBean.class);
   private EntityContext context;
      private int callCount;
      private Principal ejbTimeoutCaller;
   
   public void createTimer(long duration, long periode, Serializable info)
   {
      TimerService timerService = context.getTimerService();
      if (periode > 0)
         timerService.createTimer(duration, periode, info);
      else
         timerService.createTimer(duration, info);
   }
   
   public void cancelFirstTimer()
   {
      TimerService timerService = context.getTimerService();
      if (timerService.getTimers().isEmpty())
         throw new EJBException("There are no timers");
      Timer timer = (Timer)timerService.getTimers().iterator().next();
      timer.cancel();
   }
   
   public Object createTimerReturnHandle(long duration)
   {
      TimerService timerService = context.getTimerService();
      Timer timer = timerService.createTimer(duration, null);
      return timer.getHandle();
   }
   
   public String passTimerHandle(Object handle)
   {
      return handle.toString();
   }
   
   public void resetCallCount()
   {
      callCount = 0;
   }
   
   public int getCallCount()
   {
      Object pk = context.getPrimaryKey();
      log.info("getCallCount [pk=" + pk + ",count=" + callCount + ",this=" + this + "]");
      return callCount;
   }
   
   public List getTimers()
   {
      TimerService timerService = context.getTimerService();
      ArrayList handles = new ArrayList();
      Iterator it = timerService.getTimers().iterator();
      while (it.hasNext())
      {
         Timer timer = (Timer) it.next();
         handles.add(timer.getHandle().toString());
      }
      return handles;
   }
   
   public Principal getEjbTimeoutCaller()
   {
      return ejbTimeoutCaller;
   }
   public void ejbTimeout(Timer timer)
   {
      callCount++;
      Object pk = context.getPrimaryKey();
      log.info("ejbTimeout [pk=" + pk + ",count=" + callCount + ",this=" + this + "] timer=" + timer);
      ejbTimeoutCaller = context.getCallerPrincipal();
      if (timer.getInfo() != null)
      {
         Properties props = (Properties)timer.getInfo();
         if ("true".equals(props.getProperty("rollback")))
         {
            props.setProperty("rollback", "false");
            throw new IllegalStateException("rollback on ejbTimeout");
         }
      }
   }
         
   
   public Integer ejbCreate(Integer pk) throws CreateException
   {
      log.info("ejbCreate [pk=" + pk + "]");
      return pk;
   }
   public void ejbPostCreate(Integer pk) throws CreateException
   {
   }
   
   public Integer ejbFindByPrimaryKey(Integer pk) throws FinderException
   {
      return pk;
   }
   public void ejbActivate() throws EJBException, RemoteException
   {
   }
   public void ejbLoad() throws EJBException, RemoteException
   {
   }
   public void ejbPassivate() throws EJBException, RemoteException
   {
   }
   public void ejbRemove() throws RemoveException, EJBException, RemoteException
   {
   }
   public void ejbStore() throws EJBException, RemoteException
   {
   }
   public void setEntityContext(EntityContext ctx) throws EJBException, RemoteException
   {
      this.context = ctx;
   }
   public void unsetEntityContext() throws EJBException, RemoteException
   {
   }
}