Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Using LDAP with JNDI

Of the directory services supported by JDK 1.3, LDAP is by far the most flexible. You can store a wide variety of items in an LDAP directory and you can get LDAP servers for a wide variety of operating systems. A good place to get a free LDAP server for Linux and Unix is http://www.openldap.org. They are also working on a version for Windows NT.

LDAP stores data in a hierarchical (tree) structure. You refer to an entry in the tree by listing the names of the nodes in the tree, starting at the one you want, working backward to the top of the tree. LDAP paths look confusing at first, but after you understand the notation, it's not so bad. Figure 18.1 shows an example LDAP tree.

Figure 18.1. LDAP stores its entries in a tree structure.

Each node in the tree has a unique name of the form nodetype=value. That is, the name includes the type of the node, at least to some extent. For example, the top part of the tree in Figure 18.1 has nodes that represent the LDAP server's domain. These topmost nodes are domain components. For a domain of http://wutka.com, you have two domain components: wutka and com. Node type for a domain component is dc, so the topmost nodes are named dc=wutka and dc=com. Underneath the wutka domain component is an organization called Wutka Consulting. An organization has a node type of o, so the Wutka Consulting node has a name of o=Wutka Consulting.

Now, if you're using JNDI to access the wutkaconsulting node, you must list the node names starting from the one you want and working backward to the top. In other words, the name you want is o=Wutka Consulting,dc=wutka,dc=com.

Listing 18.1 shows a program that reads the Wutka Consulting object and prints out its attributes.

Listing 18.1 Source Code for ShowWC.java

package usingj2ee.naming;

import javax.naming.*;
import javax.naming.directory.*;

public class ShowWC
{
    public static void main(String[] args)
    {
        try
        {
// Get the initial context
            InitialDirContext ctx = new InitialDirContext();

// Locate the Wutka Consulting object on the server running
// at ldap.wutka.com
            Attributes attrs = ctx.getAttributes(
                "ldap://ldap.wutka.com/o=Wutka Consulting, dc=wutka, 
dc=com");

// Get the attributes for the object
            NamingEnumeration e = attrs.getAll();

            while (e.hasMoreElements())
            {
// Get the next attribute
                Attribute attr = (Attribute) e.nextElement();

// Print out the attribute's value(s)
                System.out.print(attr.getID()+" = ");
                for (int i=0; i < attr.size(); i++)
                {
                    if (i > 0) System.out.print(", ");
                    System.out.print(attr.get(i));
                }
                System.out.println();
            }
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }
}
					

Figure 18.2 shows the output from the ShowWC program.

Figure 18.2. It's easy to print the attributes in an LDAP object.

Note

Due to possible network changes, you may not be able to access http://ldap.wutka.com in the future. You might need to set up your own LDAP server to run the example.


  • + Share This
  • 🔖 Save To Your Account