Example: Setting the provider URL property to select a different root context as the initial context
Each server contains its own server root context, and, when bootstrapping to a server, the server root is the default initial JNDI context. Most of the time, this default is the desired initial context, since system artifacts such as EJB homes are bound there. However, other root contexts exist, which can contain bindings of interest. It is possible to specify a provider URL to select other root contexts.
Examples for selecting other root contexts follow:
- Initial root contexts with a CORBA object URL
- Initial root contexts with the namespace root property
Select the initial root context with a CORBA object URL
There are several object keys registered with the bootstrap server we can use to select the root context for the initial context. To select a particular root context with a CORBA object URL object key, set the object key to the corresponding value. The default object key is NameService. Using JNDI yields the server root context. A table that lists the different root contexts and their corresponding object key follows:
Root Context CORBA Object URL Object Key Server Root NameServiceServerRoot Cell Persistent Root NameServiceCellPersistentRoot Cell Root NameServiceCellRoot Node Root NameServiceNodeRoot The following example shows the use of a corbaloc URL with the object key set to select the cell persistent root context as the initial context.
... 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/NameServiceCellPersistentRoot"); Context initialContext = new InitialContext(env); ...
Select the initial root context with the namespace root property
We can also select the initial root context by passing a namespace root property setting to the InitialContext constructor. Generally, the object key setting previously described is sufficient. Sometimes a property setting is preferable. For example, we can set the root context property on the Java invocation to make which server root is being used as the initial context transparent to the application. The default server root property setting is defaultroot, which yields the server root context.
Root Context Namespace Root Property Value Server Root bootstrapserverroot Cell Persistent Root cellpersistentroot Cell Root cellroot Node Root bootstrapnoderoot The initial context factory ignores the namespace root property if the provider URL contains an object key other than NameService.
The following example shows use of the namespace root property to select the cell persistent root context as the initial context. Note that available constants are used instead of hard-coding the property name and value.
... import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import com.ibm.websphere.naming.PROPS; ... 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"); env.put(PROPS.NAME_SPACE_ROOT, PROPS.NAME_SPACE_ROOT_CELL_PERSISTENT); Context initialContext = new InitialContext(env); ...
Developing applications that use JNDI Initial context support