Exemplo n.º 1
0
 public void handle(HttpExchange ex) throws IOException {
   if (ex == null) {
     throw new NullPointerException();
   }
   HttpContext context = ex.getHttpContext();
   new Filter.Chain(context.getFilters(), context.getHandler()).doFilter(ex);
 }
  public static void main(String[] args) throws Exception {
    final String filename = "log4j.properties";
    PropertyConfigurator.configure(SeleniumGridExtras.class.getClassLoader().getResource(filename));
    logger.info("Loaded Grid Logger from " + filename);

    RuntimeConfig.load(true);

    SelfHealingGrid.checkStatus(RuntimeConfig.getGridExtrasPort(), RuntimeConfig.getConfig());

    HttpServer server =
        HttpServer.create(new InetSocketAddress(RuntimeConfig.getGridExtrasPort()), 0);

    List<ExecuteOSTask> tasks = new LinkedList<ExecuteOSTask>();
    for (String module : RuntimeConfig.getConfig().getActivatedModules()) {
      tasks.add((ExecuteOSTask) Class.forName(module).newInstance());
    }

    logger.debug(RuntimeConfig.getSeleniungGridExtrasHomePath());

    logger.info("Initializing Task Modules");
    for (final ExecuteOSTask task : tasks) {

      if (task.initialize()) {

        HttpContext context =
            server.createContext(
                task.getEndpoint(),
                new HttpExecutor() {
                  @Override
                  String execute(Map params) {

                    logger.debug(
                        "End-point "
                            + task.getEndpoint()
                            + " was called with HTTP params "
                            + params.toString());
                    String result =
                        new GsonBuilder().setPrettyPrinting().create().toJson(task.execute(params));
                    return result;
                  }
                });

        context.getFilters().add(new ParameterFilter());
      }
    }

    logger.info("API documentation");
    logger.info("/api - Located here");
    HttpContext context =
        server.createContext(
            "/api",
            new HttpExecutor() {
              @Override
              String execute(Map params) {
                String apiDocs = ApiDocumentation.getApiDocumentation();
                logger.debug(apiDocs);
                return apiDocs;
              }
            });

    if (RuntimeConfig.getConfig().getAutoStartHub()) {
      logger.info("Grid Hub was set to Autostart");
      ExecuteOSTask grid = new StartGrid();
      logger.debug(grid.execute("hub").toString().toString());
    }

    if (RuntimeConfig.getConfig().getAutoStartNode()) {
      logger.info("Grid NodeConfig was set to Autostart");
      ExecuteOSTask grid = new StartGrid();
      logger.debug(grid.execute("node").toString().toString());
    }

    context.getFilters().add(new ParameterFilter());

    server.setExecutor(null);
    server.start();
    logger.info("Server has been started");
  }