Introduction To JNDI
Return to the Java Programming Corner.
Naming and Directory Concepts
This brief introduction to JNDI will explore naming and directory concepts in detail and discusses how Java programs can use name and directory services.
The concept of a naming service is the means by which names are associated with objects and objects are found based on their names. The chief function of a naming service is to map people-friendly names to objects, such as addresses, identifiers, or objects typically required and used by computer programs. For example:
- Domain Name Systems (DNS) maps machine names (such as java.sun.com) to IP addresses (such as 220.127.116.11)
- File systems map a filename (for example /u10/app/oracle/9.2.0/dbs/initCUSTDB.ora) to a file handle that a program can use to access the contents of the file.
A variety of naming are extended with a directory services. A directory service associates names with objects and also allows such objects to have attributes. It is possible to not only look up an object by its name but also get the object's attributes or even search for the object based on its attributes. Take for example a telephone company's directory service. It is used to map a subscriber's name to his address and telephone number. A computer's directory service is very much like a telephone company's directory service in that both can be used to store information such as a telephone number or address. The computer's directory service is much more powerful because it is available online and can be used to store a variety of information that can be utilized by users, programs, and even the computer itself and other computers.
The JNDI architecture consists of an API and a service provider interface (SPI). All Java applications that utilize naming and directory services use the JNDI API to access a variety of naming and directory services. The SPI on the other hand enables a variety of naming and directory services to be plugged in transparently, thereby allowing the Java application using the JNDI API to access their services.
The JNDI API is included in the Java 2 SDK, v1.3 and higher. For those using JDK v1.1 and Java 2 SDK, v1.2, the JNDI API is available as a Java Standard Extension.
In order to take advantage of the JNDI, you not only need the JNDI classes but also one or more service providers. The Java 2 SDK, v1.3 includes three service providers for the following naming/directory services:
- Lightweight Directory Access Protocol (LDAP)
- Common Object Request Broker Architecture (CORBA) Common Object Services (COS)
- Java Remote Method Invocation (RMI) Registry
Many other service providers are available for download from the JNDI web site (http://java.sun.com/products/jndi) or obtained from other vendors. Remember that if you are using the Java Standard Extensions with JDK v1.1 and Java 2 SDK, v1.2, you must first download the JNDI classes and one or more service providers.
The one service provider I like to use for testing and will never figure out why Sun does not yet include it in the standard SDK is file system service provider (
fscontext.jar). It can be downloaded from:
The Initial Context
In the JNDI, all naming and directory operations are performed relative to a context. There are no absolute roots and this is where why the JNDI defines an initial context,
InitialContext, which provides a starting point for naming and directory operations. Once you have an initial context, you can use it to look up other contexts and objects.