ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Developing A White Pages Service with LDAP and JNDI
Pages: 1, 2, 3, 4, 5, 6, 7

Developing a White Pages Service

A white pages service for locating a person in an LDAP server. As mentioned previously, I use the LDAP server from OpenLDAP. In order to keep the project simple, I use the person object defined in the core.schema file.

For convenience, the person object in the core.schema file is re-presented here.

objectclass ( NAME 'person' SUP top STRUCTURAL
  MUST ( sn $ cn )
  MAY ( userPassword $ telephoneNumber $ seeAlso $ description )

The person object has two mandatory attributes: sn and cn, and four optional attributes:

  • userPassword
  • telephoneNumber
  • seeAlso
  • description

Adding Some Entries

To test the code in this project, you need to populate the directory:

ldapadd -x -D "cn=Manager ,dc=sendal,dc=jepit,dc=edu,dc=au" -w 
secret -f example.ldif

This reads the example.ldif file and insert its content as entries to the server. The example.ldif file contains the following.

dn: cn=Bulbul, dc=sedal,dc=usyd,dc=edu,dc=au
objectclass: person
cn: Bulbul Kurniawan
sn: Kurniawan
userPassword: secret
telephoneNumber: +61 98371313

dn: cn=boni, dc=sedal,dc=usyd,dc=edu,dc=au
objectclass: person
cn: Boni Milliken
sn: Milliken
userPassword: dog
telephoneNumber: +61 9555 1212

dn: cn=boy, dc=sedal,dc=usyd,dc=edu,dc=au
objectclass: person
cn: Boy Milliken
sn: Milliken
userPassword: taboo
telephoneNumber: +61 98989898

Make sure that you have installed the correct service provider and your CLASSPATH variable contains the path to the JNDI packages.

The Code

The code for the white pages service is given in Listing 1. The Java code allows you to access the LDAP server and search a person or persons by passing a surname. The code starts by preparing a environment Hashtable object and setting the necessary properties for the environment.

Hashtable env = new Hashtable();


And then, as explained above, you need a DirContext object as the initial context, which is done by calling the InitialDirContext constructor, passing the environment Hashtable.

DirContext ctx = new InitialDirContext(env);

Once you have a DirContext object, you can use it to access the LDAP service. To start searching, use the search method by passing a SearchControls object.

SearchControls constraints = new SearchControls();
NamingEnumeration persons =
        "(objectclass=person)", constraints);

Then, display the search result, i.e., the attributes of all the person objects that match the search criteria.

For each person object found, you use the getAttributes method to retrieve the object's attributes. This method returns the Attributes object. You can then use the get method of the Attributes object to obtain the value of an attribute by passing the attribute name.

attributes.get( attributeName );

The part of the code that displays the attribute names of the person objects found is given below.

System.out.println("Distinguished Name \t| " +
  "Common Name \t| Surname \t| Phone");

while (persons != null && persons.hasMore()) {
  SearchResult sr = (SearchResult) persons.next();  
  System.out.print( sr.getName() + "\t| ");  // distinguised name
  Attributes attrs = sr.getAttributes();
  attrs.put(new BasicAttribute("sn", searchedSurname));
  // attrs.put(new BasicAttribute("cn", "boy"));
  System.out.print(attrs.get("cn") + "\t| "); // common name
  System.out.print(attrs.get("sn") + "\t| "); // surname
  System.out.println(attrs.get("telephoneNumber")); // phone

} // end of while

If you run the code in Listing 1, you can see the result that looks something like the following.

Distinguished Name  |  Common Name  |  Surname  |  Phone

cn=Boni Milliken  |cn: boy  |sn: Milliken  | +61 9555 1212
cn=Boy Milliken  |cn: boy  |sn: Milliken  | +61 98989898


Naming and directory services are important, providing a way to find objects based on their name or other attributes. A directory service is an extension of a naming service in which object has various attributes. So you can you look up an object by its name, and you can get the object's attributes or search for the object based on its attributes. Using a directory service such as an LDAP server, you can create many applications, including the white pages service described above.

Budi Kurniawan is a senior J2EE architect and author.

Return to ONJava.com.