Compare commits
4 Commits
master
...
refact-pro
Author | SHA1 | Date | |
---|---|---|---|
6b85081efa | |||
632833308a | |||
1d8141e6b0 | |||
|
1634da7162 |
3
hsarback/doc/README.branch
Normal file
3
hsarback/doc/README.branch
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
TODO
|
||||||
|
1. postmap processor
|
||||||
|
2. bash shellscript template processor
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package de.hsadmin.core.qserv;
|
|||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -19,9 +20,35 @@ public class JDBCProcessor extends AbstractProcessor {
|
|||||||
private String url;
|
private String url;
|
||||||
private String user;
|
private String user;
|
||||||
private String password;
|
private String password;
|
||||||
private List<String> sql;
|
private List<SQL> list;
|
||||||
private String errorMsg;
|
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) {
|
public JDBCProcessor(String driver, String url, String user, String password) {
|
||||||
this.driver = driver;
|
this.driver = driver;
|
||||||
this.url = url;
|
this.url = url;
|
||||||
@ -36,9 +63,15 @@ public class JDBCProcessor extends AbstractProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addSQL(String sqlStatement) {
|
public void addSQL(String sqlStatement) {
|
||||||
if (sql == null)
|
if (list == null)
|
||||||
sql = new ArrayList<String>();
|
list = new ArrayList<SQL>();
|
||||||
sql.add(sqlStatement);
|
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 {
|
public Object process() throws ProcessorException {
|
||||||
@ -57,19 +90,26 @@ public class JDBCProcessor extends AbstractProcessor {
|
|||||||
password = config.getProperty("pgsqladmin.password");
|
password = config.getProperty("pgsqladmin.password");
|
||||||
}
|
}
|
||||||
if (user == null || password == null) {
|
if (user == null || password == null) {
|
||||||
throw new ProcessorException("database admin-user configuration failed");
|
throw new ProcessorException(
|
||||||
|
"database admin-user configuration failed");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Class.forName(driver);
|
Class.forName(driver);
|
||||||
c = DriverManager.getConnection(url, user, password);
|
c = DriverManager.getConnection(url, user, password);
|
||||||
if (c == null)
|
if (c == null)
|
||||||
throw new ProcessorException("cannot connect to '" + url + "'");
|
throw new ProcessorException("cannot connect to '" + url + "'");
|
||||||
Statement s = c.createStatement();
|
for (SQL sql : list) {
|
||||||
for (String sqlStatement : sql) {
|
PreparedStatement s = c.prepareStatement(sql.getStatement());
|
||||||
s.addBatch(sqlStatement);
|
String[] params = sql.getParams();
|
||||||
System.out.println("SQL: " + sqlStatement);
|
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) {
|
} catch (SQLException aSqlExc) {
|
||||||
Exception exc = aSqlExc.getNextException();
|
Exception exc = aSqlExc.getNextException();
|
||||||
if (exc == null) {
|
if (exc == null) {
|
||||||
@ -95,8 +135,8 @@ public class JDBCProcessor extends AbstractProcessor {
|
|||||||
StringBuffer log = new StringBuffer("JDBCProcessor\n");
|
StringBuffer log = new StringBuffer("JDBCProcessor\n");
|
||||||
log.append(url);
|
log.append(url);
|
||||||
log.append("\n");
|
log.append("\n");
|
||||||
for (String s : sql) {
|
for (SQL s : list) {
|
||||||
log.append(s);
|
log.append(s.getStatement());
|
||||||
log.append("\n");
|
log.append("\n");
|
||||||
}
|
}
|
||||||
log.append("Error: ");
|
log.append("Error: ");
|
||||||
|
55
hsarback/src/de/hsadmin/core/qserv/PostmapProcessor.java
Normal file
55
hsarback/src/de/hsadmin/core/qserv/PostmapProcessor.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user