Previous Table of Contents Next


CHAPTER 4
What Is JNDI?

Internet and intranet applications rely heavily on naming and directory services for accessing network resources. These services provide networkwide sharing of information related to users, machines, other network services, and remote applications. Java Naming and Directory Interface (JNDI) is an API that describes a standard Java library for accessing naming and directory services such as Domain Naming Service (DNS), Lightweight Directory Access Protocol (LDAP), Remote Method Invocation (RMI), or even the file system on your computer. JNDI is powerful because it allows various services to be linked through a single API, enabling Java applications that use JNDI to navigate seamlessly across file systems, databases, servers, and so on. For example, you could look up a specific RMI registry by requesting the information from an LDAP server, then use the same JNDI API to request a specific object from the registry and save a reference to the object in your file system using the JNDI file system service provider API.

This chapter describes the terminology, goals, and architecture of JNDI.

Terminology

In order to understand when and how to use JNDI, you must be familiar with the two types of services it supports: directory and naming services. Directory services typically provide access to a hierarchical tree of objects, such as the directories in the file system on your computer. A naming service allows access to objects by name. For example, a corporate LDAP server could allow you to find an e-mail address by entering the person’s last name and department number. DNS, on the other hand, allows programs to look up a computer’s IP address by name. JNDI for directory and naming services enables the Java developer to build applications that leverage the features of the Java object model and, at the same time, integrate well with most commercial enterprise applications. In order to use JNDI, you should also be familiar with at least one service provider with which JNDI works, such as LDAP. Both the file system service provider and the LDAP service provider are used in the examples in Chapter 5, “Using JNDI.” Table 4.1, which appears later in this chapter, lists the services that are currently available.

Naming Service

A naming service provides a method for mapping unique identifiers, or names with a specific value, a range of values, or an object that is referenced by the name service. For example, Common Object Request Broker Architecture (CORBA) allows you to associate a name with an object so that you can remotely access the object by requesting it by name. When you save a file on your computer, you name the file so that you can refer to it and access it later.

The following terms are important to understand when using a naming service: binding, namespace, compound name, composite name, and service providers. These terms are discussed in more detail in the following sections.

Binding

The mapping between a name and a unique object is referred to as a name binding. One obvious computer naming service that performs binding is the file system on your computer. Each file and directory has a name that identifies it. When you create a file, you give it a name. This is the name binding for your data; it can be retrieved by that name later. When you rename a file, you are actually rebinding the file to a new name.

Namespace

A namespace is a set of names in which all names are unique. If you think of a directory on your computer as a namespace, you realize each file within the directory must have a unique name. However, you can have two files with the same name if each is in a different directory, or namespace. With multiple namespaces, you can reuse names without conflicts.

Compound Name

A compound name is a sequence of names that conform to the naming convention of a namespace. In a file system, you may have a compound name such as /usr/tmp/ myfile.txt./, usr, tmp, and mysfile.txt—these are all names of objects in the file system. The symbol, or name /, maps to the root directory; the name usr maps to a subdirectory under /; and the name tmp maps to the subdirectory called tmp under usr. The name myfile.txt maps to a file. Together they create a compound name that is, for example, different from /usr/local/myfile.txt.

Composite Name

Composite names span multiple namespaces. Composite names are common on the World Wide Web. A URL usually consists of at least three parts: protocol name, server name, and resource name. For example, in the URL http://www.wiley.com/index.html, the protocol name is http; the name of the server is //www.wiley.com; and the name of the resource is index.html. Ultimately, it is hoped that JNDI will support highly composite names. For example, you might use the first part of a name as a URL to find an LDAP server, use the next part of the name to find a resource on the LDAP server, and use the results of the LDAP query and the final part of the name to find an object using RMI.

Service Providers

Although JNDI provides a common, unified API for name services, each name service requires a service provider that maps JNDI into the specific operations supported by the name service. Name services differ in description of names, organization of the name space, schema description (if any), and the list of operations supported by inserting and searching for objects and values in the service. This mapping of an API to a service provider’s code is similar to the mapping between the JDBC interfaces and driver implementations discussed in Chapter 2, “What Is JDBC?” and Chapter 3, “Basic JDBC Programming.”

DNS and RMI are good examples of naming services because, although they each organize information differently, they still use unique names in a namespace to identify their objects. A list of currently available service providers appears in Table 4.1.

DNS

The Domain Name System (DNS) is an Internet naming service that maps the Internet address of computer systems, such as 192.42.172.21, with simple names like pri.com. These names allow users direct access to remote computer systems through recognizable names.

RMI

Remote Method Invocation (RMI) is an example of a distributed object management system that provides a naming service for mapping names to objects residing on remote computers. The client could request the service of an object in another address space simply by referring to it by name. For example, your application could request a printing object from an RMI repository to print a specific type of report. The code might look like this:

Printer p = (Printer)rmiService.lookup(“Report_Printer”);
p.print(myReportObject);


Previous Table of Contents Next