blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

LDAP Operation: Creating sub-Context with attributes (aka LDAP entry)

Let's try to create a sub-context under "o=csRepository" called "Contractors".
try {
 //In the main() method of the program, create an initial directory context. 
 //This is similar to creating an initial context in the previous naming example, 
 //except that you use the constructor for InitialDirContext
 Hashtable env = new Hashtable();
 env.put(Context.INITIAL_CONTEXT_FACTORY,
  "com.sun.jndi.ldap.LdapCtxFactory");
 env.put(Context.PROVIDER_URL, "ldap://localhost:10389/o=csRepository");

   // 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=Contractors", 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){
 e.printStackTrace();
}

Output:
ou=Contractors: javax.naming.directory.DirContext
ou=employees: javax.naming.directory.DirContext

Continue reading: LDAP Operations

No comments:

Post a Comment