The JNDI (the Java naming and directory Interface,java naming and directory interface) is a set of APIs that access the naming and directory services in Java applications. The naming service links names to objects so that we can access them by name. A directory service is a naming service in which objects have names as well as attributes.
Naming or directory services allows you to centrally store shared information, which is important in Web applications because it makes such applications more coordinated and easier to manage. For example, you can store printer settings in a directory service for use with printer-related applications.
This article gives a quick tutorial using code examples to enable you to start using Jndi. It:
1, provides a jndi overview
2, describes the characteristics of the Jndi
3, experience a bit with Jndi development and application
4, indicating how to use Jndi access to the LDAP server, for example, Sun one directory server
5, indicating how to use the Jndi access to the Java service
6, provided the sample code, you can adapt it for your own application
Jndi Overview
We all unknowingly use the naming service every day. For example, when you enter a url,http://java.sun.com in a Web browser, DNS (Domain Name System, field name systems) Converts this symbolic URL name to the traffic ID (IP address). The objects in the naming system can be the names in the DNS records, the EJB components in the application server (Enterprise JavaBeans Component), LDAP (lightweight Directory Access Protocol) The user profile in.
The directory service is a natural extension of the naming service. The key difference between the two is that the object in the directory service can have attributes (for example, the user has an email address), and the object in the naming service has no attributes. Therefore, in the directory service, you can search for objects based on attributes. Jndi allows you to access files in the file system, locate remote RMI-registered objects, Access directory services such as LDAP, and locate EJB components on the network.
Jndi is a good choice for applications like LDAP clients, Application Launcher, class browsers, network management utilities, and even address book.
Jndi schema
The JNDI architecture provides a standard set of APIs that are independent of the naming system, built on the drivers associated with naming systems. This layer helps separate the application from the actual data source, so whether the application accesses LDAP, RMI, DNS, or other directory services. In other words, Jndi is independent of the specific implementation of the directory service, so long as you have a directory service to provide the interface (or drive), you can use the directory. As shown in Figure 1. Figure 1:jndi Architecture
The important point to note about Jndi is that it provides an application programming interface (application programming Interface,api) and a service provider Interface (services provider INTERFACE,SPI). The real implication of this is that for your application to interact with a naming service or directory service, you must have a Jndi service provider for the service, which is where the Jndi SPI works. A service provider is basically a set of classes that implement a JNDI interface for a variety of specific naming and directory services-much like JDBC drivers implement JDBC interfaces for various specific database systems. As an application developer, you don't have to worry about Jndi SPI. You just need to make sure that each named or directory service you want to use has a service provider.
J2SE and Jndi
The version of the Java 2 SDK 1.3 and above contains Jndi. There is also a standard extension for JDK 1.1 and 1.2. The latest version of the Java 2 SDK 1.4.x includes several enhancements and the following naming/directory service providers:
1. LDAP (lightweight Directory Access Protocol) service provider
2. CORBA COS (Common object Request Broker Architecture Common Object Services) naming service providers
3. RMI (Java Remote method invocation) Registration service provider L DNS (Domain Name System) service provider
More Service Providers
You can find a list of service providers that can be downloaded at the following Web site: http://java.sun.com/products/jndi/serviceproviders.html It is perhaps particularly interesting that the following Web site provides windows Registry JNDI Service Provider: Http://cogentlogic.com/cocoon/CogentLogicCorporation/JNDI.xml This service provider enables you to access Windows xp/2000/nt/me/ 9x Windows registry. You can also download the Jndi/ldap booster pack:http://java.sun.com/products/jndi/This booster Pack contains support and extensions for popular LDAP controls. It replaces the booster pack bundled with the LDAP 1.2.1 service provider. More information on controls and extensions can be found on the following Web site: http://java.sun.com/products/jndi/tutorial/ldap/ext/index.html Another interesting service provider is Sun's support DSML v2.0 A service provider (directory service Markup Language, Catalog Services Markup Language). The purpose of DSML is to bridge the directory service and XML.