Chapter 11. EJB Client

Table of Contents

11.1. Overview
11.2. Programming a Client to Access EJB
11.3. Configuring InitialContext
11.3.1. Configuring Naming Attributes Using JVM Properties
11.3.2. Configuring Naming Attributes Using a Hashtable

This chapter describes an example of EJB client programming and how to configure the InitialContext object.

11.1. Overview

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.

Note

For information about client container applications, refer to the "JEUS Application Client Guide".

11.2. Programming a Client to Access EJB

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 about Initial Context settings, refer to "11.3. Configuring InitialContext".


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.

  • INITIAL_CONTEXT_FACTORY (required)

    Must be set to the value, "JNSContextFactory". The is the factory class name used to build the naming context.

  • URL_PKG_PREFIXES

    Set to jeus.jndi.jns.url. Used to look up objects by using the URL scheme of the JEUS InitialContext.

  • PROVIDER_URL

    Contains the IP address of the JEUS naming server (JNS server) to be used. Default is 127.0.0.1 (“localhost”).

  • SECURITY_PRINCIPAL

    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.

  • SECURITY_CREDENTIALS

    Sets a user password that will be used during the authentication process. If the authentication fails, previous UserInfo will be maintained in its previous state. If it succeeds, the new UserInfo will be used.

Note

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".

11.3.1. Configuring Naming Attributes Using JVM Properties

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.

  • INITIAL_CONTEXT_FACTORY (required)

    -Djava.naming.factory.initial=jeus.jndi.JNSContextFactory
  • URL_PKG_PREFIXES

    -Djava.naming.factory.url.pkgs=jeus.jndi.jns.url
  • PROVIDER_URL

    -Djava.naming.provider.url=<host_address>
  • SECURITY_PRINCIPAL

    -Djava.naming.security.principal=<username>
  • SECURITY_CREDENTIALS

    -Djava.naming.security.credentials=<password>

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

Note

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.

11.3.2. Configuring Naming Attributes Using a Hashtable

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

Note

Using a hashtable is not recommended because the values are hard coded.