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

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import javax.naming.Context;
import org.jboss.aop.Advisor;
import org.jboss.ejb3.Container;
import org.jboss.ejb3.ProxyFactory;
import org.jboss.naming.Util;

/**
 * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
 * @version $Revision: 1.1.4.3 $
 */
public abstract class BaseServiceProxyFactory implements ProxyFactory
{

   protected Class proxyClass;
   protected Constructor proxyConstructor;
   protected Context proxyFactoryContext;
   protected String jndiName;
   protected Container container;
   protected Advisor advisor;

   public void start() throws Exception
   {
      initializeJndiName();
      Class[] interfaces = getInterfaces();
      Class proxyClass = java.lang.reflect.Proxy.getProxyClass(container.getBeanClass().getClassLoader(), interfaces);
      final Class[] constructorParams =
              {InvocationHandler.class};
      proxyConstructor = proxyClass.getConstructor(constructorParams);

      Util.rebind(container.getInitialContext(), jndiName, createProxy());
   }

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

   protected abstract Class[] getInterfaces();

   protected abstract void initializeJndiName();

   public void setContainer(Container container)
   {
      this.container = container;
      this.advisor = (Advisor) container;
   }

}
