
JNDI Contexts
• The JNDI Context object is your key to start working with JNDI.
• The javax.naming.Context interface provides methods to bind, and lookup services in JNDI.
• Before we start working with JNDI we need an initial context.
- The initial context is a base or root of the JNDI tree.
- When working with BEA we will get the initial context using a JNDI Initial Context factory.
• The javax.naming.InitialContext class provided three constructor options.
- A default constructor can be called.
- You can specify a properties object and pass it into the constructor.
- You can work with a Hashtable object instead of a properties object.
• Whether you use a properties object or a Hashtable you need to define several standard JNDI
properties.
• The properties used have mappings to constants in the javax.naming.Context interface.
- The property java.naming.factory.initial is javax.naming.Context.INITIAL_CONTEXT_FACTORY.
weblogic.jndi.WLInitialContextFactory
- The property java.naming.factory.object is javax.naming.Context.OBJECT_FACTORIES.
A colon separated list of Factory objects
- The property java.naming.provider.url is javax.naming.Context.PROVIDER_URL.
t3://localhost:7001
- The property java.naming.security.principal is javax.naming.Context.SECURITY_PRINCIPAL.
The username for the login, in a simplified case “system”
- The property java.naming.security.credentials is javax.naming.Context.SECURITY_CREDENTIALS.
The password for the security principal.
• Establishing an initial context using a Hashtable is straightforward.
public static Context getInitialContext() throws NamingException{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
“weblogic.jndi.WLInitialContextFactory");
env.put(Context.PROVIDER_URL, "t3://localhost:7001");
env.put(Context.SECURITY_PRINCIPAL, "system");
env.put(Context.SECURITY_CREDENTIALS, "secret");
return new InitialContext(env);
}
- In the above code we created a Hashtable to use to create the initial Context.
- This code puts four of the properties into the Hashtable.
- The Context constants are the keys and the values for each of these keys are used to create the initial
context.
• The same effect could be achieved using the default constructor if the system properties were set before
running this code.
java -Djava.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
-Djava.naming.provider.url=t3://localhost:7001
-Djava.naming.security.principal=system
-Djava.naming.security.credentials=secret TheJavaClassYouWantToRun
Exploring a Context
• The InitialContext object represents the base or root of the JNDI tree.
- The InitialContext is a type of context.
- Contexts can be used to explore the tree and get information about bindings.
- You can call listBindings or list to explore all objects bound to a context.
• Recall that we obtained the initial context earlier; in the following code we will use the initial context to
display the JNDI Tree.
- WebLogic uses a period separated JNDI tree.
- We use list instead of listBindings, so the Enumeration that is returned contains NameClassPairs.
- This recursive code adds more spaces to represent depth of the JNDI tree.
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NameClassPair;
import javax.naming.NamingException;
public class NamingTest{
private static Context initialContext;
public static void main(String[] args){
try{
initialContext = getInitialContext(); //this method looked at earlier
printBindings("","",""); //print all from the root.
}catch(NamingException e){
System.out.println(e);
}
}
public static void printBindings(String subContext, String context, String depth)
throws NamingException{
String fullContext = subContext;
if(context.length()!= 0){
fullContext = context+"."+ subContext;
}
try{
NamingEnumeration enum = initialContext.list(fullContext);
while(enum.hasMore()){
NameClassPair pair = (NameClassPair)enum.next();
String name = pair.getName();
System.out.println(depth + name);
printBindings( name, fullContext, depth+" ");
}
}catch(Exception e){}
}
//…
}
• When run, with WLServer running and the getInitialContext method in place this code produces the
following result.
- The program was run on a default implementation.
javax
transaction
UserTransaction
TransactionManager
weblogic
management
adminhome
server
home
localhome
myserver
common
T3Services
rmi
transaction
UserTransaction
coordinators
myserver
resources
TransactionManager
servlet
internal
roidlookup_default
Weblogic
j2ee
Deployer
- This simple program demonstrates one use of a context.
- To actually view the JNDI tree it would be much simpler to use WLServer console.
JNDI Contexts
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials