Table of Contents
This chapter describes an example of EJB client programming and how to configure the InitialContext object.
An EJB client is any kind of application that accesses and invokes business logic of EJB components deployed inside the EJB engine. EJB clients thus include Servlets, applets, other EJBs, standalone Java programs, etc.
This chapter covers the basic information needed to implement and execute an EJB client through a standalone Java program.
For information on client container applications, refer to the "JEUS Application Client Guide".
The following example illustrates the implementation of a stand-alone Java application client that looks up an EJB with the export name HelloApp and invokes a business method, sayHello(), on it.
[Example 11.1] <<HelloClient.java>>
package hello;
import java.rmi.*;
import javax.ejb.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
public class HelloClient {
private void run()
{
try {
//Receiving JEUS InitialContext
InitialContext ctx = new InitialContext();
//Receiving EJB
Hello hello = (Hello)ctx.lookup(“HelloApp”);
//Calling Business Method
String s = hello.sayHello();
}
catch (Exception e)
{ // Exception handling. }
}
public static void main(String args[])
{
HelloClient hclient = new HelloClient();
hclient.run();
}
}
In the previous example, note the bold line InitialContext ctx = new InitialContext() where the InitialContext object is acquired. In order for this to execute correctly, you need to set up the attributes of the InitialContext. For more information on Initial Context settings, refer to "11.3. Configuring InitialContext".
InitialContext is an important component required for using the EJBs bound to the JNDI namespace.
It is used to look up all objects specified in the JNDI namespace. EJB uses it to search for actual bean references of the EJB engine. In order to obtain a JEUS InitialContext instance, you need to specify at least one of the five naming context attributes before attempting to acquire an InitialContext, so that your client application will be able to communicate with the JEUS Naming Server (JNSServer).
The following describes how to set up the JNDI InitialContext object either through the use of JVM "–D" properties (recommended approach) or through a hashtable in the EJB client code. It will also show how to actually invoke the EJB client.
The following are five supported naming context environment attributes that must be configured prior to acquiring a JEUS InitialContext object.
Sets UserInfo that is used during the authentication process of the JEUS naming service.
If no UserInfo is registered with the execution thread, the default value, guest, will be used. If it is specified later, the UserInfo with a username defined in the JEUS security realm will be used.
You can also configure some additional properties, apart from the previous settings. Those additional properties are described in JEUS Server Guide. "Chapter 4. JNDI Naming Server".
The attributes in the previous sub-section may be configured by using the "-D" switch of the JVM interpreter. The switch should be appended to the "java" command that is used to start the EJB client application.
The following shows how to configure each naming attribute using the "-D" switch.
The following example shows how to use the "-D" option to configure the naming context of an EJB client during its invocation. The example shows how a user named "user1" with the password "password1" is used for an EJB client that looks up EJB stubs through a JEUS naming server with the IP address, 192.168.10.10:
$ java -classpath %JEUS_HOME%/lib/client/jclient.jar -Djava.naming.factory.initial=jeus.jndi.JNSContextFactory -Djava.naming.factory.url.pkgs=jeus.jndi.jns.url -Djava.naming.provider.url=192.168.10.10 -Djava.naming.security.principal=user1 -Djava.naming.security.credentials=password1 HelloClient
A client library is needed, if the version of an EJB deployed on JEUS is 2.x. This library is usually generated when compiling EJB using an appcompiler.
If a class FTP server is used, clients may automatically download and use the necessary classes. None is required when using EJB version 3.0 or later.
Another possibility for setting the naming context attributes is to use a hashtable and populate it with naming attribute data directly inside the client code.
The following code fragment shows how this can be done.
Context
ctx = null; Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,jeus.jndi.JNSContextFactory”);
ht.put(Context.URL_PKG_PREFIXES, “jeus.jndi.jns.url”);
ht.put(Context.PROVIDER_URL, “192.168.10.10”);
ht.put(Context.SECURITY_PRINCIPAL, “user1”);
ht.put(Context.SECURITY_CREDENTIALS, “password1”);
try { ctx = new InitialContext(ht); . . .
In the example, note how the hashtable is passed as the InitialContext object constructor argument.
The following example shows how to execute EJB client when a naming attribute is configured inside the client class.
$ java -classpath $JEUS_HOME/lib/client/jclient.jar HelloClient
Using a hashtable is not recommended because the values are hard coded.