下面列出了javax.script.ScriptException#getMessage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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());
}
}
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);
}
}
}
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);
}
}
@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);
}
}
}
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);
}
}
}
/**
* 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);
}
}
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);
}
}
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());
}
}
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);
}
}
@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);
}
}
}
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);
}
}
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);
}
}
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);
}
}
@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);
}
}
@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);
}
}
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());
}
}
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);
}
}
/**
* Construct a new script eval exception with the specified original exception.
*/
public StandardScriptEvalException(ScriptException ex) {
super(ex.getMessage());
this.scriptException = ex;
}
/**
* Construct a new script eval exception with the specified original exception.
*/
public StandardScriptEvalException(ScriptException ex) {
super(ex.getMessage());
this.scriptException = ex;
}
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;
}
}