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: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 {//from   www  . j a v a  2s . c  om
        // 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: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 w  w .j a  va 2  s .c  om
      .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: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 {//from w w  w.ja  v  a 2 s .  co m
        // 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);
    }
}

From source file:com.app.server.node.NodeServer.java

/**
 * @param args/*from   w ww  .  j a  va  2s. c o m*/
 */
public static void main(String[] args) {
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
    // TODO Auto-generated method stub
    new NodeServer(Integer.parseInt(args[0])).start();
    try {
        new NodeResourceSender(args[0], deployer);
    } catch (Exception e) {
        log.error("Error in executing the NodeSender ", e);
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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 {/*from w w w  .j a va2 s.c  o  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:SortedResults.java

public static void main(String[] args) throws IOException {

    // 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/ou=People,o=JNDITutorial");

    try {/*from www  . j av a2 s . c  o m*/
        // Create initial context with no connection request controls
        LdapContext ctx = new InitialLdapContext(env, null);

        // Create a sort control that sorts based on CN
        String sortKey = "cn";
        ctx.setRequestControls(new Control[] { new SortControl(sortKey, Control.CRITICAL) });

        // Perform a search
        NamingEnumeration results = ctx.search("", "(objectclass=*)", new SearchControls());

        // Iterate over search results
        System.out.println("---->sort by cn");
        while (results != null && results.hasMore()) {
            // Display an entry
            SearchResult entry = (SearchResult) results.next();
            System.out.println(entry.getName());

            // Handle the entry's response controls (if any)
            if (entry instanceof HasControls) {
                // ((HasControls)entry).getControls();
            }
        }
        // Examine the sort control response
        Control[] controls = ctx.getResponseControls();
        if (controls != null) {
            for (int i = 0; i < controls.length; i++) {
                if (controls[i] instanceof SortResponseControl) {
                    SortResponseControl src = (SortResponseControl) controls[i];
                    if (!src.isSorted()) {
                        throw src.getException();
                    }
                } else {
                    // Handle other response controls (if any)
                }
            }
        }

        // Close when no longer needed
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

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 {/*w  w w  .  ja  va 2s.c o  m*/
        // 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:RegUnsol.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 av  a  2s .c o  m
        // Get event context for registering listener
        EventContext ctx = (EventContext) (new InitialContext(env).lookup("ou=People"));

        // Create listener
        NamingListener listener = new UnsolListener();

        // Register listener with context (all targets equivalent)
        ctx.addNamingListener("", EventContext.ONELEVEL_SCOPE, listener);

        // Wait 1 minutes for listener to receive events
        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            System.out.println("sleep interrupted");
        }

        // Not strictly necessary if we're going to close context anyhow
        ctx.removeNamingListener(listener);

        // Close context when we're done
        ctx.close();

    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:SerObjWithCodebase.java

public static void main(String[] args) {

    if (args.length != 1) {
        System.err.println("usage: java SerObjWithCodebase <codebase URL>");
        System.exit(-1);//ww w.  j a va2  s .  c om
    }

    String codebase = args[0];

    // 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 {
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);

        // Create object to be bound
        Flower f = new Flower("rose", "pink");

        // Perform bind and specify codebase
        ctx.bind("cn=Flower", f, new BasicAttributes("javaCodebase", codebase));

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

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

From source file:ModAttrs.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 av a2s.  c om
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);
        String name = "cn=Ted Geisel, ou=People";

        // Save original attributes
        Attributes orig = ctx.getAttributes(name, new String[] { "mail", "telephonenumber", "jpegphoto" });

        // Specify the changes to make
        ModificationItem[] mods = new ModificationItem[3];

        // Replace the "mail" attribute with a new value
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("mail", "geisel@wizards.com"));

        // Add additional value to "telephonenumber"
        mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
                new BasicAttribute("telephonenumber", "+1 555 555 5555"));

        // Remove the "jpegphoto" attribute
        mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("jpegphoto"));

        // Perform the requested modifications on the named object
        ctx.modifyAttributes(name, mods);

        // Check attributes
        System.out.println("**** new attributes *****");
        printAttrs(ctx.getAttributes(name));

        // Revert changes
        ctx.modifyAttributes(name, DirContext.REPLACE_ATTRIBUTE, orig);

        // Check that the attributes got restored
        System.out.println("**** reverted to original attributes *****");
        printAttrs(ctx.getAttributes(name));

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