Java's gorgeous Turn-J2EE specifications (I) JNDI Java Naming and Directory Interface

Source: Internet
Author: User

Java Naming and directory interfaces are a group of APIS for accessing naming and directory services in Java applications. It provides developers with universal and unified interfaces for searching and accessing various naming and directory services. JDBC is built on the abstraction layer.
 

The naming service associates the name with the object and can use the name to access the object. Directory Service is a naming service in which objects not only have names but also attributes.

 

I. Naming and directory services

 

Naming Service: as an infrastructure that appears in many computer systems, it provides a naming service for associating objects by name and matching objects by object. The naming service allows you to find an object corresponding to it by name. For example, domain namingservice (DNS) on the Internet is a naming service that maps domain names to IP addresses. When you open a website, you generally enter a name in the browser, find the corresponding IP address through DNS and open it.

 

Directory Service: A special type of database, which is opposite to SQL Server, Access, Oracle, and other relational database management systems. The purpose of constructing a directory service is to process behavior-based transactions, A relational information model is used.

 

In addition to the one-to-one relationship, the Directory Service provides a hierarchical information library. For the directory service, this hierarchy is usually used to optimize search operations, and can also be distributed or replicated across networks according to the actual situation.

The directory service is a naming service and does not need to be a directory service.

 

Naming and directory services associate identifiers with resources.


 

Ii. JNDI Architecture


 

 

The JNDI architecture is very similar to the JDBI architecture. The JNDI architecture provides APIs for a set of standard naming systems. In applications, we only use the above package types. The specific call class and communication process are transparent to users. The jndi api provides a standard and unified implementation for accessing different JNDI services. The specific implementation can be completed by different Service providers.

 

The middle layer is named management layer. The functions should be completed by the jndi spi.

The lowest layer is the jndi spi api and its implementation.

 

We only need to pass the relevant parameters of the serverJNDI APIThe specific call process consistsSPI.


III. Basic use

 

1. register the JNDI provider

Obtain the JNDI provider and register it before using it.

 

2. Connection Service

After registration, you can implement the service connection. The name service starts with InitialContext, And the Directory Service uses InitialDirContext. They implement Context and DirContext respectively. These two interfaces are the most important interfaces in JNDI.

For example:

 

System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"   com.sun.jndi.fscontext.FSContextFactory"); InitialContext ctx = new InitialContext(); 


 

3. Search for objects

You can use lookup to search for objects. Besides using String as a parameter, you can also use the Name interface as a parameter.

importjavax.naming.*;try{    Context ic = new Context();Objectobj;obj= ic.lookup("javax.transaction.UserTransaction");UserTransactionut = (UserTransaction)obj;ut.begin();ic.close();}catch(NamingExceptione){}

 

4. Remote Object binding


 

For example:

Public staticContext getInitialContext () throws NamingException {Environment env = new Environment (); env. setProviderUrl ("t3: // localhost: 7001"); env. setSecurityPrincipa ("system"); env. setSecurityCredendtials ("weblogic"); Contextcontext = env. getInitialContext (); returncontext;} // obtain intial contextContext ctx = getInitialContext (); // create the Bank myBank = newBank () object named Band; // bind the ctx object. bind ("theBank", myBank); // re-bind the object to the JNDI tree ctx. rebind ("theBank", myBank); // unbind ctx. unbind ("theBank"); ctx. close ();

5. Rename an object


ctx.rename("report.txt", "old_report.txt"); 
 

6. Get attributes

Attribute-related interfaces are Attribute and Attributes, all of which are in the javax. naming. directory package. You can use the getAttributes method of DirContext to obtain the Attribute set of the object, use the get method of Attributes to obtain the corresponding Attribute, and finally obtain the Attribute value through the get method of Attribute.

String dn = "uid = me, dc = mycompany, dc = com, ou = customer, o = ExampleApp"; Context user = (Context) ctx. lookup (dn); // obtain all Attributes attrs = user. getAttributes (""); Attribute test = attrs. get ("test"); Object testValue = test. get ();

7. modify attributes

Use the modifyAttributes method of DirContext and InitialDirContext. The so-called modification process is to first construct the attribute list to be modified, and then use the above method to submit. When an attribute contains multiple values, you need to include non-modified values of the attribute. Otherwise, the server will think that those values do not need to be deleted.

public void updateAddress(String dn,String address, String country, String phone) {        BasicAttributes mod_attrs = new BasicAttributes();       if(address != null)           mod_attrs.put("address", address);       if(country != null)            mod_attrs.put("c", country);        if(phone != null)           mod_attrs.put("phonenumber", phone);        if(mod_attrs.size() != 0)           initial_ctx.modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, mod_attrs);

Summary

The idea of JNDI is to provide a common interface. The underlying name service time limit can be of various types, and the JNDI uses the SPI time limit code to shield the differences between various names and Directory Service implementations.


JNDI can solve the problem of data sharing, distributed applications, and tight coupling. Therefore, it can be applied to a large number of software development projects, distributed systems, or projects that pay more attention to post-maintenance and upgrade.


These are some of the most basic knowledge accumulation. We recommend the expansion of JNDI.


Recommended address:

J2EE Summary: JNDIThis section describes the functions of JNDI.

JNDIFocuses on introducing the attributes and functions of JNDI from the perspective of LDAP.

Role of JNDI in J2EEThis section mainly introduces the burden of enterprise-level application deployment in the JNDi stage, and helps applications to free up from deployment details.




Related Article

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.