/*
 * JBoss, the OpenSource EJB server
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.ejb3.test.initial;

import java.io.Serializable;
import javax.ejb.AroundInvoke;
import javax.ejb.CallbackListener;
import javax.ejb.Interceptor;
import javax.ejb.InvocationContext;
import javax.ejb.Remote;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import org.jboss.annotation.ejb.cache.simple.CacheConfig;


/**
 * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
 * @version $Revision: 1.7.2.4 $
 */
@Stateful
@Interceptor(FirstInterceptor.class)
@CallbackListener(ExternalCallbackListener.class)
@CacheConfig(maxSize = 1)
@Remote(InterceptedSFTest.class)
public class InterceptedSFTestBean implements InterceptedSFTest, Serializable
{
   int val;

   public int testMethod(int i)
   {
      System.out.println("InterceptedSFTestBean testMethod");
      System.out.println("val: " + i);
      val = i;
      return i;
   }

   public int getVal()
   {
      return val;
   }

   @Remove
   public void clear()
   {

   }

   @AroundInvoke
   public Object myInterceptor(InvocationContext ctx) throws Exception
   {
      if (ctx.getMethod().getName().equals("testMethod"))
      {
         System.out.println("Intercepting in InterceptedSFTestBean.myInterceptor()");
         int val = (Integer) ctx.getContextData().get("DATA");

         int ret = (Integer) ctx.proceed();

         ctx.getEJBContext().setRollbackOnly();
         val = (Integer) ctx.getContextData().get("DATA");
         ret += val;
         if (ctx.getBean() != this) throw new RuntimeException("ctx.getBean() != this: " + ctx.getBean() + " != " + this);
         return ret;
      }

      return ctx.proceed();
   }
}
