
import java.util.Properties;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import mypackage1.*;

/**
 * This class illustrates calling the stateless session bean MySessionEJB
 * that was created with JDeveloperthe following exercises:
 */
public class Client {

  private static final String JNDI_NAME = &quot;MySessionEJB&quot;;
  private String url;
  private MySessionEJBHome home;

  public Client(String url)
    throws NamingException
  {

    this.url       = url;

    home = lookupHome();
  }


  public static void main(String[] args) {
    log(&quot;\nBeginning statefulSession.Client...\n&quot;);

    String url      = &quot;t3://localhost:7001&quot;;

    // Parse the argument list
     if (args.length != 1) {
      System.out.println(&quot;Usage: Client t3://hostname:port&quot;);
      return;
    } else {
      url = args[0];
    }

    Client client = null;

    try {
      client = new Client(url);
    } catch (NamingException ne) {
      System.exit(1);
    }

    try {
      client.example();
    } catch (Exception e) {
      log(&quot;There was an exception while creating and using the MySessionEJB.&quot;);
      log(&quot;This indicates that there was a problem communicating with the server: &quot;+e);
    }

    log(&quot;\nEnd statefulSession.Client...\n&quot;);
  }

  /**
   * Runs the example.
   */
  public void example()
    throws CreateException,  RemoteException,
           RemoveException
  {
    // Create a MySessionEJB
    log(&quot;Creating MySessionEJB\n&quot;);
    MySessionEJB mysession = (MySessionEJB) narrow(home.create(), MySessionEJB.class);

    String igot = mysession.getInfo();

    System.out.println(&quot;Here's what MySessionEJB returns: &quot;+igot);
    mysession.remove();

  }

  /**
   * RMI/IIOP clients should use this narrow function
   */
  private Object narrow(Object ref, Class c) {
    return PortableRemoteObject.narrow(ref, c);
  }

  /**
   * Lookup the EJBs home in the JNDI tree
   */
  private MySessionEJBHome lookupHome()
    throws NamingException
  {
    // Lookup the beans home using JNDI
    Context ctx = getInitialContext();

    try {
      Object home = ctx.lookup(JNDI_NAME);
      return (MySessionEJBHome) narrow(home, MySessionEJBHome.class);
    } catch (NamingException ne) {
      log(&quot;The client was unable to lookup the EJBHome.  Please make sure &quot;);
      log(&quot;that you have deployed the ejb with the JNDI name &quot;+JNDI_NAME+&quot; on the WebLogic server at &quot;+url);
      throw ne;
    }
  }

  /**
   * 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,
        &quot;weblogic.jndi.WLInitialContextFactory&quot;);
      h.put(Context.PROVIDER_URL, url);
      return new InitialContext(h);
    } catch (NamingException ne) {
      log(&quot;We were unable to get a connection to the WebLogic server at &quot;+url);
      log(&quot;Please make sure that the server is running.&quot;);
      throw ne;
    }
  }

  /**
   * This is the Java2 version to get an InitialContext.
   * This version relies on the existence of a jndi.properties file in
   * the application's classpath.
   *
   */
//    private static Context getInitialContext()
//      throws NamingException
//    {
//      return new InitialContext();
//    }

  private static void log(String s) {
    System.out.println(s);
  }

}
