Java continues the unification and standardization process to enable developers to use enterprise technologies with the JNDI API. The JNDI API provides a standard and consistent way for Java applications to access naming and directory services implemented by different vendors or service providers. Similar to the way JDBC API drivers are used to access different databases, the JNDI API can be used to access naming and directory services provided by different vendors. JNDI comprises the JNDI API and the JNDI SPI. The JNDI API (application programming interface) is the interface that developers deal with. The JNDI SPI (service provider interface) consists of interfaces that vendors of naming and directory services implement to make their naming and directory services compliant and accessible using the JNDI API. The WebLogic Server implements the JNDI SPI, which enables applications to access and use WebLogic's naming and directory services using the JNDI API. This lesson will focus on the JNDI API only. Figure 9.6 shows how JNDI achieves the unification of disparate naming and directory service providers. The JNDI architecture consists of four layers:
Figure 9.6. JNDI architecture.The next section will be covering the important classes in the JNDI API. JNDI APIThe JNDI API is composed primarily of the following packages:
Today's lesson will be focusing on the naming and directory packages because a client using the JNDI API uses the classes in these packages. Now look at the important classes in these two packages in detail. javax.naming PackageThe interfaces and classes used for accessing naming services are organized in the javax.naming package. Interfaces such as Context and Name and classes implementing these different interfaces (IntialContext, CompoundName, and CompositeName) form part of the javax.naming package. Take a look at the different interfaces and classes in this package. The Context InterfaceThe Context interface in the javax.naming package is the naming context composed of the name and object bindings. The Context interface contains the bind(), rebind(), rename(), lookup(), list(), createSubcontext(), unbind(), and other methods that are used by client applications and server-side components to access and register themselves with the naming service. Apart from these methods, the Context interface contains constants such as INITIAL_CONTEXT_FACTORY, PROVIDER_URL, SECURITY_CREDENTIALS, and so on that are used to set and retrieve the environment properties for different contexts. The InitialContext class implements the Context interface. Take a look at the important methods in the Context interface:
The InitialContext ClassThe InitialContext class implements all the methods in the Context interface. The IntialContext class is always used when any JNDI operations need to be initiated. Because the InitialContext class implements the Name interface, environment properties to define the state for the InitialContext such as the context provider, security credentials, and so on should be set in an instance of the InitialContext. Hence, a JNDI-compatible service provider will provide the InitialContext class. The Name InterfaceThe Name interface of the javax.naming package provides methods to manipulate the names defined for objects in the naming service. The important methods in the Name interface include add(), remove(), size(), startsWith(), and endsWith(). The CompoundName and CompositeName classes implement the Name interface. Service providers of naming services need to provide the CompoundName and CompositeName classes. The CompoundName ClassThe CompoundName class implements the methods in the Name interface. The manipulation methods in the CompoundName are primarily used for working with compound names, that is, names defined in a hierarchical format. The names defined in a compound name format have well-defined syntax and structure rules. Service providers for naming services that support compound names provide an implementation of the CompoundName class. A compound name is a name in a single naming system (for example, LDAP) and is made up of a set of components separated by the separator character of that naming system. The ordering of compound names is from right to left. Here is an example of a compound name: cn=Teach Yourself WebLogic Server 7.0 in 21 Days, ou=SAMS Publishing where the two parts of the compound name are ou=SAMS Publishing cn=Teach Yourself WebLogic Server 7.0 in 21 Days The CompositeName ClassThe CompositeName class implements the methods in the Name interface. The CompositeName class should be used when the names defined for objects in the naming service follow a composite name format, that is, a set of names in different namespaces. Each of these names in a namespace can be a compound name (that is, a hierarchical name) that can be further broken down. Service providers for different naming services that support compound names provide an implementation of the CompoundName class. Here is an example of a composite name: cn=Teach Yourself WebLogic Server 7.0 in 21 Days, ou=SAMS Publishing/examples/jndisrc/JNDISampleApp.java This example shows a composite name in string format representing the contents of two different namespaces: LDAP and a file system. The first part of the composite name is the LDAP compound name, and the remaining parts are the compound names in the file system namespace separated by the / namespace separator. A lookup() method call using this composite name returns the File object for the JNDISampleApp.java file. javax.naming.directory PackageThe javax.naming.directory package contains interfaces and classes used for accessing directory services. Interfaces such as DirContext, Attribute, and Attributes and classes implementing these different interfaces (IntialDirContext, BasicAttribute, and BasicAttributes) form part of the javax.naming.directory package. Take a look at the different interfaces and classes in this package. The DirContext InterfaceThe DirContext interface in the javax.naming.directory package extends the javax.naming.Context interface. The DirContext provides additional methods for manipulating attributes associated with objects and names in the directory service. The important methods in the DirContext interface are bind(), createSubContext(), rebind(), and search(). Methods such as bind() and createSubContext() are overloaded to deal with manipulation of attributes along with names and to deal with the same methods (with different signatures) inherited from the Context interface. The InitialDirContext class implements the DirContext interface. Service providers of different directory services need to provide the InitialDirContext class that implements the DirContext interface. Now you'll look at some methods that demonstrate the use of attributes with names in the directory service.
The InitialDirContext ClassThe InitialDirContext class implements the methods in the DirContext interface. Like the InitialContext class that provides the functionality for the methods in the Context interface, the InitialDirContext class provides the functionality for the methods in the DirContext class. Moreover, because the InitialDirContext class also extends the InitialContext class, it automatically inherits the functionality that implements the methods in the Context interface. The Attribute InterfaceThe Attribute interface in the javax.naming.directory package defines the ways to access and manipulate an attribute of an object in a namespace. An attribute can contain zero or more values that may or may not be ordered. The important methods in the Attribute interface are add(), remove(), get(), set(), and so on. The BasicAttribute class in the javax.naming.directory package implements the Attribute interface. The BasicAttribute ClassThe BasicAttribute class in the javax.naming.directory package implements the methods in the Attribute interface. Server objects that need to set an attribute along with their name in the directory interface or client applications that need to search objects in a directory service based on an attribute use the BasicAttribute class. The Attributes InterfaceThe Attributes interface contains methods to manipulate a collection of Attribute objects. The important methods in this interface are related to retrieving and setting attributes for an object. The BasicAttributes ClassThe BasicAttributes class in the javax.naming.directory package provides the underlying functionality for the methods in the Attributes interface. Named Resources in WebLogic ServerWebLogic Server supports different types of named resources that can be looked up and used by client applications or components using the JNDI API. Examples of resources that can be registered in the naming service of WebLogic Server are as follows:
Because WebLogic Server provides its own implementation of the JNDI SPI, it provides its own implementation of the InitialContext environment and context factory. Context creation using WebLogic Server's extension to the JNDI API can be done in three ways:
|