Host On-Demand J2EE Connector

Host On-Demand J2EE Connector


Sample Application: Separate Logon/logoff Classes

This sample application uses a separate class to handle Login/Logout process and performs actual work in the main class. This is an example of a TN5250 application running in an unmanaged environment. Similar samples are located in the directory ...\samples\tn5250\.

/**
 *  Sample client Logon/Logoff class. It should use provided
 *  Connection to perform logon and logoff to the EIS system.
 */

// Import necessary packages and classes needed
import javax.resource.*;
import javax.resource.cci.*;
import javax.resource.spi.*;
import javax.resource.spi.security.*;
import java.util.*;
import com.ibm.connector2.cci.*;
import com.ibm.connector2.hod.*;
import com.ibm.eNetwork.ECL.ECLScreenDesc;

public class J2HODScreenVMLogonLogoff implements com.ibm.connector2.cci.LogonLogoff
{
  private static final String TRACE_HEADER = "J2HODScreenVMLogonLogoff";
  static final String TRACE_ENTRY  = "-) [";
  static final String TRACE_EXIT   = "(- [";
  static final String TRACE_ERROR  = " ***ERROR*** ";
  static final String TRACE_STATUS = " + ";
  static final String TRACE_INPUT_ONLY   = " *** Expect to see Input screen only ***";
  static final String TRACE_INPUT_OUTPUT = " *** Expect to see both Input and Output screens ***";
  static final String TRACE_OUTPUT_ONLY  = " *** Expect to see Output screen only ***";

  public J2HODScreenVMLogonLogoff()
  {
    super();
  }

  // Handles Logging-in process
  public void logon(Connection connection, javax.security.auth.Subject subject) throws javax.resource.ResourceException
  {
    System.out.println(TRACE_ENTRY + TRACE_HEADER + ".logon()]");
    J2HODConnection j2hodconn = null;
    try {
      j2hodconn = (J2HODConnection)connection;
    }
    catch (java.lang.Throwable exception) {
      throw new javax.resource.ResourceException("Invoked using invalid Connection specified.");
    }

    String userName = null;
    String password = null;

    Iterator it = subject.getPublicCredentials().iterator();
    PasswordCredential pc = null;
    while(it.hasNext())
    { // gets Password credential information
      Object o = it.next();
      try{
        pc = (PasswordCredential)o;
      }
      catch(ClassCastException exn1){}
    }

    if (pc != null)
    { // gets User Name and Password
      userName = pc.getUserName();
      password = new String(pc.getPassword());
    }

    // Create records for Input and Output
    // Input is null in this case since we'll be receiving information only at this time
    // thus having no input
    J2HODScreenRecord input = null;
    J2HODScreenRecord output = new J2HODScreenRecord(24, 80);

    // receive current screen and check for the login screen
    try {
      // Create an InteractionSpec and set properties of what to recognize in the
      // resulting output screen from the execution
      J2HODInteractionSpec interactionSpec = new J2HODInteractionSpec();
      interactionSpec.setInteractionVerb(new Integer(J2HODInteractionSpec.SYNC_RECEIVE));
      interactionSpec.setRecognizeRow(new Integer(1));
      interactionSpec.setRecognizeColumn(new Integer(36));
      interactionSpec.setRecognizeString("Sign On");
      interactionSpec.setScreenName("Logon screen");

      // Execute interaction
      System.out.println (TRACE_OUTPUT_ONLY);
      j2hodconn.execute(interactionSpec, input, output);

      // checks for output result
      output.checkBytes("Sign On", 1, 36, false, true);
    }
    catch (java.lang.Throwable exception) {
      throw new javax.resource.ResourceException("Logon exception");
    }

    // now we are currently at the login screen
    try {
      // enter username, password
      input = (J2HODScreenRecord)output.clone();
      input.setString(userName, 6, 53);
      input.setString(password, 7, 53);


      // For more complex description of output screen, a vector of ECLScreenDesc instances
      // can be used to describe the possible next output screen.
      // ( ** Refer to Host Access Client Library (HACL) document for more information on
      //   ECLScreenDesc class and other HACL classes. )
      // In this case after entering ID and password, "Main Menu" or "Messages" screen is expected.
      Vector v = new Vector();
      ECLScreenDesc screenDesc1 = new ECLScreenDesc();
      screenDesc1.AddOIAInhibitStatus(ECLScreenDesc.NOTINHIBITED);
      screenDesc1.AddStringInRect("AS/400 Main Menu", 1, 33, 1, 48, false);
      screenDesc1.SetName("Main Menu");
      v.add(screenDesc1);

      ECLScreenDesc screenDesc2 = new ECLScreenDesc();
      screenDesc2.AddOIAInhibitStatus(ECLScreenDesc.NOTINHIBITED);
      screenDesc2.AddStringInRect("Press Enter to continue.", 19, 2, 19, 25, false);
      screenDesc2.SetName("Message");
      v.add(screenDesc2);

      // Sets interaction specs with screen descriptor vector and key
      J2HODInteractionSpec interactionSpec = new J2HODInteractionSpec();
      interactionSpec.setScreenDescriptors(v);
      interactionSpec.setKeyName(J2HODInteractionSpec.ENTER);

      // Execute interaction and output should be either a "Main Menu" or a "Message" screen.
      System.out.println (TRACE_INPUT_OUTPUT);
      j2hodconn.execute(interactionSpec, input, output);

      // Check for which output screen:  "Main Menu" or "Message"
      boolean bOK = output.checkBytes("Press Enter to continue.", 19, 2, 19, 25, false, false);

      if (bOK == true)
      { // In "Message" screen, so need to press "ENTER" to get to "Main Menu"
        input = null;
        interactionSpec = new J2HODInteractionSpec();
        interactionSpec.setKeyName(J2HODInteractionSpec.ENTER);
        System.out.println (TRACE_OUTPUT_ONLY);
        j2hodconn.execute(interactionSpec, input, output);
      }
      // Verify that output screen is in "Main Menu" screen
      output.checkBytes("AS/400 Main Menu", 1, 33, 1, 48, false, true);
    }
    catch (java.lang.Throwable exception) {
      throw new javax.resource.ResourceException("Logon exception");
    }
    System.out.println(TRACE_ENTRY + TRACE_HEADER + ".logon()] with userName: " + userName);
  } // end logon method

  // Handles Logging off
  public void logoff(Connection connection)
  {
    System.out.println(TRACE_ENTRY + TRACE_HEADER + ".logoff()]");
    J2HODConnection j2hodconn = null;
    try {
      j2hodconn = (J2HODConnection)connection;
    }
    catch (java.lang.Throwable exception) {
      System.out.println("Invoked using invalid Connection specified.");
    }

    // Create records for Input and Output
    J2HODScreenRecord input = null;
    J2HODScreenRecord output = new J2HODScreenRecord(24, 80);

    try {
      // Receive current screen to check for "90. Sign off" menu availability
      J2HODInteractionSpec interactionSpec = new J2HODInteractionSpec();
      interactionSpec.setInteractionVerb(new Integer(J2HODInteractionSpec.SYNC_RECEIVE));

      // Execute interaction
      System.out.println (TRACE_OUTPUT_ONLY);
      j2hodconn.execute(interactionSpec, input, output);

      // Check to see if "90. Sign off" menu is available
      boolean bOK = output.checkBytes("90. Sign off", -1, -1, -1, -1, false, false);

      // send PF3 to exit the current screen until "90. Sign off"
      // menu is available in the new output screen
      while (!bOK)
      {
        input = null;
        interactionSpec = new J2HODInteractionSpec();
        interactionSpec.setKeyName(J2HODInteractionSpec.PF3);
        // Execute interaction
        System.out.println (TRACE_OUTPUT_ONLY);
        j2hodconn.execute(interactionSpec, input, output);
        bOK = output.checkBytes("90. Sign off", -1, -1, -1, -1, false, false);
      }
      // "90. Sign off" menu is now available

      // Take previous output as an input and create a new record for a new output
      input = output;
      output = new J2HODScreenRecord(24, 80);

      // Send logoff command.  No output checking is done here
      input.setString("90", 20, 7); //command
      interactionSpec = new J2HODInteractionSpec();
      interactionSpec.setInteractionVerb(new Integer(J2HODInteractionSpec.SYNC_SEND));
      interactionSpec.setScreenName("Logon screen"); //output screen name

      // Execute interaction
      System.out.println (TRACE_INPUT_OUTPUT);
      j2hodconn.execute(interactionSpec, input, output);
    }
    catch (java.lang.Throwable exception) {
      System.out.println("***ERROR*** Logoff exception");
    }
    System.out.println(TRACE_EXIT + TRACE_HEADER + ".logoff()]");
  }
}


/**
  * An implementation of the simple application client. It accesses
  * the J2EE Connector directly without providing any additional
  * Quality of Service.  It uses above logon/logoff class to handle logging on and logging off
  * from the EIS.
  */

// Import necessary packages and classes needed
import java.util.*;
import com.ibm.connector2.hod.*;
import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;
import com.ibm.eNetwork.ECL.*;
import com.ibm.eNetwork.ECL.screenreco.*;

public class J2HODConnectorSample2
{
  public static void main(String[] args)
  {
    try {
      // Create a ManagedConnectionFactory for Telnet5250 connection
      J2HOD5250ManagedConnectionFactory mcf = new J2HOD5250ManagedConnectionFactory();

      // Set properties of the ManagedConnectionFactory
      mcf.setTraceLevel(new Integer(J2HODBaseManagedConnectionFactory.RAS_TRACE_ENTRY_EXIT));
      mcf.setServerName("a_host_name");

      // Specify which class is handling Logon/Logoff process
      mcf.setLogonLogoffClassName("J2HODScreenVMLogonLogoff");

      // Set admin logWriter to System.err
      mcf.setLogWriter(new java.io.PrintWriter(System.err));

      // Create EIS specific connection factory
      J2HODConnectionFactory cf = (J2HODConnectionFactory)mcf.createConnectionFactory();


      // set up properties
      J2HODConnectionSpec connectionSpec = new J2HODConnectionSpec();
      connectionSpec.setUserName("user_ID");
      connectionSpec.setPassword("Password");

      // Create J2HODConnection - triggers to start the logon method
      J2HODConnection j2hodconn = (J2HODConnection)cf.getConnection(connectionSpec);
      System.out.println("getConnection successfull: " + j2hodconn.toString());

      // Create interaction
      J2HODInteraction interaction = (J2HODInteraction)j2hodconn.createInteraction();

      // Repeat the process of using input and output records, screenDesc, v, interaction,
      // and interactionSpec from ":Repeat begin" to ":Repeat end" to execute different
      // commands.  Resulting output record should be used as an input record for the
      // next command.

      // :Repeat begin
      // Create records for input and output
      J2HODScreenRecord r1 = new J2HODScreenRecord(24, 80);
      J2HODScreenRecord r2 = new J2HODScreenRecord(24, 80);
      Vector v = new Vector();
      ECLScreenDesc screenDesc = new ECLScreenDesc();

      // Place an input command in input record
      r1.setString("1", 20, 7);

      // Add screen descriptions to recognize from the output screen record
      screenDesc.Clear();
      screenDesc.AddOIAInhibitStatus(ECLScreenDesc.NOTINHIBITED);
      screenDesc.AddStringInRect("User Tasks", 1, 36, 1, 45, false);
      v.add(screenDesc);

      // Create interactionSpec and set its properties such as screen descriptors and key name
      J2HODInteractionSpec interactionSpec = new J2HODInteractionSpec();
      interactionSpec.setScreenDescriptors(v);
      interactionSpec.setKeyName(J2HODInteractionSpec.ENTER);

      // Execute interaction
      interaction.execute(interactionSpec, r1, r2);

      // Check the current output
      r2.checkBytes("User Tasks", 1, 36, 1, 45, false, true);

      // go back where ":Repeat begin" starts and go through a similar process to set the command
      // and screen descriptors then execute the interaction.

      // :Repeat end

      // When all the executions are done and ready to end follow the rest to end

      // Close interaction
      interaction.close();

      // Close J2HODConnection - triggers to start the logoff method
      j2hodconn.close();
    } catch (javax.resource.ResourceException exn2) {
        System.out.println("***ERROR*** Operation failed: " + exn2);
    } catch (Exception ex) {
        System.out.println("***ERROR*** Operation failed: " + ex);
    }
    System.exit(0);
  }
}

[ Top of Page | Previous Page | Next Page | Table of Contents ]