6.3. Introducing the Context
A naming service associates names with objects. An association between a name and an object is called a binding, and a set of such bindings is called a context. A name in a context can be bound to another context that uses the same naming conventions; the bound context is called a subcontext. For example, in a filesystem, a directory (such as /temp) is a context that contains bindings between filenames and objects that the system can use to manipulate the files (often called file handles). If a directory contains a binding for another directory (e.g., /temp/javax), the subdirectory is a subcontext.
JNDI represents a context in a naming system using the javax.naming.Context interface. This is the key interface for interacting with naming services. A Context knows about its set of bindings in the naming system, but little else. While you might be tempted to think of a Context as an exotic java.io.File object, you should resist making that analogy, as it will just confuse you. Unlike a File object, which can tell you its absolute and relative names as well as return a reference to its parent, a Context object can tell you only about its bindings. A Context cannot go up a level, tell you its absolute pathname, or even tell you its own name. When you think of a Context, think of an object that encapsulates its children as data and has methods that perform operations on that data, not on the Context itself.
6.3.1. Using the InitialContext Class
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 InitialContextobject. 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. Here is how we created an InitialContext in the Lookup example:
Properties props = new Properties(); props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); props.put(Context.PROVIDER_URL, "file:///"); // Create the initial context from the properties we just created 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:
java.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory java.naming.provider.url=ldap://192.168.1.20/o=Planetary,c=US java.naming.security.authentication=simple java.naming.security.principal=cn=kris java.naming.security.credentials=secret
These properties create an InitialContext for an organization called "Planetary" in the global X.500 namespace.
6.3.2. Other Naming Systems
There are many companies that support JNDI, and therefore many naming system service providers. You can find a reasonably comprehensive list of public JNDI providers from the JNDI page on the Sun web site (currently at http://java.sun.com/products/jndi/serviceproviders.html ). You should contact the vendor of your enterprise naming system or directory for more details regarding its specialized providers. Table 6-1 lists the factory classes for some common JNDI providers.
Table 6-1. JNDI Factory Classes
Copyright © 2001 O'Reilly & Associates. All rights reserved.