Previous | Next | Trail Map | Getting Started | Examples

Directory Example

This example shows you how to write a program that retrieves attributes from a directory object. This example uses an LDAP service provider to access an LDAP service.

Import the JNDI directory packages

Using your favorite text editor, create a file named Getattr.java. You can either import the entire package or individual classes and interfaces. The following code imports each class that is used from the javax.naming(in the API reference documentation) and javax.naming.directory(in the API reference documentation) packages:
import javax.naming.Context;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.DirContext;
import javax.naming.directory.Attributes;
import javax.naming.NamingException;

Create an Initial Directory Context

In the main() method of the program, first create an initial directory context. This is similar to creating an initial context in the naming example, except that you use the constructor for InitialDirContext(in the API reference documentation):
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, 
    "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

DirContext ctx = new InitialDirContext(env);

Similar to the naming example, we indicate that we're using the LDAP service provider by setting the Hashtable parameter to the InitialContext constructor(in the API reference documentation) appropriately. Details about how to set up the parameters for this constructor is explained in more details in The Basics (in the Basics trail) trail. For now, the only thing to understand is that the program by default identifies an LDAP server on the local machine. If your LDAP server is located on another machine or is using another port, then you need to edit the LDAP URL ("ldap://localhost:389/o=JNDITutorial") accordingly. Instructions for setting up a sample LDAP server for this tutorial are given in the Preparations (in the Basics trail) lesson.

Get a Directory Object's Attributes

Then, use getAttributes()(in the API reference documentation) to get an object's attributes. This code retrieves all the attributes associated with the object bound to the name "cn=Ted Geisel, ou=People":
Attributes attrs = ctx.getAttributes("cn = Ted Geisel, ou=People");

Extract the Attribute You Want

From a set of attributes, Attributes(in the API reference documentation), you can ask for a particular attribute using Attributes.get()(in the API reference documentation), and then from that attribute, get its value. The following line first gets the surname attribute "sn", and then invokes Attribute.get()(in the API reference documentation) on it to get its value:
attrs.get("sn").get();

Catch NamingException

The operations shown so far can throw a NamingException(in the API reference documentation). So you need to wrap these calls inside a try/catch clause. Here's the code fragment repeated with the try/catch clause:
try {

    // Create the initial directory context
    DirContext ctx = new InitialDirContext(env);
	    
    // Ask for all attributes of object 
    Attributes attrs = ctx.getAttributes("cn=Ted Geisel, ou=People");

    // Find the surname attribute ("sn") and print it out
    System.out.println("sn is : " + attrs.get("sn").get());

} catch (NamingException e) {
    System.err.println("Problem getting attribute:" + e);
}

Compile the Program

Next, you need to compile the source file using the Java compiler. As is the case with the naming example, you need to have access to the JNDI classes.

If the compilation succeeds, the compiler creates a file named Getattr.class in the same directory (folder) as the Java source file (Getattr.java).

If the compilation fails, make sure you typed in and named the program exactly as shown above, using the capitalization shown. If you are still having problems, see Common Problems (in the Getting Started trail) for help.

Run the Program

As is the case with the naming example, you need to have access to both the JNDI classes and the directory containing the Getattrs.class. You also need to have access to the LDAP service provider classes (ldap.jar and providerutil.jar). If you are using the Java 2 SDK, v 1.3, these classes are already included.

Here's an example of a command line for running Getattr and the output it generated:

# java Getattr 
sn: Geisel
This command queries the LDAP server on machine localhost listening on port 389, serving the "o=JNDITutorial" namespace. It asks for the attributes of the entry cn=Ted Geisel, ou=People. Once it has the attributes, it extracts the surname attribute (sn). If you have any trouble running this example, see Common Problems (in the Getting Started trail).

Examples: End of Lesson

What's next? Now you can:


Previous | Next | Trail Map | Getting Started | Examples