コード例 #1
0
 public static void main(String[] args) throws Exception {
   ScriptEngineManager manager = new ScriptEngineManager();
   ScriptEngine jsengine = Helper.getJsEngine(manager);
   if (jsengine == null) {
     System.out.println("Warning: No js engine found; test vacuously passes.");
     return;
   }
   String langVersion = jsengine.getFactory().getLanguageVersion();
   if (!langVersion.equals(JS_LANG_VERSION)) {
     throw new RuntimeException("Expected JavaScript version is " + JS_LANG_VERSION);
   }
   String engineVersion = jsengine.getFactory().getEngineVersion();
   if (!engineVersion.equals(JS_ENGINE_VERSION)) {
     throw new RuntimeException("Expected Rhino version is " + JS_ENGINE_VERSION);
   }
 }
コード例 #2
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();
  }
コード例 #3
0
  @SuppressWarnings("unchecked")
  protected static void includeScriptLibraries(ScriptEngine engine) throws ScriptException {
    FacesContext context = FacesContext.getCurrentInstance();

    String language = engine.getFactory().getLanguageName();
    Set<String> mimeTypes = new HashSet<String>(engine.getFactory().getMimeTypes());
    mimeTypes.add("text/x-script-" + language);

    Set<String> includedLibraries =
        (Set<String>) getScopeMap().get("_" + language + "IncludedLibraries");
    if (includedLibraries == null) {
      includedLibraries = new HashSet<String>();
      getScopeMap().put("_" + language + "IncludedLibraries", includedLibraries);
    }

    // Now look through the view's resources for appropriate scripts and load them
    UIViewRootEx2 view = (UIViewRootEx2) context.getViewRoot();
    if (view != null) {
      for (Resource res : view.getResources()) {
        if (res instanceof ScriptResource) {
          ScriptResource script = (ScriptResource) res;
          if (script.getType() != null && mimeTypes.contains(script.getType())) {
            // Then we have a script - find its contents and run it

            String properName = (script.getSrc().charAt(0) == '/' ? "" : "/") + script.getSrc();
            if (!includedLibraries.contains(properName)) {
              InputStream is =
                  context
                      .getExternalContext()
                      .getResourceAsStream("/WEB-INF/" + language + properName);
              if (is != null) {
                engine.eval(new InputStreamReader(is));
              }

              includedLibraries.add(properName);
            }
          }
        }
      }
    }
  }