Example: Getting an initial context by setting the provider URL property
In general, 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 name space 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. You can use two different provider URL forms with theWAS initial context factory:
- A CORBA object URL (new for J2EE 1.3)
- 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 WAS initial context factory.
The following examples illustrate the use of these URLs.
- Use a CORBA object URL
- Use a CORBA object URL with multiple name server addresses
- Use a CORBA object URL from a non-WAS JNDI implementation
- Use an IIOP URL
Use 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); ...
Use a CORBA object URL with multiple name server addresses
CORBA object URLs can contain more than one bootstrap address. You can use this feature when attempting to obtain an initial context from a server cluster. You can specify the bootstrap addresses for all servers in the cluster in the URL. The operation succeeds if at least one of the servers is running, eliminating a single point of failure. There is no guarantee of any particular order in which the address list will be processed. For example, the second bootstrap address may be used to obtain the initial context even though the server at the first bootstrap address in the list is available.
Multiple-address provider URLs resolving to servers on non-z/OS systems cannot contain bootstrap addresses for node agent processes. The URLs should only contain the bootstrap addresses of members of the same cluster. Otherwise, incorrect behavior might occur. When resolving to servers running on the z/OS operating system, the URL can contain bootstrap addresses for node agent processes.
An example of a corbaloc URL with multiple addresses follows.
... 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"); // All of the servers in the provider URL below are members of // the same cluster. env.put(Context.PROVIDER_URL, "corbaloc::myhost1:9810,:myhost1:9811,:myhost2:9810"); Context initialContext = new InitialContext(env); ...
Use a CORBA object URL from a non-WAS JNDI implementation
Initial context factories for CosNaming JNDI plug-in implementations other than the WAS 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 WAS name server, the initial context is the cell root context. Since 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. 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-WAS JNDI implementation. This example assumes full CORBA object URL support by the non-WAS 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.
Use 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); ...
Related tasks
Developing applications that use JNDI
Related Reference
Initial context support
Example: Looking up an EJB home with JNDI
Lookup names support in deployment descriptors and thin clients
Reference topic