Package javax.script

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


        }
    }
   
    @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

    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

    }
  }

  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

  }

  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

        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

            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

    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

     return engine;
  }

  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()){
      ClassLoader old=Thread.currentThread().getContextClassLoader();
      Thread.currentThread().setContextClassLoader(classLoaders.get(manager));
      engine=manager.getEngineByMimeType(mimeType);
      Thread.currentThread().setContextClassLoader(old);
View Full Code Here

  public ScriptEngine getEngineByName(String shortName) {
    //TODO this is a hack to deal with context class loader issues
    for(ScriptEngineManager manager: classLoaders.keySet()){
      ClassLoader old=Thread.currentThread().getContextClassLoader();
      Thread.currentThread().setContextClassLoader(classLoaders.get(manager));
      ScriptEngine engine=manager.getEngineByName(shortName);
      Thread.currentThread().setContextClassLoader(old);
      if (engine!=null){
        return new OSGiScriptEngine(engine, new OSGiScriptEngineFactory(engine.getFactory(), classLoaders.get(manager)));
      }
    }
    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.