コード例 #1
0
  /**
   * Checks {@link GridSystemProperties#GG_JETTY_PORT} system property and overrides default
   * connector port if it present. Then initializes {@code port} with the found value.
   *
   * @param con Jetty connector.
   */
  private void override(AbstractNetworkConnector con) {
    String host = System.getProperty(GG_JETTY_HOST);

    if (!F.isEmpty(host)) con.setHost(host);

    int currPort = con.getPort();

    Integer overridePort = Integer.getInteger(GG_JETTY_PORT);

    if (overridePort != null && overridePort != 0) currPort = overridePort;

    con.setPort(currPort);
    port = currPort;
  }
コード例 #2
0
  private void sendRequest(int id, int count) throws Exception {
    int idx = id - 1;

    if (idx < 0) throw new IllegalArgumentException("Connection ID <= 0");

    _socket[idx] =
        _socket[idx] == null ? new Socket("localhost", _connector.getLocalPort()) : _socket[idx];
    _out[idx] =
        _out[idx] == null ? new PrintWriter(_socket[idx].getOutputStream(), true) : _out[idx];
    _in[idx] =
        _in[idx] == null
            ? new BufferedReader(new InputStreamReader(_socket[idx].getInputStream()))
            : _in[idx];

    _connect.reset();

    _out[idx].write("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
    _out[idx].flush();

    _connect.await();

    assertEquals(count, _statistics.getConnectionsOpen());

    String line = _in[idx].readLine();
    while (line != null) {
      if ("Server response".equals(line)) break;
      line = _in[idx].readLine();
    }
  }
コード例 #3
0
  @BeforeClass
  public static void initClass() throws Exception {
    _connect = new CyclicBarrier(2);

    _server = new Server();
    _connector = new ServerConnector(_server);
    _statistics = new ConnectorStatistics();
    _connector.addBean(_statistics);
    _server.addConnector(_connector);

    HandlerWrapper wrapper =
        new HandlerWrapper() {
          @Override
          public void handle(
              String path,
              Request request,
              HttpServletRequest httpRequest,
              HttpServletResponse httpResponse)
              throws IOException, ServletException {
            try {
              _connect.await();
            } catch (Exception ex) {
              LOG.debug(ex);
            } finally {
              super.handle(path, request, httpRequest, httpResponse);
            }
          }
        };
    _server.setHandler(wrapper);

    Handler handler =
        new AbstractHandler() {
          @Override
          public void handle(
              String target,
              Request baseRequest,
              HttpServletRequest request,
              HttpServletResponse response)
              throws IOException, ServletException {
            try {
              Thread.sleep(1);
            } catch (Exception e) {
            }
            baseRequest.setHandled(true);
            PrintWriter out = response.getWriter();
            out.write("Server response\n");
            out.close();

            response.setStatus(HttpServletResponse.SC_OK);
          }
        };
    wrapper.setHandler(handler);

    _server.start();
  }
コード例 #4
0
  /** {@inheritDoc} */
  @SuppressWarnings("BusyWait")
  @Override
  public void start(GridRestProtocolHandler hnd) throws GridException {
    InetAddress locHost;

    try {
      locHost = U.resolveLocalHost(ctx.config().getLocalHost());
    } catch (IOException e) {
      throw new GridException(
          "Failed to resolve local host to bind address: " + ctx.config().getLocalHost(), e);
    }

    System.setProperty(GG_JETTY_HOST, locHost.getHostAddress());

    jettyHnd =
        new GridJettyRestHandler(
            hnd,
            new C1<String, Boolean>() {
              @Override
              public Boolean apply(String tok) {
                return F.isEmpty(secretKey) || authenticate(tok);
              }
            },
            log);

    String jettyPath = ctx.config().getRestJettyPath();

    final URL cfgUrl;

    if (jettyPath == null) {
      cfgUrl = null;

      if (log.isDebugEnabled())
        log.debug("Jetty configuration file is not provided, using defaults.");
    } else {
      cfgUrl = U.resolveGridGainUrl(jettyPath);

      if (cfgUrl == null)
        throw new GridSpiException("Invalid Jetty configuration file: " + jettyPath);
      else if (log.isDebugEnabled()) log.debug("Jetty configuration file: " + cfgUrl);
    }

    loadJettyConfiguration(cfgUrl);

    AbstractNetworkConnector connector = getJettyConnector();

    try {
      host = InetAddress.getByName(connector.getHost());
    } catch (UnknownHostException e) {
      throw new GridException("Failed to resolve Jetty host address: " + connector.getHost(), e);
    }

    int initPort = connector.getPort();

    int lastPort = initPort + ctx.config().getRestPortRange() - 1;

    for (port = initPort; port <= lastPort; port++) {
      connector.setPort(port);

      if (startJetty()) {
        if (log.isInfoEnabled()) log.info(startInfo());

        return;
      }
    }

    U.warn(
        log,
        "Failed to start Jetty REST server (possibly all ports in range are in use) "
            + "[firstPort="
            + initPort
            + ", lastPort="
            + lastPort
            + ']');
  }