Package javax.script

Examples of javax.script.Invocable


   */ 
  public Document createAST(Reader parser, String jsfile) throws Exception{   
    String ast = null;
    try {
      engine.eval(parser);
      Invocable invocableEngine = (Invocable)engine;
      // Store the script in a String
      String js = Utils.readFile2String(jsfile);
      //String js = getURLContent(codeURL);
     
      // (function name, script source, boolean for position details)
      ast = (String)invocableEngine.invokeFunction("parse2AST", js, true);
      //AST = invocableEngine.invokeFunction("parse2JSON", js, true);

    } catch (Exception ex) {
      ex.printStackTrace();
    }
View Full Code Here


 
  public Document createAST(Reader parser, URL jslink) throws Exception{   
    String ast = null;
    try {
      engine.eval(parser);
      Invocable invocableEngine = (Invocable)engine;
      // Store the script in a String
      String js = Utils.getURLContent(jslink);
     
      // (function name, script source, boolean for position details)
      ast = (String)invocableEngine.invokeFunction("parse2AST", js, true);
      //AST = invocableEngine.invokeFunction("parse2JSON", js, true);

    } catch (Exception ex) {
      ex.printStackTrace();
    }
View Full Code Here

    @Override
    public String process() throws Exception {
        Object result = compiledScript == null ? engine.eval(scriptContent) : compiledScript.eval();
        if (engine instanceof Invocable) {
            final Invocable invocable = (Invocable) engine;

            try {
                result = invocable.invokeFunction(getFunctionName("process"));
            } catch (final NoSuchMethodException e) {
                //ignore
            }
        }
View Full Code Here

    @Override
    public Object processItem(final Object item) throws Exception {
        final Object result = compiledScript == null ? engine.eval(scriptContent) : compiledScript.eval();
        if (engine instanceof Invocable) {
            final Invocable invocable = (Invocable) engine;
            try {
                return invocable.invokeFunction(getFunctionName("processItem"), item);
            } catch (final NoSuchMethodException e) {
                //the script does not implement processItem method, so just return the original item as is
                return item;
            }
        } else {
View Full Code Here

   
    @Override
    public void run(Request request, Response response, ScriptEngine engine)
            throws ScriptException, IOException {

        Invocable invocable = (Invocable) engine;
        Object exportsObj = engine.get("exports");
        Scriptable exports = null;
        if (exportsObj instanceof Scriptable) {
            exports = (Scriptable) exportsObj;
        } else {
            throw new ScriptException("Couldn't get exports for app.");
        }
        Scriptable scope = exports.getParentScope();
        Context cx = CommonJSEngine.enterContext();
        Scriptable jsgiRequest = null;
        try {
            jsgiRequest = new JsgiRequest(request, response, cx, scope);
        } catch (Exception e) {
            throw new ScriptException(e);
        } finally {
            Context.exit();
        }
        Object appReturn = null;
        try {
            appReturn = invocable.invokeMethod(exports, "app", jsgiRequest);
        } catch (NoSuchMethodException e) {
            throw new ScriptException(e);
        }
        if (!(appReturn instanceof Scriptable)) {
            throw new ScriptException("bad return from JSGI app");
View Full Code Here

        super(plugin);
    }
   
    @Override
    public Object run(Object object, List<Object> args, ScriptEngine engine) throws ScriptException {
        Invocable invocable = (Invocable) engine;
        Object results;
        Object exportsObj = engine.get("exports");
        Scriptable exports = null;
        if (exportsObj instanceof Scriptable) {
            exports = (Scriptable) exportsObj;
        } else {
            throw new ScriptException("Couldn't get exports for function.");
        }
        Scriptable scope = exports.getParentScope();
        Context.enter();
        Object geoObject;
        try {
            geoObject = GeoObject.javaToJS(object, scope);
        } catch (Exception e) {
            // We can only convert to GeoScript objects if a script has already
            // called require('geoscript').
            geoObject = object;
        }
        Object geoArgs;
        try {
            geoArgs = GeoObject.javaToJS(args, scope);
        } catch (Exception e) {
            // As above, scripts must require('geoscript') first for this to
            // work with GeoScript objects.
            geoArgs = args;
        }
        try {
            results = invocable.invokeMethod(exports, "run", geoObject, geoArgs);
            results = GeoObject.jsToJava(results);
        } catch (NoSuchMethodException e) {
            throw new ScriptException(e);
        } finally {
            Context.exit();
View Full Code Here

                                                Object result = null;

                                                String methodName = method.getName();
                                                Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
                                                if (bindings.containsKey(methodName)) {
                                                    Invocable invocable;
                                                    try {
                                                        invocable = (Invocable)scriptEngine;
                                                    } catch (ClassCastException exception) {
                                                        throw new SerializationException(exception);
                                                    }

                                                    result = invocable.invokeFunction(methodName, args);
                                                }

                                                // If the function didn't return a value, return the default
                                                Class<?> returnType = method.getReturnType();
                                                if (returnType == Vote.class
View Full Code Here

        this.baseFolder = ".";
    }

    public ColumnTransformer getColumnTransformer(String scriptName) {
        String content = load(COLUMN_SCRIPT_FOLDER, scriptName);
        Invocable invocable = (Invocable) engine;
        try {
            engine.eval(content);
        } catch (ScriptException ex) {
            throw new IllegalStateException("Cannot evaluate script", ex);
        }
        return invocable.getInterface(ColumnTransformer.class);
    }
View Full Code Here

TOP

Related Classes of javax.script.Invocable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.