J2EE Sample Code

The following code sample illustrates how to use the J2EE connector inside the J2EE Session EJB application defined by the task file. This sample assumes that you have deployed a J2EE Session EJB application to your application server containing the task file with a connector JNDI name of "AccountQueryConnector".

This sample uses AccountQuery task file with the AccountLookupTask. This task uses an AccountNumber and returns the account Balance.

Create and deploy the Web application

From the MCS for Screens management console, create and deploy an application to your selected application server. You must use the same connector JNDI name used in the sample code name.

See How to Deploy and Manage J2EE Session EJBs and Connectors for information on setting up your J2EE configurations.

Creating the RarSample.war

To distribute a Web application, you must package it in a Web application archive (WAR). The WAR supports SunOne, WebLogic, WebSphere, and Oracle, with some minor differences that are noted below. Each file name is linked to its sample code.

Deploying the WAR

After you have deployed the Web application (WAR), it can be accessed through the following URL:

http://machine:port/RarSample/index.jsp

This application server
Uses this default port...
SunOne 81 (If IIS is already installed and port 80 is being used)
WebLogic 7001
WebSphere 9080
Oracle9i 7777

Support for Task Chaining

To support task chaining you must save a reference to the RarSample class instance. Then, open a connection and execute several tasks before closing the connection. If task chaining is not required, re-write the RarSample class, open, and execute, then close inside of the execute method.


Source code for RarSample.java

import com.attachmate.resource.cci.InteractionSpecImpl;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.resource.ResourceException;
import javax.resource.cci.ConnectionFactory;
import javax.resource.cci.Connection;
import javax.resource.cci.Interaction;
import javax.resource.cci.RecordFactory;
import javax.resource.cci.MappedRecord;

public class RarSample
{
    RecordFactory m_recordFactory = null;

public RarSample()
    {
    }
public void open() throws NamingException, ResourceException
    {
        // Get InitialContext to your application servers JNDI service.
        // See your application server's specific documentation for details
        // on how this is done. On most application servers this is sufficient.
        InitialContext initialContext = new InitialContext();
        
        // Get the ConnectionFactory for the J2C named "AccountQueryConnector".
        ConnectionFactory connectionFactory = (ConnectionFactory)
            initialContext.lookup("java:comp/env/eis/AccountQueryConnector");
        
        // Get the RecordFactory for the "AccountQueryConnector" J2C.
        m_recordFactory = connectionFactory.getRecordFactory();
        
        // Get a Connection for the "AccountQueryConnector" J2C.
        m_connection = connectionFactory.getConnection();
        
        // Get an Interaction to the Connector for "AccountQueryConnector" J2C.
        m_interaction = m_connection.createInteraction();
   }

    public String executeAccountLookupTask(String strAccountNumber) throws ResourceException
    {
        // Get the input and output Records for the function/task
       	// "AccountLookupTask" on the "AccountQueryConnector" J2C.
        // The input and output Records are accessed by name.
        // The name is composed of TASK/MODE where TASK is the input and
        // output Records are always MappedRecords.

       // AccountQuery/transactions/transaction{id='AccountLookupTask'}/input/schema/ScreenInputs
        MappedRecord inputRecord = m_recordFactory.createMappedRecord("AccountLookupTask/input");
        // AccountQuery/transactions/transaction{id='AccountLookupTask'}/output/schema/ScreenOutputs
        MappedRecord outputRecord = m_recordFactory.createMappedRecord("AccountLookupTask/output");
        
        // Set the values on the input Record.  
        // The input record is a MappedRecord with one element that is a string with 
      // the key "AccountNumber".
	  
	   // AccountQuery/transactions/transaction{id='AccountLookupTask'}/input/schema/ScreenInputs/AccountNumber
        inputRecord.put("AccountNumber", strAccountNumber);
 
       // Create on InteractionSpec for task "AccountLookupTask" in the "AccountQueryConnector" J2C.
        // This class can be found in the ra-client.jar,
        // see the Client Installer help for the location of this file.
        InteractionSpecImpl spec = new InteractionSpecImpl();
        // AccountQuery/transactions/transaction{id='AccountLookupTask'}
        spec.setFunctionName("AccountLookupTask"); 
        
        // Execute the "AccountLookupTask" task on the "AccountQueryConnector" J2C
        // given the input and output Records.
        m_interaction.execute(spec, inputRecord, outputRecord);
        
        // AccountQuery/transactions/transaction{id='AccountLookupTask'}/output/schema/ScreenOutputs/Balance
        return (String) outputRecord.get("Balance");
   }

    /*
        // Getting hierarchical values from the output Record (table data).
        // Access the MappedRecord with key "AccountDetails".
        // This Mapped record has one value that is an IndexedRecord with key "Row".
        // Row contains a sequence of MappedRecords with two string
        // values with keys "Date" and "Amount".

        // AccountQuery/transactions/transaction{id='AccountDetails'}/output/schema/ScreenOutputs/Details
        MappedRecord accountDetails = (MappedRecord) outputRecord.get("AccountDetails");
         // AccountQuery/transactions/transaction{id='AccountDetails'}/output/schema/ScreenOutputs/Details/Row
        IndexedRecord rows = (IndexedRecord) accountDetails.get("Row");
         // AccountQuery/transactions/transaction{id='AccountDetails'}/output/schema/ScreenOutputs/Details/Row[0]
        MappedRecord row0 = (MappedRecord) rows.get(0); 
         // AccountQuery/transactions/transaction{id='AccountDetails'}/output/schema/ScreenOutputs/Details/Row[0]/Date
        String date0 = (String) row0.get("Date"); 
        // AccountQuery/transactions/transaction{id='AccountDetails'}/output/schema/ScreenOutputs/Details/Row[0]/Amount
        String amount0 = (String) row0.get("Amount"); 
        // AccountQuery/transactions/transaction{id='AccountDetails'}/output/schema/ScreenOutputs/Details/Row[1]
        MappedRecord row1 = (MappedRecord) rows.get(1); 
        // AccountQuery/transactions/transaction{id='AccountDetails'}/output/schema/ScreenOutputs/Details/Row[1]/Date
        String date1 = (String) row1.get("Date");
       // AccountQuery/transactions/transaction{id='AccountDetails'}/output/schema/ScreenOutputs/Details/Row[1]/Amount
        String amount1 = (String) row1.get("Amount");
        // etc.
    */

    public void close() throws ResourceException
    {
        // Close the Interaction and Connection.
        m_interaction.close();
        m_connection.close();                

        // cleanup member variables
        m_recordFactory = null;
        m_connection = null;
        m_interaction = null;
    }
}

Source code for index.jsp

<%@page contentType="text/html"%>

<%@ page import="RarSample" %>

<%
    RarSample rsClass = null;
    String AccountNumber = request.getParameter("AccountNumber");
    String Balance = "";

    if (AccountNumber == null)
    {
        AccountNumber = "1000";
    }

    // get sample class out of the session object
    // Note: required for task chaining
    rsClass = (RarSample) session.getAttribute("RarSample");

    if ("Execute".equals(request.getParameter("Execute")))
    {
        if (rsClass == null)
        {
            // couldn't find sample class, create a new one
            rsClass = new RarSample();
            rsClass.open();

            // save sample class in the session object for future reference
            // Note: required for task chaining
            session.setAttribute("RarSample", rsClass);
        }

        // execute AccountLookupTask on account number for balance
        Balance = rsClass.executeAccountLookupTask(AccountNumber);
    }
    else if ("Remove".equals(request.getParameter("Remove")))
    {
        if (rsClass != null)
        {
            // close connection
            rsClass.close();
            // cleanup session object, etc.
            session.removeAttribute("RarSample");
            rsClass = null;
        }
    }

%>

<html>
<head>
 <title> Rar Sample </title>
</head>
<body>
<b> Rar Sample </b>

<FORM action='<%=request.getRequestURI()%>' method='POST'>

Account Number:
<INPUT type='TEXT' name='AccountNumber' size='10' value='<%=AccountNumber%>'>
<br>

Balance:
<b><%=Balance%></b>
<br>

<INPUT type='SUBMIT' name='Execute' value='Execute'>
<INPUT type='SUBMIT' name='Remove' value='Remove' <%= rsClass == null ? " disabled='true'" : ""%>>

</FORM>

</body>
</html>

Source code for sun-web.xml

Required when deploying to SunOne.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.
//DTD Sun ONE Application Server 7.0 Servlet 2.3//EN"" "http://www.sun.com/software/sunone/appserver/dtds/sun-web-app_2_3-0.dtd">
<sun-web-app>
	<session-config>
		<session-manager persistence-type="memory">
			<manager-properties/>
			<store-properties/>
		</session-manager>
		<session-properties/>
		<cookie-properties/>
	</session-config>
	<resource-ref>
		<res-ref-name>eis/AccountQueryConnector</res-ref-name>
		<jndi-name>AccountQueryConnector</jndi-name>
	</resource-ref>
	<cache max-entries="4096" timeout-in-seconds="30" enabled="false">
		<default-helper/>
	</cache>
	<jsp-config>
		<property name="classdebuginfo" value="true">
			<description>Enable debug info compilation in the generated servlet class</description>
		</property>
		<property name="mappedfile" value="true">
			<description>Maintain a one-to-one correspondence between static content and the generated servlet class' java code</description>
		</property>
	</jsp-config>
</sun-web-app>

Source code for web.xml

Required for all application servers.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<session-config>
    <session-timeout>
            30
    </session-timeout>
  </session-config>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
	</welcome-file-list>
	<resource-ref>
		<description>AccountQueryConnector</description>
		<res-ref-name>eis/AccountQueryConnector</res-ref-name>
		<res-type>javax.resource.cci.ConnectionFactory</res-type>
		<res-auth>Container</res-auth>
		<res-sharing-scope>Unshareable</res-sharing-scope>
	</resource-ref>
</web-app>

Source code for weblogic.xml

Required when deploying to WebLogic.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-web-app PUBLIC "-//BEA Systems, Inc.
//DTD Web Application 7.0//EN"
"http://www.bea.com/servers/wls700/dtd/weblogic700-web-jar.dtd">
	
<weblogic-web-app>
	<description>Rar Sample</description>
	<reference-descriptor>
		<resource-description>
			<res-ref-name>eis/AccountQueryConnector</res-ref-name>
			<jndi-name>AccountQueryConnector</jndi-name>
		</resource-description>
	</reference-descriptor>
	<session-descriptor>
		<session-param>
			<param-name>URLRewritingEnabled</param-name>
			<param-value>true</param-value>
		</session-param>
		<session-param>
			<param-name>IDLength</param-name>
			<param-value>52</param-value>
		</session-param>
		<session-param>
			<param-name>CookieComment</param-name>
			<param-value>WebLogic Server Session Tracking Cookie</param-value>
		</session-param>
		<session-param>
			<param-name>CookieMaxAgeSecs</param-name>
			<param-value>-1</param-value>
		</session-param>
		<session-param>
			<param-name>CookieName</param-name>
			<param-value>JSESSIONID</param-value>
		</session-param>
		<session-param>
			<param-name>InvalidationIntervalSecs</param-name>
			<param-value>60</param-value>
		</session-param>
		<session-param>
			<param-name>JDBCConnectionTimeoutSecs</param-name>
			<param-value>120</param-value>
		</session-param>
		<session-param>
			<param-name>PersistentStoreCookieName</param-name>
			<param-value>WLCOOKIE</param-value>
		</session-param>
		<session-param>
			<param-name>PersistentStoreDir</param-name>
			<param-value>session_db</param-value>
		</session-param>
		<session-param>
			<param-name>PersistentStoreType</param-name>
			<param-value>memory</param-value>
		</session-param>
		<session-param>
			<param-name>PersistentStoreTable</param-name>
			<param-value>wl_servlet_sessions</param-value>
		</session-param>
		<session-param>
			<param-name>CookiesEnabled</param-name>
			<param-value>true</param-value>
		</session-param>
		<session-param>
			<param-name>TrackingEnabled</param-name>
			<param-value>true</param-value>
		</session-param>
		<session-param>
			<param-name>TimeoutSecs</param-name>
			<param-value>3600</param-value>
		</session-param>
	</session-descriptor>
	<context-root>/RarSample</context-root>
</weblogic-web-app>

Source code for orion-web.xml

Required when deploying to Oracle.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE orion-web-app PUBLIC "-//Evermind//DTD Orion Web Application 2.3//EN" 
"http://xmlns.oracle.com/ias/dtds/orion-web.dtd">

<orion-web-app>
	<resource-ref-mapping location="AccountQueryConnector" name="eis/AccountQueryConnector"/>
</orion-web-app>

The ra-client.jar file

See Configuring Your Development Environment for information on the client installer and the location of the ra-client.jar file.

Related Topics
Bullet Using Tasks in Your Applications, Overview
Bullet Configuring Your Development Environment
Bullet Using J2EE Connector Architecture, Overview
  Attachmate