package examples.jndi;

import javax.naming.*;
import java.util.*;
import java.io.*;
import weblogic.common.*;
import weblogic.jndi.*;

/**
 * This example illustrates how to obtain an initial WebLogic naming context. 
 * It shows how to create a context with default (guest access) to the naming 
 * system, and how to create a context that has authenticated access to the 
 * naming system.
 * <p>
 * Build the example:
 * <ol>
 * <li>
 * Set up your development shell, as described in 
 * <a href="../../techstart/environment.html">Setting up 
 * your environment</a>.
 * <li>
 * Compile the files in this directory as shown in this examples for Windows NT:
 * <pre>  $<font face="Courier New"><b> javac -d %CLIENT_CLASSES% InitialContextExample.java</b></font></pre>
 * </ol>
 * Run the example:
 * <ol>
 * <li>
 * Start WebLogic Server in a new command shell.
 * <li>
 * In your development shell, run the client with the following command:
 * <pre>  $<font face="Courier New"><b> java examples.jndi.InitialContextExample t3://<i>hostname</i>:<i>port username password</i></b></font></pre>
 * where:
 * <dl>
 * <dt><i><font face="Courier New" size = -1><b>hostname</b></font></i>
 * <dd>Host name of the WebLogic Server
 * <dt><i><font face="Courier New" size = -1><b>port</b></font></i>
 * <dd>Port where the WebLogic Server is listening for connections
 * (<font face="Courier New" size=-1>weblogic.system.ListenPort</font>). 
 * <dt><i><font face="Courier New" size = -1><b>username password</b></font></i>
 * <dd>If provided, the example creates an initial context with authenticated access
 * privileges by also specifying the <tt>Context.SECURITY_CREDENTIALS</tt>
 * property
 * </dl>
 *
 * @author Copyright (c) 1998-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class InitialContextExample {
  /**
   * Runs this example from the command line.
   */
  public static void main(String[] args) {
    if (args.length > 3) {
      System.out.println("Usage:  java examples.jndi.InitialContextExample "
                         + "[WebLogicURL [userName password]]");
      System.out.println("Example:java examples.jndi.InitialContextExample "
                         + "t3://localhost:7001 john mypasswd");
      
    }
    else {
      int idx = 0;
      // parse the url, user, and password if given
      String url      = (args.length > idx) ? args[idx++] : null;
      String user     = (args.length > idx) ? args[idx++] : null;
      String password = (args.length > idx) ? args[idx++] : "";
      Context ctx = null;
      
      try {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, 
          weblogic.jndi.WLInitialContextFactory.class.getName());
        if (url != null) {
          env.put(Context.PROVIDER_URL, url);
        }
        if (user == null) {
          ctx = new InitialContext(env);
          System.out.println("WebLogic context created on behalf of \"guest\"");
        } else {
          // Package user and password in T3User to ensure that password is
          // encrypted.
          env.put(Context.SECURITY_CREDENTIALS, new T3User(user, password));
          // The following 2 lines show an alternative method for specifying name 
          // and password that transmits the password in clear text:
          //   env.put(Context.SECURITY_PRINCIPAL, user);
          //   env.put(Context.SECURITY_CREDENTIALS, password);
          ctx = new InitialContext(env);
          System.out.println("WebLogic context created on behalf of \"" + user + "\"");
        }
      }
      catch (AuthenticationException e) {
        System.out.println("You've specified an invalid user name or password");
      }
      catch (CommunicationException e) {
        System.out.println(
          "Failed to contact " + 
          ((url == null) ? "t3://localhost:7001" : url) + ".\n" +
          "Is there a server running at this address?"
        );
      }
      catch (Exception e) {
        System.out.println("An unexpected exception occurred:" +e);
        e.printStackTrace();
      }
      finally {
        if (ctx != null) {
        // Always close context when finished.
          try {
            ctx.close();
          }
          catch (NamingException e) {
            System.out.println("Failed to close context due to: " + e);
          }
        }
      }
    }
  }
}  









