Monday, March 05, 2007

Calling the Operating System (OS) from BPEL

Sometimes you would like to call a script on the operating system to perform some complex tasks that are not possible to do in BPEL. This is often seen in Unix environments, when you want to call some scripts to run batch program for example. The following code is an example that shows how to call the OS. Note that the 'command' contains the full path to the executable script.

<bpelx:exec name="CallOS"
language="Java" version="1.4">
<![CDATA[
Element inVarElem = (Element)getVariableData
( "inputVariable"
, "payload"
, "/CallOSRequest/client:command");
String inVar = inVarElem.getNodeValue();
String command = inVar;
String result;
InputStreamReader isr;
InputStream isForProcess;
String isResult;

try
{
Runtime myProcessRuntime = Runtime.getRuntime();
Process process = myProcessRuntime.exec(command);
int exitVal = process.waitFor();
isResult = new String();

if (exitVal > 0)
{
isForProcess = process.getErrorStream();
}
else
{
isForProcess = process.getInputStream();
}
isr = new InputStreamReader(isForProcess);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null)
{
isResult = isResult + line;
}

addAuditTrailEntry("Input is: " + command);
addAuditTrailEntry("Output is: " + isResult);
result = "" + exitVal;
addAuditTrailEntry("Exit status: " + result);

setVariableData(
"outputVariable"
, "payload"
, "/client:CallOSResponse/client:result"
, result);

System.out.println("Executed (Input: " + inVar + ")");
}
catch (Exception ex)
{
ex.printStackTrace();
}]]>
</bpelx:exec>

Post a Comment