Example usage for javax.naming Context close

List of usage examples for javax.naming Context close

Introduction

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

Prototype

public void close() throws NamingException;

Source Link

Document

Closes this context.

Usage

From source file:Lookup.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 ava2 s  . c o m
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Perform lookup and cast to target type
        LdapContext b = (LdapContext) ctx.lookup("cn=Rosanna Lee,ou=People");

        System.out.println(b);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Lookup failed: " + e);
    }
}

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 {//from   w w w  .j ava 2 s .  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:LookupLdapName.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/");

    try {/*ww w .j a  v  a  2  s. co m*/
        // Create the initial context
        Context ctx = new InitialContext(env);

        // A string representation of an LDAP name
        LdapName dn = new LdapName("ou=People,o=JNDITutorial");

        // Perform the lookup using the dn
        Object obj = ctx.lookup(dn);

        System.out.println(obj);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Lookup failed: " + e);
    }
}

From source file:SerObj.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 {// www  . j  a  v  a  2  s  .co m
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Create object to be bound
        Button b = new Button("Push me");

        // Perform bind
        ctx.bind("cn=Button", b);

        // Check that it is bound
        Button b2 = (Button) ctx.lookup("cn=Button");
        System.out.println(b2);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Operation failed: " + e);
    }
}

From source file:List.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 . j  a  va  2 s .  com
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Get listing of context
        NamingEnumeration list = ctx.list("ou=People");

        // Go through each item in list
        while (list.hasMore()) {
            NameClassPair nc = (NameClassPair) list.next();
            System.out.println(nc);
        }

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("List failed: " + e);
    }
}

From source file:ListBindings.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 www . ja v a 2s  .  c o m
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Get listing of context
        NamingEnumeration bindings = ctx.listBindings("ou=People");

        // Go through each item in list
        while (bindings.hasMore()) {
            Binding bd = (Binding) bindings.next();
            System.out.println(bd.getName() + ": " + bd.getObject());
        }

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("List Bindings failed: " + e);
    }
}

From source file:Create.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 .  ja va  2  s  . co  m*/
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);

        // Create attributes to be associated with the new context
        Attributes attrs = new BasicAttributes(true); // case-ignore
        Attribute objclass = new BasicAttribute("objectclass");
        objclass.add("top");
        objclass.add("organizationalUnit");
        attrs.put(objclass);

        // Create the context
        Context result = ctx.createSubcontext("ou=NewOu", attrs);

        // Check that it was created by listing its parent
        NamingEnumeration list = ctx.list("");

        // Go through each item in list
        while (list.hasMore()) {
            NameClassPair nc = (NameClassPair) list.next();
            System.out.println(nc);
        }

        // Close the contexts when we're done
        result.close();
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Create failed: " + e);
    }
}

From source file:Destroy.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  av a2  s. c om
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Destroy the context
        ctx.destroySubcontext("ou=NewOu");

        // Check that it has been destroyed by listing its parent
        NamingEnumeration list = ctx.list("");

        // Go through each item in list
        while (list.hasMore()) {
            NameClassPair nc = (NameClassPair) list.next();
            System.out.println(nc);
        }

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("destroy failed: " + e);
    }
}

From source file:Rebind.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  ww  .j a  va2s  . c  o  m
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Create the object to be bound
        Fruit fruit = new Fruit("lemon");

        // Perform the bind
        ctx.rebind("cn=Favorite Fruit", fruit);

        // Check that it is bound
        Object obj = ctx.lookup("cn=Favorite Fruit");
        System.out.println(obj);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Operation failed: " + e);
    }
}

From source file:Bind.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  om
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Create the object to be bound
        Fruit fruit = new Fruit("orange");

        // Perform the bind
        ctx.bind("cn=Favorite Fruit", fruit);

        // Check that it is bound
        Object obj = ctx.lookup("cn=Favorite Fruit");
        System.out.println(obj);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Operation failed: " + e);
    }
}