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


/**
 * Pools EJBs within a ThreadLocal.
 *
 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 * @version $Revision: 1.3.2.4 $
 */
public class ThreadlocalPool extends AbstractPool
{
   protected ThreadLocal pool = new ThreadLocal();

   public ThreadlocalPool()
   {
   }

   public BeanContext get()
   {
      BeanContext ctx = (BeanContext) pool.get();
      if (ctx != null)
      {
         pool.set(null);
         return ctx;
      }

      ctx = create();
      return ctx;
   }

   public BeanContext get(Class[] initTypes, Object[] initValues)
   {
      BeanContext ctx = (BeanContext) pool.get();
      if (ctx != null)
      {
         pool.set(null);
         return ctx;
      }

      ctx = create(initTypes, initValues);
      return ctx;
   }

   public void release(BeanContext ctx)
   {
      if (pool.get() != null)
         remove(ctx);
      else
         pool.set(ctx);
   }

}
