How to lookup an EJB and other Resources in WebSphere Application Server using a Oracle JDK client

Technote (FAQ)


Question

- What jars or code are needed to lookup an EJB or other resources in IBM WebSphere Application server using a Oracle Java™ client?

- Can I use the Oracle CNCtxFactory to lookup EJBs/Resources in WebSphere Application Server?

- How can I use the WsnIntialContextFactory in an Oracle client?

Answer



I. Overview and Considerations

There are a number of different combinations that one can use to lookup an EJB/Resources in WebSphere AppServer using an Oracle Java client. These methods will typically work for any Oracle Java client, even if it is running inside a third party application server. Each method will be outlined below with the considerations for each. Another consideration is what type of context factory will be used by the client as it will affect your code/JVM settings.


CNCtxFactory vs. WsnInitialContextFactory

The com.sun.jndi.cosnaming.CNCtxFactory is Oracle's initial context factory shipped with the Oracle JDK. WebSphere AppServer ships it's own initial context factory which extends the Oracle CNCtxFactory in the class com.ibm.websphere.naming.WsnInitialContextFactory. The WsnInitialContextFactory has additional features below that make writing a WebSphere AppServer client a bit easier:

  • Support for short name lookups like "ejb/myEjb" instead of the fully-qualified name "cell/nodes/myNode/servers/myServer/ejb/myEJB".
  • Ability to lookup non-CORBA objects in WebSphere AppServer's name space. This includes resources like JMS Queues and Connection Factories, JDBC resources, Strings, etc.
  • Automatic use of the IBM ORB and its plug-ins that provide additional functionality.

When deciding which initial context factory to use, it is important to know if you will be using a pure Oracle JDK client, or if you will have access to the WebSphere AppServer jars (AppClient or AppServer) on the system where your client runs. WebSphere does not support copying the AppClient or AppServer jars individually to other systems, so we have provided examples of the proper way to include the correct jars when running in the collocated scenario (see each scenario below for details).

In the case where you are running the client on a system where only the Oracle JDK is installed, you will need to use the CnCtxFactory. Starting at v6.1 and higher, WebSphere AppServer also ships thin client runtime jars under the <WAS_HOME>/runtimes directory which can be used when WebSphere AppServer or WebSphere Application Client are not installed on the client system.

WebSphere Thin Client Runtimes

The WebSphere Thin Client runtimes allow you to copy a small number of jars to your client system which will allow you to lookup an EJB or other resources using JNDI. The WebSphere Information Center has documentation about each runtime jar and what it can be used for:

v7 Client Runtimes (use the ORB thin client along with the type of resource client you are trying to access)
http://publib.boulder.ibm.com/infocenter/......./ccli_standaloneclient.html

v6.1 Client Runtimes (use the Admin Thin Client for EJBs)

http://publib.boulder.ibm.com/infocenter/.../v6r1/..../txml_adminclient.html

Note: v6.0 does not provide a runtime client capable of looking up EJBs, so one of the other methods under the Scenarios section should be followed.

II. Scenarios and Example Code

Oracle client with WebSphere Pluggable Application Client
For this type of client, you would have a existing Oracle JDK along with the WebSphere Pluggable Client installed, but would not have WebSphere AppServer on the same system. With this approach, you have all of the necessary WebSphere AppServer jars needed for client access included with the Pluggable client. You can also apply maintenance as it is released to the Pluggable client to match the WebSphere AppServer version.

Sample Code:

package com.test;

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import ejbs.HelloHome;
import ejbs.HelloBean;

public class PluggableClient {

public static void main(String[] args) {

try{
Hashtable env = new Hashtable();
env.put("org.omg.CORBA.ORBClass", "com.ibm.CORBA.iiop.ORB");
env.put("java.naming.factory.initial", "com.ibm.websphere.naming.WsnInitialContextFactory");
env.put("java.naming.provider.url", "corbaloc::myhost:2809");
Context ctx = new InitialContext(env);
Object obj = ctx.lookup("ejb/ejbs/HelloHome");
                HelloHome home = (HelloHome) PortableRemoteObject.narrow(obj, HelloHome.class);
            HelloBean bean = home.create();
            System.out.println("EJB Output -> " + bean.hello());
} catch (Exception e){
e.printStackTrace();
}

}

}


Java Command Line:
<SUN_JDK>/java/bin/java -Djavax.ext.dirs="<WAS_CLIENT>/java/jre/lib:<WAS_CLIENT>/java/jre/lib/ext:<WAS_CLIENT>/lib:<WAS_CLIENT>/plugins" -classpath "PluggableClient.jar:HelloEJBClient.jar" com.test.PluggableClient


Oracle client with WebSphere AppServer

In this scenario, your client would be running on the same box where WebSphere AppServer is installed. This client utilizes the jars from the AppServer instead of having to install the WebSphere Application Client. As the AppServer gets updated, the client would also get the latest version of the jars.

Sample Code:

package com.test;

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import ejbs.HelloHome;
import ejbs.HelloBean;

public class WASClient {

public static void main(String[] args) {

try{
Hashtable env = new Hashtable();
env.put("org.omg.CORBA.ORBClass", "com.ibm.CORBA.iiop.ORB");
env.put("java.naming.factory.initial", "com.ibm.websphere.naming.WsnInitialContextFactory");
env.put("java.naming.provider.url", "corbaloc::myhost:2809");
Context ctx = new InitialContext(env);
Object obj = ctx.lookup("ejb/ejbs/HelloHome");
                HelloHome home = (HelloHome) PortableRemoteObject.narrow(obj, HelloHome.class);
            HelloBean bean = home.create();
            System.out.println("EJB Output -> " + bean.hello());
} catch (Exception e){
e.printStackTrace();
}

}

}


Java Command Line:
<SUN_JDK>/java/bin/java -Djavax.ext.dirs="<WAS_HOME>/java/jre/lib:<WAS_HOME>/java/jre/lib/ext:<WAS_HOME>/lib:<WAS_HOME>/plugins" -classpath "WASClient.jar:HelloEJBClient.jar" com.test.WASClient


Oracle client without WebSphere installation

In the third scenario, there would be no WebSphere Application Server or AppClient code installed on the system where the Oracle client is running. In this case, we cannot use the WsnInitialContextFactory and would have to slightly modify the client code to work with Oracle's implementation of CNCtxFactory.

Sample Code:

package com.test;

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import ejbs.HelloHome;
import ejbs.HelloBean;

public class OracleClient {

public static void main(String[] args) {

try{
Hashtable env = new Hashtable();
env.put("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
env.put("java.naming.provider.url", "iiop:://myhost:2809");
Context ctx = new InitialContext(env);
Object obj = ctx.lookup("cell/nodes/Node01/servers/server1/ejb/ejbs/HelloHome");
                HelloHome home = (HelloHome) PortableRemoteObject.narrow(obj, HelloHome.class);
            HelloBean bean = home.create();
            System.out.println("EJB Output -> " + bean.hello());
} catch (Exception e){
e.printStackTrace();
}

}

}


Java Command Line:
<ORACLE_JDK>/java/bin/java -classpath "OracleClient.jar:HelloEJBClient.jar" com.test.OracleClient


Final Considerations

If you are using an Oracle Client with the WsnInitialContextFactory, and getting the following exception below, make sure you are including the orb.properties file which is originally located at <WAS_ROOT_INSTALL>/java/jre/lib to be present in the <ORACLE_JDK>/java/jre/lib directory.

javax.naming.NamingException: Failed to
initialize the ORB [Root exception is java.lang.ClassCastException:
com.sun.corba.se.impl.orb.ORBImpl]
at com.ibm.ws.naming.util.Helpers.getOrb(Helpers.java:314)
at com.ibm.ws.naming.util.WsnInitCtxFactory.getInitialContextInternal
(WsnInitCtxFactory.java:383)
at com.ibm.ws.naming.util.WsnInitCtx.getContext(WsnInitCtx.java:113)
at com.ibm.ws.naming.util.WsnInitCtx.getContextIfNull(WsnInitCtx.java:
428)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:144)
at javax.naming.InitialContext.lookup(InitialContext.java:351)

Other ClassCastExceptions can occur when looking up resources like JMS Queues and Connection Factories. In these cases, you must use the SIB Client Jars as documented in the Information Centers along with the Sun/WsnInitialContextFactory example:


http://publib.boulder.ibm.com/infocenter/....atform.doc/info/ae/ae/tjj_jmsthcli_dev.html

http://www.ibm.com/support/docview.wss?uid=swg24012804

Rate this page:

(0 users)Average rating

Document information


More support for:

WebSphere Application Server
JNDI/Naming

Software version:

6.0, 6.1, 7.0

Operating system(s):

AIX, HP-UX, Linux, Solaris, Windows

Reference #:

1382740

Modified date:

2009-04-23

Translate my page

Machine Translation

Content navigation