Wednesday, October 1, 2008

JNDI access outside OC4J container

How to connect Standalone Application with DataSource / EJB Beans / any Container Resources ?.
Java Naming and Directory Interface (JNDI)

An application client helps to connect to container resources by referring resources in config xmls. We really not interested in doing all this magic. We know the resource(DataSource/Beans/JMS QUeue/topic) type and the location where it is located.

A JNDI(Java Naming and Directory Interface) name is the reference name specified in container to refer the resource from local and remote.

A J2EE application client is a standalone program launched from the command line or desktop, and typically accesses Enterprise JavaBean programs running on the J2EE application server. In Application client, we have to bundle an application such a way that to access the beans. Please refer for more info J2EE Application Client.

Again this is hectic way of accessing the resources from Application container, then what will I do ?.

yeah, we have the answer for this. Using RMI protocol, we can access any of the Beans/DataSource/Topic/Queue from server.

The following four properties and requesting object executables(classes) in your class path is enough to communicate with server.

java.naming.factory.initial=oracle.j2ee.rmi.RMIInitialContextFactory
java.naming.provider.url=ormi://localhost:23791/
java.naming.security.principal=oc4jadmin
java.naming.security.credentials=welcome1

The above mentioned properties will vary based on what kind of Application server, we are using. Here, I have taken Oracle 10.1.3 Application server OC4J to explain.

we can specify these properties in either of three approaches

1. Programmatically including these attributes

In this Approach, we have to always pass a Hashtable to create a Initialcontext. That HashTable has to store all these properties and values. Most of the time this approach is not suggested, because of the reason we have to modify these values(hard coded values) or we have to pick from external environment and given to Hash table

Hashtable env = new Hashtable();
env.put("java.naming.factory.initial","oracle.j2ee.rmi.RMIInitialContextFactory");
env.put(java.naming.provider.url","ormi://localhost:23791/");
env.put(java.naming.security.principal","oc4jadmin");
env.put(java.naming.security.credentials","welcome1");

2. Specifying in Java Command

Other way is directly specifying in Java command using -D option.

java -Djava.naming.factory.initial=oracle.j2ee.rmi.RMIInitialContextFactory \
-Djava.naming.provider.url=ormi://localhost:23791/ \
-Djava.naming.security.principal=oc4jadmin \
-Djava.naming.security.credentials=welcome1 \ <javaprogram>

3. Specifying in jndi.properties file

Best way of segregating configuration and application is specify these configuration details separately in a proprty file and name it as jndi.properties. This file containing folder has to be included in classpath.

java -cp .;%cp% yourapplication

Note:
This %cp% should load all of your application required classes containing jar files.


I am not able to access some of my beans, what to do ?.

Check java.naming.provider.url is correct or not. If you are accessing beans/topic/queue then you have to specify EJB Application also in this URL. Example, If your EJB Application name is Bank then specify like java.naming.provider.url=ormi://localhost:23791/Bank

Refer Specifying Environment Properties


Weblogic

Weblogic uses t3 protocol to make RMI connection:

java.naming.provider.url=t3://<hostname>:7001
java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
java.naming.security.principal=weblogic
java.naming.security.credentials=weblogic1

Reactions:

1 comments:

Post a Comment

Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More