Ejemplo n.º 1
0
 // This code worked successfully on command-line but never as JUnit test
 // <script>:1: undefined method `+' for nil:NilClass (NoMethodError)
 // raised at "Object obj1 = engine1.eval("$Value + 2010.to_s");"
 // @Test
 public void testMultipleEngineStates() throws ScriptException {
   logger1.info("Multiple Engine States");
   ScriptEngine engine1;
   ScriptEngine engine2;
   synchronized (this) {
     System.setProperty("org.jruby.embed.localcontext.scope", "singlethread");
     System.setProperty("org.jruby.embed.localvariable.behavior", "global");
     ScriptEngineManager manager = new ScriptEngineManager();
     List<ScriptEngineFactory> factories = manager.getEngineFactories();
     ScriptEngineFactory factory = null;
     while (factories.iterator().hasNext()) {
       factory = factories.iterator().next();
       if ("ruby".equals(factory.getLanguageName())) {
         break;
       }
     }
     engine1 = factory.getScriptEngine();
     engine2 = factory.getScriptEngine();
   }
   engine1.put("Value", "value of the first engine");
   engine2.put("Value", new Double(-0.0149));
   Object obj1 = engine1.eval("$Value + 2010.to_s");
   Object obj2 = engine2.eval("$Value + 2010");
   assertNotSame(obj1, obj2);
   engine1 = null;
   engine2 = null;
 }
Ejemplo n.º 2
0
 /** The Script Engine used to evaluate the script. */
 protected ScriptEngine createScriptEngine() {
   for (ScriptEngineFactory factory : new ScriptEngineManager().getEngineFactories()) {
     for (String name : factory.getNames()) {
       if (name.equalsIgnoreCase(scriptEngineLookup)) {
         return factory.getScriptEngine();
       }
     }
     for (String ext : factory.getExtensions()) {
       String scriptEngineLookupLowercase = scriptEngineLookup.toLowerCase();
       if (scriptEngineLookupLowercase.endsWith(ext.toLowerCase())) {
         return factory.getScriptEngine();
       }
     }
   }
   return null;
 }
Ejemplo n.º 3
0
  @Override
  protected ScriptEngine makeEngine() {

    try {
      Class<?> c;
      c = this.getClass().getClassLoader().loadClass("com.sun.script.java.JavaScriptEngineFactory");
      ScriptEngineFactory fact = (ScriptEngineFactory) c.newInstance();

      ScriptEngine engine = fact.getScriptEngine();

      engine
          .getContext()
          .setAttribute(
              "com.sun.script.java.parentLoader",
              Trampoline2.trampoline.getClassLoader(),
              ScriptContext.ENGINE_SCOPE);
      engine
          .getContext()
          .setAttribute(
              "parentLoader", Trampoline2.trampoline.getClassLoader(), ScriptContext.ENGINE_SCOPE);
      return engine;

    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
    return null;
  }
Ejemplo n.º 4
0
  private L2ScriptEngineManager() {
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = scriptEngineManager.getEngineFactories();
    if (Config.SCRIPT_CACHE) {
      _cache = loadCompiledScriptCache();
    } else {
      _cache = null;
    }
    _log.info("Initializing Script Engine Manager");

    for (ScriptEngineFactory factory : factories) {
      try {
        ScriptEngine engine = factory.getScriptEngine();
        boolean reg = false;
        for (String name : factory.getNames()) {
          ScriptEngine existentEngine = _nameEngines.get(name);

          if (existentEngine != null) {
            double engineVer = Double.parseDouble(factory.getEngineVersion());
            double existentEngVer =
                Double.parseDouble(existentEngine.getFactory().getEngineVersion());

            if (engineVer <= existentEngVer) {
              continue;
            }
          }

          reg = true;
          _nameEngines.put(name, engine);
        }

        if (reg) {
          _log.info(
              "Script Engine: "
                  + factory.getEngineName()
                  + " "
                  + factory.getEngineVersion()
                  + " - Language: "
                  + factory.getLanguageName()
                  + " - Language Version: "
                  + factory.getLanguageVersion());
        }

        for (String ext : factory.getExtensions()) {
          if (!ext.equals("java") || factory.getLanguageName().equals("java")) {
            _extEngines.put(ext, engine);
          }
        }
      } catch (Exception e) {
        _log.warn("Failed initializing factory. ");
        e.printStackTrace();
      }
    }

    preConfigure();
  }
  private ScriptEngine getScriptEngineByName(String name) {
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    List<ScriptEngineFactory> scriptEngineFactories = scriptEngineManager.getEngineFactories();

    for (ScriptEngineFactory scriptEngineFactory : scriptEngineFactories) {
      if (scriptEngineFactory.getNames().contains(name)) {
        return scriptEngineFactory.getScriptEngine();
      }
    }
    return null;
  }
  private ScriptEngine getScriptEngineByLanguage(String language) {
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    List<ScriptEngineFactory> scriptEngineFactories = scriptEngineManager.getEngineFactories();

    for (ScriptEngineFactory scriptEngineFactory : scriptEngineFactories) {
      if (scriptEngineFactory.getLanguageName().equals(language)) {
        return scriptEngineFactory.getScriptEngine();
      }
    }
    return null;
  }
Ejemplo n.º 7
0
 @Override
 public void run() {
   final String fileExtension = FileUtils.getExtension(file);
   final ScriptEngineFactory factory = scriptService.getByFileExtension(fileExtension);
   try {
     final Object result = factory.getScriptEngine().eval(new FileReader(file));
     if (result != null) {
       System.out.println(result.toString());
     }
   } catch (final ScriptException e) {
     scriptService.getLogService().error(e.getCause());
   } catch (final Throwable e) {
     scriptService.getLogService().error(e);
   }
 }
 @Override
 public ScriptEngine resolveScriptEngine(String name) {
   try {
     BufferedReader in = new BufferedReader(new InputStreamReader(configFile.openStream()));
     String className = in.readLine();
     in.close();
     Class<?> cls = bundle.loadClass(className);
     if (!ScriptEngineFactory.class.isAssignableFrom(cls)) {
       throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName());
     }
     ScriptEngineFactory factory = (ScriptEngineFactory) cls.newInstance();
     List<String> names = factory.getNames();
     for (String test : names) {
       if (test.equals(name)) {
         ClassLoader old = Thread.currentThread().getContextClassLoader();
         ScriptEngine engine;
         try {
           // JRuby seems to require the correct TCCL to call
           // getScriptEngine
           Thread.currentThread().setContextClassLoader(factory.getClass().getClassLoader());
           engine = factory.getScriptEngine();
         } finally {
           Thread.currentThread().setContextClassLoader(old);
         }
         LOGGER.finest("Resolved ScriptEngineFactory: " + engine + " for expected name: " + name);
         return engine;
       }
     }
     LOGGER.fine(
         "ScriptEngineFactory: "
             + factory.getEngineName()
             + " does not match expected name: "
             + name);
     return null;
   } catch (Exception e) {
     LOGGER.log(Level.WARNING, "Cannot create ScriptEngineFactory: " + e.getClass().getName(), e);
     return null;
   }
 }
 @Override
 public ScriptEngine getScriptEngine() {
   return factory.getScriptEngine();
 }