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

import org.jboss.aop.advice.Interceptor;
import org.jboss.aop.joinpoint.Invocation;
import org.jboss.ejb3.BeanContext;
import org.jboss.ejb3.Container;
import org.jboss.ejb3.EJBContainerInvocation;
import org.jboss.ejb3.Pool;
import org.jboss.logging.Logger;

/**
 * Comment
 *
 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 * @version $Revision: 1.4.2.4 $
 */
public class StatelessInstanceInterceptor implements Interceptor
{
   private static final Logger log = Logger.getLogger(StatelessInstanceInterceptor.class);

   public String getName()
   {
      return "StatelessInstanceInterceptor";
   }

   public Object invoke(Invocation invocation) throws Throwable
   {
      EJBContainerInvocation ejb = (EJBContainerInvocation) invocation;
      Pool pool = ((Container) ejb.getAdvisor()).getPool();
      BeanContext ctx = pool.get();
      ejb.setTargetObject(ctx.getInstance());
      ejb.setBeanContext(ctx);
      Object rtn = ejb.invokeNext();
      ejb.setTargetObject(null);
      ejb.setBeanContext(null);
      pool.release(ctx);
      return rtn;
   }
}
