My JNDI Notes..



The architecture of JNDI is somewhat like the JDBC architecture, in that both provide a standard protocol−independent API built on top of protocol−specific driver or provider implementations. This layer insulates an application from the actual data source it is using, so, for example, it doesn't matter whether the application is accessing an NDS or LDAP directory service.


A Java application uses the JNDI API to access naming and directory services, primarily through the Context interface.

Context.INITIAL_CONTEXT_FACTORY specifies the factory class that creates an initial context for the service we want to use. The class com.sun.jndi.fscontext.RefFSContextFactory is a factory class from the file system service provider from Sun. Context.PROVIDER_URL tells the factory class the protocol, server name, and path to use in creating an initial context. We specify the URL file:/// to indicate the root of the local file system. This works on any Unix or Windows file system.

Once we have created the Properties object, we pass it to the javax.naming.InitialContext constructor, which returns the initial context object that is our entry point into this particular naming system. Next, we call the lookup() method on initialContext, specifying the name we want to look up. Depending on the value you use for the factory class, the object you look up could be an Enterprise JavaBeans component, a reference to a remote object, or something else.


The javax.naming.InitialContext class implements the Context interface and serves as our entry point to a naming system. To use JNDI to access objects in a naming system, you must first create an InitialContext object. The InitialContext constructor takes a set of properties, in the form of a java.util.Hashtable or one of its subclasses, such as a Properties object.
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
props.put(Context.PROVIDER_URL, "file:///");
Context initialContext = new InitialContext(props);


The most fundamental property key is "java.naming.factory.initial", which corresponds to the
Context.INITIAL_CONTEXT_FACTORY constant. The value for this property specifies the name of a factory class in a JNDI service provider. It is the job of this factory class to create an InitialContext that is appropriate for its service and hand the object back to us. We have to give the factory class all the information it needs to create an InitialContext in the form of other property values. For example, the factory class learns the protocol, server name, and path to use from the "java.naming.provider.url" property (Context.PROVIDER_URL).

The filesystem factory class (com.sun.jndi.fscontext.RefFSContxtFactory) doesn't require
much in the way of information. Other factory classes can be more demanding. For example, the factory class in Sun's LDAP service provider requires the URL of the LDAP server and directory entry you want to access, a username and password, and an authentication type. Here are some properties (shown in the file format used by the Properties class) you might use to create an InitialContext with the LDAP factory class:

There are many companies that support JNDI, and therefore many naming system service providers. Below are factory classes for some common JNDI providers.