To access an enterprise resource such as a data source or JavaMail session in a distributed computing environment, you can use the JNDI API to locate that object so that you can use it in your code. To do this, you must first obtain the initial context that contains the resource (a Java object).
You can obtain an initial JNDI context in one of two ways:
Get an initial context using JNDI properties found in the current environment
The current environment includes the Java system properties and properties defined in properties files found in the classpath. This code snippet illustrates how to obtain the initial context from these properties:
import javax.naming.Context; import javax.naming.InitialContext; // ... class declaration, method declaration code here ... Context initialContext = new InitialContext();
Get an initial context by explicitly setting JNDI properties
In general, JNDI clients should assume that the correct environment is already configured. If this is the case, there is no need for you to explicitly set property values and pass them to the constructor of the InitialContext object. However, a JNDI client might need to access a namespace other than the one identified in its environment. In this event, you must explicitly set one or more properties used by the InitialContext constructor. Any property values you pass to the InitialContext constructor take precedence over settings of the equivalent properties found elsewhere in the client's environment.
You can use two different provider URL forms with WebSphere Application Server's initial context factory:
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.
Using 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 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 should only contain the bootstrap addresses of members of the same cluster. Otherwise, incorrect behavior may occur.
... 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); ...
Using a CORBA object URL from an 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. 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. 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); ...