Search in sources :

Example 46 with ScriptException

use of javax.script.ScriptException in project dubbo by alibaba.

the class ScriptRouter method route.

@SuppressWarnings("unchecked")
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
    try {
        List<Invoker<T>> invokersCopy = new ArrayList<Invoker<T>>(invokers);
        Compilable compilable = (Compilable) engine;
        Bindings bindings = engine.createBindings();
        bindings.put("invokers", invokersCopy);
        bindings.put("invocation", invocation);
        bindings.put("context", RpcContext.getContext());
        CompiledScript function = compilable.compile(rule);
        Object obj = function.eval(bindings);
        if (obj instanceof Invoker[]) {
            invokersCopy = Arrays.asList((Invoker<T>[]) obj);
        } else if (obj instanceof Object[]) {
            invokersCopy = new ArrayList<Invoker<T>>();
            for (Object inv : (Object[]) obj) {
                invokersCopy.add((Invoker<T>) inv);
            }
        } else {
            invokersCopy = (List<Invoker<T>>) obj;
        }
        return invokersCopy;
    } catch (ScriptException e) {
        // fail then ignore rule .invokers.
        logger.error("route error , rule has been ignored. rule: " + rule + ", method:" + invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e);
        return invokers;
    }
}
Also used : CompiledScript(javax.script.CompiledScript) ScriptException(javax.script.ScriptException) Invoker(com.alibaba.dubbo.rpc.Invoker) Compilable(javax.script.Compilable) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Bindings(javax.script.Bindings)

Example 47 with ScriptException

use of javax.script.ScriptException in project meecrowave by apache.

the class MeecrowaveRunMojo method scriptCustomization.

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);
        }
    }
}
Also used : ScriptException(javax.script.ScriptException) SimpleBindings(javax.script.SimpleBindings) ScriptEngineManager(javax.script.ScriptEngineManager) StringReader(java.io.StringReader) ScriptEngine(javax.script.ScriptEngine)

Example 48 with ScriptException

use of javax.script.ScriptException in project scheduling by ow2-proactive.

the class AbstractIModeCommand method execute.

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    currentContext.setProperty(IMODE, true);
    ScriptEngine engine = currentContext.getEngine();
    try {
        // load supported functions
        engine.eval(new InputStreamReader(script()));
    } catch (ScriptException error) {
        throw new CLIException(CLIException.REASON_OTHER, error);
    }
    while (!currentContext.getProperty(TERMINATE, Boolean.TYPE, false)) {
        try {
            String command = readLine(currentContext, "> ");
            if (command == null) {
                // EOF, exit interactive shell
                break;
            }
            engine.eval(command);
        } catch (ScriptException se) {
            handleError(String.format("An error occurred while executing the script:"), se, currentContext);
        }
    }
}
Also used : ScriptException(javax.script.ScriptException) InputStreamReader(java.io.InputStreamReader) CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) ScriptEngine(javax.script.ScriptEngine)

Example 49 with ScriptException

use of javax.script.ScriptException in project scheduling by ow2-proactive.

the class EvalScriptCommand method execute.

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    ScriptEngine engine = currentContext.getEngine();
    Writer writer = currentContext.getDevice().getWriter();
    if (scriptArgs != null) {
        engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE).putAll(bindings(scriptArgs));
    }
    String script = FileUtility.readFileToString(new File(scriptPathname));
    try {
        engine.eval(script);
    } catch (ScriptException e) {
        e.printStackTrace(new PrintWriter(writer, true));
    }
}
Also used : ScriptException(javax.script.ScriptException) File(java.io.File) ScriptEngine(javax.script.ScriptEngine) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 50 with ScriptException

use of javax.script.ScriptException in project tomee by apache.

the class ScriptLoginModule method login.

@Override
public boolean login() throws LoginException {
    File script = getScriptFile((String) this.options.get("scriptURI"));
    if (script == null) {
        script = getScriptFile(JavaSecurityManagers.getSystemProperty("openejb.ScriptLoginModule.scriptURI"));
        if (script == null) {
            script = getScriptFile(null);
        }
    }
    if (script == null) {
        throw new LoginException("No login script defined");
    }
    final String scriptText;
    try {
        scriptText = new Scanner(script).useDelimiter("\\Z").next();
    } catch (final FileNotFoundException e) {
        throw new LoginException("Invalid login script URI.");
    }
    this.userData = getUserData();
    final ScriptEngineManager manager = new ScriptEngineManager();
    final ScriptEngine engine = manager.getEngineByName((String) this.options.get("engineName"));
    // new context for the execution of this script
    final ScriptContext newContext = new SimpleScriptContext();
    // creating the bidings object for the current execution
    final Bindings bindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE);
    bindings.put("user", this.userData.user);
    bindings.put("password", this.userData.pass);
    final List<String> myGroups;
    try {
        myGroups = (List) engine.eval(scriptText, newContext);
    } catch (final ScriptException e) {
        throw new LoginException("Cannot execute login script. Msg: " + e.getMessage());
    }
    this.userData.groups.addAll(myGroups);
    return true;
}
Also used : Scanner(java.util.Scanner) ScriptException(javax.script.ScriptException) SimpleScriptContext(javax.script.SimpleScriptContext) FileNotFoundException(java.io.FileNotFoundException) ScriptEngineManager(javax.script.ScriptEngineManager) LoginException(javax.security.auth.login.LoginException) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) File(java.io.File) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Aggregations

ScriptException (javax.script.ScriptException)106 ScriptEngine (javax.script.ScriptEngine)42 IOException (java.io.IOException)41 Bindings (javax.script.Bindings)30 ScriptEngineManager (javax.script.ScriptEngineManager)20 InputStreamReader (java.io.InputStreamReader)12 CompiledScript (javax.script.CompiledScript)12 Map (java.util.Map)11 Invocable (javax.script.Invocable)11 ScriptContext (javax.script.ScriptContext)11 SimpleBindings (javax.script.SimpleBindings)10 File (java.io.File)8 Reader (java.io.Reader)7 Writer (java.io.Writer)7 HashMap (java.util.HashMap)7 PrintWriter (java.io.PrintWriter)6 ArrayList (java.util.ArrayList)6 SlingBindings (org.apache.sling.api.scripting.SlingBindings)6 MissingMethodException (groovy.lang.MissingMethodException)5 MissingPropertyException (groovy.lang.MissingPropertyException)5