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


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.util.Hashtable;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.jboss.ejb3.remoting.Proxy;

/**
 * Comment
 *
 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 * @version $Revision: 1.5.4.4 $
 */
public abstract class LocalProxy implements InvocationHandler, Serializable, Proxy
{
   protected Container container;
   protected String ejbName;

   public LocalProxy()
   {
   }

   public LocalProxy(Container container)
   {
      this.container = container;
      this.ejbName = container.getEjbName();
   }

   private void readObject(ObjectInputStream in)
           throws IOException, ClassNotFoundException
   {
      this.ejbName = in.readUTF();
      Hashtable props = (Hashtable)in.readObject();
      try
      {
         InitialContext ctx = EJB3Util.getInitialContext(props);

         this.container = (Container) ctx.lookup("containers/" + ejbName);
      }
      catch (NamingException e)
      {
         throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
      }
   }

   private void writeObject(ObjectOutputStream out)
           throws IOException
   {
      out.writeUTF(container.getEjbName());
      out.writeObject(container.getInitialContextProperties());
   }

   public abstract String toString();
}
