コード例 #1
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);
            }
          }
        }
      }
    }
  }