Java Naming and Directory Services
A Naming Service provides a mechanism for giving names to objects so you can retrieve and use those objects without knowing the location of the object. Objects can be located on any machine accessible from your network, not necessarily the local workstation.
A real-world example is a phone directory. It stores telephone numbers against names and addresses. To find people’s phone numbers is simply a matter of using their name (and possibly address) to identify an entry in the phone book and obtaining the stored phone number. There are a few complications, such as finding the right phone book to look in, but it is essentially a simple process.
Incidentally, naming services have a similar problem to that of finding the right phone book. This is known as obtaining a context. A name can only be found if you examine the right context (phone book).
A Directory Service also associates names with objects but provides additional information by associating attributes with the objects.
The yellow pages phone directory is a simple form of a directory service. Here, businesses often include advertisements with additional information such as a list of products sold, professional qualifications, affiliated organizations, and even maps to their premises. These attributes add value to the name entry. A directory service will normally provide the ability to find entries that have particular attributes or values for attributes.
Yellow page style phone books also store names under categories—for example, hairdressers or lawyers. Categorizing entries can simplify searching for a particular type of entry. These categorized entries are a form of sub-context within the directory context of the local phone book.
Why Use Naming And Directory Services?
Naming Services provide an vital mechanism for de-coupling the provider of a service from the consumer of the service. Naming services allow a supplier of a service to register their service against a name. Users, or clients, of the service need only know the name of the service to use it.
Think of the phone book once more, and how difficult it would be to find someone’s phone number without it. Obtaining your friend’s phone number would mean going to their home and asking, or waiting until you meet up with them again—which may be difficult to organize because you can’t phone them to arrange the meeting.
It is very difficult to imagine a world without naming services.
What is JNDI and why we NEEDED it?
When you build an application, chances are you have to depend on some other resources built by somebody else. There is a directory or naming service that maps or bind name to such resources so that you can use the name and get the resource into your application. Such resources could be an object or simply just location to object. Such services are important because it provides way to expose the those resources to be used and reused. For instance:
DNS (Domain Name System):
It maps people-friendly names (such as www.google.com) into computer-friendly IP addresses in dotted-quad notation (192.168.0.1).
LDAP (Lightweight Directory Access Protocol)
It provides access to hierarchical structural information about users, networks or systems over IP network through url and other attributes names.(e.g cn=Martin Bond, ou=Authors, o=SAMS, c=us).
NIS (Network Information System)
It provides access to file and application resources through ID and password.
So, the common thing is they all are naming systems that binds the name to the object of our interest. Those objects or resources may or may not be on the same host and just be reference to object. No mater where they are naming service provide the lookup service to find such object by name.
The difference is the naming convention each of those naming services has undertaken. For instance:
- DNS uses the dot notation to construct the full domain name. (www.google.com)
- LDAP uses comma to separate the components while uses key/value pair to distinguish each components. (‘cn=Rupesh Bho, o=ComPlus, c=US’) .
With such differences, it would be nightmare for the application developers if they have to spend writing code to bring various naming servers every time they work on the application. This is where JNDI comes into play.
JNDI is a API which provides the uniform standard to access any naming or directory services into your application. Remember JNDI is just interface, not implementation.
JNDI architecture consists of API and Service Provider Interface(SPI). Java application use the JNDI API to access a variety of naming and directory services. The SPI enables a variety of naming and directory services to be plugged in transparently, thereby allowing the Java application using JNDI API to access their services.
JNDI is included in Java SE. To use JNDI, you must have the JNDI classes and one or more service providers. Some service providers like LDAP, RMI and DNS are included with the JDK, other you can find from other vendors.
Context and InitialContext:
The Context interface plays a central role in JNDI. A context represents a set of bindings within a naming service that all share the same naming convention. A Context object provides the methods for binding names to objects and unbinding names from objects, for renaming objects, and for listing the bindings.
JNDI performs all naming operations relative to a context. To assist in finding a place to start, the JNDI specification defines an InitialContext class. This class is instantiated with properties that define the type of naming service in use and, for naming services that provide security, the ID and password to use when connecting.
Let’s take a look at Context’s methods:
- void bind(String stringName, Object object): Binds a name to an object. The name must not be bound to another object. All intermediate contexts must already exist.
- void rebind(String stringName, Object object): Binds a name to an object. All intermediate contexts must already exist.
- Object lookup(String stringName): Returns the specified object.
- void unbind(String stringName): Unbinds the specified object.