package examples.ejb.extensions.readMostly;

import java.rmi.RemoteException;
import java.util.Properties;

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

/**
 * @author Copyright (c) 2000 by BEA WebXpress. All Rights Reserved.
 */

public final class Client {

  private final static boolean verbose = true;

  private String url;
  private String user;
  private String password;
  private StockHome readerHome;
  private StockWriterHome writerHome;

  public Client(String url, String user, String password)
    throws NamingException
  {

    this.url       = url;
    this.user      = user;
    this.password  = password;

    readerHome = (StockHome)
      narrow(lookupHome("readMostly.StockHome"), StockHome.class);
    writerHome = (StockWriterHome)
      narrow(lookupHome("readMostly.StockWriterHome"), StockWriterHome.class);
  }

  public static void main(String[] args) {
    System.out.println("\nBeginning readMostly.Client...\n");

    String url       = "t3://localhost:7001";
    String user      = null;
    String password  = null;
     
    // Parse the argument list 
    switch(args.length) {
    case 3:
      password  = args[2];
      // fall through
    case 2:
      user      = args[1];
      // fall through
    case 1:
      url       = args[0];
      break;
    }
    
    Client client = null;

    try {
      client = new Client(url, user, password);
    } catch (NamingException ne) {
      System.exit(1);
    }

    try {
      client.example();
    } catch (Exception e) {
      log("There was an exception while creating and using the Beans.");
      log("This indicates that there was a problem communicating with the server: "+e);
    } 

    System.out.println("\nEnd readMostly.Client...\n");
  }

  public void example() throws CreateException, FinderException, RemoteException, RemoveException {

    String stock = "BEAS";
    log("Creating a StockWriter for " + stock);
    StockWriter writer = (StockWriter)
      narrow(writerHome.create(stock, 100, 1000), StockWriter.class);

    log("Looking up Stock info for " + stock);
    Stock reader = (Stock)
      narrow(readerHome.findByPrimaryKey(stock), Stock.class);
    log(reader.getSymbol() + ":");
    log("\tPrice: " + reader.getPrice());
    log("\tVolume: " + reader.getVolume());
    log("\t52 Week High: " + reader.get52weekHigh());
    log("\t52 Week Low: " + reader.get52weekLow());

    log("Removing the StockWriter");
    writer.remove();

  }

  /**
   *  RMI/IIOP clients should use PortableRemoteObject.narrow()
   */
  private Object narrow(Object ref, Class c) {
    return PortableRemoteObject.narrow(ref, c);
  }

  private Object lookupHome(String homeName)
    throws NamingException
  {
    // Lookup the beans home using JNDI
    Context ctx = getInitialContext();
   
    try {
      return ctx.lookup(homeName);
    } catch (NamingException ne) {
      log("The client was unable to lookup the EJBHome.  Please make sure ");
      log("that you have deployed the ejb with the JNDI name "+homeName+" on the WebLogic server at "+url);
      throw ne;
    }
  }


  /**
   * Java2 clients can use the jndi.properties file to set the
   * INITIAL_CONTEXT_FACTORY and the PROVIDER_URL
   */
//    private Context getInitialContext() throws NamingException {
//      return new InitialContext();
//    }

  /**
   * Using a Properties object will work on JDK 1.1.x and Java2
   * clients
   */
  private Context getInitialContext() throws NamingException {
    
    try {
      // Get an InitialContext
      Properties h = new Properties();
      h.put(Context.INITIAL_CONTEXT_FACTORY,
        "weblogic.jndi.WLInitialContextFactory");
      h.put(Context.PROVIDER_URL, url);
      if (user != null) {
        log ("user: " + user);
        h.put(Context.SECURITY_PRINCIPAL, user);
        if (password == null) 
          password = "";
        h.put(Context.SECURITY_CREDENTIALS, password);
      } 
      return new InitialContext(h);
    } catch (NamingException ne) {
      log("We were unable to get a connection to the WebLogic server at "+url);
      log("Please make sure that the server is running.");
      throw ne;
    }
  }

  private static void log(String s) {
    System.out.println(s);
  }

}
