Comments
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Cloud Computing
Conference & Expo
November 2-4, 2009 NYC
Register Today and SAVE !..
SYS-CON.TV
Today's Top SOA Links


Java Naming Services Internals
Implementing a Simple Client/Server-Based JNDI Naming Service

The Java Naming and Directory Interface (JNDI) is a standard API to access different naming and directory service implementations like LDAP. A naming service provides naming functionality and a directory service provides applications with directory functionality. The Java naming service is a fundamental component of every J2EE system.

JNDI consists of a client API and a service provider interface (SPI). The client application uses the client API to access various naming and directory services. The SPI lets naming and directory service implementations be plugged into the JNDI framework.

In this article, we will explore how to use the JNDI SPI to implement a simple client/server-based naming service.

Naming Service Architecture
Figure 1 shows the architecture and the various components of a typical Java naming service:

The first thing any JNDI client does is to create an InitialContext object. The InitialContext uses an URLContextFactory to create a Context implementation. The naming service provider supplies the URLContextFactory and the corresponding Context implementation. The JNDI InitialContext object delegates every naming method to the provider's context implementation.

Hashtable properties=new HashTable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,FACTORY_CLS_NAME);
InitialContext ic=new InitialContext(properties);
Object appConfig=Ic.lookup("java:comp/env/appConfig");

Implementing the Java Naming Service - Server
Data Structure
A naming context refers to a set of name-object mappings. In a hierarchical naming system, a name in a context can map to a child context. At the server, the name-object mappings (bindings) can be stored in a Map data structure:


public class Store {
Map bindings=new HashMap();
public Binding get(String name)
{ // get value from data }
public void set(String name, Binding b)
{ // put value into data }
}

A binding instance contains the triple: the name, object to be stored, and the object class name.

Naming Server Interface
The naming server is a remote interface that defines server methods that correspond to every method on the javax.naming.Context interface:


public interface NamingServer extends Remote, Serializable
{
public void bind(Name name, Object object, String className)
throws NamingException,RemoteException;
public Object lookup(Name name)
throws NamingException,RemoteException;
// Other methods corresponding to methods in javax.naming.Context
}

Name
Each naming method in the Context class has two overloaded forms - one that takes in a String name and another that takes in a Name object. For instance, the Context class has a method lookup(String name) and a method lookup(String name). The Name object is a collection of various sub-components in a name.

In the case of a name 'comp/env/appOwner,' the Name object represents the list '{comp,env,appOwner}.' It's the NameParser of a naming system that parses the string representation of a name and creates the corresponding Name object. The parsing rules depend on the naming system and its naming parser implementation.

There are two different kinds of structured names that implement the Name interface- CompositeName and CompoundName. A CompositeName represents a name that can span multiple naming systems.

For instance, the name 'cn=aper-son, ou=ust_india,o=ust/profile/publications' represents two parts: an LDAP name 'cn=aperson,ou=ust_india,o=ust' and a File System name 'profile/publications.' When this name is used in a lookup operation, the method resolves through the LDAP naming service and passes into the File System naming service to find the target object.

In a CompositeName the various components are separated by a forward slash ('/') character. Hence, the given name has three components '{(cn=aperson,ou=ust_india,o= ust),(profile),(publications)}'. The first component represents a name (compound name) in the LDAP naming system and the last two components represent a name in the File System naming system. The mechanism by which a JNDI system resolves through multiple naming systems is known as a naming federation. We will explore this technique later in this article.

A CompoundName is a name in a single naming system. It is a convenience class that can parse a string representation of a name to create a Name object. Its parser can be customized by providing the required parsing syntax:

Properties syntax=new Properties();
Syntax.setProperty("jndi.syntax.direction","left_to_right");
Syntax.setProperty("jndi.syntax.ignorecase","false");
Syntax.setProperty("jndi.syntax.separator","/"); CompoundName name=new CompoundName("comp/env/appOwner",syntax);

This parses the string representation using the syntax provided to create an equivalent CompoundName in the current naming system. A typical naming parser can also use the CompoundName to parse a string name to a Name object:


Public class NamingParser implements NameParser{
Public Name parse(String name) throws NamingException {
return new CompoundName(name,syntax);
}
}

Naming Server Implementation
The naming server implementation uses the 'Store' data structure to store name-object mappings:


public class NamingServerImpl implements NamingServer{

private NameParser nameParser=new NamingParser();
private Store rootContextStore=new Store();

public void bind(Name name, Object object,String className)
throws NamingException,RemoteException{
Store contextStore= rootContextStore;
Object[] storeNamePair=new Object[]{rootContextStore,name};
iterateNameComponents(storeNamePair);
contextStore=storeNamePair[0];
name=storeNamePair[1];
// bind the name to the given object
contextStore.put(name,new Binding(name,className,obj,true));
// check for NameAlreadyBoundException
}

private void iterateNameComponents(Object[] storeNamePair) {

Store contextStore=storeNamePair[0];
Name name= storeNamePair[1];

// iterate the name till the last component
while (name.size() > 1){
Object obj=contextStore.get(name.get(0));
// name.get(0) gets the first component in the given name
// obj should be a context
If !(obj instanceof Store)
{ // throw NotContextException }
contextStore=(Store)obj;
// resolve the remaining name in new context
// get remaining name component
name=name.getSuffix(1);
}

storeNamePair[0]=contextStore;
storeNamePair[1]=name;
}

// other methods
}
About Kishore Kumar
Kishore Kumar works as a Java architect at U.S. Technology (www.ustri.com). He specializes in J2EE applications.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE