package examples.ejb.utils;

import javax.naming.*;
import javax.ejb.*;
import java.util.*;

import weblogic.jndi.*;

/**
 * This class demonstrates finding all the Enterprise JavaBeans
 *  in the JNDI tree
 *
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc, Inc. All Rights Reserved.
 */

public class ListAll {

  static String url      = "t3://localhost:7001";
  static String user     = null;
  static String password = null;

  /**
   * Runs this example from the command line. Example:
   * <p>
   * <tt>java examples.ejb.utils.ListAll</tt>
   * <p>
   * The parameters are optional, but if any are supplied,
   * they are interpreted in this order:
   * <p>
   * @param url               URL such as "t3://localhost:7001" of Server
   * @param user              User name, default null
   * @param password          User password, default null
   */
  public static void main(String[] args) {

    System.out.println("Starting to search tree for all EJBeans...\n");

    // Parse the argument list 
    if ((args != null) && (args.length == 3)) {
      url = args[0];
      user = args[1];
      password = args[2];
    }

    try {
      Context ctx = getInitialContext();
      printAllEJBs(ctx);
      System.out.println("\nFinished searching tree for all EJBeans...\n");
    }
    catch (Exception e) {
      System.out.println("ListAll: main() error:\n" + e);
    } 
  }

  // prints all EJBs in tree recursively 
  private static void printAllEJBs(Context ctx) {
    try {
      NamingEnumeration enum = ctx.listBindings(""); 
      while (enum.hasMore()) {
        Binding binding = (Binding)enum.next(); 
        Object object = binding.getObject(); 
        if (object instanceof EJBHome) { 
          System.out.println(""); 
          System.out.println("EJBean Home: " + ((WLContext)ctx).getNameInNamespace(binding.getName()));
          System.out.println("Classname: " + object.getClass().getName());
        }
        else if (object instanceof Context) {
          try { 
            printAllEJBs((Context)object);
          }
          catch (Exception e) {
            // ignore
          } 
        }
      }
    }
    catch (NamingSecurityException nse) {
      // ignore
    } 
    catch (SecurityException se) {
      // ignore
    } 
    catch (Exception e) {
      // ignore
    } 
  }

  /**
   * Gets an initial context.
   *
   * @return                  Context
   * @exception               java.lang.Exception if there is
   *                          an error in getting a Context
   */
  static public Context getInitialContext() throws Exception {
    Hashtable h = new Hashtable();
    h.put(Context.INITIAL_CONTEXT_FACTORY,
        "weblogic.jndi.WLInitialContextFactory");
    h.put(Context.PROVIDER_URL, url);
    if (user != null) {
      System.out.println ("user: " + user);
      h.put(Context.SECURITY_PRINCIPAL, user);
      if (password == null) 
      password = "";
      h.put(Context.SECURITY_CREDENTIALS, password);
    } 
    return new InitialContext(h);
  }
}
