Package javax.script

Examples of javax.script.CompiledScript


     */
    private void compileAndCache(String scriptId, String script) throws ScriptException
    {
        if ( compiler_ != null )
        {
            CompiledScript compiledScript = compiler_.compile(script);
            calloutCache_.put(scriptId, compiledScript);
        }
    }
View Full Code Here


     * @param args arguments to be passed to the callouts.
     */
    public Object executeCallout(String callout, Object ... args)
    {
        Object result = null;
        CompiledScript script = calloutCache_.get(callout);
        if ( script != null )
        {
            try
            {
                Bindings binding = new SimpleBindings();
                binding.put("args", args);
                result = script.eval(binding);
            }
            catch(ScriptException ex)
            {
                logger_.warn(LogUtil.throwableToString(ex));
            }
View Full Code Here

    @Test
    @LoadGraphWith(LoadGraphWith.GraphData.CLASSIC)
    public void shouldBeThreadSafeOnCompiledScript() throws Exception {
        final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine();
        final CompiledScript script = engine.compile("t = g.V().has('name',name); if(t.hasNext()) { t } else { null }");

        int runs = 500;
        final CountDownLatch latch = new CountDownLatch(runs);
        final List<String> names = Arrays.asList("marko", "peter", "josh", "vadas", "stephen", "pavel", "matthias");
        final Random random = new Random();

        for (int i = 0; i < runs; i++) {
            new Thread() {
                public void run() {
                    String name = names.get(random.nextInt(names.size() - 1));
                    try {
                        final Bindings bindings = engine.createBindings();
                        bindings.put("g", g);
                        bindings.put("name", name);
                        Object result = script.eval(bindings);
                        if (name.equals("stephen") || name.equals("pavel") || name.equals("matthias"))
                            assertNull(result);
                        else
                            assertNotNull(result);
                    } catch (ScriptException e) {
View Full Code Here

        assertNotNull(se);
        se.put("key", "value");
        assertEquals("value",se.eval("key"));
        if (se instanceof Compilable){
            Compilable co = (Compilable) se;
            CompiledScript cs = co.compile("key");
            assertNotNull(cs);
            assertEquals("value",cs.eval());
            assertEquals("value",cs.eval());
        } else {
            fail("Expected engine to implement Compilable");
        }
    }
View Full Code Here

                    if (supportsCompilable) {
                        String cacheKey =
                                getScriptLanguage()+"#"+
                                scriptFile.getAbsolutePath()+"#"+
                                        scriptFile.lastModified();
                        CompiledScript compiledScript =
                                compiledScriptsCache.get(cacheKey);
                        if (compiledScript==null) {
                            synchronized (compiledScriptsCache) {
                                compiledScript =
                                        compiledScriptsCache.get(cacheKey);
                                if (compiledScript==null) {
                                    // TODO Charset ?
                                    fileReader = new BufferedReader(new FileReader(scriptFile),
                                            (int)scriptFile.length());
                                    compiledScript =
                                            ((Compilable) scriptEngine).compile(fileReader);
                                    compiledScriptsCache.put(cacheKey, compiledScript);
                                }
                            }
                        }
                        return compiledScript.eval(bindings);
                    } else {
                        // TODO Charset ?
                        fileReader = new BufferedReader(new FileReader(scriptFile),
                                (int)scriptFile.length());
                        return scriptEngine.eval(fileReader, bindings);                   
                    }
                } finally {
                    IOUtils.closeQuietly(fileReader);
                }
            }  else {
                throw new ScriptException("Script file '"+scriptFile.getAbsolutePath()+"' does not exist or is unreadable for element:"+getName());
            }
        } else if (!StringUtils.isEmpty(getScript())){
            if (supportsCompilable && !StringUtils.isEmpty(cacheKey)) {
                CompiledScript compiledScript =
                        compiledScriptsCache.get(cacheKey);
                if (compiledScript==null) {
                    synchronized (compiledScriptsCache) {
                        compiledScript =
                                compiledScriptsCache.get(cacheKey);
                        if (compiledScript==null) {
                            compiledScript =
                                    ((Compilable) scriptEngine).compile(getScript());
                            compiledScriptsCache.put(cacheKey, compiledScript);
                        }
                    }
                }
                return compiledScript.eval(bindings);
            } else {
                return scriptEngine.eval(getScript(), bindings);
            }
        } else {
            throw new ScriptException("Both script file and script text are empty for element:"+getName());           
View Full Code Here

        return compile(new StringReader(script));
    }
   
    @Override
    public CompiledScript compile(java.io.Reader script) throws ScriptException {
        CompiledScript ret = null;
        Context cx = enterContext();
       
        try {
            String filename = (String) get(RhinoScriptEngine.FILENAME);
            if (filename == null) {
View Full Code Here

            }
        }catch(Throwable t){}
    }
   
    public CompiledScript getCompiled(final String script) throws ScriptException{
        CompiledScript result = null;
        final String signature = this.getSignature(script);
        if (StringUtils.hasText(signature)){
            result = _cache.get(script);
            if (null==result){
                // compile and add
                CompiledScript cs = _engine.compile(script);
                if (null!=cs){
                    _cache.put(signature, cs);
                }
            }
        }
View Full Code Here

        engineScope.putAll(bindings);
        return result;
    }

    private Object run(final Reader reader, Map<String, Object> bindings) throws ScriptException {
        CompiledScript compiled = null;
        if (_compile) {
            compiled = this.compile(reader);
        }

        if (null != compiled) {
View Full Code Here

        }
    }

    private Object run(final String script,
            final Map<String, Object> bindings) throws ScriptException {
        CompiledScript compiled = null;
        if (_compile) {
            compiled = this.compile(script);
        }

        if (null != compiled) {
View Full Code Here

          log.warning("----------Key-------" + keyword);
          NewsSearchModel model = new NewsSearchModel();
          model.setPage(page);
          model.search(keyword);

          CompiledScript compiledscript = CompileScriptEngine
              .getCompileScript(config.getApplicationId(),
                  "search", template.getSearch());

          Bindings bind = compiledscript.getEngine().getBindings(
              ScriptContext.ENGINE_SCOPE);
          bind.put("title_for_layout", config.getTitle());
          bind.put("description_for_layout", config.getDescription());
          bind.put("keyword_for_layout", config.getKeyword());
          bind.put("css_for_layout", template.getCss());
          bind.put("js_for_layout", template.getJs());

          CategoryModel cateModel = new CategoryModel();
          cateModel.prepareAll();
          TagModel tagModel = new TagModel();
          tagModel.prepareList();

          bind.put("keyword", keyword);
          bind.put("recent_view", RecentViewHelper.getRecentView());
          bind.put("list_tag", tagModel.getListResult());
          bind.put("list_category", cateModel.getListResult());

          bind.put("current_page", model.getPage());
          bind.put("total_page", model.getTotalPage());
          bind.put("articles", model.getListResult());
          try {
            resp.getWriter().print((String) compiledscript.eval());
          } catch (Exception e) {
            e.printStackTrace();
            log.warning(e.toString());
          }
        } else {
View Full Code Here

TOP

Related Classes of javax.script.CompiledScript

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.