Example: Getting an initial context by setting the provider URL property
In general, Java™ Naming
and Directory Interface (JNDI) clients should assume the correct environment
is already configured so there is no need to explicitly set property
values and pass them to the InitialContext
constructor.
However, a JNDI client might need to access a namespace other than
the one identified in its environment. In this case, it is necessary
to explicitly set the java.naming.provider.url (provider URL) property
used by the InitialContext
constructor. A provider
URL contains bootstrap server information that the initial context
factory can use to obtain an initial context. Any property values
passed in directly to the InitialContext
constructor
take precedence over settings of those same properties found elsewhere
in the environment.
- A CORBA object URL
- An IIOP URL
CORBA object URLs are more flexible than IIOP URLs and are the recommended URL format to use. CORBA object URLs are part of the OMG CosNaming Interoperable Naming Specification. A corbaname URL, for example, can include initial context and lookup name information and can be used as a lookup name without the need to explicitly obtain another initial context. The IIOP URLs are the legacy JNDI format, but are still supported by the WebSphere Application Server initial context factory.
The following examples illustrate the use of these URLs.
Using a CORBA object URL
This example shows a CORBA object URL.
...
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
...
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
env.put(Context.PROVIDER_URL, "corbaloc:iiop:myhost.mycompany.com:2809");
Context initialContext = new InitialContext(env);
...
Using a CORBA object URL from a non-WebSphere Application Server JNDI implementation
Initial context factories for CosNaming JNDI plug-in implementations
other than the WebSphere Application Server initial
context factory most likely obtain an initial context using the object
key, NameService
. When you use such a context factory
to obtain an initial context from a WebSphere Application Server name server, the
initial context is the cell root context. Because system artifacts
such as EJB homes associated with a server are bound under the server's
server root context, names used in JNDI operations must be qualified.
If you want to use relative names, ensure your initial context is
the server root context under which the target object is bound. In
order to make the server root context the initial context, specify
a corbaloc provider URL with an object key of NameServiceServerRoot
.
This example shows a CORBA object type URL from a non-WebSphere Application Server JNDI implementation.
This example assumes full CORBA object URL support by the non-WebSphere Application Server JNDI implementation.
The object key of NameServiceServerRoot
is specified
so that the initial context will be the specified server's server
root context.
...
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
...
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.somecompany.naming.TheirInitialContextFactory");
env.put(Context.PROVIDER_URL,
"corbaname:iiop:myhost.mycompany.com:9810/NameServiceServerRoot");
Context initialContext = new InitialContext(env);
...
If qualified names are used, you can use the default key of NameService.
Using an IIOP URL
The IIOP type of URL is a legacy format which is not as flexible as CORBA object URLs. However, URLs of this type are still supported. The following example shows an IIOP type URL as the provider URL.
...
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
...
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
env.put(Context.PROVIDER_URL, "iiop://myhost.mycompany.com:2809");
Context initialContext = new InitialContext(env);
...