Java Reference
In-Depth Information
These are the basics of searching an LDAP server. Now we can move on and learn how to use
JNDI to manipulate the information that is stored there.
Adding an Object to an LDAP Server
Adding an object to an LDAP server is not difficult, as long as you have a little knowledge of
the structure of the information in the server.
The first piece of knowledge necessary is the object type. There is a special attribute for every
object in the LDAP server called objectClass . As we discussed earlier, the directory server
comes with a predefined schema. The schema includes definitions for many objects, including
the possible attributes available for each object. The existing object definitions can be reused
by other objects, similar to inheritance in Java. The objectClass attribute holds multiple val-
ues, specifying which object definitions are used to define the object's schema. This attribute is
required when adding an object so that the LDAP server knows what type of object is being
added.
You also need to know where to place the object into the directory tree. Essentially you are
going to bind a group of attributes to a new DN in the structure. The simple method used to
bind a new object in an LDAP server looks like this:
public void add(String dn, HashMap attributes, String[] objectClass)
throws NamingException {
// parse the attributes that were passed in
Attributes atts = parseAttributes(attributes);
// add objectClass attribute to the list of attributes
Attribute oc = new BasicAttribute(“objectClass”);
for (int i = 0; i < objectClass.length; i++) {
oc.add(objectClass[i]);
}
atts.put(oc);
// perform the addition to the LDAP server
DirContext ctx = getInitialContext(getEnvironment());
ctx.bind(dn, null, atts);
}
This method is very simple. As you can see, you accept the object type information as an array
of strings, and build an Attribute object out of them. All the attribute names and values to
place in the new object are passed to the method using a standard Java collection so that the
client does not need to learn to handle the Attribute and Attributes objects. This collection
is parsed into the required form in another method in the LDAPManager . That method is used in
several places and appears like this:
Search WWH ::




Custom Search