06-19-2012 05:01 PM
06-22-2012 02:41 AM
Note: there will be only one instance of that Java class created for the serviceTask it is defined on. All process-instances share the same class instance that will be used to call execute(DelegateExecution). This means that the class must not use any member variables and must be thread-safe, since it can be executed simultaneously from different threads. This also influences the way Field injection is handled.
06-22-2012 08:19 AM
public class ShellServiceTask implements JavaDelegate {
final static Logger logger = Logger.getLogger(ShellServiceTask.class.getName());
protected Expression command;
protected Expression wait;
protected Expression arg1;
protected Expression arg2;
protected Expression arg3;
protected Expression arg4;
protected Expression arg5;
protected Expression outputVariable;
protected Expression errorCodeVariable;
protected Expression redirectError;
protected Expression cleanEnv;
protected Expression directory;
@Override
public void execute(DelegateExecution execution) throws Exception {
logger.info("In " + command.getValue(execution) + " ShellTask script with variables = " + execution.getVariables());
String commandStr = getStringFromField(command, execution);
String arg1Str = getStringFromField(arg1, execution);
String arg2Str = getStringFromField(arg2, execution);
String arg3Str = getStringFromField(arg3, execution);
String arg4Str = getStringFromField(arg4, execution);
String arg5Str = getStringFromField(arg5, execution);
String waitStr = getStringFromField(wait, execution);
String resultVariableStr = getStringFromField(outputVariable, execution);
String errorCodeVariableStr = getStringFromField(errorCodeVariable, execution);
String redirectErrorStr = getStringFromField(redirectError, execution);
String cleanEnvStr = getStringFromField(cleanEnv, execution);
String directoryStr = getStringFromField(directory, execution);
Boolean waitFlag = waitStr == null || waitStr.equals("true");
Boolean redirectErrorFlag = redirectErrorStr != null && redirectErrorStr.equals("true");
Boolean cleanEnvBoolean = cleanEnvStr != null && cleanEnvStr.equals("true");
// Build commands for shell command
List<String> argList = new ArrayList<String>();
argList.add(commandStr);
if (arg1Str != null)
argList.add(arg1Str);
if (arg2Str != null)
argList.add(arg2Str);
if (arg3Str != null)
argList.add(arg3Str);
if (arg4Str != null)
argList.add(arg4Str);
if (arg5Str != null)
argList.add(arg5Str);
// Start async thread
logger.info("Firing executor with processInstanceId = " + execution.getProcessInstanceId());
Thread executor = new Thread(new ShellTaskThread(argList, waitFlag, redirectErrorFlag, cleanEnvBoolean, directoryStr,
resultVariableStr, errorCodeVariableStr, execution.getProcessInstanceId()));
executor.start();
logger.info("Executor started…leaving workflow");
logger.info("Leaving " + command.getValue(execution) + " ShellTask script with variables = " + execution.getVariables());
}
public String toString() {
return "command="+command+", arg1="+arg1+", arg2="+arg2+", arg3="+arg3+", arg4="+arg4+", arg5="+arg5+
", wait="+wait+", outputVariable="+outputVariable+", errorCodeVariable="+errorCodeVariable;
}
protected String getStringFromField(Expression expression, DelegateExecution execution) {
if (expression != null) {
Object value = expression.getValue(execution);
if (value != null) {
return value.toString();
}
}
return null;
}
}
class ShellTaskThread implements Runnable {
final static Logger logger = Logger.getLogger(ShellTaskThread.class.getName());
List<String> command;
boolean waitFlag;
String resultVariableStr;
String errorCodeVariableStr;
boolean cleanEnv;
boolean redirectErrorFlag;
String directoryStr;
String processId;
public ShellTaskThread(List<String> command, boolean waitFlag, boolean redirectErrorFlag,
boolean cleanEnv, String directoryStr,
String resultVariableStr, String errorCodeVariableStr, String processId) {
this.command = command;
this.waitFlag = waitFlag;
this.resultVariableStr = resultVariableStr;
this.errorCodeVariableStr = errorCodeVariableStr;
this.redirectErrorFlag = redirectErrorFlag;
this.cleanEnv = cleanEnv;
this.directoryStr = directoryStr;
this.processId = processId;
}
@Override
public void run() {
logger.info("running async thread with command: " + command);
logger.info("other vars: resultVariableStr: " + resultVariableStr + " errorCodeVariableStr: " +
errorCodeVariableStr + " processId: " + processId);
// set activiti context
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
Execution execution = runtimeService.createExecutionQuery().processInstanceId(processId).singleResult();
logger.info("execution = " + execution.getId() + ": " + execution.getProcessInstanceId());
ProcessBuilder processBuilder = new ProcessBuilder(command);
try {
processBuilder.redirectErrorStream(redirectErrorFlag);
if (cleanEnv) {
Map<String, String> env = processBuilder.environment();
env.clear();
}
if (directoryStr != null && directoryStr.length() > 0)
processBuilder.directory(new File(directoryStr));
Process process = processBuilder.start();
if (waitFlag) {
try {
int errorCode = process.waitFor();
logger.info("errorCode = " + errorCode);
if (resultVariableStr != null) {
String result = convertStreamToStr(process.getInputStream());
runtimeService.setVariable(execution.getId(), resultVariableStr, result);
}
if (errorCodeVariableStr != null) {
runtimeService.setVariable(execution.getId(), errorCodeVariableStr, Integer.toString(errorCode));
}
} catch (InterruptedException e) {
if (resultVariableStr != null) {
runtimeService.setVariable(execution.getId(), resultVariableStr, e);
}
if (errorCodeVariableStr != null) {
runtimeService.setVariable(execution.getId(), errorCodeVariableStr, "1");
}
throw e;
}
}
} catch (Exception e) {
logger.info("Could not execute shell command " + command + " with error = " + e);
} finally {
logger.info("Leaving thread with process variables = " + runtimeService.getVariables(execution.getId()));
runtimeService.signal(execution.getId());
}
}
public static String convertStreamToStr(InputStream is) throws IOException {
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
return writer.toString();
} else {
return "";
}
}
}
06-22-2012 08:25 AM
06-28-2012 01:18 PM
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
Execution execution = runtimeService.createExecutionQuery().processInstanceId(processId).singleResult();
06-29-2012 01:32 AM
06-29-2012 09:04 AM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.