Search in sources :

Example 21 with Interpreter

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);
        }
    }
}
Also used : Interpreter(bsh.Interpreter) Data(org.jumpmind.symmetric.model.Data) TargetError(bsh.TargetError)

Example 22 with Interpreter

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);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Interpreter(bsh.Interpreter) EvalError(bsh.EvalError) TargetError(bsh.TargetError) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 23 with Interpreter

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;
}
Also used : Interpreter(bsh.Interpreter) EvalError(bsh.EvalError) TargetError(bsh.TargetError)

Example 24 with Interpreter

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;
}
Also used : Interpreter(bsh.Interpreter) PythonInterpreter(org.python.util.PythonInterpreter)

Aggregations

Interpreter (bsh.Interpreter)24 EvalError (bsh.EvalError)9 TargetError (bsh.TargetError)5 Map (java.util.Map)3 Bundle (android.os.Bundle)2 ParseException (bsh.ParseException)2 Async (br.com.brjdevs.java.utils.async.Async)1 CollectionUtils.random (br.com.brjdevs.java.utils.collections.CollectionUtils.random)1 NameSpace (bsh.NameSpace)1 XThis (bsh.XThis)1 Color (java.awt.Color)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 java.util (java.util)1