Package javax.script

Examples of javax.script.ScriptEngine


                reader = new StringReader(this.scriptText);
            }

            // pre-compile script if we have it as text
            if (reader != null) {
                ScriptEngine engine = this.scriptEngineFactory.getScriptEngine();
                if (engine instanceof Compilable) {
                    Compilable compilable = (Compilable) engine;
                    this.compiledScript = compilable.compile(scriptText);
                    LOG.debug("Using compiled script: {}", this.compiledScript);
                }
View Full Code Here


                }
            }
        }

        // fallback to get engine by name
        ScriptEngine engine = createScriptEngine(language);
        if (engine != null) {
            return engine.getFactory();
        }
        return null;
    }
View Full Code Here

        return null;
    }

    protected static ScriptEngine createScriptEngine(String language) {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = null;

        // some script names has alias
        String[] names = getScriptNames(language);
        for (String name : names) {
            try {
                engine = manager.getEngineByName(name);
                if (engine != null) {
                    break;
                }
            } catch (NoClassDefFoundError ex) {
                LOG.error("Cannot load the scriptEngine for " + name + ", the exception is " + ex
                          + ", please ensure correct JARs is provided on classpath.");
            }
        }
        if (engine == null) {
            engine = checkForOSGiEngine(language);
        }
        if (engine == null) {
            throw new IllegalArgumentException("No script engine could be created for: " + language);
        }
        if (isPython(language)) {
            ScriptContext context = engine.getContext();
            context.setAttribute("com.sun.script.jython.comp.mode", "eval", ScriptContext.ENGINE_SCOPE);
        }
        return engine;
    }
View Full Code Here

    }

    protected Object evaluateScript(Exchange exchange) {
        try {
            // get a new engine which we must for each exchange
            ScriptEngine engine = scriptEngineFactory.getScriptEngine();
            ScriptContext context = populateBindings(engine, exchange, attributes);
            addScriptEngineArguments(engine, exchange);
            Object result = runScript(engine, exchange, context);
            LOG.debug("The script evaluation result is: {}", result);
            return result;
View Full Code Here

 
 
  public static void main(String[] args) throws IOException, ScriptException {
 
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("Renjin");

    engine.put("benchmarkArgs", new StringArrayVector(args));

    engine.eval(new InputStreamReader(new FileInputStream("src/main/R/runner.R")));
  }
View Full Code Here

  public void putJavaObject() throws ScriptException {
    // create a script engine manager
    RenjinScriptEngineFactory factory = new RenjinScriptEngineFactory();

    // create an R engine
    ScriptEngine engine = factory.getScriptEngine();

    HashMap<String, String> h = new HashMap<String, String>();
    engine.put("h", h);
    engine.eval("import(java.util.HashMap) \n"
                    + " print(h$size())");
  }
View Full Code Here

    public boolean isActive(FeatureState featureState, FeatureUser user) {

        String lang = featureState.getParameter(PARAM_LANG);
        String script = featureState.getParameter(PARAM_SCRIPT);

        ScriptEngine engine = engineManager.getEngineByName(lang);
        if (engine == null) {
            log.error("Could not find script engine for: " + lang);
            return false;
        }

        engine.put("user", user);
        engine.put("date", new Date());
        try {

            Object result = engine.eval(script);
            if (result instanceof Boolean) {
                return ((Boolean) result).booleanValue();
            }

        } catch (ScriptException e) {
View Full Code Here

    }

    @Override
    public ScriptEngine getEngineByExtension(String extension) {
        //TODO this is a hack to deal with context class loader issues
        ScriptEngine engine = null;
        for (ScriptEngineManager manager : classLoaders.keySet()) {
            Thread currentThread = Thread.currentThread();
            ClassLoader old = currentThread.getContextClassLoader();
            currentThread.setContextClassLoader(classLoaders.get(manager));
            engine = manager.getEngineByExtension(extension);
View Full Code Here

    }

    @Override
    public ScriptEngine getEngineByMimeType(String mimeType) {
        //TODO this is a hack to deal with context class loader issues
        ScriptEngine engine = null;
        for (ScriptEngineManager manager : classLoaders.keySet()) {
            Thread currentThread = Thread.currentThread();
            ClassLoader old = currentThread.getContextClassLoader();
            currentThread.setContextClassLoader(classLoaders.get(manager));
            engine = manager.getEngineByMimeType(mimeType);
View Full Code Here

        for (ScriptEngineManager manager : classLoaders.keySet()) {
            Thread currentThread = Thread.currentThread();
            ClassLoader old = currentThread.getContextClassLoader();
            ClassLoader contextClassLoader = classLoaders.get(manager);
            currentThread.setContextClassLoader(contextClassLoader);
            ScriptEngine engine = manager.getEngineByName(shortName);
            currentThread.setContextClassLoader(old);
            if (engine != null) {
                OSGiScriptEngineFactory factory = new OSGiScriptEngineFactory(engine.getFactory(), contextClassLoader);
                return new OSGiScriptEngine(engine, factory);
            }
        }
        return null;
    }
View Full Code Here

TOP

Related Classes of javax.script.ScriptEngine

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.