WebSphere Context Factory

The next listing shows an example of some client source code that uses the WebSphere context factory to locate the home for a HelloWorld bean:
      import org.omg.CORBA.ORB; 
      import java.io.*; 
      import javax.naming.*; 
      import examples.helloworld.*; 
      import java.util.*;  

      public class WASNamingClient {
         public static void main(String[] argv) {
            try {

      // Set the necessary properties
            Properties prop = new Properties();

      // These four are *fixed* values, you never need to change them.
   
        prop.put(Context.INITIAL_CONTEXT_FACTORY,
             "com.ibm.websphere.naming.WsnInitialContextFactory"); 

        prop.put("com.ibm.websphere.naming.namespaceroot","bootstraphostroot");
        prop.put("com.ibm.ws.naming.ldap.config","local");
        prop.put("com.ibm.ws.naming.implementation","WsnLdap"); 

      // These two depend on your server settings and should match your CICS region settings

        prop.put("com.ibm.ws.naming.ldap.containerdn","ibm-wsnTree=WASNaming,c=us");
        prop.put("com.ibm.ws.naming.ldap.noderootrdn",
				"ibm-wsnName=legacyroot,ibm-wsnName=PLEX2,ibm-wsnName=domainRoots");        

		 // Finally, instead of com.ibm.cics.ejs.nameserver, 
		 // set com.ibm.ws.naming.ldap.masterurl to your destination LDAP server

          prop.put("com.ibm.ws.naming.ldap.masterurl","ldap://wibble.hursley.ibm.com:389");

          InitialContext ctx = new InitialContext(prop);  
          org.omg.CORBA.Object obj = 
               (org.omg.CORBA.Object)ctx.lookup("samples/HelloWorld");

          HelloWorldHome hhome = 
               (HelloWorldHome)javax.rmi.PortableRemoteObject.narrow
               (obj,HelloWorldHome.class);

          System.out.println("HelloWorldHome successfully found!");
          HelloWorld hello = hhome.create();
          System.out.println(hello.sayHello());

    } catch (Exception e) {
      System.err.println("Exception whilst looking up and calling the HelloWorld bean:");
      e.printStackTrace();
     }
   }
 } 
As noted in the the comments, the first four properties are fixed, the remaining three match settings for your CICS region (Albeit the com.ibm.cics.ejs.nameserver property has become com.ibm.ws.naming.ldap.masterurl). However, the WebSphere Context Factory has dependencies on components of WebSphere so in order to run it from the command line you must run a script to set up your environment appropriately.

The script DFHWAS4Setup.bat is a command line script provided with CICS. It can be downloaded from the utils subdirectory in the HFS area where CICS is installed. It must be run on a system that has WebSphere installed, because it relies on the environment variable WAS_HOME being set to point to the location where WebSphere has been installed, for example c:\WebSphere\AppServer. When the the script has been run, you should extend your CLASSPATH further to include the necessary client side code for your Enterprise Bean. For the example above this is the HelloWorld.jar - then the code above can be compiled and executed. (The example code assume that the home is published in a CorbaServer whose JNDI Prefix is samples).

In CICS we set com.ibm.cics.ejs.nameserver = <hostname> but in this client program, we set com.ibm.ws.naming.ldap.masterurl = <hostname>. CICS understands the former, WebSphere understands the latter.