use of bsh.Interpreter in project symmetric-ds by JumpMind.
the class AbstractDatabaseWriter method script.
protected boolean script(CsvData data) {
try {
statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
String script = data.getParsedData(CsvData.ROW_DATA)[0];
Map<String, Object> variables = new HashMap<String, Object>();
bindVariables(variables);
Interpreter interpreter = new Interpreter();
if (variables != null) {
for (String variableName : variables.keySet()) {
interpreter.set(variableName, variables.get(variableName));
}
}
if (log.isDebugEnabled()) {
log.debug("About to run: {}", script);
}
interpreter.eval(script);
statistics.get(batch).increment(DataWriterStatisticConstants.SCRIPTCOUNT);
} catch (EvalError e) {
throw new RuntimeException(e);
}
return true;
}
use of bsh.Interpreter in project symmetric-ds by JumpMind.
the class TransformTable method evaluateTargetDmlAction.
public TargetDmlAction evaluateTargetDmlAction(DataContext dataContext, TransformedData transformedData) {
TargetDmlAction action = null;
try {
action = TargetDmlAction.valueOf(updateAction);
} catch (Exception ex) {
}
if (action == null) {
Interpreter interpreter = getInterpreter(dataContext);
Map<String, String> sourceValues = transformedData.getSourceValues();
try {
interpreter.set("sourceDmlType", transformedData.getSourceDmlType());
interpreter.set("sourceDmlTypeString", transformedData.getSourceDmlType().toString());
interpreter.set("transformedData", transformedData);
CsvData csvData = dataContext.getData();
if (csvData != null) {
interpreter.set("externalData", csvData.getAttribute("externalData"));
} else {
interpreter.set("externalData", null);
}
for (String columnName : sourceValues.keySet()) {
interpreter.set(columnName.toUpperCase(), sourceValues.get(columnName));
interpreter.set(columnName, sourceValues.get(columnName));
}
if (transformedData.getOldSourceValues() != null) {
for (Map.Entry<String, String> oldColumn : transformedData.getOldSourceValues().entrySet()) {
interpreter.set("OLD_" + oldColumn.getKey(), oldColumn.getValue());
interpreter.set("OLD_" + oldColumn.getKey().toUpperCase(), oldColumn.getValue());
}
}
String transformExpression = updateAction;
String methodName = String.format("transform_%d()", Math.abs(transformExpression.hashCode()));
if (dataContext.get(methodName) == null) {
//create BSH-Method if not exists in Context
interpreter.set("context", dataContext);
interpreter.set("bshContext", bshContext);
interpreter.eval(String.format("%s {\n%s\n}", methodName, transformExpression));
dataContext.put(methodName, Boolean.TRUE);
}
//call BSH-Method
Object result = interpreter.eval(methodName);
//evaluate Result of BSH-Script
action = TargetDmlAction.valueOf((String) result);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return action;
}
use of bsh.Interpreter in project symmetric-ds by JumpMind.
the class TransformTable method getInterpreter.
protected Interpreter getInterpreter(Context context) {
Interpreter interpreter = (Interpreter) context.get(INTERPRETER_KEY);
if (interpreter == null) {
interpreter = new Interpreter();
context.put(INTERPRETER_KEY, interpreter);
}
return interpreter;
}
use of bsh.Interpreter in project symmetric-ds by JumpMind.
the class AbstractParameterService method substituteScripts.
protected String substituteScripts(String value) {
if (log.isDebugEnabled()) {
log.debug("substituteScripts starting value is: {}", value);
}
int startTick = StringUtils.indexOf(value, '`');
if (startTick != -1) {
int endTick = StringUtils.lastIndexOf(value, '`');
if (endTick != -1 && startTick != endTick) {
// there's a bean shell script present in this case
String script = StringUtils.substring(value, startTick + 1, endTick);
if (log.isDebugEnabled()) {
log.debug("Script found. Script is is: {}", script);
}
Interpreter interpreter = new Interpreter();
try {
interpreter.set("hostName", AppUtils.getHostName());
interpreter.set("log", log);
interpreter.set("nodeGroupId", nodeGroupId);
interpreter.set("syncUrl", syncUrl);
interpreter.set("registrationUrl", registrationUrl);
interpreter.set("externalId", externalId);
interpreter.set("engineName", engineName);
Object scriptResult = interpreter.eval(script);
if (scriptResult == null) {
scriptResult = "";
}
if (log.isDebugEnabled()) {
log.debug("Script output is: {}", scriptResult);
}
value = StringUtils.substring(value, 0, startTick) + scriptResult.toString() + StringUtils.substring(value, endTick + 1);
} catch (EvalError e) {
throw new RuntimeException(e.getMessage(), e);
}
if (log.isDebugEnabled()) {
log.debug("substituteScripts return value is {}", value);
}
}
}
return value;
}
use of bsh.Interpreter in project symmetric-ds by JumpMind.
the class BshDataRouter method getInterpreter.
protected Interpreter getInterpreter(SimpleRouterContext context) {
Interpreter interpreter = (Interpreter) context.getContextCache().get(INTERPRETER_KEY);
if (interpreter == null) {
interpreter = new Interpreter();
context.getContextCache().put(INTERPRETER_KEY, interpreter);
}
return interpreter;
}
Aggregations