Example usage for javax.naming.directory InitialDirContext InitialDirContext

List of usage examples for javax.naming.directory InitialDirContext InitialDirContext

Introduction

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

Prototype

public InitialDirContext(Hashtable<?, ?> environment) throws NamingException 

Source Link

Document

Constructs an initial DirContext using the supplied environment.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
    env.put(Context.PROVIDER_URL, MY_HOST);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, MGR_DN);
    env.put(Context.SECURITY_CREDENTIALS, MGR_PW);
    DirContext ctx = new InitialDirContext(env);
    ctx.destroySubcontext(MY_ENTRY);//  ww w.ja v a2s.  c o  m
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
    env.put(Context.PROVIDER_URL, MY_HOST);
    DirContext ctx = new InitialDirContext(env);
    SearchControls constraints = new SearchControls();
    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
    NamingEnumeration results = ctx.search(MY_SEARCHBASE, MY_FILTER, constraints);
    while (results != null && results.hasMore()) {
        SearchResult sr = (SearchResult) results.next();
        String dn = sr.getName() + ", " + MY_SEARCHBASE;

        System.out.println("Distinguished Name is " + dn);
        Attributes ar = ctx.getAttributes(dn, MY_ATTRS);
        if (ar == null) {
            System.out.println("Entry " + dn + " has none of the specified attributes\n");
            return;
        }/*  www .ja  v  a 2  s.c o m*/
        for (int i = 0; i < MY_ATTRS.length; i++) {
            Attribute attr = ar.get(MY_ATTRS[i]);
            if (attr == null) {
                continue;
            }
            System.out.println(MY_ATTRS[i] + ":");
            for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
                System.out.println("\t" + vals.nextElement());

            }
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Hashtable<String, String> env = new Hashtable<String, String>();

    env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);

    env.put(Context.PROVIDER_URL, MY_HOST);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, MGR_DN);
    env.put(Context.SECURITY_CREDENTIALS, MGR_PW);

    DirContext ctx = new InitialDirContext(env);

    ModificationItem[] mods = new ModificationItem[2];

    Attribute mod0 = new BasicAttribute("number", "555-555-5555");
    Attribute mod1 = new BasicAttribute("1", "AAA");

    mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod0);
    mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, mod1);

    ctx.modifyAttributes("uid=mewilcox, ou=People, o=airius.com", mods);
}

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 ww w. j  av a  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: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  ww. j  a  v  a2  s. co m*/
        // 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:RenameDiffParent.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  a2s  .  co  m*/
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Perform rename
        ctx.rename("cn=C. User, ou=NewHires", "cn=C. User, ou=People");

        // Check that it worked
        System.out.println(ctx.lookup("cn=C. User, ou=People"));

        // Revert change
        ctx.rename("cn=C. User, ou=People", "cn=C. User, ou=NewHires");

        // Check that we are back at our original setup
        System.out.println(ctx.lookup("cn=C. User, ou=NewHires"));

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

From source file:NewConn.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 www.  j ava2s .  c  om
        // Create initial context (first connection)
        DirContext ctx = new InitialDirContext(env);

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

        // Change authentication properties in ctx2
        ctx2.addToEnvironment(Context.SECURITY_PRINCIPAL, "cn=C. User, ou=NewHires, o=JNDITutorial");
        ctx2.addToEnvironment(Context.SECURITY_CREDENTIALS, "mysecret");

        // Method on ctx2 will use new connection
        System.out.println(ctx2.getAttributes("ou=NewHires"));

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

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 {//from  ww  w.  j  a  v  a  2 s .  c om
        // 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();
    }
}

From source file:Timeout.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");

    // Specify timeout to be 5 seconds
    env.put("com.sun.jndi.ldap.connect.timeout", "5000");

    try {/* w w w . ja va 2s  . co m*/
        // 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:Compare.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 {/*ww  w  . j a  va  2 s  .c om*/
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Value of attribute
        byte[] key = { (byte) 0x61, (byte) 0x62, (byte) 0x63, (byte) 0x64, (byte) 0x65, (byte) 0x66,
                (byte) 0x67 };

        // Set up search controls
        SearchControls ctls = new SearchControls();
        ctls.setReturningAttributes(new String[0]); // return no attrs
        ctls.setSearchScope(SearchControls.OBJECT_SCOPE); // search object only

        // Invoke search method that will use the LDAP "compare" operation
        NamingEnumeration answer = ctx.search("cn=S. User, ou=NewHires", "(mySpecialKey={0})",
                new Object[] { key }, ctls);

        // Print the answer
        // FilterArgs.printSearchEnumeration(answer);

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