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

import java.lang.reflect.InvocationTargetException;
import org.jboss.annotation.ejb.LocalHome;
import org.jboss.annotation.ejb.RemoteBinding;
import org.jboss.ejb3.JBossProxy;
import org.jboss.ejb3.ProxyFactoryHelper;
import org.jboss.naming.NonSerializableFactory;
import org.jboss.naming.Util;


/**
 * Comment
 *
 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 * @version $Revision: 1.2.2.5 $
 */
public class StatefulLocalProxyFactory extends BaseStatefulProxyFactory
{
   protected Class[] getInterfaces()
   {
      Class[] interfaces;

      Class[] localInterfaces = ProxyFactoryHelper.getLocalInterfaces(container);
      StatefulContainer statefulContainer = (StatefulContainer) container;
      LocalHome localHome = (LocalHome) statefulContainer.resolveAnnotation(LocalHome.class);
      if (localHome != null)
         interfaces = new Class[localInterfaces.length + 2];
      else
         interfaces = new Class[localInterfaces.length + 1];

      System.arraycopy(localInterfaces, 0, interfaces, 0, localInterfaces.length);
      interfaces[localInterfaces.length] = JBossProxy.class;

      if (localHome != null)
         interfaces[localInterfaces.length + 1] = localHome.value();

      return interfaces;
   }

   protected void initializeJndiName()
   {
      jndiName = ProxyFactoryHelper.getLocalJndiName(container);
   }

   public void start() throws Exception
   {
      super.start();
      NonSerializableFactory.rebind(container.getInitialContext(), jndiName + PROXY_FACTORY_NAME, this);
   }

   public void stop() throws Exception
   {
      super.stop();
      Util.unbind(container.getInitialContext(), jndiName + PROXY_FACTORY_NAME);
   }

   public Object createProxy()
   {
      try
      {
         Object[] args = {new StatefulLocalProxy(container)};
         return proxyConstructor.newInstance(args);
      }
      catch (InstantiationException e)
      {
         throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
      }
      catch (IllegalAccessException e)
      {
         throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
      }
      catch (IllegalArgumentException e)
      {
         throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
      }
      catch (InvocationTargetException e)
      {
         throw new RuntimeException(e.getTargetException());  //To change body of catch statement use Options | File Templates.
      }
   }

   public Object createProxy(Class[] initTypes, Object[] initValues)
   {
      try
      {
         Object[] args = {new StatefulLocalProxy(container, initTypes, initValues)};
         return proxyConstructor.newInstance(args);
      }
      catch (InstantiationException e)
      {
         throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
      }
      catch (IllegalAccessException e)
      {
         throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
      }
      catch (IllegalArgumentException e)
      {
         throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
      }
      catch (InvocationTargetException e)
      {
         throw new RuntimeException(e.getTargetException());  //To change body of catch statement use Options | File Templates.
      }
   }

   protected StatefulHandleImpl getHandle()
   {
      StatefulHandleImpl handle = new StatefulHandleImpl();
      RemoteBinding remoteBinding = (RemoteBinding)advisor.resolveAnnotation(RemoteBinding.class);
      if (remoteBinding != null)
         handle.jndiName = remoteBinding.jndiBinding();
 
      return handle;
   }
}
