The JNDI service is a set of APIs published by Sun that interface to a directory to locate named objects. APIs allow Java programs to store and lookup objects using multiple naming services in a standard manner. The naming service may be either LDAP, a file system, or a RMI registry. Each naming service has a corresponding provider implementation that can be used with JNDI. The ability for JNDI to plug-in any implementation for any naming service (or span across naming services with a federated naming service) easily provides another level of programming abstraction. This level of abstraction allows Java code using JNDI to be portable against any naming service. For example, no code changes should be needed by the Java client code to run against an RMI registry or an LDAP server.
Any J2EE compliant application server, such as the WebLogic Server, has a JNDI subsystem. The JNDI subsystem is used in an Application Server as a directory for such objects as resource managers and Enterprise JavaBeans (EJBs). Objects managed by the WebLogic container have default environments for getting the JNDI InitialContext loaded when they use the default InitialContext() constructor. For a Collaboration using a WebLogic EJB Object Type Definition (OTD) to find the home interface of an EJB, the JNDI properties must be configured and associated with the OTD. However, for other external clients, accessing the WebLogic naming service requires a Java client program that sets up the appropriate JNDI environment when creating the JNDI Initial Context.
There are essentially two environments that have to be configured, Context.PROVIDER_URL and Context.INITIAL_CONTEXT_FACTORY.
For WebLogic, the Context.PROVIDER_URL environment is
t3://<wlserverhost>:<port>/
where,
<wlserverhost> is the hostname on which the WebLogic Server instance is running
<port> is the port at which the Webserver instance is listening for connections
For example,
t3://localhost:7001/
The initial context factory class for the WebLogic JNDI is weblogic.jndi.WLInitialContextFactory. This class should be supplied to the Context.INITIAL_CONTEXT_FACTORY environment property when constructing the initial context. The overloaded InitialContext(Map) constructor must be used in this case.
The following code is an example of creating an initial context to WebLogic JNDI from a stand-alone client:
HashMap env = new HashMap();
env.put (Context.PROVIDER_URL, "t3://localhost:7001/");
env.put (Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
Context initContext = new InitialContext (env);
...
Once an initial context is created, sub-contexts can be created, objects can be bound, and objects can be retrieved using the initial context. For example the following segment of code retrieves a Topic object:
Topic topic
=(Topic)initContext.lookup("sbyn.inTopicToSunMicrosystemsTopic");
...
Here's an example of how to bind a Sun Microsystems Queue object:
Queue queue = null;
try {
queue = new STCQueue("inQueueToSunMicrosystemsQueue");
initContext.bind ("sbyn.ToSunMicrosystemsQueue", queue);
}
catch (NameAlreadyBoundException ex)
{
try
{
if (queue != null)
initContext.rebind ("sbyn.ToSunMicrosystemsQueue", queue);
}
catch (Exception ex)
{
throw ex;
}
}
For information, see To View the JNDI Tree Structure in Configuring WebLogic for Asynchronous Communications.