Java Code Examples for javax.script.ScriptException#getMessage()

The following examples show how to use javax.script.ScriptException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaScriptTransformationService.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Transforms the input <code>source</code> by Java Script. It expects the
 * transformation rule to be read from a file which is stored under the
 * 'configurations/transform' folder. To organize the various
 * transformations one should use subfolders.
 *
 * @param filename the name of the file which contains the Java script
 *            transformation rule. Transformation service inject input
 *            (source) to 'input' variable.
 * @param source the input to transform
 */
@Override
public @Nullable String transform(String filename, String source) throws TransformationException {
    if (filename == null || source == null) {
        throw new TransformationException("the given parameters 'filename' and 'source' must not be null");
    }

    final long startTime = System.currentTimeMillis();
    logger.debug("about to transform '{}' by the JavaScript '{}'", source, filename);

    String result = "";

    try {
        final CompiledScript cScript = manager.getScript(filename);
        final Bindings bindings = cScript.getEngine().createBindings();
        bindings.put("input", source);
        result = String.valueOf(cScript.eval(bindings));
        return result;
    } catch (ScriptException e) {
        throw new TransformationException("An error occurred while executing script. " + e.getMessage(), e);
    } finally {
        logger.trace("JavaScript execution elapsed {} ms. Result: {}", System.currentTimeMillis() - startTime,
                result);
    }
}
 
Example 2
Source File: MeecrowaveRunMojo.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
private void scriptCustomization(final List<String> customizers, final String ext, final Map<String, Object> customBindings) {
    if (customizers == null || customizers.isEmpty()) {
        return;
    }
    final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(ext);
    if (engine == null) {
        throw new IllegalStateException("No engine for " + ext + ". Maybe add the JSR223 implementation as plugin dependency.");
    }
    for (final String js : customizers) {
        try {
            final SimpleBindings bindings = new SimpleBindings();
            bindings.put("project", project);
            engine.eval(new StringReader(js), bindings);
            bindings.putAll(customBindings);
        } catch (final ScriptException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }
}
 
Example 3
Source File: GenericMenu.java    From freeacs with MIT License 6 votes vote down vote up
public static String eval(String evalStr) {
  evalStr = evalStr.replaceAll(" LT ", " < ");
  evalStr = evalStr.replaceAll(" lt ", " < ");
  evalStr = evalStr.replaceAll(" LE ", " <= ");
  evalStr = evalStr.replaceAll(" le ", " <= ");
  evalStr = evalStr.replaceAll(" EQ ", " == ");
  evalStr = evalStr.replaceAll(" eq ", " == ");
  evalStr = evalStr.replaceAll(" NE ", " != ");
  evalStr = evalStr.replaceAll(" ne ", " != ");
  evalStr = evalStr.replaceAll(" GE ", " >= ");
  evalStr = evalStr.replaceAll(" ge ", " >= ");
  evalStr = evalStr.replaceAll(" GT ", " > ");
  evalStr = evalStr.replaceAll(" gt ", " > ");
  evalStr = evalStr.replaceAll("NULL", "null");
  evalStr = evalStr.replaceAll(" or ", " || ");
  evalStr = evalStr.replaceAll(" OR ", " || ");
  try {
    return scriptEngine.eval(evalStr).toString();
  } catch (ScriptException se) {
    throw new IllegalArgumentException(
        "The evaluation of " + evalStr + " failed: " + se.getMessage());
  }
}
 
Example 4
Source File: TomEEEmbeddedMojo.java    From tomee with Apache License 2.0 6 votes vote down vote up
private void scriptCustomization(final List<String> customizers, final String ext, final String base) {
    if (customizers == null || customizers.isEmpty()) {
        return;
    }
    final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(ext);
    if (engine == null) {
        throw new IllegalStateException("No engine for " + ext + ". Maybe add the JSR223 implementation as plugin dependency.");
    }
    for (final String js : customizers) {
        try {
            final SimpleBindings bindings = new SimpleBindings();
            bindings.put("catalinaBase", base);
            engine.eval(new StringReader(js), bindings);
        } catch (final ScriptException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }
}
 
Example 5
Source File: ScriptedActionHandler.java    From nifi with Apache License 2.0 6 votes vote down vote up
public void execute(PropertyContext context, Action action, Map<String, Object> facts) {
    // Attempt to call a non-ActionHandler interface method (i.e. execute(context, action, facts) from PropertyContextActionHandler)
    final Invocable invocable = (Invocable) scriptEngine;

    try {
        // Get the actual object from the script engine, versus the proxy stored in ActionHandler. The object may have additional methods,
        // where ActionHandler is a proxied interface
        final Object obj = scriptEngine.get("actionHandler");
        if (obj != null) {
            try {
                invocable.invokeMethod(obj, "execute", context, action, facts);
            } catch (final NoSuchMethodException nsme) {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("Configured script ActionHandler is not a PropertyContextActionHandler and has no execute(context, action, facts) method, falling back to"
                    + "execute(action, facts).");
                }
                execute(action, facts);
            }
        } else {
            throw new ScriptException("No ActionHandler was defined by the script.");
        }
    } catch (ScriptException se) {
        throw new ProcessException("Error executing onEnabled(context) method: " + se.getMessage(), se);
    }
}
 
Example 6
Source File: ScriptRecordWriter.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final NullWritable key, final VertexWritable vertex) throws IOException {
    if (null != vertex) {
        try {
            final Bindings bindings = this.engine.createBindings();
            bindings.put(VERTEX, vertex.get());
            final String line = (String) engine.eval(WRITE_CALL, bindings);
            if (line != null) {
                this.out.write(line.getBytes(UTF8));
                this.out.write(NEWLINE);
            }
        } catch (final ScriptException e) {
            throw new IOException(e.getMessage(), e);
        }
    }
}
 
Example 7
Source File: FilterExpression.java    From enhydrator with Apache License 2.0 5 votes vote down vote up
public Boolean execute(Row columns, String expression) {
    Bindings bindings = ScriptingEnvironmentProvider.create(this.manager, this.scriptEngineBindings, columns);
    try {
        this.expressionListener.accept("Executing: " + expression);
        Object result = this.engine.eval(expression, bindings);
        this.expressionListener.accept("Got result: " + result);
        if (!(result instanceof Boolean)) {
            return Boolean.FALSE;
        } else {
            return (Boolean) result;
        }
    } catch (ScriptException ex) {
        throw new IllegalStateException(ex.getMessage(), ex);
    }
}
 
Example 8
Source File: ScriptingEngines.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected Object evaluate(String script, String language, Bindings bindings) {
    ScriptEngine scriptEngine = getEngineByName(language);
    try {
        return scriptEngine.eval(script, bindings);
    } catch (ScriptException e) {
        throw new ActivitiException("problem evaluating script: " + e.getMessage(), e);
    }
}
 
Example 9
Source File: ScriptEngineLambda.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public void accept(final Object a, final Object b) {
    try {
        final Bindings bindings = new SimpleBindings();
        bindings.put(A, a);
        bindings.put(B, b);
        this.engine.eval(this.script, bindings);
    } catch (final ScriptException e) {
        throw new IllegalArgumentException(e.getMessage());
    }
}
 
Example 10
Source File: PythonExecutor.java    From mdw with Apache License 2.0 5 votes vote down vote up
@Override
public Object execute(String script, Map<String,Object> bindings) throws ExecutionException {
    PythonRunner runner = new PythonRunner();
    try {
        return runner.exec(script, bindings);
    }
    catch (ScriptException ex) {
        throw new ExecutionException("Error running script " + name + ": " + ex.getMessage(), ex);
    }
}
 
Example 11
Source File: PythonEvaluator.java    From mdw with Apache License 2.0 5 votes vote down vote up
@Override
public Object evaluate(String expression, Map<String,Object> bindings) throws ExecutionException {
    PythonRunner runner = new PythonRunner();
    try {
        return runner.eval(expression, bindings);
    }
    catch (ScriptException ex) {
        throw new ExecutionException("Error evaluating expression " + name + ": " + ex.getMessage(), ex);
    }
}
 
Example 12
Source File: Expression.java    From enhydrator with Apache License 2.0 5 votes vote down vote up
public Row execute(Row input, String expression) {
    Bindings bindings = ScriptingEnvironmentProvider.create(manager, this.scriptEngineBindings, input);
    try {
        this.expressionListener.accept("Executing: " + expression);
        Object result = this.engine.eval(expression, bindings);
        this.expressionListener.accept("Got result: " + result);
        if (!(result instanceof Row)) {
            return input;
        } else {
            return (Row) result;
        }
    } catch (ScriptException ex) {
        throw new IllegalStateException(ex.getMessage(), ex);
    }
}
 
Example 13
Source File: CompiledExecutableScript.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public Object evaluate(ScriptEngine scriptEngine, VariableScope variableScope, Bindings bindings) {
  try {
    LOG.debugEvaluatingCompiledScript(language);
    return getCompiledScript().eval(bindings);
  } catch (ScriptException e) {
    if (e.getCause() instanceof BpmnError) {
      throw (BpmnError) e.getCause();
    }
    String activityIdMessage = getActivityIdExceptionMessage(variableScope);
    throw new ScriptEvaluationException("Unable to evaluate script" + activityIdMessage +": " + e.getMessage(), e);
  }
}
 
Example 14
Source File: ScriptRecordWriter.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public ScriptRecordWriter(final DataOutputStream out, final TaskAttemptContext context) throws IOException {
    this.out = out;
    final Configuration configuration = context.getConfiguration();
    this.engine = manager.getEngineByName(configuration.get(SCRIPT_ENGINE, "gremlin-groovy"));
    final FileSystem fs = FileSystem.get(configuration);
    try {
        this.engine.eval(new InputStreamReader(fs.open(new Path(configuration.get(SCRIPT_FILE)))));
    } catch (final ScriptException e) {
        throw new IOException(e.getMessage(),e);
    }
}
 
Example 15
Source File: ScriptedRulesEngine.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
@OnEnabled
public void onEnabled(final ConfigurationContext context) {
    synchronized (scriptingComponentHelper.isInitialized) {
        if (!scriptingComponentHelper.isInitialized.get()) {
            scriptingComponentHelper.createResources();
        }
    }
    super.onEnabled(context);

    // Call an non-interface method onEnabled(context), to allow a scripted RulesEngineService the chance to set up as necessary
    final Invocable invocable = (Invocable) scriptEngine;
    if (configurationContext != null) {
        try {
            // Get the actual object from the script engine, versus the proxy stored in RulesEngineService. The object may have additional methods,
            // where RulesEngineService is a proxied interface
            final Object obj = scriptEngine.get("rulesEngine");
            if (obj != null) {
                try {
                    invocable.invokeMethod(obj, "onEnabled", context);
                } catch (final NoSuchMethodException nsme) {
                    if (getLogger().isDebugEnabled()) {
                        getLogger().debug("Configured script RulesEngineService does not contain an onEnabled() method.");
                    }
                }
            } else {
                throw new ScriptException("No RulesEngineService was defined by the script.");
            }
        } catch (ScriptException se) {
            throw new ProcessException("Error executing onEnabled(context) method: " + se.getMessage(), se);
        }
    }
}
 
Example 16
Source File: ScriptingEngines.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected Object evaluate(String script, String language, Bindings bindings) {
  ScriptEngine scriptEngine = getEngineByName(language);
  try {
    return scriptEngine.eval(script, bindings);
  } catch (ScriptException e) {
    throw new ActivitiException("problem evaluating script: " + e.getMessage(), e);
  }
}
 
Example 17
Source File: ScriptEngineLambda.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public void accept(final Object a) {
    try {
        final Bindings bindings = new SimpleBindings();
        bindings.put(A, a);
        this.engine.eval(this.script, bindings);
    } catch (final ScriptException e) {
        throw new IllegalArgumentException(e.getMessage());
    }
}
 
Example 18
Source File: StandardScriptEvalException.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Construct a new script eval exception with the specified original exception.
 */
public StandardScriptEvalException(ScriptException ex) {
	super(ex.getMessage());
	this.scriptException = ex;
}
 
Example 19
Source File: StandardScriptEvalException.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Construct a new script eval exception with the specified original exception.
 */
public StandardScriptEvalException(ScriptException ex) {
	super(ex.getMessage());
	this.scriptException = ex;
}
 
Example 20
Source File: SourceExecutableScript.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
public CompiledScript compile(ScriptEngine scriptEngine, String language, String src) {
  if(scriptEngine instanceof Compilable && !scriptEngine.getFactory().getLanguageName().equalsIgnoreCase("ecmascript")) {
    Compilable compilingEngine = (Compilable) scriptEngine;

    try {
      CompiledScript compiledScript = compilingEngine.compile(src);

      LOG.debugCompiledScriptUsing(language);

      return compiledScript;

    } catch (ScriptException e) {
      throw new ScriptCompilationException("Unable to compile script: " + e.getMessage(), e);

    }

  } else {
    // engine does not support compilation
    return null;
  }

}