Java's Jndi explanation

Source: Internet
Author: User
Tags mx record tomcat server

Reprinted from (http://blog.csdn.net/u010430304/article/details/54601302)

Basic application of Jndi
Jndi is a shorthand for Java naming and directory Interface (Java naming and directory interface), which is the API for providing naming and directory Access services for Java applications (application programing Interface, application programming Interface).

1. The concept and application of nomenclature
The naming (naming) in Jndi is the binding of a Java object to a container environment (context) in the form of a name. A later call to the container environment (context) lookup method can also be used to find out which Java object A name is bound to. Readers may wonder: what does it mean to create a Java object, bind it to a JNDI container environment, and then query it? In a real-world project application, a system program or a box loader is usually the first to bind a resource object to a JNDI environment, and a module program that is later run in that system or framework can look up these resource objects from the JNDI environment. For example, a Tomcat server can create a data source (DataSource) object connected to a database system at startup and bind the data source (DataSource) object to a JNDI environment. Later Servlets and JSPs running in this Tomcat server can query the data source (DataSource) object from the JNDI environment without worrying about how the data source (DataSource) object was created, This approach greatly enhances the maintainability of the system, and when the connection parameters of the database system change, this is only one thing that a Tomcat system administrator should be concerned about, regardless of all application developers.
The container environment (context) itself is also a Java object, and it can be bound to another container environment (context) by one name. Binds a context object to another context object, which forms a parent-child cascade, and multiple context objects can eventually be cascaded into a tree structure, and each context object in the tree can be bound to several Java objects, as shown in 6.10.

Figure 6.10
Each of the boxes in Figure 6.10 represents a context object, with names of A, B, C, D, E,b, and C being a sub-context,d of B and sub-context of D, respectively. Each small ellipse in each box in Figure 9.x represents a Java object, and they all have a binding name, such as dog, pig, sheet, and so on, where two Java objects of the same name cannot be bound in the same context. Binding objects of the same name can appear in different context. As can be seen, the cascade structure of the context tree is very similar to the directory structure in the file system, and the relationship between the context and the Java object in which it is bound is very similar to the directory-to-file relationship in the file system. As you can see from Figure 6.10, to get a Java object in the context tree, you first want the context object in which it resides, and as soon as you get a context object, you can call its query (lookup) method to get the Java object it is bound to. In addition, the lookup method that invokes a context object can also obtain any of the context objects in the context tree, which only requires the corresponding context path in the lookup method. There is no concept of "root" context in Jndi, that is, performing a jndi operation does not start with a "root" context object, but can start from any context in the context tree. In any case, the program must obtain a context object as the entry for the operation to perform various Jndi naming operations, and for this purpose, a InitialContext class is provided in the Jndi API to create the portal context object used as the Jndi naming operation. Context is an interface where the context object is actually an instance object of an implementation class of context, and the process of selecting this specific context implementation class and creating its instance object is done by a context factory class, The class name of this factory class can be specified by the environment attribute java.naming.factory.initial of the jndi, or it can be selected according to the schema of the URL parameter of the context operation method.

2. The concept and application of the catalogue
The directory in Jndi is very different from the concept of a directory in a file system, and the directory in Jndi refers to saving all the property information of an object into a container environment. The Jndi directory principle is very similar to the Jndi naming (naming) principle, where the main difference is that the directory container environment holds the object's property information, not the object itself, so the directory provides various operations on the property. In fact, Jndi directories (directory) and naming (naming) are often used together, and the class representing the directory container environment provided in the Jndi API is Dircontext,dircontext is the subclass of the context, Obviously it can do all the naming (naming) operations in addition to the directory-related operations. DirContext is an extension to the context, which adds the ability to manipulate directory properties on a context basis, where you can bind the object's property information and find the object's property information. The directory in Jndi is shown in structure 6.11.

Figure 6.11
Each outermost box in Figure 6.11 represents a DirContext object, each of which is bound with a, b,b is a sub-dircontext. Each of the small ellipses in the outermost box in Figure 6.11 represents a Java object, each of which represents the properties of an object, respectively. As you can see from the contents of DirContext named A, you can bind the object itself in a DirContext container environment, or you can bind the object's property information, the bound object and the bound property are completely independent of the two things, even if their binding name is the same, They are also completely independent of the operation. In addition, a property can have multiple property values, for example, the category property of the dog object sets two property values: Meat and pet. As you can see from DirContext in the name B, a DirContext container environment can also bind only the property information of an object without binding any object itself. Similar to the operating principle of the context, a InitialDirContext class is provided in the Jndi API to create a portal DirContext object that acts as a JNDI naming and directory attribute operation.

3. Jndi Service program for DNS queries
The Jndi API is a programming interface for application developers that needs to invoke a specific Jndi service program at runtime, the relationship between the Jndi API and the Jndi service program, like the relationship between JDBC and the JDBC driver. Starting with JDK 1.3, the Jndi API is integrated into the JDK, and the Jndi service program for DNS queries is integrated in the version beginning with JDK 1.4, so if we use JDK 1.4 and a higher JDK version to develop a DNS information query program, you do not need to download and install Jndi API and the JNDI service program for DNS queries. The Jndi service program provided by Sun for querying DNS information binds the DNS information of a domain name to the DirContext object representing the domain name as a property. Open the first page of the JDK Help document, where you can search for the "jndi" keyword and see a "Jndi" hyperlink, and click the hyperlink to go to the "Java naming and Directory Interface" help page, as shown in 6.12.

Figure 6.12
Click the "DNS service Provider" hyperlink in Figure 6.12 to go to the help page for the DNS services program. As long as we have some basic knowledge of JDNI programming, plus the information provided in this Help documentation page, we know how to invoke this DNS service to get DNS information and MX records for a domain.

The following is an experimental Jndi program that helps us to familiarize ourselves with and master the use of the Jndi API, and also helps us understand how DNS's Jndi service program returns DNS information.

Hands-on: Get DNS information using the Jndi API
Routinely 6-5 write a program called Dnsquery.java, which uses the Jndi API to obtain DNS information for a domain and extract the name of an SMTP server from the domain, many of which are designed to help us become familiar with Jndi The use of the API and the DNS information content returned by the Jndi service program of the DNS is added. To run this program, you need to specify one or two parameters, the first parameter is required, for the domain name to be queried, the second parameter is optional, the IP address of the DNS server used for the query, if you do not specify a second parameter, The DNS server for the Jndi service will use the DNS servers set on the underlying operating system.

Routine 6-5 Dnsquery.java

Import Java. util. Hashtable; Import Javax. naming. Context; Import Javax. naming. Namingenumeration; Import Javax. naming. Namingexception; Import Javax. naming. Directory. Attribute; Import Javax. naming. Directory. Attributes; Import Javax. naming. Directory. DirContext; Import Javax. naming. Directory. InitialDirContext;p Ublic class DnsQuery {public static void main (string[] args) throws Namingexception {/* The first parameter specifies the domain or host name to query, the second parameter specifies the DNS server for the query, omitting strict parameter error checking for easy readability of the program */String domain = args[0]; String DNSServer = args.length<2?"" : ("//" + args[1]); To specify the factory class of the context through the environment properties Hashtable env = new Hashtable (); Env. put (Context. Initial_context_factory,"Com.sun.jndi.dns.DnsContextFactory"); Env. put (Context. Provider_url,"DNS:" + dnsserver); DirContext CTX = new InitialDirContext (env); Gets the Attributes object that contains all the properties and only the MX attribute, respectively Attributes Attrsall = CTX. GetAttributes (Domain); Attributes attrsmx = CTX. getattributes (Domain, new string[]{"MX"});/* The entire program code above can also be replaced by the following code, this program code by querying the URL of the schema information to automatically select the context of the factory class *//* DirContext CTX = new InitialDirContext (); Attributes Attrsall = ctx.getattributes ("DNS:" + dnsserver + "/" + domain); Attributes attrsmx = ctx.getattributes ("DNS:" + dnsserver + "/" + Domain, new string[]{"MX"}); */System. Out. println ("Print out Domain" + domain +"Information in the Attributes object:"); System. Out. println (Attrsall); System. Out. println ("--------------------------"); System. Out. println ("Print Search domains only" + domain +"The MX record for the Attributes object:"); System. Out. println (Attrsmx); System. Out. println ("--------------------------"); System. Out. println ("Print out each property in the attributes object individually:"); Namingenumeration attributes = Attrsall. GETALL (); while (attributes. Hasmore ()) {System. Out. println (Attributes. Next ()); } System. Out. println ("--------------------------"); Call the Get method directly from the Attrsmx collection to retrieve the MX property System. Out. println ("Directly retrieves the MX attribute in the Attributes object:"); Attribute attrmx = Attrsall. Get ("MX"); System. Out. println (ATTRMX); System. Out. println ("--------------------------"); Gets the first value in the MX attribute: System. Out.println ( "gets the first value in the MX attribute:")  ; String recordmx = (string) attrmx.get () .out.println (recordMx) .out.println (" from the MX attribute value extracted from the mail server address: ") ; String smtpserver = Recordmx.substring (Recordmx ") + 1) ; System.out.println (smtpServer)              
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • Ten
    • one
    • 2
    • (
    • )
    • +
    • +
    • /
    • 0
    • +
    • all
    • +
    • +
    • +
    • -
    • 29
    • +
    • +
    • all
    • +
    • +
    • PNS
    • up
    • i>39
    • 48
    • all
    • /
    • /
    • /
    • /li>
    • ,
    • ,
    • ,
    • up-
    • -
    • +
    • -
    • +
    • *
    • +
    • ,
    • ,
    • +
    • $
    • "
    • " "
    • "

(2) After compiling the Dnsquery.java program in the Windows command Line window, execute the following command in the Command line window:

ipconfig /all
    • 1

If the ipconfig command displays a result that contains DNS server information, then we can then use the following command to start the execution of the DnsQuery class:

java  DNSQuery  sina.com 
    • 1

The result of running the above command is 6.13.

Figure 6.13
() Assume that the DNS server configured on the local computer that you see in the previous step with the ipconfig command is 202.106.46.151, then we then execute the following command:

java  DNSQuery  sina.com  202.106.46.151
    • 1

Once this command is executed, the information in Figure 6.13 can be displayed. We then deliberately specify the DNS server parameter in the above command as an incorrect IP address for execution, and the modified command statement looks like this:

java  DNSQuery  sina.com  192.168.1.151
    • 1

The result of this command after execution is as follows: 6.14

Figure 6.14
If your computer can only connect to the Internet through a proxy server, execute the following command directly on that computer:

java  DNSQuery  sina.com 
    • 1

This will also cause the error in figure 6.14. If you want to go online through a proxy server, the correct execution of the above program, you can use the following command:

java -DsocksProxyHost=162.105.1.200 -DsocksProxyPort=808 DNSQuery sina.com 202.106.46.151
    • 1

Because the above command is too long, in the typesetting time has become two lines to write, the reader in the above command, do not manually change line. Readers should modify the proxy server address, the proxy port number, and the address of the DNS server according to their actual situation.

Java's Jndi explanation

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.