Use JNDI to access local file system.

Okay, it has been driving me crazy trying to find information on how to write Java code to access an LDAP server.

I finally found a tutorial by Sun which seems to explain it in what I would describe as a very haphazard manner. In fact, I would call the tutorial absolute crap. However, it’s all I’ve got. I am going to try to elucidate on it here.

To start, we are going to write a small program that we can use to access a local file system, i.e., the one on your desktop. This will provide the building blocks we will need later to connect to an LDAP. Ready? Let’s dive in.

  1. First you need to download additional JNDI service provider interfaces, as they are not included as part of the standard JDK distro. (Four providers, including an LDAP interface are included in the regular JDK, however I downloaded the additional LDAP providers.) Go to Sun’s Java JNDI homepage. Click on the link to download the “JNDI/LDAP Booster Pack.” Click on the”Download JNDI 1.2.1 &More Button that says ‘Download‘” Accept the license agreement and then go ahead and download all the Service Provider Interfaces. You probably don’t need them all, but what the heck. A true geek wants all the stuf he can get. You never know when it will come in handy. You will for sure need to download the “File System Service Provider” and the “LDAP Service Provider.”
  2. Extract the zip files into your C:\dev directory (where you store everything related to your development). Now if you are not using an IDE such as Eclipse or NetBeans, then you will have to manually add the required jars to the classpath. If you are using an IDE, you will need to bring the required jars into the classpath through the IDE. In this case you want to add the jars from the file beginning “fscontext,” which are: fscontext.jar and providerutil.jar. Once you have made sure these jars are on the classpath, you can begin coding.
  3. Create a file called Lookup.java. This file is only going to have one method, which will be the main method. Now add import statements for the classes you are going to need. Here’s what it will look like (import statements will be explained, and you have to throw exceptions – or try/catch them)…

    import java.util.Hashtable;

    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;

    public class Lookup

    {
    public static void main (String[] args) throws NamingException
    {

    [...]

    }
    }

  4. The first real line of the program creates a Hashtable to hold environment variables. We then put the values of the system configuration as understood by the Context into the Hashtable.

    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.fscontext.RefFSContextFactory");

  5. Next we create a Context for the program. Essentially what this does is create the relationship between the user’s system and the program.

    Context ctx = new InitialContext(env);

  6. Then we use the Context we just created to get information about the file or directory we have passed into the program as an argument. This is one of the class methods of Context.

    Object obj = ctx.lookup(args[0]);

  7. Finally, we print out the results to the screen.

    System.out.println(args[0] + " is bound to: "+ obj);

  8. If you use a file as the parameter being passed in, you will receive information about the file like this:

    C:\\dev\\fscontext\\lib\\fscontext.jar is bound to: C:\dev\fscontext\lib\fscontext.jar

  9. If you use a directory as the parameter being passed in, you will receive information about the file like this:

    C:\\dev\\fscontext\\lib is bound to: com.sun.jndi.fscontext.RefFSContext@e5855a

  10. I’ll attach the Java file to this posting for your reference. Lookup.java.doc The entirety of the final program is this:

    import java.util.Hashtable;

    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;

    public class Lookup
    {

    public static void main (String[] args) throws NamingException
    {
    // Set up environment for creating an initial context
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.fscontext.RefFSContextFactory");

    Context ctx = new InitialContext(env);

    Object obj = ctx.lookup(args[0]);

    System.out.println(args[0] + " is bound to: "+ obj);

    }

    }