Example: Getting an initial context with CosNaming
In the WebSphere Application Server, an initial context is obtained from a bootstrap server. The address for the bootstrap server consists of a host and port. To get an initial context, know the host and port for the server that is used as the bootstrap server.
Obtaining an initial context consists of two basic steps:
- Obtain an ORB reference
- Invoke a method on the ORB to obtain the initial reference
These steps are now explained in more detail.
Obtaining an ORB reference
Pure CosNaming clients, that is clients that are not running in a server process, must create and initialize an ORB instance with which to obtain the initial context. CosNaming clients which run in server processes can obtain a reference to the server ORB with a JNDI lookup. The following examples illustrate how to create and initialize a client ORB and how to obtain a server ORB reference.
Creating a client ORB instance
To create an ORB instance, invoke the static method, org.omg.CORBA.ORB.init. The init method requires a property set to the name of the ORB class you want to instantiate. An ORB implementation with the class name com.ibm.CORBA.iiop.ORB is included with the WebSphere Application Server. The WAS ORB recognizes additional properties with which one can specify initial references.
The basic steps for creating an ORB are as follows:
- Create a Properties object.
- Set the ORB class property to WebSphere Application Server's ORB class.
- If the bootstrap server is INS-compliant, set the initial reference properties. If the bootstrap server is not INS-compliant (meaning, WebSphere Application Server v4.0.x or earlier), set bootstrap host and port for bootstrap server.
- Invoke ORB.init, passing in the Properties object.
Usage scenario
... import java.util.Properties; import org.omg.CORBA.ORB; ... Properties props = new Properties(); props.put("org.omg.CORBA.ORBClass", "com.ibm.CORBA.iiop.ORB"); props.put("com.ibm.CORBA.ORBInitRef.NameService", "corbaloc:iiop:myhost.mycompany.com:2809/NameService"); props.put("com.ibm.CORBA.ORBInitRef.NameServiceServerRoot", "corbaloc:iiop:myhost.mycompany.com:2809/NameServiceServerRoot"); ORB _orb = ORB.init((String[])null, props); ...Notice the initial reference definitions for NameService and NameServiceServerRoot. The initial context returned for NameService depends on the type of bootstrap server. The key NameServiceServerRoot is a key introduced in WAS V5. For more information on initial contexts, see the section Initial Contexts.
Note: The properties com.ibm.CORBA.BootstrapHost and com.ibm.CORBA.BootstrapPort are deprecated. They are needed, however, to connect to WAS appservers of V4.0.x or earlier. The default bootstrap host is the local host and the default port is 2809.
Obtaining a reference to the server ORB
CosNaming clients which run in a server process can obtain a reference to the server ORB with a JNDI lookup on a java: name, shown as follows:
Usage scenario
... import javax.naming.Context; import javax.naming.InitialContext; import org.omg.CORBA.ORB; ... Context initialContext = new InitialContext(); ORB orb = (ORB) initialContext.lookup("java:comp/ORB"); ...Using an ORB reference to get an initial naming reference
There are two basic ways to get an initial CosNaming context. Both ways involve an ORB method invocation. The first way is to invoke the resolve_initial_references method on the ORB with an initial reference key. For this call to work, the ORB must be initialized with an initial reference for that key. The other way is to invoke the string_to_object method on the ORB, passing in a CORBA object URL with the host and port of the bootstrap server. The following examples illustrate both approaches.
Invoking resolve_initial_references
Once an ORB reference is obtained, invoke the resolve_initial_references method on the ORB to obtain a reference to the initial context. The following code example invokes resolve_initial_reference on an ORB reference.
Usage scenario
... import org.omg.CORBA.ORB; import org.omg.CosNaming.NamingContextExt; import org.omg.CosNaming.NamingContextExtHelper; ... // Obtain ORB reference as shown in examples earlier in this section ... org.omg.CORBA.Object obj = _orb.resolve_initial_references("NameService"); NamingContextExt initCtx = NamingContextExtHelper.narrow(obj); ...Note that the key NameService is passed to the resolve_initial_references method. Other initial context keys are registered in WebSphere Application Servers. For example, NameServiceServerRoot can be used to obtain a reference to the server root context in the bootstrap name server. For more information on the initial contexts registered in server ORBs, see the section Initial Contexts.
Invoking string_to_object with a CORBA object URL
We can use an INS-compliant ORB to obtain an initial context even if the ORB is not initialized with any initial references or bootstrap properties, or if those property settings are for a different server than the name server from which you want to obtain the initial context. To obtain an initial context by explicitly specifying the bootstrap name server, invoke the string_to_object method on the ORB, passing in a CORBA object URL which contains the bootstrap server host and port.
The code in the example below invokes the string_to_object method on an existing ORB reference, passing in a CORBA object URL which identifies the desired initial context.
Usage scenario
... import org.omg.CORBA.ORB; import org.omg.CosNaming.NamingContextExt; import org.omg.CosNaming.NamingContextExtHelper; ... // Obtain ORB reference as shown in examples earlier in this section ... org.omg.CORBA.Object obj = orb.string_to_object("corbaloc:iiop:myhost.mycompany.com:2809/NameService"); NamingContextExt initCtx = NamingContextExtHelper.narrow(obj); ...Note that the key NameService is used in the corbaloc URL. Other initial context keys are registered in WAS appservers. For example, use NameServiceServerRoot to obtain a reference to the server root context in the bootstrap name server.
Using an existing ORB and invoking string_to_object with a CORBA object URL with multiple name server addresses to get an initial context
CORBA object URLs can contain more than one bootstrap server address. Use this feature when attempting to obtain an initial context from a server cluster. We can specify the bootstrap server addresses for all servers in the cluster in the URL. The operation will succeed 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 server address may be used to obtain the initial context even though the first bootstrap server in the list is available. An example of a corbaloc URL with multiple addresses follows.
Example
... import org.omg.CORBA.ORB; import org.omg.CosNaming.NamingContextExt; import org.omg.CosNaming.NamingContextExtHelper; ... // Assume orb is an existing ORB instance org.omg.CORBA.Object obj = orb.string_to_object( "corbaloc::myhost1:9810,:myhost1:9811,:myhost2:9810/NameService"); NamingContextExt initCtx = NamingContextExtHelper.narrow(obj); ...
See Also
Name space logical view
Related Tasks
Developing applications that use CosNaming (CORBA Naming interface)
Related reference
Initial context support