package ejava.examples.personnel.ejbclient;

import ejava.examples.personnel.ejb20.RegistrarRemoteHome;
import ejava.examples.personnel.ejb20.RegistrarRemote;
import ejava.examples.personnel.Person;


import javax.ejb.FinderException;
import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;

import java.util.Hashtable;
import java.io.Serializable;

public class RegistrarAccessor implements Serializable {

   public RegistrarAccessor() {}

   public Person insert(Person p)
      throws NamingException, RemoteException, CreateException {

      RegistrarRemoteHome rHome = getRegistrarHome();
      RegistrarRemote registrar = rHome.create();
      return registrar.add(p);
   }

   protected RegistrarRemoteHome getRegistrarHome() 
      throws NamingException {

      Context ctx = getInitialContext();
      Object object = ctx.lookup(getName());
      RegistrarRemoteHome rHome = (RegistrarRemoteHome)
         PortableRemoteObject.narrow(object, RegistrarRemoteHome.class);

      return rHome;
   }

   protected String getName() { return "java:comp/env/RegistrarRemoteHome"; }

   protected Context getInitialContext() throws NamingException {
      return new InitialContext();
   }

   public static void main(String args[]) throws Exception {
      
      String url = "t3://localhost:7001";
      String driver = "weblogic.jndi.WLInitialContextFactory";
      String user = null;
      String password = null;
      String name = "personnel/RegistrarRemoteHome";
      String command = null;
      Person insertPerson=null;
      for(int i=0; i<args.length; i++) {
         if (args[i].equals("-url"))      { url=args[++i]; }
         if (args[i].equals("-driver"))   { driver=args[++i]; }
         if (args[i].equals("-user"))     { user=args[++i]; }
         if (args[i].equals("-password")) { password=args[++i]; }
         if (args[i].equals("-name"))     { name=args[++i]; }
         if (args[i].equals("-insert"))   { 
	    String fName=args[++i];
	    String lName=args[++i];
	    String address=args[++i];
	    String pNumber=args[++i];
	    insertPerson = new Person(null, fName, lName, address, pNumber);
	    command="insert";
	 }
      }

      final Hashtable props = new Hashtable();
      props.put(Context.PROVIDER_URL, url);
      props.put(Context.INITIAL_CONTEXT_FACTORY, driver);
      if (user != null) { 
         props.put(Context.SECURITY_PRINCIPAL, user); 
      }
      if (password != null) { 
         props.put(Context.SECURITY_CREDENTIALS, password); 
      }

      final String contextName = name;
      RegistrarAccessor accessor = new RegistrarAccessor() {
         protected Context getInitialContext() throws NamingException {
            return new InitialContext(props);
	 }
         protected String getName() { return contextName; }
      };

      try {
         if (command.equals("insert")) {
            System.out.println("inserted:"+ accessor.insert(insertPerson));
         }
      }
      catch (Exception ex) {
         System.out.println(command + "failed:" + ex.getMessage());
      }


   }

}
