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

import java.util.Properties;
import java.util.Hashtable;
import javax.jms.Destination;
import javax.naming.InitialContext;
import org.jboss.annotation.ejb.MessageProperties;
import org.jboss.aop.advice.Interceptor;
import org.jboss.naming.NonSerializableFactory;
import org.jboss.naming.Util;

/**
 * comment
 *
 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 */
public class LocalProducerFactory extends ProducerFactory
{
   protected String factoryName;

   public LocalProducerFactory(Class producer, MessageProperties props, Destination dest, InitialContext ctx, Hashtable icProperties)
   {
      super(producer, props, dest, ctx, icProperties);

      try
      {
         factoryName = pImpl.connectionFactory();
         if (factoryName.equals("")) factoryName = "java:/ConnectionFactory";
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }
   }

   public Object createProxy()
   {
      Class[] interfaces = {producer, ProducerObject.class};

      ProducerManagerImpl mImpl = null;

      mImpl = new ProducerManagerImpl(pImpl, dest, factoryName, props.delivery(), props.timeToLive(), props.priority(), methodMap, initialContextProperties);

      Interceptor[] interceptors = {mImpl};
      ProducerProxy ih = new ProducerProxy(mImpl, interceptors);
      return java.lang.reflect.Proxy.newProxyInstance(producer.getClassLoader(), interfaces, ih);
   }

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

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