Example usage for javax.naming Context INITIAL_CONTEXT_FACTORY

List of usage examples for javax.naming Context INITIAL_CONTEXT_FACTORY

Introduction

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

Prototype

String INITIAL_CONTEXT_FACTORY

To view the source code for javax.naming Context INITIAL_CONTEXT_FACTORY.

Click Source Link

Document

Constant that holds the name of the environment property for specifying the initial context factory to use.

Usage

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 a 2 s.  c o  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:Unbind.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 {/* ww  w  .j  a  v  a  2  s .c om*/
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Remove the binding
        ctx.unbind("cn=Favorite Fruit");

        // Check that it is gone
        Object obj = null;
        try {
            obj = ctx.lookup("cn=Favorite Fruit");
        } catch (NameNotFoundException ne) {
            System.out.println("unbind successful");
            return;
        }

        System.out.println("unbind failed; object still there: " + obj);

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

From source file:BadPasswd.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 give incorrect password
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "notmysecret");

    try {/*from  w  w w  .ja  v a 2  s.c om*/
        // 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: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 {//w  w  w. j  a va 2  s . c o 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: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   w  ww  . j  a v  a  2s  . co 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: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 {/* w w  w.jav a2  s .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: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 {//w w  w  .  j a  va 2s.  c o m
        // 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: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   w  w  w.j  a  v  a  2  s. c  o  m*/
        // 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: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 {/* ww  w.j  av a  2s . 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:RenameKeepRDN.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");

    // Set property to keep RDN
    env.put("java.naming.ldap.deleteRDN", "false");

    try {//from  ww 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=Claude User,ou=NewHires");

        // Check that it worked
        System.out.println(ctx.getAttributes("cn=Claude User,ou=NewHires"));

        // Revert change
        // Make sure new name doesn't get converted into attribute

        ctx.removeFromEnvironment("java.naming.ldap.deleteRDN");
        ctx.rename("cn=Claude User, ou=NewHires", "cn=C. User,ou=NewHires");

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

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