Examples of ScriptEngine


Examples of javax.script.ScriptEngine

     * method returns <code>null</code>.
     *
     * @param mimeType The MIME type to be mapped to an extension.
     */
    public String getExtension(String mimeType) {
        ScriptEngine se = scriptEngineManager.getEngineByMimeType(mimeType);
        if (se != null) {
            List<?> extensions = se.getFactory().getExtensions();
            if (extensions != null && extensions.size() > 0) {
                return String.valueOf(extensions.get(0));
            }
        }

View Full Code Here

Examples of javax.script.ScriptEngine

        // Log messages to verify which ScriptEngine is actually used
        // for our registered extensions
        if (log.isInfoEnabled()) {
            for (Object o : extensions) {
                final String ext = o.toString();
                final ScriptEngine e = tmp.getEngineByExtension(ext);
                if (e == null) {
                    log.warn("No ScriptEngine found for extension '{}' that was just registered", ext);
                } else {
                    log.info("Script extension '{}' is now handled by ScriptEngine '{}', version='{}', class='{}'", new Object[] { ext,
                            e.getFactory().getEngineName(), e.getFactory().getEngineVersion(), e.getClass().getName() });
                }
            }
        }
    }
View Full Code Here

Examples of javax.script.ScriptEngine

public class ExecuteGroovyFromJSR223 {
    private static Logger log = Logger.getLogger(ExecuteGroovyFromJSR223.class.getName());
   
    public static void main(String[] args) {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");
        try {
            engine.eval("println 'Hello, Groovy!'");
            engine.eval(new FileReader("src/mjg/scripting/hello_world.groovy"));
           
            engine.put("street","Blackheath Avenue");
            engine.put("city","Greenwich");
            engine.put("state","UK");
            engine.eval(new FileReader("src/geocodeV3.groovy"));
            double latitude = Double.parseDouble((String) engine.get("lat"));
            double longitude = Double.parseDouble((String) engine.get("lng"));
            log.info("latitude = " + latitude);
            log.info("longitude = " + longitude);
        } catch (ScriptException | FileNotFoundException e) {
            e.printStackTrace();
        }
View Full Code Here

Examples of javax.script.ScriptEngine

        }
    }
   
    @Test
    public void testLatLngJSR223() {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");
        engine.put("street", "Blackheath Avenue");
        engine.put("city","Greenwich");
        engine.put("state", "UK");
        try {
            engine.eval(new FileReader("src/geocodeV3.groovy"));
        } catch (ScriptException | FileNotFoundException e) {
            e.printStackTrace();
        }
        assertEquals(51.4752654,
            Double.parseDouble((String) engine.get("lat")),0.000001);
        assertEquals(0.0014342,
            Double.parseDouble((String) engine.get("lng")),0.000001);
    }
View Full Code Here

Examples of javax.script.ScriptEngine

    return evaluate(script, Collections.<String, Object> emptyMap());
  }

  @Override
  public Object evaluate(ScriptSource script, Map<String, Object> arguments) {
    ScriptEngine engine = discoverEngine(script, arguments);

    Bindings bindings = (!CollectionUtils.isEmpty(arguments) ? new SimpleBindings(arguments) : null);

    try {
      return (bindings == null ? engine.eval(script.getScriptAsString()) : engine.eval(
          script.getScriptAsString(), bindings));
    } catch (IOException ex) {
      throw new ScriptCompilationException(script, "Cannot access script", ex);
    } catch (ScriptException ex) {
      throw new ScriptCompilationException(script, "Execution failure", ex);
View Full Code Here

Examples of javax.script.ScriptEngine

    }
  }

  protected ScriptEngine discoverEngine(ScriptSource script, Map<String, Object> arguments) {
    ScriptEngineManager engineManager = new ScriptEngineManager(classLoader);
    ScriptEngine engine = null;

    if (StringUtils.hasText(language)) {
      engine = engineManager.getEngineByName(language);
    }
    else {
      // make use the extension (enhanced ScriptSource interface)
      Assert.hasText(extension, "no language or extension specified");
      engine = engineManager.getEngineByExtension(extension);
    }

    Assert.notNull(engine, "No suitable engine found for "
        + (StringUtils.hasText(language) ? "language " + language : "extension " + extension));

    if (log.isDebugEnabled()) {
      ScriptEngineFactory factory = engine.getFactory();
      log.debug(String.format("Using ScriptEngine %s (%s), language %s (%s)", factory.getEngineName(),
          factory.getEngineVersion(), factory.getLanguageName(), factory.getLanguageVersion()));
    }

    return engine;
View Full Code Here

Examples of javax.script.ScriptEngine

  }

  private String signDecipher(final String signature, final String playercode) {
    try {
      final ScriptEngine engine = new ScriptEngineManager()
      .getEngineByName("nashorn");
      engine.eval(new FileReader(Constants.DATA_PATH
          + "scripts/decrypt.js"));
      final Invocable invocable = (Invocable) engine;

      final Object result = invocable.invokeFunction("getWorkingVideo",
          signature, playercode);
View Full Code Here

Examples of javax.script.ScriptEngine

        if (value == null)
            result = "--";
        else {
            // Create the script engine.
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("js");

            DataPointVO point = tgetDataPoint();

            // Put the values into the engine scope.
            engine.put("value", value.getValue().getObjectValue());
            engine.put("htmlText", Functions.getHtmlText(point, value));
            engine.put("renderedText", Functions.getRenderedText(point, value));
            engine.put("time", value.getTime());
            engine.put("pointComponent", this);
            engine.put("point", point);
            // Copy properties from the model into the engine scope.
            engine.put(BaseDwr.MODEL_ATTR_EVENTS, model.get(BaseDwr.MODEL_ATTR_EVENTS));
            engine.put(BaseDwr.MODEL_ATTR_HAS_UNACKED_EVENT, model.get(BaseDwr.MODEL_ATTR_HAS_UNACKED_EVENT));
            engine.put(BaseDwr.MODEL_ATTR_TRANSLATIONS, model.get(BaseDwr.MODEL_ATTR_TRANSLATIONS));

            // Create the script.
            String evalScript = SCRIPT_PREFIX + script + SCRIPT_SUFFIX;

            // Execute.
            try {
                Object o = engine.eval(evalScript);
                if (o == null)
                    result = null;
                else
                    result = o.toString();
            }
View Full Code Here

Examples of javax.script.ScriptEngine

            Class engineFactory=Class.forName(line.trim());
            ScriptEngineManager manager=new ScriptEngineManager(engineFactory.getClassLoader());
            for (ScriptEngineFactory f: manager.getEngineFactories()){
              ClassLoader old=Thread.currentThread().getContextClassLoader();
              Thread.currentThread().setContextClassLoader(engineFactory.getClassLoader());
              ScriptEngine e=f.getScriptEngine();//.eval(f.getOutputStatement("Hello world"));
              Thread.currentThread().setContextClassLoader(old);
              e.eval("File.new(\"\")");
              Thread.currentThread().sleep(1000);
            }
          } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
            catch (ScriptException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
         
        }
      }
    }
View Full Code Here

Examples of javax.script.ScriptEngine

    return bindings;
  }

  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()){
      ClassLoader old=Thread.currentThread().getContextClassLoader();
      Thread.currentThread().setContextClassLoader(classLoaders.get(manager));
      engine=manager.getEngineByExtension(extension);
      Thread.currentThread().setContextClassLoader(old);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.