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

import javax.ejb.Local;
import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
import javax.ejb.Remote;
import javax.ejb.Stateful;
import javax.naming.InitialContext;
import org.jboss.annotation.ejb.cache.Cache;
import org.jboss.annotation.ejb.cache.tree.CacheConfig;

/**
 * Comment
 *
 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 * @version $Revision: 1.6.2.4 $
 */
@Stateful
@Cache(org.jboss.ejb3.cache.tree.StatefulTreeCache.class)
@CacheConfig(name = "jboss.cache:service=EJB3TreeCache", maxSize = 1000, idleTimeoutSeconds = 1)
@Local(StatefulLocal.class)
@Remote(StatefulRemote.class)
public class StatefulBean implements StatefulRemote, java.io.Serializable, StatefulLocal
{
   public static boolean postActivateCalled = false;
   public static boolean prePassivateCalled = false;

   private String state;

   public long bench(int iterations) throws Exception
   {
      InitialContext ctx = new InitialContext();
      StatefulLocal local = (StatefulLocal) ctx.lookup(StatefulLocal.class.getName());
      long start = System.currentTimeMillis();
      for (int i = 0; i < iterations; i++)
      {
         local.getState();
      }
      long end = System.currentTimeMillis() - start;
      System.out.println(iterations + " stateful iterations took: " + end);
      return end;
   }

   public void longRunning() throws Exception
   {
      Thread.sleep(5000);
   }

   public boolean getPostActivate()
   {
      return postActivateCalled;
   }

   public boolean getPrePassivate()
   {
      return prePassivateCalled;
   }

   public void setState(String state)
   {
      this.state = state;
   }

   public String getState()
   {
      return this.state;
   }

   public void reset()
   {
      state = null;
      postActivateCalled = false;
      prePassivateCalled = false;
   }

   @PostActivate
   public void postActivate()
   {
      postActivateCalled = true;
   }

   @PrePassivate
   public void prePassivate()
   {
      prePassivateCalled = true;
   }


}
