Developing applications with Oracle WLS - Oracle Weblogic JNDI lookup of resources

 

Oracle Weblogic JNDI lookup of resources

Looking up a Java EE resource from an application which is running on WLS can be achieved using the standard Java EE API. For example, here’s how you can lookup the Oracle data source we have defined in the data source chapter:

Context context = new InitialContext();

dataSource = (javax.sql.DataSource) context.lookup("jdbc/OracleDS");

Things are a bit different if we need to look up a server resource (e.g. an EJB) from a remote endpoint:

Remote resource lookup

Looking up a remote resource from WLS requires using WLS Initial Context classes; therefore it’s a part of your application which will need some refactoring if you are moving from or to Oracle WLS. Here’s how you can look up the Oracle data source from a remote client:

Hashtable<String, String> h = new Hashtable<String, String>();

h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");

h.put(Context.PROVIDER_URL, "t3://localhost:7001");

h.put(Context.SECURITY_PRINCIPAL, “username”);

h.put(Context.SECURITY_CREDENTIALS, “password”);

 

InitialContext context = new InitialContext(h);

dataSource = (javax.sql.DataSource) context.lookup("jdbc/OracleDS");

Remote EJB lookup

Looking up a remote EJB requires using a special JNDI pattern:

EJB MappedName#package.RemoteBeanName

So, for example, supposing you were to deploy the following Stateless EJB:

package com.sample;

@Stateless(mappedName=”HelloWorld”)

public class HelloWorldBean implements HelloWorld {

. . .

}

Then, on the client side you could use the following lookup JNDI String:

Context ctx = new InitialContext(env);

System.out.println(“Initial Context created”);

helloWorld = (HelloWorld) ctx.lookup(“HelloWorld#com.sample.HelloWorld”);

 

Francesco Google+