October 3, 2012
by Niranjan Tallapalli
Naming Service? Is a service which allows us to store and retrieve simple objects with a unique name. It is just like one of the storage unit.
Directory Service? Is an added service on top of naming service which allows us to store and retrieve objects with some directory names (i.e, maintained into groups)
These services are implemented by different vendors where each vendor provides their own APIs and protocols to interact with theirs naming and directory services.
If any java application wants to use these services, it has to plugin to any of the vendor provided implementation, then
- Application becomes vendor specific
- While deploying we need to know about the vendor and its service API
- While migrating the application from one vendor to another we need to change the code base of the application
To avoid these problems we want a standard abstraction through which a java application can interact with any vendor implemented Naming and Directory Service. To meet the above requirement SUN has released standard API providing solution to the above mentioned problems. These standards have been released under JNDI (Java Naming and Directory Service)

JNDI is an standard abstraction which allows any java application to interact with any vendor implemented naming and Directory Service.
In this case if java application wants to interact with any naming registry it just requires to prepare a request in JNDI format using JNDI API i.e, request is explained and submitted to JNDI API classes where JNDI API uses the information given by the java application and locates the SPI implementation and initializes it.
JNDI API dispatches the request to the SPI implementation through JNDI SPI.
SPI implementation converts the request and interact with the respective registry to process the request, collects the response convert it into the JNDI format and return it.
JNDI API returns response to the java application.
JNDI API
javax.naming.Context
Is an interface given under JNDI specification. Declared with methods required to read, insert, update and remove the objects like lookup, bind, rebind, unbind, list
This interface should be implemented by the vendors as part of SPI implementation.
javax.naming.InitialContext
Is a non-abstract class implementing Context interface given under JNDI specification. It implements wrapper pattern, it wraps one context implementation instance (i.e, instance of context interface implementation class given by third party vendor)
This is given to simplify the process of locating and initializing SPI implementation objects. i.e, it separates logic required to locate and initiaze SPI implementation objects from java objects.
Using JNDI API to connect to the naming registry
- Set the required properties like
a. Name: Context.INITIAL_CONTEXT_FACTORY
Value: should be the qualified class name of the implementation class of javax.naming.spi.InitialContextFactory interface
Example: weblogic.jndi.WLInitialContextFactory is the implementation class name of javax.naming.spi.InitialContextFactory. This allows us to interact with weblogic naming registry
b. Name: Context.PROVIDER_URL
Value: should be the url pointing to the naming registry
Example: t3:\\localhost:7001 : points to weblogic naming registry running on the local machine on 7001 port.
These properties can be set into the system properties into hashtable
- Create an instance of InitialContext (IC) where it has following constructors
a. IC as no arguments: In this case it gets the properties from the system props object
b. IC(Hashtable ht): It gets the properties from hashtable
Note: The first property given under the step1 is mandatory and if it is not found, IC object will not be created and it throws NamingException describing NoInitialContextFoundException
Step 1:
Hastable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY, “weblogic.jndi.WLInitialContextFactory”);
ht.put(Context.PROVIDER_URL, “t3:\\localhost:7001”);
//Here t3 is weblogic specific protocol
InitialContext ic = new InitialContext(ht);
Step 2:
System.setProperty(Context. INITIAL_CONTEXT_FACTORY, “weblogic.jndi.WLInitialContextFactory”);
System.setProperty(Context.PROVIDER_URL, “t3:\\localhost:7001”);
InitialContext ic = new InitialContext(ht);
The above steps connects to the naming registry, now we can use IC object reference and interact with the naming registry to get the object and store the object.
Like this:
Like Loading...
Recent Comments