/**
   * Starts the code server with the given command line options. To shut it down, see {@link
   * WebServer#stop}.
   *
   * <p>Only one code server should be started at a time because the GWT compiler uses a lot of
   * static variables.
   */
  public static WebServer start(Options options) throws IOException, UnableToCompleteException {
    if (options.getModuleNames().isEmpty()) {
      throw new IllegalArgumentException("Usage: at least one module must be supplied");
    }

    PrintWriterTreeLogger logger = new PrintWriterTreeLogger();
    logger.setMaxDetail(TreeLogger.Type.INFO);
    Modules modules = new Modules();

    File workDir = ensureWorkDir(options);
    System.out.println("workDir: " + workDir);

    for (String moduleName : options.getModuleNames()) {
      AppSpace appSpace = AppSpace.create(new File(workDir, moduleName));

      Recompiler recompiler = new Recompiler(appSpace, moduleName, options.getSourcePath(), logger);
      modules.addModuleState(new ModuleState(recompiler, logger));
    }

    SourceHandler sourceHandler = new SourceHandler(modules, logger);

    WebServer webServer =
        new WebServer(sourceHandler, modules, options.getBindAddress(), options.getPort(), logger);
    webServer.start();

    return webServer;
  }
 @Override
 public void loadJsni(BrowserChannelClient channel, String jsniString) {
   if (logger.isLoggable(TreeLogger.SPAM)) {
     logger.log(TreeLogger.SPAM, "LOAD_JSNI: " + jsniString);
   }
   ScriptResult scriptResult = htmlPage.executeJavaScript(jsniString);
   if (logger.isLoggable(TreeLogger.INFO)) {
     logger.log(TreeLogger.INFO, "LOAD_JSNI: scriptResult=" + scriptResult);
   }
 }
Exemple #3
0
 private TreeLogger makeCompileLogger(CompileDir compileDir, TreeLogger parent)
     throws UnableToCompleteException {
   try {
     PrintWriterTreeLogger fileLogger = new PrintWriterTreeLogger(compileDir.getLogFile());
     fileLogger.setMaxDetail(options.getLogLevel());
     return new CompositeTreeLogger(parent, fileLogger);
   } catch (IOException e) {
     parent.log(TreeLogger.ERROR, "unable to open log file: " + compileDir.getLogFile(), e);
     throw new UnableToCompleteException();
   }
 }
  HtmlUnitSessionHandler(Window window, JavaScriptEngine jsEngine) {
    this.window = window;
    logger.setMaxDetail(TreeLogger.ERROR);
    this.jsEngine = jsEngine;
    htmlPage = (HtmlPage) this.window.getWebWindow().getEnclosedPage();
    if (logger.isLoggable(TreeLogger.INFO)) {
      logger.log(TreeLogger.INFO, "jsEngine = " + jsEngine + ", HtmlPage = " + htmlPage);
    }

    jsObjectToRef = new IdentityHashMap<Scriptable, Integer>();
    nextRefId = 1;
    refToJsObject = new HashMap<Integer, Scriptable>();

    // related to JavaObject cache.
    javaObjectCache = new HashMap<Integer, JavaObject>();
  }
Exemple #5
0
 private static TreeLogger getRootLogger() {
   PrintWriterTreeLogger logger = new PrintWriterTreeLogger(new PrintWriter(System.err, true));
   logger.setMaxDetail(TreeLogger.ERROR);
   return logger;
 }
  @SuppressWarnings("unchecked")
  @Override
  public ExceptionOrReturnValue invoke(
      BrowserChannelClient channel, Value thisObj, String methodName, Value[] args) {
    if (logger.isLoggable(TreeLogger.DEBUG)) {
      logger.log(
          TreeLogger.DEBUG,
          "INVOKE: thisObj: " + thisObj + ", methodName: " + methodName + ", args: " + args);
    }
    /*
     * 1. lookup functions by name. 2. Find context and scope. 3. Convert
     * thisObject to ScriptableObject 4. Convert args 5. Get return value
     */
    Context jsContext = Context.getCurrentContext();
    ScriptableObject jsThis = null;
    if (thisObj.getType() == ValueType.NULL) {
      jsThis = window;
    } else {
      Object obj = makeJsvalFromValue(jsContext, thisObj);
      if (obj instanceof ScriptableObject) {
        jsThis = (ScriptableObject) obj;
      } else if (obj instanceof SimpleScriptableProxy<?>) {
        jsThis = ((SimpleScriptableProxy<SimpleScriptable>) obj).getDelegee();
      } else {
        logger.log(
            TreeLogger.ERROR,
            "Unable to convert "
                + obj
                + " to either "
                + " ScriptableObject or SimpleScriptableProxy");
        return new ExceptionOrReturnValue(true, new Value(null));
      }
    }
    Object functionObject = ScriptableObject.getProperty(window, methodName);
    if (functionObject == ScriptableObject.NOT_FOUND) {
      logger.log(
          TreeLogger.ERROR,
          "function "
              + methodName
              + " NOT FOUND, thisObj: "
              + jsThis
              + ", methodName: "
              + methodName);
      // TODO: see if this maps to QUIT
      return new ExceptionOrReturnValue(true, new Value(null));
    }
    Function jsFunction = (Function) functionObject;
    if (logger.isLoggable(TreeLogger.SPAM)) {
      logger.log(TreeLogger.SPAM, "INVOKE: jsFunction: " + jsFunction);
    }

    Object jsArgs[] = new Object[args.length];
    for (int i = 0; i < args.length; i++) {
      jsArgs[i] = makeJsvalFromValue(jsContext, args[i]);
    }
    Object result = null;
    try {
      if (args.length == 1 && methodName.indexOf(REPLACE_METHOD_SIGNATURE) != -1) {
        // getUrl() is not visible
        String currentUrl = window.jsxGet_location().toString();
        currentUrl = getUrlBeforeHash(currentUrl);
        String newUrl = getUrlBeforeHash((String) args[0].getValue());
        if (!newUrl.equals(currentUrl)) {
          WebWindow webWindow = window.getWebWindow();
          do {
            webWindow.getJobManager().removeAllJobs();
            webWindow = webWindow.getParentWindow();
          } while (webWindow != webWindow.getTopWindow());
        }
      }
      result = jsEngine.callFunction(htmlPage, jsFunction, jsContext, window, jsThis, jsArgs);
    } catch (JavaScriptException ex) {
      if (logger.isLoggable(TreeLogger.INFO)) {
        logger.log(
            TreeLogger.INFO,
            "INVOKE: JavaScriptException "
                + ex
                + ", message: "
                + ex.getMessage()
                + " when invoking "
                + methodName);
      }
      return new ExceptionOrReturnValue(true, makeValueFromJsval(jsContext, ex.getValue()));
    } catch (Exception ex) {
      if (logger.isLoggable(TreeLogger.INFO)) {
        logger.log(
            TreeLogger.INFO,
            "INVOKE: exception "
                + ex
                + ", message: "
                + ex.getMessage()
                + " when invoking "
                + methodName);
      }
      return new ExceptionOrReturnValue(true, makeValueFromJsval(jsContext, Undefined.instance));
    }
    if (logger.isLoggable(TreeLogger.INFO)) {
      logger.log(TreeLogger.INFO, "INVOKE: result: " + result + " of jsFunction: " + jsFunction);
    }
    return new ExceptionOrReturnValue(false, makeValueFromJsval(jsContext, result));
  }