Confirmer EJB
J2EE Developer's Guide Samples on iPlanet Application Server

Updated June 8, 2001

This sample is based on the Confirmer EJB sample included in Sun's J2EE Developer's Guide, version 1.2.1 . The following instructions describe how to assemble and deploy the same EJB to iPlanet Application Server and how to access the EJB from both a simple Java client application using RMI/IIOP as well as from a Servlet client using HTTP. Although the client application and EJB offers minimal functionality, this sample should provide you with a basic understanding of using the JavaMail API from within an EJB. For a more complex example of using the JavaMail within a J2EE application, see the Java Pet Store application setup instructions.

Setting Up the Environment

Getting Started includes a checklist of prerequisites for deploying sample applications to iPlanet.

J2EE Developer's Guide Samples Notes describes special considerations when deploying these sample.

Enable RMI/IIOP Support in the application server. (Performed automatically in a Test Drive installation).

Registering JavaMail Session in the application server.

Deploying and Running the Application

Select one of two approaches to deploying the application:

Command Line-based Deployment describes how to manually register the application in iPlanet using a Command Line Interface (CLI). This is the fastest means of deploying the application.

GUI-based Deployment describes how to use the iPlanet Deployment Tool to import and deploy the application. It also addresses assembling the application from scratch using the Deployment Tool.

Deploying the Client Application describes how to deploy the Java client application.

Running the Application describes how to start the application, navigate through it and how to troubleshoot in the event of problems.

Further Exploration

Compiling and Assembling the Application describes how to use command line tools to recompile the sources and rebuild the WAR and EJB modules and the overall EAR file.


Registering JavaMail Session

Since the sample EJB uses JavaMail to send an e-mail message, you must first register a mail session in the JNDI name space of the application server.

Registering the Mail Resource

1. Go to the install_dir/ias/ias-samples/j2eeguide/confirmer/src/schema/ directory and edit the mail-resource.xml file. Change the host, mail server user name and reply to address fields to suit your environment.

<ias-resource>
<resource>
<jndi-name>mail/j2eeguide/confirmer/MailSession</jndi-name>
<mail>
<host>mailhost.iplanet.com</host>
<name>mailuser</name>
<address>mailuser@iplanet.com</address>
</mail>
</resource>
</ias-resource>

2. Use the iasdeploy CLI, register the mail resource:

iasdeploy regdatasource mail-resource.xml

Mail Resource's Relationship to Source Code

If you browse the application's ejb-jar.xml and ias-ejb-jar.xml files, you will see the linkage between the logical mail session name and the actual JNDI name used to register mail server information.

Java Source Code in

The Confirmer EJB performs a lookup for the mail session resource using logical name of the resource. The value of this reference is identified in the EJB's ejb-jar.xml file.

Session session = (Session) initial.lookup("java:comp/env/TheMailSession");

ejb-jar.xml File

The logical name mail/TheMailSession is mapped to an absolute name of the resource in the ias-ejb-jar.xml file.

 <resource-ref>



     <description>description</description>



     <res-ref-name>mail/TheMailSession</res-ref-name>



     <res-type>javax.mail.Session</res-type>



     <res-auth>Container</res-auth>



 </resource-ref> 

ias-ejb-jar.xml File

The absolute name as specified in the ejb-jar.xml file matches the absolute name specified in the resource's XML registration file.

 <resource-ref>



     <res-ref-name>mail/TheMailSession</res-ref-name>



     <jndi-name>mail/j2eeguide/confirmer/MailSession</jndi-name>



 </resource-ref> 

Mail Session Resource File

<ias-resource>



   <resource>



     <jndi-name>mail/j2eeguide/confirmer/MailSession</jndi-name>



     <mail>



       <host>mailhost.iplanet.com</host>



       <name>mailuser</name>



       <address>mailuser@iplanet.com</address>



     </mail>



   </resource>



</ias-resource> 

Command Line-based Deployment

You have a choice of  deploying and registering the application through either command line utilities or the iPlanet Application Server Deployment Tool GUI. Since a complete EAR file is supplied, the fastest means of setting up the application is to use the command line utilities as described in this section. If you would like to experience either deploying through a GUI tool or assembling an application from scratch, then follow instructions in GUI-based Deployment.

The pre-built j2eeguide-confirmer.ear file is a Enterprise ARchive file that contains the Web Archive (WAR) and EJB JAR files for the Confirmer application. Within each of these JAR files reside the XML deployment descriptor files, application class files, JSPs and other content required by the application.

Deploying the pre-built j2eeguide-confirmer.ear file is simple.

1. Go to the root of the Confirmer sample directory:

install_dir/ias/ias-samples/j2eeguide/confirmer/

2. Execute iasdeploy to deploy application to the local application server instance:

iasdeploy deployapp j2eeguide-confirmer.ear

Refer to the Getting Started section for more information on using the iasdeploy command.

The deployment process involves the following operations:

Now you're ready to exercise the application by Deploying the Client Application.


GUI-Based Deployment

The Deployment Tool provides an easy-to-use means of assembling J2EE applications and deploying applications to iPlanet Application Server. For most cases, use of Deployment Tool is recommended over the approach of manually creating XML-based deployment descriptors and manually assembling J2EE modules and application JAR files.

Two approaches to using the Deployment Tool are described:

Import Pre-existing EAR File to quickly deploy the application to iPlanet using the Deployment Tool. None of the application assembly steps are covered by this section. On average, this approach will take 10 minutes.

or

Assemble the Application to learn how to use Deployment Tool to assemble the application from scratch and to deploy it to the application server. On average, this approach will take from 15-25 minutes.

Import Pre-existing EAR File

Since a pre-built Enterprise ARchive (EAR) file for the Confirmer application is shipped with the application server, you can use the Deployment Tool to quickly read in the .ear file, modify deployment settings and deploy to the application server.Open the pre-existing sample EAR file.

1. Launch the Deployment Tool.

2. Open the EAR file

Proceed to Deploying the Application


Assemble Confirmer Application

These instructions describe how to assemble the Confirmer application from scratch using only the compiled source files and the Deployment Tool.
  1. Compile Application Sources
  2. Create EJB JAR Module
  3. Create WAR Module
  4. Assemble EAR File
  5. Deploy Application

1. Compile Application Sources

To compile the application sources, simply execute "build compile" under the application's src/ directory. See the section Compiling and Assembling the Application for more information on recompiling the application using the supplied build facility.

2. Create EJB JAR Module

Insert EJB Classes

1. Create assembly location:
Create the new directory install_dir\ias\ias-samples\j2eeguide\confirmer\assemble\ outside the Deployment Tool. You will assemble the application into JAR files and save them to this directory.
2. Launch the Deployment Tool:
UNIX: Execute install_dir/ias/bin/deploytool&

Windows: Programs->iPlanet Application Server 6.0->iAS Deployment Tool

3. Create j2eeguide-confirmerEjb.jar:

4. Now insert all class files needed in the j2eeguide-confirmerEjb.jar file:

Modify Deployment Descriptor Settings for Confirmer EJB

1. Modify Descriptor for Confirmer EJB

2. Begin Modifying the Confirmer EJB Descriptor

3. Close the Descriptor for the EJB

Now we can close the EJB descriptor by clicking on the X in the upper right hand corner of the descriptor window. Click on Yes when asked to save the changes.

Save and close j2eeguide-confirmerEjb.jar

Now that we've assembled the EJB JAR module, we're ready to move on to assembling the Web Application aRchive (WAR) file.

3. Create WAR Module

Identify the Content for WAR File

1. Create j2eeguide-confirmer.war:

2. Now insert all class files needed in the j2eeguide-confirmer.war file:

Modify Web Application Descriptor

Now we need to make several changes to settings at the WAR module level. To do so, select either the WAR file path if in File View or the WAR module name if in Component View. Right click and select Edit Descriptor.

As described for the j2eeguide-confirmerEjb.jar, in the following paragraphs, the title gives the tab name, where you have to enter the data given below.

1. General Tab

Name: j2eeguide-confirmer
Distributable checked
Session Timeout (seconds): -1 (take server default)

2. iAS Tab

Distributed Session checked
Timeout (seconds): -1
Timeout Type Since last access
Data Synchronization Distributed

3. References Tab

EJB References:

Reference Bean Type Bean Home Interface Bean Remote Interface Linked to Bean
MyConfirmer Session j2eeguide.confirmer.ConfirmerHome j2eeguide.confirmer.Confirmer MyConfirmer

Save and close j2eeguide-confirmer.war

Now that we've assembled both J2EE modules (EJB JAR and WAR), we're ready to move on to assembling the modules into an EAR file.

4. Assemble EAR File

1. Create j2eeguide-confirmer.ear:

2. Add j2eeguide-confirmerEjb.jar and j2eeguide-confirmer.war Files:

3. Set Context Root for Web Application

Next, we need to set the context root of the web application. This value will appear in URLs that access web application components. For example, in the URL http://localhost/NASApp/j2eeguide-confirmer/ConfirmerServlet the "j2eeguide-confirmer" value is the context root of the web application.

4. Save j2eeguide-confirmer.ear:

Now that the EAR file has been assembled, the next step is to deploy the application to an application server instance.

5. Deploy the Application

Now you're about to deploy the application by transferring the EAR file to a target iPlanet Application Server. First, you will identify the target server. After the EAR file is deployed to the server, post processing is performed. The post processing step involves running iasdeploy against the transferred j2eeguide-confirmer.ear file. You will see the output from iasdeploy fed back to the deployment window. After this step, you will be able to run the application. No server restart is required.

Although you could perform the deployment steps manually by transferring the EAR file to the remote application server and executing j2eeappreg to register the EAR file, Deployment Tool provides a point and click means of deploying applications to remote iPlanet Application Servers.

If you followed the assembly instructions, then j2eeguide-confirmer.ear is opened in Deployment Tool. If you opened the pre-existing EAR file, you are working with install_dir\ias\ias-samples\j2eeguide\confirmer\j2eeguide-confirmer.ear. If you are following the "Create EAR File from Scratch" instructions, you are modifying install_dir\ias\ias-sample\j2eeguide\confirmer\assemble\j2eeguide-confirmer.ear.

If you entered all deployment information manually, you can skip the following steps 1 to 3, because you have entered this information already.

1. Select the j2eeguide-confirmer.ear file under the J2EE Application window.

2. Select File -> Deploy

3. If you have not already registered a target application server, do so now by clicking on the Register button. Enter the host name, administrative port number and username/password for the target application server. Otherwise, select a pre-registered target server.

Note: You may authorize additional users to deploy applications to an iAS server by using the iAS Administrative Console. See the Security settings tab in the application server's Administrative Console.

4. Click on Overwrite modules in case you are repeating the deployment step.

5. Click on Deploy to start the deployment process.

6. Now the file transfer and application registration begins. See the Deploy tab for the status of the deployment. Once the status of the deployment changes to Success, proceed to the next section.

Verifying Registration

After you've deployed the application, you're ready to use several iAS administrative tools to verify that the application is available. The first
  1. Start the application server Administrative Tool
  2. UNIX:
    install_dir/ias/bin/ksvradmin
    Windows:
    Start->Programs->iPlanet Application Server->iAS Administration Tool
  3. Connect to your application server instance by selecting File->New->Server. Click on Localhost to specify the default connection settings. Enter the application server's administrator's password and click on OK.
  4. Select the server name (default name of iAS1) and select the Application button in the top right hand corner of the window to see the applications registered in this instance of the application server.
  5. You should see several folders for the j2eeguide-confirmer J2EE application. The j2eeguide-confirmer folder with the world icon represents the web application module while thej2eeguide-confirmerEjb folder represents the EJB module packaged as part of the sample application.
To see more details associated with the sample application, you can use the lower level tool kregedit:
  1. Execute kregedit to start the iPlanet Application Server Registry Editor GUI.
  2. Navigate to the SOFTWARE/iPlanet/Application Server/6.0/ portion of the tree.
  3. Browse the J2EE-Application/ tree and look for the j2eeguide-confirmer application.
  4. Expand the j2eeguide-confirmer folder and explore this portion of the directory tree.
  5. Now open the J2EE-Module/ tree and look for the j2eeguide-confirmer web application module entry and the j2eeguide-confirmerEjb EJB module entry.
  6. Expand each of these folders and explore their contents.
  7. Once you find the GUID associated with each EJB in the sample application, you can navigate through the SOFTWARE/iPlanet/Application Server/ClassDef/ folders to find out more details on each EJB. Expanding the matching GUID folders under the ClassDef folder will show you the detailed settings associated with each EJB. Many of these settings appear in the XML deployment descriptors for EJBs.

Deploy the Client Application

Although in practice, you will most likely script the following client application deployment steps, the manual steps are described below in order to provide you with a sense of deploying an RMI/IIOP client. Since manual steps such as setting CLASSPATH variables and such are prone to error, please review each step carefully.

As you review the sample build facility described in Compiling and Assembling the Application, you'll note the install_client target that automates steps 1, 5 and 6. Since the other steps are one time configuration changes, using the "build install_client" command will prove to be a great time saver for local client redeployments.

  1. Compile Client Source File
  2. Setup Client Directories
  3. Install RMI/IIOP Client Support
  4. Set the Client Environment Variables
  5. Copy Client Application Main Class
  6. Copy EJB Interfaces and iPlanet-Specific Stubs
  7. Check Your Client Setup

1. Compile Client Source File

To compile the application sources, simply execute "build compile" under the application's src/ directory. See the section Compiling and Assembling the Application for more information on recompiling the application using the supplied build facility.

2. Setup Client Directories

Establish a directory on your client system to support the client application class files, the EJB home and remote interface classes as well as the RMI/IIOP stub classes. The specific directory names shown below are not required and may be changed to suit your environment.

For example, on UNIX:

/opt/rmi-client/

For example, on Windows:

d:\rmi-client\

3. Install RMI/IIOP Client Support

If you are planning to run the Java application client on a machine other than the one on which the application server resides, refer to the RMI/IIOP chapter of the Developer's Guide for details on the configuration of the remote client environment.

4. Set the Client Environment Variables

Ensure that the PATH and CLASSPATH variables reference the appropriate JDK, javax.jar and iasclient.jar files.

Testing RMI/IIOP Client Access to EJBs on Same System
If you are experimenting with RMI/IIOP client access using a client that is on the same machine as the application server, you can take a shortcut to setting up the PATH and CLASSPATH variables. Simply reference the existing, pre-installed copies of the javax.jar, iasclient.jar and usr/java/bin/. For example, to test RMI/IIOP access locally, set the CLASSPATH variable as follows (since the Windows System PATH environment variable already contains usr/java/bin/ of the bundled JDK, there is no need to set this again on Windows):

Windows:

set CLASSPATH=d:\iplanet\ias6\ias\lib\java\javax.jar;d:\iplanet\ias6\ias\classes\java\iasclient.jar;%CLASSPATH%

You could set the Windows System CLASSPATH to avoid having to manually set the variable.

UNIX:

export CLASSPATH=/usr/iplanet/ias6/ias/classes/java/javax.jar:/usr/iplanet/ias6/ias/classes/java/iasclient.jar:$CLASSPATH

On UNIX, you will also need to modify the PATH to include the bundled JDK directory:

export PATH=/usr/iplanet/ias6/ias/usr/java/bin:$PATH

Testing RMI/IIOP Client Access to EJBs from a Remote System
If you are using a remote client system, then follow these steps to establish the appropriate PATH and CLASSPATH settings.

UNIX

Set your PATH environment variable to include the appropriate Java 2 bin/ directory:

export PATH=<Java 2 install>/usr/java/bin:$PATH

Set your CLASSPATH to include the standard Java Extension classes and the iPlanet RMI/IIOP client support JAR:

export CLASSPATH=/opt/rmi-client/iasclient.jar:/opt/rmi-client/javax.jar:$CLASSPATH

Doublecheck the CLASSPATH to ensure that it is set correctly (your CLASSPATH may vary from the one shown below).

echo $CLASSPATH
/opt/rmi-client/iasclient.jar:/opt/rmi-client/javax.jar:

Windows

Set your PATH environment variable to include the appropriate Java 2 bin/ directory:

set PATH=<Java 2 install>\usr\java\bin;%PATH%

Set your CLASSPATH to include the standard Java Extension classes (javax.jar) and the iPlanet client support JAR (iasclient.jar).

set CLASSPATH=d:\rmi-client\javax.jar;d:\rmi-client\iasclient.jar;%CLASSPATH%

5. Copy Client Application Main Class

Copy the ConfirmerClient.class file from the compile location to the same package directory structure under your client application directory. (Typically, the client application would most likely belong to a different Java package, but for simplicity reasons, we've packaged the client and EJB in the same Java package).

Copy ConfirmerClient.class to...

UNIX: /opt/rmi-client/j2eeguide/confirmer/

Windows: d:\rmi-client\j2eeguide\confirmer\
 

Set your client's CLASSPATH to include the Confirmer client directory path:
UNIX:

export CLASSPATH=/opt/rmi-client:$CLASSPATH

Windows:

set CLASSPATH=d:\rmi-client;%CLASSPATH%

This setting will enable both the application main class as well as the Confirmer-specific EJB interfaces and stubs to be accessed at run time.

6. Copy EJB Interfaces and Application Server-Specific Stubs

From the APPS/j2eeguide-confirmer/j2eeguide-confirmerEjb/ deployment area on the application server, copy the following files to the same package location (j2eeguide/confirmer/) under the client application directory.

Confirmer EJB Interface Classes:

EJB-Specific iPlanet Client Stubs:

7. Check Your Client Setup

Go to the rmi-client/ directory and navigate to the j2eeguide/confirmer/ directory. You should see the following files:

Running the Sample

Review Configuration

Before running the sample, first double check your work thus far by ensuring that:

From the rmi-client/ directory, execute the following command:
java j2eeguide.confirmer.ConfirmerClient <hostname> <port> <mail address>

For example,

java j2eeguide.confirmer.ConfirmerClient hamlet 9010

Where:

<hostname> is the host name of the application server.

<port> is the IIOP port number of the RMI-IIOP bridge or cxs process on the application server.

<mail address> is e-mail address of the intended recipient.

The application should emit the following messages (these are the currency values returned from the ConfirmerEJB):

Message sent succesfully to: <mail address>

If you've configured the target mail server settings correctly in the mail resource XML file, then you should see the message arrive in the recipient's mailbox shortly after running the client.

Execute Servlet Client

Go to the following URL to exercise the servlet front end:

http://<hostname>/NASApp/j2eeguide-confirmer/ConfirmerServlet

Enter the recipient's e-mail address and click on submit to send the message. If you've configured the target mail server settings correctly in the mail resource XML file, then you should see the message arrive in the recipient's mailbox shortly after running the web application.

Troubleshooting

If you encounter problems when running the application, review the General Troubleshooting section to learn how to view logs files. Also review the Troubleshooting section of the RMI/IIOP chapter of the Developer's Guide.


Compiling and Assembling the Application

To easily recompile, assemble and deploy the application, see the Sample Application Build Facility document for details on using a build facility to quickly perform these tasks.

For example, to rebuild the entire application from scratch, follow these steps:

1. Compile and Assemble EJB JAR Components and Web Application

Execute "build" under j2eeguide/confirmer/src/

The default target core will be executed to rebuild the EJB JAR and WAR files as well as the web application's EAR file.

2. Redeploy Application

Execute "build deploy" under j2eeguide/confirmer/src/

3. Install the Java Client Application

Execute "build install_client" under j2eeguide/confirmer/src/

Executing this target will install the class files required to run the client application under:

install_dir/ias/rmi-client/

4. Restart Application Server

An application server restart will be necessary if you've modified either deployment descriptors or EJBs. For servlet and/or JSP modification, no restart is is necessary.

To clean the application build and assembly areas, execute "build clean".

Copyright (c) 2001 Sun Microsystems, Inc. All rights reserved.