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

View File

@ -1,5 +1,12 @@
package de.hsadmin.core.qserv; 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. * 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 static final long serialVersionUID = 1L;
private String aSystemCall; private String systemCall;
private String aInput; private String input;
private String aOutput; private String output;
private String aErrors; private String error;
public ShellProcessor(String aSystemCall, String aInput) { public ShellProcessor(String aSystemCall, String aInput) {
this.aSystemCall = aSystemCall; this.systemCall = aSystemCall;
this.aInput = aInput; this.input = aInput;
} }
public ShellProcessor(String aSystemCall) { public ShellProcessor(String aSystemCall) {
this.aSystemCall = aSystemCall; this.systemCall = aSystemCall;
this.aInput = null; 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 @Override
public Object process() throws ProcessorException { public Object process() throws ProcessorException {
try { try {
aOutput = CommandShell.execute(aSystemCall, aInput); output = CommandShell.execute(systemCall, input);
return aOutput; return output;
} catch (ShellException aExc) { } catch (ShellException aExc) {
aErrors = aExc.getMessage(); error = aExc.getMessage();
aExc.printStackTrace(System.err); // Logging aExc.printStackTrace(System.err); // Logging
throw new ProcessorException(aExc); throw new ProcessorException(aExc);
} }
@ -40,9 +68,9 @@ public class ShellProcessor extends AbstractProcessor {
@Override @Override
public String logInfo() { public String logInfo() {
StringBuffer log = new StringBuffer("ShellProcessor\n"); StringBuffer log = new StringBuffer("ShellProcessor\n");
log.append(aSystemCall); log.append(systemCall);
log.append("\nError: "); log.append("\nError: ");
log.append(aErrors); log.append(error);
log.append("\n"); log.append("\n");
return log.toString(); return log.toString();
} }