Sunday, February 26, 2012

JNDI & Directory Services

JNDI is a Java standard API for accessing Directory Services.

So, what are directory services?

Directory services are essentially "lookups".

Suppose you wanted John's phone number, you can lookup the phone directory.
If you wanted to order a pizza, you lookup the yellow pages.

These are traditional examples of directory services.

Let us look at some more abstract directory services.

Suppose your server administrator kept a secured file which contained a list of users allowed to use your application.

Your application would lookup this file whenever someone logged in and tried to use your application.

This file is a directory service.

Suppose your administrator maintained a database server, where he kept all the users and their privileges.

Now you would lookup this database server to verify user privileges. This server is now providing a directory service.

What make a directory service a useful programming model?

A directory service usually contains information that usually does not change often. Like people's phone numbers, pizza shops, user privileges.

They are usually kept up-to-date by an administrator or authority.

Users of this information do not usually have any role to play in its creation or maintenance.

Why do we need JNDI?
Because directory services are not maintained by the user, the user has no control over their implementation. Those who provide the directory service, called 'Service Providers', can change implementation as necessary. They might move their implementation from file based to database to improve security. They might change their database to improve efficiency.

None of this should affect the user.

JNDI provides a standard way to lookup the directory service. It separates the implementation of the service from the usage.

It does so by the use of a JNDI "Context".

Every service provider is expected to provide a library which provides access to their directory service. They can create one of their own, or use any one of the standard libraries.

Users are then able to load a "Context" object for this directory service.

Once they have this object, they can lookup anything they want from that directory service.

JNDI is a named directory service interface.

Users can do a lookup by providing a string (name), just like they would use a key to a hashtable.

The directory service is expected to return the object corresponding to this name.

No comments: