From 1d8141e6b012957d5092cb943650792eae294e65 Mon Sep 17 00:00:00 2001 From: Michael Hierweck Date: Thu, 20 Jun 2013 15:40:44 +0200 Subject: [PATCH] ShellProcessor supports templates. --- .../de/hsadmin/core/qserv/CommandShell.java | 34 ++++++------ .../de/hsadmin/core/qserv/ShellProcessor.java | 54 ++++++++++++++----- 2 files changed, 58 insertions(+), 30 deletions(-) diff --git a/hsarback/src/de/hsadmin/core/qserv/CommandShell.java b/hsarback/src/de/hsadmin/core/qserv/CommandShell.java index cfee80f..a567eac 100644 --- a/hsarback/src/de/hsadmin/core/qserv/CommandShell.java +++ b/hsarback/src/de/hsadmin/core/qserv/CommandShell.java @@ -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 += "< templateVars) { + this.systemCall = mergeTemplate(aTemplate, templateVars); + this.input = null; + } + + public ShellProcessor(String aTemplate, HashMap templateVars, String aInput) { + this.systemCall = mergeTemplate(aTemplate, templateVars); + this.input = aInput; + } + + private static String mergeTemplate(String aTemplate, + HashMap 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(); }