Middleware
Application Server
First Created: 01-Nov-2004
Last Updated: 16-Jan-2006
Author: Frances Zhao
This demo illustrates two ways of InitialContext creation in J2EE application clients, and the basic JNDI lookup in OC4J:
When an application client must look up a resource that is available in a J2EE server application, the client uses ApplicationClientInitialContextFactory in the com.evermind.server package to construct the initial context.
There are some environment properties that ApplicationClientInitialContextFactory needs. These can be specified in the client code, or can be specified in the jndi.properties file that is part of the application's JAR file that is included in the EAR file.
This demo requires that the following sofware components are installed and configured correctly:
For the purpose of this How-to, a simple session bean is created, to be looked up from the application client. This session bean implements a very simple Stack, which offers 3 external operations: push, pop, and display of the stack contents as a string. The implementation simply wraps around a java.util.Stack object. The code is under: %HOWTO_HOME%/src/ejb/bean.
The first way to create an InitialContext is to use the parameterized constructor InitialContext(Hashtable). In the client code of this How-to, we used:
Hashtable env = new Hashtable();
env.put("java.naming.factory.initial",
"com.evermind.server.ApplicationClientInitialContextFactory");
env.put("java.naming.provider.url","ormi://localhost/jndidemo");
env.put("java.naming.security.principal","SCOTT");
env.put("java.naming.security.credentials","TIGER");
Context initial = new InitialContext(env);
Alternatively, the initial context can be created based on a file called "jndi.properties" that is bundled with the J2EE application's EAR file. For this How-to, this file looks like:
java.naming.factory.initial=com.evermind.server.ApplicationClientInitialContextFactory
java.naming.provider.url=ormi://localhost/jndidemo
java.naming.security.principal=SCOTT
java.naming.security.credentials=TIGER
Note: In both ways of creating an InitialContext, if you are going to run this how-to within an OracleAS environment (as opposed to using a standalone OC4J), you need to modify the "java.naming.provider.url" to use the "opmn:ormi:" protocol prefix, instead of simply "ormi:". The modification should be for both jndi.properties and DemoClient.java. This allows a client to attempt a lookup without hard-coding the ORMI port information (by default 23791). OC4J JNDI will contact the OPMN process to determine the proper ORMI port to use for the OracleAS environment. For example, use "java.naming.provider.url=opmn:ormi://hostname/jndidemo" instead of "java.naming.provider.url=ormi://hostname/jndidemo".
Once the initial context is created, you can look up a desired object like the following. This is an EJB home object. The lookup name is specified in application-client.xml, and the reference would be retrieved from the application-local context (i.e., java:comp/env).
Object homeObject =
context.lookup("java:comp/env/SimpleStackBean");
Here is the application-client.xml part that defined the lookup name:
<ejb-ref>
<ejb-ref-name>SimpleStackBean</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<home>jndidemo.SimpleStackHome</home>
<remote>jndidemo.SimpleStack</remote>
</ejb-ref>
In this How-to, we have used the JNDI location "jndidemo/SimpleStackBean" for the stack bean, as defined in orion-ejb-jar.xml. Therefore, we also need an orion-application-client.xml file that defines the mapping from the above name "SimpleStackBean" to this real JNDI location. Like:
<orion-application-client>
<ejb-ref-mapping name="SimpleStackBean" location="jndidemo/SimpleStackBean" />
</orion-application-client>
The following instructions are for running this demonstration on a standalone instance of Oracle Containers for J2EE 10g (10.1.3).
Please check to make sure that the following properties are configured correctly in the ant-oracle.properties file located in the root of the sample's distribution (NOTE: Some of these properties will default to the values of corresponding environment variables as noted below. If you have these variables setup in your environment you may not have to alter the values in the file). If necessary, modify these variable to the proper values for you environment:
Stand Alone Installation: %ORACLE_HOME%/bin/oc4j start
Note that the oc4j command expects the JAVA_HOME environment variable to point to a full JDK installation.
OracleAS Managed Installation: %ORACLE_HOME%/opmn/bin/opmnctl startall
In the top-level %HOWTO_HOME% directory, type the command:
You should now have newly created servlet_demo.ear in your %HOWTO_HOME%/dist directory.
This command would also attempt to deploy the application if the build is successful. It will first test whether OC4J is running.
You can also deploy the application separately by using the following command. Make sure the %ORACLE_HOME% environment variable is defined. In the top-level %HOWTO_HOME% directory, type the command:
There is another target in the build.xml, called "run". Make sure the application is successfully built and deployed, and in the top-level %HOWTO_HOME% directory, simply type the command:
You should be able to see two successful InitialContext creations and lookups, followed by continuous dumps of the stack content as output.
In this document, you should have: