JNDI-based application development _ MySQL

Source: Internet
Author: User
Tags ldap
JNDI (TheJavaNamingandDirectoryInterface, Java Naming and Directory Interface) is a group of APIs for accessing naming and directory services in Java applications. The naming service associates the name with the object so that we can access the object with the name. Directory service is a naming service in which objects not only have names but also attributes. Name or directory service for your JNDI program development

The Java Naming and Directory Interface (Java Naming and Directory Interface) is a group of APIs that access Naming and Directory services in Java applications. The naming service associates the name with the object so that we can access the object with the name. Directory service is a naming service in which objects not only have names but also attributes.

Naming or directory services allow you to centrally store shared information. this is important for network applications, because it makes such applications more coordinated and easier to manage. For example, you can store printer settings in the directory service for use by printer-related applications.

This article provides a quick tutorial using the sample code so that you can start using JNDI. It:

1. provides the JNDI overview

2. describes the characteristics of JNDI.

3. experienced the use of JNDI to develop applications

4. demonstrate how to access the LDAP server using JNDI, such as the Sun ONE Directory Server

5. demonstrate how to use JNDI to access the J2EE service

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

JNDI overview

We all use the naming service unconsciously every day. For example, when you enter a URL, http://java.sun.com in a web browser, DNS (Domain Name System) converts the signed URL Name to a communication identifier (IP address ). The objects in the naming system can be the name in the DNS record, the EJB Component (Enterprise JavaBeans Component) in the application server, and the user Profile in LDAP (Lightweight Directory Access Protocol.

The directory service is a natural extension of the naming service. The key difference between the two is that objects in the directory service can have attributes (for example, users have email addresses), while objects in the naming service have 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 objects registered by remote RMI, Access Directory services such as LDAP, and locate EJB components on the network.

For applications such as LDAP clients, application launcher, class browsers, network management utilities, and even address book, JNDI is a good choice.

JNDI architecture

The JNDI architecture provides a set of standard naming system-independent APIs built on drivers related to the naming system. This layer helps to separate the application from the actual data source, regardless of 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. as long as you have a directory service to provide interfaces (or drivers), you can use the directory. 1. : JNDI architecture

One important thing to note about JNDI is that it provides application programming interface (API) and service provider interface (SPI ). The true meaning of this point is that to allow your application to interact with the naming service or directory service, you must have the JNDI service provider of this service, which is exactly where the jndi spi works. Service providers are basically a group of classes that implement the JNDI interface for various specific naming and directory services, just as the JDBC driver implements JDBC interfaces for various specific database systems. As an application developer, you don't have to worry about jndi spi. You only need to confirm that each name or directory service you want to use has a service provider.

J2SE and JNDI

Java 2 SDK 1.3 and later versions 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 following naming/directory service providers:

1. LDAP (Lightweight Directory Access Protocol) service provider

2. name the service provider for the Common Object Request Broker Architecture Common Object Services (COS ).

3. RMI (Java Remote Method Invocation) registers the service provider l DNS (Domain Name System) service provider

More service providers

You can find the list of service providers that can be downloaded at the following URL: http://java.sun.com/products/jndi/serviceproviders.html is particularly interesting, perhaps the Windows Registry JNDI service provider provided at the following URL: http://cogentlogic.com/cocoon/CogentLogicCorporation/JNDI.xml this service provider gives you access to Windows XP/2000/NT/Me/9x windows registries. You can also download the JNDI/LDAP Booster Pack at the URL below: the http://java.sun.com/products/jndi/ 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 about control and scaling can be seen on the following website: Another interesting Service provider for http://java.sun.com/products/jndi/tutorial/ldap/ext/index.html is Sun's support for DSML v2.0 (Directory Service Markup Language, Directory Service Markup Language) service provider. DSML aims to build a bridge between directory service and XML.

JNDI API

The jndi api consists of five packages:

1. Javax. naming: Contains classes and interfaces for accessing the naming service. For example, it defines the Context interface, which is the entry for the naming service to execute queries.

2. Javax. naming. directory: expands the name package and provides classes and interfaces for accessing the directory service. For example, it adds a new class for the attribute, provides the DirContext interface that represents the directory context, and defines the methods for checking and updating the attributes of the directory object.

3. Javax. naming. event: provides support for time notifications when accessing the naming and directory services. For example, the NamingEvent class is defined to indicate the events generated by the naming/directory service, and the NamingListener interface for listening to NamingEvents is defined.

4. Javax. naming. ldap: This package provides support for the operation and control of LDAP Version 3 expansion. the general package javax. naming. directory does not contain these operations and controls.

5. Javax. naming. spi: This package provides a method to dynamically add support for access naming and directory services through javax. naming and related packages. This package is provided for developers who are interested in creating service providers.

JNDI context

As mentioned earlier, the naming service associates names with objects. This kind of contact is called binding ). A group of such bindings is called context, which provides parsing (that is, the query operation of the returned object ). Other operations include name binding and unbinding, and listing the name of the binding. Note that the name of a context object can be bound to another context object with the same naming conventions. This is called subcontext. For example, if the directory/home in UNIX is a context, the sub-context relative to the directory is the sub-context. for example, in/home/guests, guests is the sub-context of home. In JNDI, the Context is represented by the interface javax. naming. Context, which is a key interface for interaction with the naming service. In Context (or discussed later

Every naming method in the DirContext interface has two overload forms:

1. Lookup (String name): accept the String name

2. Lookup (javax. naming. name): accept structure names, for example, CompositeName (spanning multiple naming systems) or CompondName (names in a single naming system); all of them implement the Name interface. An example of Compound name is cn = mydir, cn = Q Mahmoud, ou = People, and composite name: cn = mydir, cn = Q Mahmoud, ou = People/myfiles/max.txt (here, myfiles/max.txt indicates the file name of the second part) Javax. naming. initialContext is a class that implements the Context interface. Use this class as the endpoint for naming services. To create an InitialContext object, the constructor sets a set of Properties in the form of java. util. Hashtable or its subclass (such as Properties. The following 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 factory class in the JNDI service provider. The 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 classes of supported service providers. Note that the factory classes of the file system service provider need to be downloaded from Sun separately. J2SE 1.4.x does not contain these classes.

Table 1: context INITIAL_CONTEXT_FACTORY 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

To retrieve or parse an Object from the naming service or directory by name, use the lookup method of Context: Object obj = contxt. lookup (name ). The Lookup method returns an object that represents the son of the context you want to find.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.