package ejava.ejb.uid.client;

import ejava.ejb.uid.UID;
import ejava.ejb.uid.UIDGenerator;
import ejava.ejb.uid.UIDGeneratorHome;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import java.util.Properties;


public class Client {
   private static String user_;
   private static String password_;

   private static UIDGeneratorHome getHome() throws NamingException {
      Context ctx = getInitialContext();
      Object object = ctx.lookup("UIDGeneratorHome");
      UIDGeneratorHome uHome = (UIDGeneratorHome)
         PortableRemoteObject.narrow(object, UIDGeneratorHome.class);

      return uHome;
   }

   private static Context getInitialContext() throws NamingException {
      Properties props = new Properties();
      props.put(Context.INITIAL_CONTEXT_FACTORY,
                "weblogic.jndi.WLInitialContextFactory");
      props.put(Context.PROVIDER_URL,
                "t3://localhost:7001");
      if (user_ != null) {
         props.put(Context.SECURITY_PRINCIPAL, user_);
      }
      if (password_ != null) {
         props.put(Context.SECURITY_CREDENTIALS, password_);
      }

      return new InitialContext(props);
   }

   public static void main(String args[]) {
      int count=1;

      for (int i=0; i<args.length; i++) {
         if (args[i].equals("-u")) user_ = args[++i];
         else if (args[i].equals("-p")) password_ = args[++i];
         else if (args[i].equals("-c")) count = Integer.parseInt(args[++i]);
      }

      System.out.println("count="+count);

      try {
         UIDGeneratorHome uidHome = getHome();
	 UIDGenerator uidGen = uidHome.find();
	 for(int i=0; i<count; i++) {
	    System.out.println(uidGen.createUID().toString());
	 }
      }
      catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}
