wood burning stoves 2.0*
The moose likes Java in General and the fly likes Simple Add using JNDI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Simple Add using JNDI" Watch "Simple Add using JNDI" New topic
Author

Simple Add using JNDI

Natesan Prabhakaran
Ranch Hand

Joined: Jul 11, 2006
Posts: 47
Hi All,
I installed OpenLDAP and i am trying to access slapd server using JNDI. Now, I am trying to add using JNDI. following is an example program taken from this mailing list.which I have modified according to my values.

Thanks
Prabhakaran.N

Add.java
--------

package test;

import java.util.Hashtable;
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;

class Add{

final static String rootContext = "dc=mycompany,dc=com";
final static String rootdn = "dc=Mobius,dc=mycompany,dc=com";

public static void main(String[] args) {

Hashtable env = new Hashtable(5, 0.75f);

env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");

// host and port to use for directory service
env.put(Context.PROVIDER_URL, "ldap://localhost:389");

// authentication information
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "secret");
env.put(Context.SECURITY_CREDENTIALS, "secret");

// entry's DN
String entryDN = "cn=people, " + rootContext ;

// entry's attributes

Attribute cn = new BasicAttribute("cn", "NewUser");
Attribute sn = new BasicAttribute("sn", "Smith"); // mandatory attribute
Attribute mail = new BasicAttribute("mail", "newuser@foo.com");
Attribute phone = new BasicAttribute("telephoneNumber", "+1 222 333 4444");

Attribute oc = new BasicAttribute("objectClass");
oc.add("top");
oc.add("person");
oc.add("organizationalPerson");
oc.add("inetOrgPerson");


DirContext ctx = null;

try {
// get a handle to an Initial DirContext
ctx = new InitialDirContext(env);

// build the entry
Attributes entry = new BasicAttributes();
entry.put(cn);
entry.put(sn);
entry.put(mail);
entry.put(phone);
entry.put(oc);

// Add the entry
ctx.createSubcontext(entryDN, entry);
System.out.println( "AddUser: added entry " + entryDN + ".");

} catch (NamingException e) {
System.err.println("AddUser: error adding entry." + e);
}
}
}


Regards,
Prabhakaran.N
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Simple Add using JNDI