Compare commits

...

4 Commits

5 changed files with 168 additions and 42 deletions

View File

@ -0,0 +1,3 @@
TODO
1. postmap processor
2. bash shellscript template processor

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

@ -2,6 +2,7 @@ package de.hsadmin.core.qserv;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
@ -19,8 +20,34 @@ public class JDBCProcessor extends AbstractProcessor {
private String url;
private String user;
private String password;
private List<String> sql;
private List<SQL> list;
private String errorMsg;
class SQL {
private String statement;
private String[] params;
public SQL(String statement) {
super();
this.statement = statement;
}
public SQL(String statement, String[] params) {
super();
this.statement = statement;
this.params = params;
}
public String getStatement() {
return statement;
}
public String[] getParams() {
return params;
}
}
public JDBCProcessor(String driver, String url, String user, String password) {
this.driver = driver;
@ -36,9 +63,15 @@ public class JDBCProcessor extends AbstractProcessor {
}
public void addSQL(String sqlStatement) {
if (sql == null)
sql = new ArrayList<String>();
sql.add(sqlStatement);
if (list == null)
list = new ArrayList<SQL>();
list.add(new SQL(sqlStatement));
}
public void addSQL(String sqlStatement, String[] sqlParams) {
if (list == null)
list = new ArrayList<SQL>();
list.add(new SQL(sqlStatement, sqlParams));
}
public Object process() throws ProcessorException {
@ -57,19 +90,26 @@ public class JDBCProcessor extends AbstractProcessor {
password = config.getProperty("pgsqladmin.password");
}
if (user == null || password == null) {
throw new ProcessorException("database admin-user configuration failed");
throw new ProcessorException(
"database admin-user configuration failed");
}
try {
Class.forName(driver);
c = DriverManager.getConnection(url, user, password);
if (c == null)
throw new ProcessorException("cannot connect to '" + url + "'");
Statement s = c.createStatement();
for (String sqlStatement : sql) {
s.addBatch(sqlStatement);
System.out.println("SQL: " + sqlStatement);
for (SQL sql : list) {
PreparedStatement s = c.prepareStatement(sql.getStatement());
String[] params = sql.getParams();
if (params != null) {
for (int i = 0; i < params.length; i++) {
s.setString(i, params[i]);
}
}
System.out.println("SQL: " + sql.getStatement());
s.execute();
}
return s.executeBatch();
return null;
} catch (SQLException aSqlExc) {
Exception exc = aSqlExc.getNextException();
if (exc == null) {
@ -95,8 +135,8 @@ public class JDBCProcessor extends AbstractProcessor {
StringBuffer log = new StringBuffer("JDBCProcessor\n");
log.append(url);
log.append("\n");
for (String s : sql) {
log.append(s);
for (SQL s : list) {
log.append(s.getStatement());
log.append("\n");
}
log.append("Error: ");

View File

@ -0,0 +1,55 @@
package de.hsadmin.core.qserv;
import java.util.ArrayList;
import java.util.HashMap;
public class PostmapProcessor extends AbstractProcessor {
private static final long serialVersionUID = 506775231401876523L;
private HashMap<String, String> operations;
private String file;
private String error;
public PostmapProcessor(String file) {
super();
this.file = file;
}
public void set(String key, String value) {
operations.put(key, value);
}
public void unset(String key) {
operations.put(key, null);
}
@Override
public String logInfo() {
StringBuffer log = new StringBuffer("Postmap\n");
log.append("\nError: ");
log.append(error);
log.append("\n");
return log.toString();
}
@Override
public Object process() throws ProcessorException {
ArrayList<String> output = new ArrayList<String>();
for (String key : operations.keySet()) {
String value = operations.get(key);
try {
if (value == null) {
output.add(CommandShell.execute("/usr/sbin/postmap -d " + key + " " + file));
} else {
output.add(CommandShell.execute("/usr/sbin/postmap -i " + file, key + "\t" + value));
}
} catch (ShellException e) {
error = e.getMessage();
e.printStackTrace(System.err); // Logging
throw new ProcessorException(e);
}
}
return output;
}
}

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();
}