JNDI is a Java API that has been available since J2SE 1.2, and is also a part of J2EE 1.3.1. If you are using older versions of the SDKs, you should be aware that JNDI is also included in J2EE 1.2 and is available as a standard Java extension for JDK 1.2 and earlier releases.

The JNDI API defines an interface that Java programs can use to access a variety of naming and directory services in a uniform way. JNDI was designed specifically for the Java platform, and uses Java’s object model.Therefore, you can use JNDI to store and retrieve Java objects of any type. It is perhaps helpful to also state what JNDI is not: It is not a naming and directory service implementation, only an API.Thus, to use JNDI, you must also have available an implementation of a naming and directory service. Without JNDI, it is necessary to learn the specific APIs that are implemented by the naming and directory service that you are using.This makes life a lot more difficult for application developers because they need to know all the APIs for the different naming and directory services used in their enterprise, thus leading to harder-to-maintain code. Figure shows the architecture of a client and multiple services that each provides its own API.

The architecture of a system that does not use JNDI.

The architecture of a system that does not use JNDI

In fact, JNDI consists of both an Application Programmer’s Interface (API) and a Service Provider’s Interface (SPI). Figure shows the architecture of how your application, the JNDI API, SPI, and the naming service implementations fit together. Because the JNDI API is defined in a way that is independent of any individual directory service implementation, it is possible to use additional naming services as long as they implement the SPI for JNDI. A service provider is basically a driver that your application can use to communicate with a directory service. The JNDI architecture’s layered design was constructed to help insulate client code from naming service provider code. The JNDI classes and interfaces are divided into five main packages: javax.naming, javax.naming.directory,javax.naming.event, javax.naming.ldap, and javax.naming.spi.These packages are covered in the next five subsections of this chapter.

The JNDI architecture.

The JNDI architecture

The javax.naming Package

The javax.naming package contains the classes and interfaces that your application can use to access naming services.The Context and Name interfaces are part of this package, as well as the Reference and Binding classes.

The javax.naming.Binding Class

A binding is a set of information that contains an object’s name, the name of the class used to instantiate the object as well as the actual object itself.

The javax.naming.Context Interface

Within a naming service, a set of bindings is referred to as a context.The javax. naming.Context interface is the principal interface in JNDI because it defines methods that enable you to

  • Bind objects to, and unbind objects from, names.
  • Rename objects.
  • Retrieve objects with the lookup method.

You can also use the list and listBindings methods to retrieve an enumeration of name-to-object bindings.The listBindings method returns an enumeration of javax.naming.NamingEnumeration, where each element in the enumeration is of type javax.naming.Binding, as described later. The list method is more lightweight, in that it also returns an enumeration, but this time each element is of type javax.naming.NameClassPair. An instance of this class contains an object’s name and the name of the class that was used to instantiate the object.The list method is useful when an application needs information about the object, but not the object itself. For example, you might be writing some kind of browser that displays a list of objects in the naming service.

The javax.naming.Name Interface

This interface symbolizes a generic name of an object that is bound into a naming service. There can be many different implementations of a name, such as URLs or host names, but the Name interface provides methods for accessing the name that are independent of the underlying naming service. A name typically consists of a string or a group of name components. In general, applications that need to manipulate individual components in a name would use the Name interface to build a name or compare with another name, for instance. Simple applications generally use a java.lang.String to perform a lookup operation.

The javax.naming.Reference Class

Depending on the naming and directory service that you use, you might or might not be able to store a serialized Java object directly within the service. Even if your service is capable of storing the object, you might decide not to do so. For example, the object might be very large, or there could be applications written in languages other than Java that access the object in which case a serialized Java object would be of no use. JNDI defines the javax.naming.Reference class that represents a reference to an object. A reference contains information that enables you to access an object. JNDI maintains the illusion that what the client looks up in the naming service (a reference) is in fact an object.

The javax.naming.directory Package

The javax.naming.directory package contains classes and interfaces that you can use to access directory services. For example, you can retrieve the attributes that are associated with an object.You can also perform searches for objects whose attributes match certain search criteria that you specify. The two most important interfaces in the javax.naming.directory package are Attribute and DirContext.

The javax.naming.directory.Attribute Interface

This interface represents an attribute of a named object in the directory service.The actual forms that an attribute’s name and value can take are dictated by the directory service. Some directory services allow you to specify a schema that sets the forms for the name and value. An attribute has zero or many values associated with it. It is perfectly legal for a particular attribute value to be null.You can use the get and getAll methods to obtain the attribute values; the set method allows you to set a value at a specified index; and the remove method deletes an attribute at a specified index.

The javax.naming.directory.DirContext Interface

This interface represents a directory context, and defines methods that enable you to write software that examines (getAttributes) and updates(modifyAttributes) the attributes of a named object in the directory service. There is also a set of overloaded search methods that enable you to search the directory service based on the name of a context (or object) and attribute values. The DirContext interface extends the javax.naming.Context interface, and thus you can also use it as a naming context.This means that any object in the directory service can act as a naming context. For example, there could be an object in the directory service that represents an employee in your company.The employee object can have attributes associated with it as well as act as a naming context so that you could locate objects that belong to the employee such as their PCs, mobile telephones, and PDAs.

The javax.naming.event Package

The javax.naming.event package defines classes and interfaces that support event notification mechanisms in naming and directory services. If you have used the Java Event Model (as used in GUIs and by JavaBeans) that has been available since JDK 1.1, the mechanism described here will sound familiar.The basic idea is that an event source generates events that are sent to registered event listeners. The event mechanism is of an asynchronous nature, and means that applications can register an interest in changes to the directory service without having to poll the directory service for changes.

The NamingEvent class and the NamingListener interface described later are part of the javax.naming.event package.

The javax.naming.event.NamingEvent Class

The javax.naming.event.NamingEvent class represents an event object that is generated when something changes in a naming or directory service.The object contains information about the event that occurred, such as the source of the event, as well as a type that indicates the form that the event took. Events are classified into those that affect the namespace, and those that do not. An example of the former category would be when an object is added, whereas an example of the latter category is when an object is changed. Other information about the change, such as information before and after the change, is also stored in the NamingEvent object. An event source creates an instance of the NamingEvent class, and passes it to the registered listeners who can then use the instance methods of the object to extract information about the event.

The javax.naming.event.NamingListener Interface

You can implement the javax.naming.event.NamingListener interface to listen for NamingEvents. However, there are several subinterfaces of NamingListener that correspond to the different categories of event that can occur. For example, there is a NamespaceChangeListener for events that change the namespace, such as the addition, removal, or renaming of an object.There is also the ObjectChangeListener for notification of modifications to objects in the namespace, which covers when an object’s binding is replaced with another and when an object’s attributes are replaced or removed. Typically, you implement a subinterface rather than directly implement NamingListener.

The javax.naming.ldap Package

You can find classes and interfaces in the javax.naming.ldap package that enable you to access features specific to LDAP v3 that are not already covered by the classes and interfaces in the javax.naming.directory package. Most JNDI applications will not need to use the javax.naming.ldap package.The only time that you will is if you are writing software that needs access to LDAP functions such as Controls, Extended Operations, and Unsolicited Notifications.

The javax.naming.spi Package

The classes and interfaces defined in the javax.naming.spi package are primarily for use by developers of naming/directory service providers.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JSP Topics