Search in sources :

Example 6 with Interpreter

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

Example 7 with Interpreter

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

Example 8 with Interpreter

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

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

Example 10 with Interpreter

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

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