Home > how do I?, Java, JNDI, LDAP > Use JNDI to access an LDAP.

Use JNDI to access an LDAP.

This is the second part in my writing documentation/tutorial on how to write Java code to connect to an LDAP server.  The first part focused on creating a connection with a local file system.  This post assumes that you have been able to work through the first part, as you will need the skills and most of the code in that first part. 

Previously we have used JNDI to access a local file system.  Now we want to access an LDAP.  Explaining what an LDAP is, and why we would want to access one, is beyond the scope of this post.  For this example, we are going to write all of our code within the main() method of the class.  (Remember, this is for instructive purposes only!) 

  1. Here are the import statements and basic structure of the program:

    import java.util.Hashtable;

    import javax.naming.Context;
    import javax.naming.NamingEnumeration;
    import javax.naming.NamingException;
    import javax.naming.directory.Attributes;
    import javax.naming.directory.BasicAttribute;
    import javax.naming.directory.BasicAttributes;
    import javax.naming.directory.DirContext;
    import javax.naming.directory.InitialDirContext;
    import javax.naming.directory.SearchResult;

    import org.apache.log4j.Logger;
    /**
     * @author Kelly.Kinney
     *
     */
    public class LDAPCtx
    {
     private static Logger log = Logger.getLogger(LDAPCtx.class);
     /**
      * @param args
      */
     public static void main(String[] args)
     {
      
     }

    }

  2. Now we add the code to create the Hashtable which will contain environment variables of the Context. This is the same as in the first part.

    // Set up the environment for creating the initial context
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.ldap.LdapCtxFactory");

  3. Now the code starts to diverge from that used to access a local file system. Instead of the local filesystem, we need to include Context information for the LDAP. Note the format of the LDAP connection string. It is the connection string, followed by a colon, the LDAP host port number (usually 389), a slash, and a reference to the top level LDAP branch to be searched. With Sun One Directory Servers, this is represented by the “o=”. However, with Active Directory it is usually a string of “DC=” value pairs to indicate the top level of the directory.

    env.put(Context.PROVIDER_URL, "ldap://{LDAP_Ctx_String}:389/o={Top_lvl_directory}");

  4. The next block of code we need to enclose in a try/catch block. Here is how to construct this block. The area indicated by the ellipses will be where the code that follows gets inserted.

    try
    {
    [...]
    }
    catch (NamingException e)
    {
    e.printStackTrace();
    }

  5. Inside the try/catch block, we put the rest of the code… (to be explained more tomorrow)

    DirContext ctx = new InitialDirContext(env);
    log.debug("Connection toString: " + ctx.toString());

    Attributes matchAttrs = new BasicAttributes(true);
    matchAttrs.put(new BasicAttribute("{attribute}","{value}"));

    NamingEnumeration answer = ctx.search("ou=People", matchAttrs);

    while(answer.hasMore())
    {
    SearchResult sr = (SearchResult)answer.next();
    log.debug(">>>" + sr.getName());
    }

Advertisements
Categories: how do I?, Java, JNDI, LDAP
  1. No comments yet.
  1. June 8, 2007 at 2:23 pm

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: