JNDI-based application development

xiaoxiao2021-03-06  50

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. Provides JNDI Overview 2, describing the characteristics of JNDI 3, experiences using JNDI development app 4, indicating how to use JNDI to access the LDAP server, for example, Sun ONE Directory Server 5, indicating how to use JNDI access J2EE Services 6, provide sample code, you can adapt it to your own application JNDI Overview We all use naming services every day. 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. The JNDI Architecture JNDI architecture provides a set of standards independent of the named system API, which is built on the drive related to 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: An important point to JNDI should pay attention to JNDI is 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) Naming Service Provider 3, RMI (Java Remote Method Invocation) Registered Service Provider L DNS (Domain Name System) Service Provider More Service Provider You can find the downloadable service provider list: http: // Java.sun.com/products/jndi/serviceProviders.html is especially interesting may be the following URL provided by the Windows Registry JNDI service provider: http://cogentlogic.com/cocoon/cogentlogiccorporation/jndi.xml This service provider You can access Windows XP / 2000 / NT / ME / 9X Windows Registry. 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 packages: 1. Javax.naming: The class and interface containing the naming service. 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 is just mentioned earlier, named service links names and objects. 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. There are two overloaded forms in each name method in Context (or later discussion): 1, lookup (string name): Accept string name 2, lookup (javax.naming.name): Accept structure name For example, Compositename (the name of multiple naming systems) or CompondName (name in a single naming 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). Here is an example: 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 JNDI service provider Chinese factory class (Factory Class). 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: INITIAL_CONTEXT_FACTORY context value 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 obtain or resolve the object from the naming service or directory, use the Lookup method of the 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-82983.html

New Post(0)