428 IBM Express Runtime V2.1
The SampleCommands.java file
Example C-8 shows the source code of the SampleCommands class.
Example: C-8 Source of SampleCommands
/*
* Created on Mar 10, 2005
*
* This sample program demonstrates how to create a wrapper.
* Based on your knowledge of your application,
* use this code as an example to develop a custom wrapper.
*/
package com.ibm.flght400;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.ibm.as400.access.AS400;
import com.ibm.as400.access.CharConverter;
import com.ibm.as400.security.auth.AS400Credential;
import com.ibm.as400.security.auth.ProfileTokenCredential;
import com.ibm.jsdt.support.SupportOS400Base;
import com.ibm.jsdt.support.SupportOS400Helper;
/**
* This is a sample class, created as part of the Redbook example.
*/
public class SampleCommands {
private String ivCommandOutput = null;
private SupportOS400Base ivBase = null;
private SupportOS400Helper ivHelper = null;
public SampleCommands(SupportOS400Base base, SupportOS400Helper helper){
ivBase = base;
ivHelper = helper;
}
/**
* Run a QSH command.
* To run a CL command use the invokeCLCommand() method.
* The command and a success or failure message are logged.
* The output can also be written to the log. The output
* from the command can be gotten by calling getCommandOuput();
* @param command the command to run.
* @param boolean if the output of the command to should be logged as well.
* @param boolean if the output needs to be converted from EBCDIC to ASCII.
* @return the return code of the command
*/
public int invokeCommand(String command, boolean logOutput, boolean convertFromEBCDIC)
{
return invokeCommand(command, command, logOutput, convertFromEBCDIC);
}
/**
* Run a QSH command.
* To run a CL command use the invokeCLCommand() method.
* The logCommand and a success or failure message are logged.
Appendix C. Source code for Flght400 user programs and script files on OS/400 429
* The output can also be written to the log. The output
* from the command can be gotten by calling getCommandOuput();
* @param command the command to run.
* @param command to write to the log - this is useful if the command contains
* sensitive information(e.g. passwords) that should not be written to the log.
* @param boolean if the output of the command to should be logged as well.
* @param boolean if the output needs to be converted from EBCDIC to ASCII.
* @return the return code of the command
*/
public int invokeCommand(String command, String logCommand, boolean logOutput, boolean
convertFromEBCDIC)
{
int rc = -1;
ivCommandOutput = "";
try {
// log the command
ivHelper.logNewLine(ivBase);
ivBase.setMessage(ivBase.getResourceString(SampleNLSkeys.CMDINVOKED, logCommand));
ivHelper.log(ivBase);
Process p = Runtime.getRuntime().exec(command);
rc = p.waitFor();
// Log if the command passed or failed
if (rc == 0) {
ivBase.setMessage(ivBase.getResourceString(SampleNLSkeys.CMD_SUCCESS));
ivHelper.log(ivBase);
} else {
ivBase.setMessage(ivBase.getResourceString(SampleNLSkeys.CMD_FAIL,new
String[]{String.valueOf(rc)}));
ivHelper.log(ivBase);
logOutput = true;
}
ivHelper.logNewLine(ivBase);
ivCommandOutput = readCommandOutput( p.getInputStream(), convertFromEBCDIC);
ivCommandOutput = ivCommandOutput + readCommandOutput(p.getErrorStream(), convertFromEBCDIC);
if (logOutput == true) {
ivBase.setMessage(ivCommandOutput);
ivHelper.log(ivBase);
}
} catch (Exception e) {
ivBase.setMessage(ivBase.getResourceString(SampleNLSkeys.CMD_EXCEPTION, new
String[]{e.getMessage()}));
ivHelper.log(ivBase);
rc = -1;
}
return rc;
}
/**
* Run a QSH command.
* To run a CL command use the invokeCLCommand() method.
* The command and a success or failure message are logged.
* The output can also be written to the log. The output
* from the command can be gotten by calling getCommandOuput();
* @param command the command to run.
* @param boolean if the output of the command to should be logged as well.
* @return the return code of the command
*/
public int invokeCommand(String command)
{

Get IBM Express Runtime V2.1 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.