example1.java

来自「100多M的J2EE培训内容」· Java 代码 · 共 352 行

JAVA
352
字号
package bible.jndi;



import javax.naming.*;
import javax.naming.directory.*;

import weblogic.jndi.*;

import java.util.*;


/**
 * Class Example1
 *
 *
 * @author
 * @version %I%, %G%
 */
public class Example1 {

  /**
   * Method getClientSideContext
   *
   *
   */
  public static void getClientSideContext() {

    Context   ctx = null;
    Hashtable ht  = new Hashtable();

    ht.put(Context.INITIAL_CONTEXT_FACTORY,
           "weblogic.jndi.WLInitialContextFactory");
    ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
    ht.put(Context.SECURITY_PRINCIPAL, "system");
    ht.put(Context.SECURITY_CREDENTIALS, "password");

    try {
      ctx = new InitialContext(ht);

      System.out.println("Context obtained; value = " + ctx.toString());
    } catch (NamingException e) {
      e.printStackTrace();
    } finally {
      try {

        // close the context when finished with it
        ctx.close();
        System.out.println("Context closed.");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  /**
   * Method getClientSideEnvContext
   *
   *
   */
  public static void getClientSideEnvContext() {

    Context     ctx = null;
    Environment env = new Environment();

    // env.setProviderUrl("t3://localhost:7001");
    env.setSecurityPrincipal("system");
    env.setSecurityCredentials("password");

    try {
      ctx = env.getInitialContext();

      System.out.println("Context obtained; value = " + ctx.toString());
    } catch (NamingException e) {
      e.printStackTrace();
    } finally {
      try {

        // close the context when finished with it
        ctx.close();
        System.out.println("Context closed.");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  /**
   * Method getLDAPAttributes
   *
   *
   * @param ctx
   *
   * @throws javax.naming.NamingException
   *
   */
  public static void getLDAPAttributes(DirContext ctx)
    throws javax.naming.NamingException {

    System.out.println("Dumping LDAP directory attributes...");

    Attributes attrs = ctx.getAttributes("");

    for (NamingEnumeration allAttr = attrs.getAll(); allAttr.hasMore(); ) {
      Attribute attr = (Attribute) allAttr.next();

      System.out.println("Attribute: " + attr.getID());

      for (NamingEnumeration values = attr.getAll(); values.hasMore(); ) {
        System.out.println("    Value: " + values.next());
      }
    }
  }

  /**
   * Method getLDAPContents
   *
   *
   * @param ctx
   *
   * @throws javax.naming.NamingException
   *
   */
  public static void getLDAPContents(DirContext ctx)
    throws javax.naming.NamingException {

    System.out.println("Dumping LDAP directory contents...");

    SearchControls cons = new SearchControls();

    cons.setSearchScope(SearchControls.SUBTREE_SCOPE);

    NamingEnumeration results = ctx.search("dc=JMZ,dc=local",
                                           "objectclass=person", cons);

    // NamingEnumeration results = ctx.search("dc=zeeware,dc=com", "objectclass=person", cons);
    while (results.hasMore()) {
      System.out.println("\n\n");

      SearchResult result = (SearchResult) results.next();
      Attributes   rAttrs = result.getAttributes();

      for (NamingEnumeration ne = rAttrs.getAll(); ne.hasMore(); ) {
        Attribute nAttr = (Attribute) ne.next();
        String    sID   = nAttr.getID();

        for (Enumeration vals = nAttr.getAll(); vals.hasMoreElements(); ) {
          System.out.println(sID + ": " + vals.nextElement());
        }
      }
    }
  }

  /**
   * Method getLDAPContext
   *
   *
   * @return
   *
   * @throws javax.naming.NamingException
   *
   */
  public static DirContext getLDAPContext()
    throws javax.naming.NamingException {

    DirContext ctx = null;
    Hashtable  ht  = new Hashtable();

    ht.put(Context.INITIAL_CONTEXT_FACTORY,
           "com.sun.jndi.ldap.LdapCtxFactory");
    ht.put(Context.PROVIDER_URL, "ldap://localhost:389");

    // ht.put(Context.PROVIDER_URL, "ldap://denver.zeeware.com:389");
    // ht.put(Context.SECURITY_AUTHENTICATION, "simple");
    // ht.put(Context.SECURITY_PRINCIPAL, "ZEEWARE\\Administrator");
    // ht.put(Context.SECURITY_CREDENTIALS, "lift^kitz");
    System.out.println("Attempting to obtain LDAP context...");

    ctx = new InitialDirContext(ht);

    System.out.println("Context obtained; value = " + ctx.toString());

    return ctx;
  }

  /**
   * Method getServerSideContext
   *
   *
   */
  public static void getServerSideContext() {

    Context   ctx = null;
    Hashtable ht  = new Hashtable();

    ht.put(Context.SECURITY_PRINCIPAL, "system");
    ht.put(Context.SECURITY_CREDENTIALS, "password");

    try {
      ctx = new InitialContext(ht);

      System.out.println("Context obtained; value = " + ctx.toString());
    } catch (NamingException e) {
      e.printStackTrace();
    } finally {
      try {

        // close the context when finished with it
        ctx.close();
        System.out.println("Context closed.");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  /**
   * Method bindObject
   *
   *
   */
  public static void bindObject() {

    Environment env = new Environment();
    Context     ctx = null;

    try {
      ctx = env.getInitialContext();

      System.out.println("Context obtained; value = " + ctx.toString());
      System.out.println("Binding a copy of the Person class...");

      Person  myPerson = new Person();
      Context subctx   = ctx.createSubcontext("JNDI Chapter");

      ctx.bind("JNDI Chapter/Person Object", myPerson);
    } catch (NamingException e) {
      e.printStackTrace();
    } finally {
      try {
        ctx.close();
        System.out.println("Context closed.");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  /**
   * Method lookupObject
   *
   *
   */
  public static void lookupObject() {

    Environment env = new Environment();
    Context     ctx = null;

    try {
      ctx = env.getContext("JNDI Chapter");

      System.out.println("Context obtained; value = " + ctx.toString());
      System.out.println("Looking up the copy of the Person class...");

      Person person = (Person) ctx.lookup("Person Object");

      System.out
        .println("Calling a method on the copy of the Person class...");
      System.out.println(person.getFullName());
    } catch (NamingException e) {
      e.printStackTrace();
    } finally {
      try {
        ctx.close();
        System.out.println("Context closed.");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  /**
   * Method getServerSideEnvContext
   *
   *
   */
  public static void getServerSideEnvContext() {

    Environment env    = new Environment();
    Context     ctx    = null;
    Context     subctx = null;

    try {
      ctx = env.getInitialContext();

      System.out.println("Context obtained; value = " + ctx.toString());
    } catch (NamingException e) {
      e.printStackTrace();
    } finally {
      try {

        // close the context when finished with it
        ctx.close();
        System.out.println("Context closed.");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  /**
   * Method main
   *
   *
   * @param args
   *
   */
  public static void main(String[] args) {

    // These calls will succeed when executed inside or outside WebLogic's JVM
    // getClientSideContext();
    // getClientSideEnvContext();
    // These calls will succeed only when executed inside WebLogic's JVM
    // getServerSideContext();
    // getServerSideEnvContext();
    // bindObject();
    // lookupObject();
    DirContext ctx = null;

    try {
      ctx = getLDAPContext();

      // getLDAPAttributes(ctx);
      getLDAPContents(ctx);
    } catch (NamingException e) {
      e.printStackTrace();
    } finally {
      try {
        ctx.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
}


/*--- Formatted in Bible Style on Thu, Sep 6, '01 ---*/


/*------ Formatted by Jindent 3.24 Gold 1.02 --- http://www.jindent.de ------*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?