Example usage for javax.naming.directory DirContext close

List of usage examples for javax.naming.directory DirContext close

Introduction

In this page you can find the example usage for javax.naming.directory DirContext close.

Prototype

public void close() throws NamingException;

Source Link

Document

Closes this context.

Usage

From source file:FullName.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//from   w ww .jav a2 s  .  c om
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        NamingEnumeration answer = ctx.search("ou=People", null);

        // Print the answer
        printSearchEnumeration(answer);

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:UseFactory.java

  public static void main(String[] args) {
  // Set up environment for creating initial context
  Hashtable<String, Object> env = new Hashtable<String, Object>(11);
  env/*  w  ww.  j av a  2 s .c  o m*/
      .put(Context.INITIAL_CONTEXT_FACTORY,
          "com.sun.jndi.ldap.LdapCtxFactory");
  env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

  // Specify the socket factory
  env.put("java.naming.ldap.factory.socket", "CustomSocketFactory");

  // Authenticate as S. User and password "mysecret"
  env.put(Context.SECURITY_AUTHENTICATION, "simple");
  env.put(Context.SECURITY_PRINCIPAL,
      "cn=S. User, ou=NewHires, o=JNDITutorial");
  env.put(Context.SECURITY_CREDENTIALS, "mysecret");

  try {
    // Create initial context
    DirContext ctx = new InitialDirContext(env);

    System.out.println(ctx.lookup("ou=NewHires"));

    // ... do something useful with ctx

    // Close the context when we're done
    ctx.close();
  } catch (NamingException e) {
    e.printStackTrace();
  }
}

From source file:ServerSasl.java

public static void main(String[] args) {

    try {// w ww . ja v  a  2 s.  c  om
        // Create initial context
        DirContext ctx = new InitialDirContext();

        // Read supportedSASLMechanisms from root DSE
        Attributes attrs = ctx.getAttributes("ldap://localhost:389",
                new String[] { "supportedSASLMechanisms" });

        System.out.println(attrs);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:GetAllAttrs.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//  w  w  w. j ava 2  s . c o  m
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);

        // Get all the attributes of named object
        Attributes answer = ctx.getAttributes("cn=Ted Geisel, ou=People");

        // Print the answer
        printAttrs(answer);

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Shared.java

public static void main(String[] args) {
    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//w  ww  .ja v a 2s .c  om
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Get a copy of the same context
        Context ctx2 = (Context) ctx.lookup("");

        // Get a child context
        Context ctx3 = (Context) ctx.lookup("ou=NewHires");

        // do something useful with ctx, ctx2, ctx3

        // Close the contexts when we're done
        ctx.close();
        ctx2.close();
        ctx3.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:UseDiff.java

public static void main(String[] args) {

    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    // Authenticate as S. User and password "mysecret"
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "mysecret");

    try {/* ww  w  . java  2s. c om*/
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // do something useful with ctx
        System.out.println(ctx.lookup("ou=NewHires"));

        // Change to using no authentication
        ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "none");

        System.out.println(ctx.lookup("ou=NewHires"));

        // do something useful with ctx

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:GetAttrs.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//from w  w  w  .  ja v  a2 s .co m
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Specify the ids of the attributes to return
        String[] attrIDs = { "sn", "telephonenumber", "golfhandicap", "mail" };

        // Get the attributes requested
        Attributes answer = ctx.getAttributes("cn=Ted Geisel, ou=People", attrIDs);

        // Print the answer
        printAttrs(answer);

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:RenameInterior.java

public static void main(String[] args) {

    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//from   w w  w .j a v  a2  s  .com
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Perform rename
        ctx.rename("ou=NewHires", "ou=OldHires");

        // Check that it worked
        System.out.println(ctx.lookup("ou=OldHires"));

        // Revert change
        ctx.rename("ou=OldHires", "ou=NewHires");

        // Check that we are back at our original setup
        System.out.println(ctx.lookup("ou=NewHIres"));

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:SearchCountLimit.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {/*w  w w  . j a  v  a2 s.co m*/
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Set search controls to limit count to 'expected'
        SearchControls ctls = new SearchControls();
        ctls.setCountLimit(expected);

        // Search for objects with those matching attributes
        NamingEnumeration answer = ctx.search("ou=People", "(sn=M*)", ctls);

        // Print the answer
        printSearchEnumeration(answer);

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:RetrievingLdapName.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//w  w w  .j a va 2  s . c  o  m
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        NamingEnumeration answer = ctx.search("ou=People", null);

        // Print the answer
        while (answer.hasMore()) {
            SearchResult sr = (SearchResult) answer.next();
            String name = sr.getNameInNamespace();
            System.out.println(name);
            LdapName dn = new LdapName(name);

            // do something with the dn
        }

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}