JNDI (Java Naming and Directory Interface) is used more and more frequently. On the J2EE platform, it cannot be left for a moment. JNDI provides a unified API interface for accessing the name and directory service. You can use JNDI to obtain shared components and resources. JNDI associates names with locations, services, information, and resources. The latest version of JNDI has been developed to 1.2.
A very important part of JNDI is the JNDI Service Provider Interface (SPI). For details, see:
Service providers is the service type that JNDI can provide. You can access http://java.sun.com/products/jndi/serviceproviders.htmlto understand the details of the JNDI service providers.
Among these service providers, I will mention one of them below: file system service provider. It seems that this type of FS is rarely used, because JNDI is generally used either with appliation server or with LDAP. However, it is also advantageous to use FS, that is, you can use JNDI independently without the cooperation of other things.
If you use FS, you must create a. bindings file for JNDI, which is bound with various components, services, and other information.
Using FS is quite simple.
1. Download The fscontext. jar and providerutil. Jar library files. Of course, the JNDI. jar must also be required.
2. To use JNDI, common usage:
Hashtable Env = new hashtable (11 );
Env. Put (context. initial_context_factory,
"Com. Sun. JNDI. fscontext. reffscontextfactory"); // Part1
Env. Put (context. provider_url,
"File:/E:/$ TEMP $/JNDI"); // Part2
Context initctx = new initialcontext (ENV );
Because JNDI provides a unified interface, the access method is unified. Specify initial_context_factory as COM at Part1. sun. JNDI. fscontext. reffscontextfactory; Specify provider_url at Part2 as the path of the JNDI binding file, for example, file:/E:/$ TEMP $/JNDI. At this time, this directory must exist. bindings file.
3. The content of the. bindings file is relatively simple. It is a bit similar to the properties file of the value-pair. Let's look at the following simple example. When I call MQSeries for Java, I use a. bindings File Created by jmsadmin.
Exampleql/refaddr/5/type = CCS
Exampleql/refaddr/8/content = default
Exampleql/refaddr/6/content = 0
Exampleql/refaddr/6/type = tc
Exampleql/refaddr/4/content =-2
Exampleql/refaddr/0/type = ver
Exampleql/refaddr/1/encoding = string
Exampleql/classname = com. IBM. MQ. JMS. mqqueue
Exampleql/refaddr/2/content =-2
Exampleql/refaddr/7/type = ENC
Exampleql/refaddr/3/encoding = string
Exampleql/refaddr/0/content = 1
Exampleql/refaddr/5/encoding = string
Exampleql/refaddr/1/type = DESC
Exampleql/refaddr/7/encoding = string
Exampleql/refaddr/9/encoding = string
Exampleql/refaddr/8/type = Qu
Exampleql/refaddr/2/type = exp
Exampleql/refaddr/9/content = qm_zhangdx
Exampleql/refaddr/7/content = 273
Exampleql/refaddr/9/type = qmgr
Exampleql/refaddr/5/content = 1381
Exampleql/factoryname = com. IBM. MQ. JMS. mqqueuefactory
Exampleql/refaddr/3/type = PRI
Exampleql/refaddr/0/encoding = string
Exampleql/refaddr/3/content =-2
Exampleql/refaddr/2/encoding = string
Exampleql/refaddr/1/content = local queue
Exampleql/refaddr/4/encoding = string
Exampleql/refaddr/4/type = per
Exampleql/refaddr/6/encoding = string
Exampleql/refaddr/8/encoding = string
You can use the bind, rebind, and unbind methods of context to modify the content of the. bindings file. refer to the following JNDI tutorial.