Using the JNDI InitialContext Class
by, 07-07-2012 at 05:10 PM (1523 Views)
Context interface is implemented by Class javax.naming.InitialContext and is considered as enterance point in a naming system. InitialContextobject shall be created first for using JNDI so that objects could be accessed. Set of properties are taken by the InitialContext constructor in a form like java.util.Hashtable and other subclasses, like a Properties object. In lookup e.g, InitialContext is created as following:
Java.naming.factory.initial is the fundamental property key that would be corresponding with context.INITIAL_CONTEXT_FACTORYconstant. This property’s value specifies the factory class names present in JNDI providers. This is factory class job to build the InitialContext which is suitable for services that will handover objects back. All required information is required to be given to the factory class so that InitialContext could be created, in other property value forms.Java Code:Properties props = new Properties(); props.put(Context.INITIAL_CONTEXT_FACTORY, "RefFSContextFactory"); props.put(Context.PROVIDER_URL, "file:///"); // Create the initial context from the properties we just created Context initialContext = new InitialContext(props);
Not much information is required in case of filesystem factory class, for example com.sun.jndi.fscontext.RefFSContxtFactory. However, many other factory classes present might be much more demanding. Let us say for example, LDAP service provider of Sun has factory class that needs the URL of server of LDAP as well as directory entry to access, authentication type and a user name/Password. Few properties are given here that might be used for creation of InitialContext along with LDAPs factory class.
Java Code:java.naming.factory.initial=LdapCtxFactory java.naming.provider.url=ldap://192.168.1.20/o=Planetary,c=US java.naming.security.authentication=simple java.naming.security.principal=cn=kris java.naming.security.credentials=secret