ShellProcessor supports templates.

This commit is contained in:
Michael Hierweck 2013-06-20 15:40:44 +02:00
parent 1634da7162
commit 1d8141e6b0
2 changed files with 58 additions and 30 deletions

View File

@ -18,31 +18,31 @@ public class CommandShell {
return execute(command, null);
}
public static String execute(String command, String stdInput) throws ShellException {
Process backend = null;
String callOutput = null;
public static String execute(String command, String shellInput) throws ShellException {
Process shell = null;
String shellOutput = null;
int exitCode = 0;
try {
String logCommand = command;
if (stdInput != null) {
logCommand += "<<EOF\n" + stdInput + "EOF";
if (shellInput != null) {
logCommand += "<<EOF\n" + shellInput + "EOF";
}
logCommand += "\n";
logger.log(Level.INFO, logCommand);
String[] cmdArray = { "/bin/bash", "-c", command };
backend = Runtime.getRuntime().exec(cmdArray);
if (stdInput != null) {
OutputStream stdInputStream = backend.getOutputStream();
PrintWriter stdInputWriter = new PrintWriter(stdInputStream);
stdInputWriter.print(stdInput);
stdInputWriter.close();
stdInputStream.close();
shell = Runtime.getRuntime().exec(cmdArray);
if (shellInput != null) {
OutputStream shellStdIn = shell.getOutputStream();
PrintWriter shellStdInWriter = new PrintWriter(shellStdIn);
shellStdInWriter.print(shellInput);
shellStdInWriter.close();
shellStdIn.close();
}
callOutput = readProcessStream(backend.getInputStream());
exitCode = backend.waitFor();
shellOutput = readProcessStream(shell.getInputStream());
exitCode = shell.waitFor();
logger.log(Level.INFO, "Return-Code: " + exitCode);
if (exitCode != 0) {
String aErr = readProcessStream(backend.getErrorStream());
String aErr = readProcessStream(shell.getErrorStream());
logger.log(Level.SEVERE, aErr);
throw new ShellException(aErr);
}
@ -51,8 +51,8 @@ public class CommandShell {
} catch (InterruptedException e) {
throw new ShellException(e);
}
if (callOutput != null) {
return callOutput.trim();
if (shellOutput != null) {
return shellOutput.trim();
}
return null;
}

View File

@ -1,5 +1,12 @@
package de.hsadmin.core.qserv;
import java.io.StringWriter;
import java.util.HashMap;
import org.apache.commons.lang.CharEncoding;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
/**
* A ShellProcessor encapsulates a shell command as a Processor.
@ -10,28 +17,49 @@ public class ShellProcessor extends AbstractProcessor {
private static final long serialVersionUID = 1L;
private String aSystemCall;
private String aInput;
private String aOutput;
private String aErrors;
private String systemCall;
private String input;
private String output;
private String error;
public ShellProcessor(String aSystemCall, String aInput) {
this.aSystemCall = aSystemCall;
this.aInput = aInput;
this.systemCall = aSystemCall;
this.input = aInput;
}
public ShellProcessor(String aSystemCall) {
this.aSystemCall = aSystemCall;
this.aInput = null;
this.systemCall = aSystemCall;
this.input = null;
}
public ShellProcessor(String aTemplate, HashMap<String, Object> templateVars) {
this.systemCall = mergeTemplate(aTemplate, templateVars);
this.input = null;
}
public ShellProcessor(String aTemplate, HashMap<String, Object> templateVars, String aInput) {
this.systemCall = mergeTemplate(aTemplate, templateVars);
this.input = aInput;
}
private static String mergeTemplate(String aTemplate,
HashMap<String, Object> templateVars) {
VelocityContext context = new VelocityContext();
for (String key : templateVars.keySet()) {
context.put(key, templateVars.get(key));
}
StringWriter writer = new StringWriter();
Velocity.mergeTemplate(aTemplate, CharEncoding.UTF_8, context, writer);
return writer.toString();
}
@Override
public Object process() throws ProcessorException {
try {
aOutput = CommandShell.execute(aSystemCall, aInput);
return aOutput;
output = CommandShell.execute(systemCall, input);
return output;
} catch (ShellException aExc) {
aErrors = aExc.getMessage();
error = aExc.getMessage();
aExc.printStackTrace(System.err); // Logging
throw new ProcessorException(aExc);
}
@ -40,9 +68,9 @@ public class ShellProcessor extends AbstractProcessor {
@Override
public String logInfo() {
StringBuffer log = new StringBuffer("ShellProcessor\n");
log.append(aSystemCall);
log.append(systemCall);
log.append("\nError: ");
log.append(aErrors);
log.append(error);
log.append("\n");
return log.toString();
}