JNDI-based application development

xiaoxiao2021-03-06  65

JNDI (The Java Naming and Directory Interface, Java Naming and Directory Interface) is a set of APIs for accessing naming and directory services in Java applications. Naming Services link names and objects so that we can access objects with names. The directory service is a naming service. In this service, the object not only has a name, but also attributes.

Naming or Directory Services allows you to centrally store common information, this is important in web applications, as this makes this application more coordinated and easier to manage. For example, the printer can be stored in a directory service to be used by applications related to the printer.

This article gives a quick tutorial in the way in this paper, so you can start using JNDI. it:

1. Provide JNDI overview

2, describe the characteristics of JNDI

3, experienced the development of JNDI

4, indicating how to use JNDI to access the LDAP server, for example, Sun ONE directory server

5, indicating how to use JNDI to access J2EE service

6. Provide sample code, you can adapt it to your own application

JNDI Overview

Everyone uses a naming service unconsciously. For example, when you enter a URL, http://java.sun.com, the DNS (Domain Name System, Domain Name System) converts this symbol URL name into a communication identifier (IP address). The object in the naming system can be the name in the DNS record, the user profile in the EJB component (Enterprise JavaBeans Component, LDAP (Lightweight Directory Access Protocol).

Directory services are nature expansion of naming services. The key difference between the two is that the objects in the directory service can have attributes (for example, the user has an email address), and the object is not attribute in the naming service. Therefore, in the directory service, you can search objects based on the property. JNDI allows you to access files in the file system, locate remote RMI registration objects, access directory services such as LDAP, and locate EJB components on the network.

For applications like LDAP clients, applying launcher, class browsers, network management utilities, even address thin, JNDI is a good choice.

JNDI architecture

The JNDI architecture provides a set of standards of APIs independent of nomenclature, which are built on top of the name associated with the naming system. This layer helps separate the application with the actual data source, so regardless of the application access, LDAP, RMI, DNS, or other directory services. In other words, JNDI is independent of the directory service, as long as you have a directory service providing interface (or driver), you can use the directory. As shown in Figure 1. Figure 1: JNDI architecture

It is important to note about JNDI to note that it provides an Application Programming Interface (API) and Service Provider Interface (SPI). The true meaning of this is that your application is interactive with the naming service or directory service, and must have this service JNDI service provider, which is where JNDI SPI works. Service providers are basically a class that implements JNDI interfaces for a variety of specific naming and directory services - very like JDBC drivers to achieve a JDBC interface for a variety of specific database systems. As an application developer, you don't have to worry about JNDI SPI. You only need to confirm that every name or directory service you want to use has a service provider.

J2SE and JNDI

Java 2 SDK 1.3 and above include JNDI. There is also a standard extension for JDK 1.1 and 1.2. The latest version of Java 2 SDK 1.4.x includes several enhancements and the name / directory service provider:

1, LDAP (Lightweight Directory Access Protocol) Service Provider 2, CORBA COS (Common Object Request Broker Architecture CommON Object Services) Name Service Provider

3, RMI (Java Remote Method Invocation) Register Service Provider L DNS (Domain Name System) Service Provider

More service providers

You can find a list of service providers you can download at the following URL: http://java.sun.com/products/jndi/serviceProviders.html is especially interesting perhaps the Windows Registry JNDI service provider provided by the following Website: http: / /cogenTlogic.com/cocoon/cogentlogiccorporation/jndi.xml This service provider allows you to access Windows Registry for Windows XP / 2000 / NT / ME / 9X. JNDI / LDAP booster pack: http ://java.sun.com/products/jva.sun.com/products/jva.sun.com/products/jva.sun.com/products/jndi/ This Booster Pack contains support and extensions for popular LDAP control. It replaces the Booster Pack that is bundled with the LDAP 1.2.1 service provider. More information about control and expansion can be seen in the following website: http://java.sun.com/products/jndi/tutorial/ldap/ext/index.html Another interesting service provider is Sun's support DSML V2.0 (Directory Service Markup Language, Directory Service Mark Language) service provider. The purpose of DSML is to make a bridge between the directory service and XML.

JNDI API

JNDI API consists of 5 packs:

1. Javax.naming: The class and interface containing the naming service is included. For example, it defines the Context interface, which is the entry of the naming service execution query.

2, javax.naming.directory: The expansion of the named package provides a class and interface that access the directory service. For example, it adds a new class to attributes, providing the DirContext interface representing the directory context to define the method of checking and updating the properties of the directory object.

3, javax.naming.event: Provides support for time notifications when accessing naming and directory services. For example, define the Namingevent class, this class is used to indicate the event generated by the named / directory service define the NamingListener interface that listens to Namingevents.

4, javax.naming.ldap: This package provides support for the operation and control of the LDAP version 3, universal package javax.naming.directory does not contain these operations and control.

5, javax.naming.spi: This package provides a way to add support for access naming and directory services through javax.naming and related package dynamics. This package is provided for developers who are interested in creating a service provider.

JNDI context

As mentioned earlier, the naming service linked the name and object. This connection is binding. A set of this bindings are called contexts, and the context provides solutions (ie the logging operation of the object). Other operations include: Name binding and cancel binding, listing the bounded names. Note that the name of a context object can be bound to another context object with the same naming convention. This is called the sub-context. For example, if Directory / Home is a context in UNIX, the subdirectory relative to this directory is the sub context - for example, Guests in / home / guest is the child's sub context. In JNDI, the context interface javax.naming.context indicates that this interface is a key interface with the named service interaction. Each naming method in the interface in Context (or later discussed) has two overload form:

1, Lookup (String Name): Accept string name

2, lookup (javax.naming.name): Accept structure name, for example, Compositename (spanning a number of named system) or CompondName (name in a single name system); they all implemented Name interfaces. An example of Compound Name is: cn = mydir, cn = q mahmoud, = people, Composite Name An example is: cn = mydir, cn = q mahmoud, ou = people / myfiles / max.txt (here, MyFiles / Max.txt is a file name representing the second part) javax.naming.initialContext is a class that implements the Context interface. Use this class as the entrance to the naming service. To create an InitialContext object, the constructor sets a group attribute in the form of java.util.hashtable or its subclass (for example, Properties). One will be given below:

Hashtable env = new Hashtable (); // select a service provider factory env.put (Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContext"); // create the initial context Context contxt = new InitialContext (env) ;

Initial_context_factory specifies the name of the Factory Class of the JNDI service provider. Factory is responsible for creating an appropriate InitialContext object for its services. In the code snippet above, the factory class is specified for the file system service provider. Table 1 shows the factory class of the supported service provider. It should be noted that the factory class of the file system service provider needs to download separately from Sun, J2SE 1.4.x does not contain these classes.

Table 1: The value of the context initial_context_factory

Name Service Provider Factory File System com.sun.jndi.fscontext.RefFSContextFactory LDAP com.sun.jndi.ldap.LdapCtxFactory RMI com.sun.jndi.rmi.registry.RegistryContextFactory CORBA com.sun.jndi.cosnaming.CNCtxFactory DNS com. Sun.jndi.dns.dnsContextFactory

In order to acquire or resolve the object from the naming service or directory, use the Lookup method of Context: Object Obj = contxt.lookup (name). The Lookup method returns an object, which is indicated by the context you want to find. Welcome to reprint in the case of retaining http://www.javajia.com!

转载请注明原文地址:https://www.9cbs.com/read-86681.html

New Post(0)