예제 #1
0
 public static void include(Context cx, Scriptable thisObj, Object[] args, Function funObj)
     throws ScriptException {
   String functionName = "include";
   int argsCount = args.length;
   if (argsCount != 1 && argsCount != 2) {
     HostObjectUtil.invalidNumberOfArgs(HOST_OBJECT_NAME, functionName, argsCount, false);
   }
   if (!(args[0] instanceof String)) {
     HostObjectUtil.invalidArgsError(
         HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
   }
   if (argsCount == 2 && !(args[1] instanceof ScriptableObject)) {
     HostObjectUtil.invalidArgsError(
         HOST_OBJECT_NAME, functionName, "2", "Object", args[1], false);
   }
   JaggeryContext jaggeryContext = getJaggeryContext();
   RhinoEngine engine = jaggeryContext.getEngine();
   if (engine == null) {
     log.error("Rhino Engine in Jaggery context is null");
     throw new ScriptException("Rhino Engine in Jaggery context is null");
   }
   Stack<String> includesCallstack = getCallstack(jaggeryContext);
   Map<String, Boolean> includedScripts = getIncludes(jaggeryContext);
   String parent = includesCallstack.lastElement();
   String fileURL = (String) args[0];
   if (isHTTP(fileURL) || isHTTP(parent)) {
     if (!isHTTP(fileURL)) {
       fileURL = parent + fileURL;
     }
     if (includesCallstack.search(fileURL) != -1) {
       return;
     }
     ScriptReader source;
     ScriptableObject scope;
     if (argsCount == 2) {
       scope = (ScriptableObject) args[1];
     } else {
       scope = jaggeryContext.getScope();
     }
     // this is a remote file url
     try {
       URL url = new URL(fileURL);
       url.openConnection();
       source = new ScriptReader(url.openStream());
       includedScripts.put(fileURL, true);
       includesCallstack.push(fileURL);
       engine.exec(source, scope, null);
       includesCallstack.pop();
     } catch (MalformedURLException e) {
       String msg = "Malformed URL. function : import, url : " + fileURL;
       log.warn(msg, e);
       throw new ScriptException(msg, e);
     } catch (IOException e) {
       String msg = "IO exception while importing content from url : " + fileURL;
       log.warn(msg, e);
       throw new ScriptException(msg, e);
     }
   } else {
     String msg = "Unsupported file include : " + fileURL;
     throw new ScriptException(msg);
   }
 }
예제 #2
0
  private static ScriptableObject executeScript(
      JaggeryContext jaggeryContext,
      ScriptableObject scope,
      String fileURL,
      final boolean isJSON,
      boolean isBuilt,
      boolean isIncludeOnce)
      throws ScriptException {
    WebAppContext webAppContext = (WebAppContext) jaggeryContext;
    Stack<String> includesCallstack = jaggeryContext.getIncludesCallstack();
    Map<String, Boolean> includedScripts = jaggeryContext.getIncludedScripts();
    ServletContext context = webAppContext.getServletConext();
    String parent = includesCallstack.lastElement();

    String keys[] = WebAppManager.getKeys(context.getContextPath(), parent, fileURL);
    fileURL = getNormalizedScriptPath(keys);
    if (includesCallstack.search(fileURL) != -1) {
      return scope;
    }
    if (isIncludeOnce && includedScripts.get(fileURL) != null) {
      return scope;
    }

    ScriptReader source;
    RhinoEngine engine = jaggeryContext.getEngine();
    if (isBuilt) {
      source =
          new ScriptReader(context.getResourceAsStream(fileURL)) {
            @Override
            protected void build() throws IOException {
              try {
                if (isJSON) {
                  sourceReader =
                      new StringReader("(" + HostObjectUtil.streamToString(sourceIn) + ")");
                } else {
                  sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
                }
              } catch (ScriptException e) {
                throw new IOException(e);
              }
            }
          };
    } else {
      source = new ScriptReader(context.getResourceAsStream(fileURL));
    }

    ScriptCachingContext sctx =
        new ScriptCachingContext(webAppContext.getTenantId(), keys[0], keys[1], keys[2]);
    sctx.setSecurityDomain(new JaggerySecurityDomain(fileURL, context));
    long lastModified = WebAppManager.getScriptLastModified(context, fileURL);
    sctx.setSourceModifiedTime(lastModified);

    includedScripts.put(fileURL, true);
    includesCallstack.push(fileURL);
    if (isJSON) {
      scope = (ScriptableObject) engine.eval(source, scope, sctx);
    } else {
      engine.exec(source, scope, sctx);
    }
    includesCallstack.pop();
    return scope;
  }