use of bsh.Interpreter in project symmetric-ds by JumpMind.
the class BshColumnTransform method transform.
public NewAndOldValue transform(IDatabasePlatform platform, DataContext context, TransformColumn column, TransformedData data, Map<String, String> sourceValues, String newValue, String oldValue) throws IgnoreColumnException, IgnoreRowException {
try {
Interpreter interpreter = getInterpreter(context);
interpreter.set("currentValue", newValue);
interpreter.set("oldValue", oldValue);
interpreter.set("channelId", context.getBatch().getChannelId());
interpreter.set("includeOn", column.getIncludeOn());
interpreter.set("sourceDmlType", data.getSourceDmlType());
interpreter.set("sourceDmlTypeString", data.getSourceDmlType().toString());
interpreter.set("transformedData", data);
interpreter.set("transformColumn", column);
Data csvData = (Data) context.get(Constants.DATA_CONTEXT_CURRENT_CSV_DATA);
if (csvData != null && csvData.getTriggerHistory() != null) {
interpreter.set("sourceSchemaName", csvData.getTriggerHistory().getSourceSchemaName());
interpreter.set("sourceCatalogName", csvData.getTriggerHistory().getSourceCatalogName());
interpreter.set("sourceTableName", csvData.getTriggerHistory().getSourceTableName());
}
for (String columnName : sourceValues.keySet()) {
interpreter.set(columnName.toUpperCase(), sourceValues.get(columnName));
interpreter.set(columnName, sourceValues.get(columnName));
}
String transformExpression = column.getTransformExpression();
String globalScript = parameterService.getString(ParameterConstants.BSH_TRANSFORM_GLOBAL_SCRIPT);
String methodName = String.format("transform_%d()", Math.abs(transformExpression.hashCode() + (globalScript == null ? 0 : globalScript.hashCode())));
if (context.get(methodName) == null) {
interpreter.set("log", log);
interpreter.set("sqlTemplate", platform.getSqlTemplate());
interpreter.set("context", context);
interpreter.set("bshContext", bshContext);
interpreter.set(DATA_CONTEXT_ENGINE, context.get(DATA_CONTEXT_ENGINE));
interpreter.set(DATA_CONTEXT_TARGET_NODE, context.get(DATA_CONTEXT_TARGET_NODE));
interpreter.set(DATA_CONTEXT_TARGET_NODE_ID, context.get(DATA_CONTEXT_TARGET_NODE_ID));
interpreter.set(DATA_CONTEXT_TARGET_NODE_GROUP_ID, context.get(DATA_CONTEXT_TARGET_NODE_GROUP_ID));
interpreter.set(DATA_CONTEXT_TARGET_NODE_EXTERNAL_ID, context.get(DATA_CONTEXT_TARGET_NODE_EXTERNAL_ID));
interpreter.set(DATA_CONTEXT_SOURCE_NODE, context.get(DATA_CONTEXT_SOURCE_NODE));
interpreter.set(DATA_CONTEXT_SOURCE_NODE_ID, context.get(DATA_CONTEXT_SOURCE_NODE_ID));
interpreter.set(DATA_CONTEXT_SOURCE_NODE_GROUP_ID, context.get(DATA_CONTEXT_SOURCE_NODE_GROUP_ID));
interpreter.set(DATA_CONTEXT_SOURCE_NODE_EXTERNAL_ID, context.get(DATA_CONTEXT_SOURCE_NODE_EXTERNAL_ID));
if (StringUtils.isNotBlank(globalScript)) {
interpreter.eval(globalScript);
}
interpreter.eval(String.format("%s {\n%s\n}", methodName, transformExpression));
context.put(methodName, Boolean.TRUE);
}
Object result = interpreter.eval(methodName);
if (csvData != null && csvData.getTriggerHistory() != null) {
interpreter.unset("sourceSchemaName");
interpreter.unset("sourceCatalogName");
interpreter.unset("sourceTableName");
}
for (String columnName : sourceValues.keySet()) {
interpreter.unset(columnName.toUpperCase());
interpreter.unset(columnName);
}
if (result instanceof String) {
return new NewAndOldValue((String) result, null);
} else if (result instanceof NewAndOldValue) {
return (NewAndOldValue) result;
} else if (result != null) {
return new NewAndOldValue(result.toString(), null);
} else {
return null;
}
} catch (TargetError evalEx) {
Throwable ex = evalEx.getTarget();
if (ex instanceof IgnoreColumnException) {
throw (IgnoreColumnException) ex;
} else if (ex instanceof IgnoreRowException) {
throw (IgnoreRowException) ex;
} else {
throw new TransformColumnException(String.format("Beanshell script error on line %d for target column %s on transform %s", evalEx.getErrorLineNumber(), column.getTargetColumnName(), column.getTransformId()), ex);
}
} catch (Exception ex) {
if (ex instanceof IgnoreColumnException) {
throw (IgnoreColumnException) ex;
} else if (ex instanceof IgnoreRowException) {
throw (IgnoreRowException) ex;
} else {
log.error(String.format("Beanshell script error for target column %s on transform %s", column.getTargetColumnName(), column.getTransformId()), ex);
throw new TransformColumnException(ex);
}
}
}
use of bsh.Interpreter in project symmetric-ds by JumpMind.
the class BshDataRouter method routeToNodes.
public Set<String> routeToNodes(SimpleRouterContext context, DataMetaData dataMetaData, Set<Node> nodes, boolean initialLoad, boolean initialLoadSelectUsed, TriggerRouter triggerRouter) {
Set<String> boundVariableNames = new LinkedHashSet<String>();
try {
long ts = System.currentTimeMillis();
Interpreter interpreter = getInterpreter(context);
context.incrementStat(System.currentTimeMillis() - ts, "bsh.init.ms");
HashSet<String> targetNodes = new HashSet<String>();
ts = System.currentTimeMillis();
bind(interpreter, dataMetaData, nodes, targetNodes, boundVariableNames, initialLoad);
context.incrementStat(System.currentTimeMillis() - ts, "bsh.bind.ms");
ts = System.currentTimeMillis();
Object returnValue = interpreter.eval(dataMetaData.getRouter().getRouterExpression());
context.incrementStat(System.currentTimeMillis() - ts, "bsh.eval.ms");
return eval(returnValue, nodes, targetNodes);
} catch (EvalError e) {
if (e instanceof TargetError) {
Throwable t = ((TargetError) e).getTarget();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new RuntimeException("Routing script failed at line " + ((TargetError) e).getErrorLineNumber(), t);
}
} else {
throw new RuntimeException("Failed to evaluate bsh router script. Bound variables were: " + boundVariableNames, e);
}
}
}
use of bsh.Interpreter in project symmetric-ds by JumpMind.
the class DefaultNodeIdCreator method evaluateScript.
protected String evaluateScript(Node node, String remoteHost, String remoteAddress) {
String script = parameterService.getString(ParameterConstants.NODE_ID_CREATOR_SCRIPT);
if (StringUtils.isNotBlank(script)) {
try {
Interpreter interpreter = new Interpreter();
interpreter.set("node", node);
interpreter.set("hostname", remoteHost);
interpreter.set("remoteHost", remoteHost);
interpreter.set("remoteAddress", remoteAddress);
interpreter.set("log", log);
Object retValue = interpreter.eval(script);
if (retValue != null) {
return retValue.toString();
}
} catch (TargetError e) {
if (e.getTarget() instanceof RuntimeException) {
throw (RuntimeException) e.getTarget();
} else {
throw new RuntimeException(e.getTarget() != null ? e.getTarget() : e);
}
} catch (EvalError e) {
log.error("Failed to evalute node id generator script. The default node id generation mechanism will be used.", e);
}
}
return null;
}
use of bsh.Interpreter in project bamboobsc by billchen198318.
the class ScriptExpressionUtils method buildBshInterpreter.
public static Interpreter buildBshInterpreter(boolean clean) {
Interpreter bshInterpreter = null;
if ((bshInterpreter = bshInterpreterTL.get()) == null) {
bshInterpreter = new Interpreter();
bshInterpreterTL.set(bshInterpreter);
}
if (clean && bshInterpreter.getNameSpace() != null) {
bshInterpreter.getNameSpace().clear();
}
return bshInterpreter;
}
Aggregations