Ejemplo n.º 1
0
 private static Connector buildSslConnector() {
   SslConnector sslConnector;
   if (usingNIO) {
     sslConnector = new SslSelectChannelConnector();
   } else {
     sslConnector = new SslSocketConnector();
   }
   sslConnector.setPort(jettySSLPort);
   sslConnector.setMaxIdleTime(maxIdleTime);
   sslConnector.setKeyPassword(keyPassword);
   if (keyFileName != null && keyFileName.length() != 0) {
     sslConnector.setKeystore(keyFileName);
   }
   return sslConnector;
 }
    @Override
    public void start() {
      jettyServer = jettyFactory.createServer();
      httpConnector = null;
      httpSecureConnector = null;
      String[] addresses = configuration.getListeningAddresses();
      if (addresses == null || addresses.length == 0) {
        addresses = new String[] {null};
      }
      Map<String, Object> attributes = new HashMap<String, Object>();
      attributes.put("javax.servlet.context.tempdir", configuration.getTemporaryDirectory());

      jettyServer.setServerConfigDir(configuration.getConfigurationDir()); // Fix for PAXWEB-193
      jettyServer.setServerConfigURL(configuration.getConfigurationURL());
      jettyServer.configureContext(
          attributes,
          configuration.getSessionTimeout(),
          configuration.getSessionCookie(),
          configuration.getSessionUrl(),
          configuration.getSessionCookieHttpOnly(),
          configuration.getWorkerName(),
          configuration.getSessionLazyLoad(),
          configuration.getSessionStoreDirectory());

      // Configure NCSA RequestLogHandler

      if (configuration.isLogNCSAFormatEnabled()) {
        jettyServer.configureRequestLog(
            configuration.getLogNCSAFormat(),
            configuration.getLogNCSARetainDays(),
            configuration.isLogNCSAAppend(),
            configuration.isLogNCSAExtended(),
            configuration.isLogNCSADispatch(),
            configuration.getLogNCSATimeZone(),
            configuration.getLogNCSADirectory());
      }

      jettyServer.start();
      for (String address : addresses) {
        Integer httpPort = configuration.getHttpPort();
        Boolean useNIO = configuration.useNIO();
        Integer httpSecurePort = configuration.getHttpSecurePort();

        if (configuration.isHttpEnabled()) {
          Connector[] connectors = jettyServer.getConnectors();
          boolean masterConnectorFound = false; // Flag is set if the
          // same connector
          // has been found
          // through xml
          // config and
          // properties
          if (connectors != null && connectors.length > 0) {
            // Combine the configurations if they do match
            Connector backupConnector = null;

            for (Connector connector : connectors) {
              if ((connector instanceof Connector) && !(connector instanceof SslConnector)) {
                if (match(address, httpPort, connector)) {
                  // the same connection as configured through
                  // property/config-admin already is
                  // configured through jetty.xml
                  // therefore just use it as the one if not
                  // already done so.
                  if (httpConnector == null) {
                    httpConnector = connector;
                  }
                  if (!connector.isStarted()) {
                    startConnector(connector);
                  }
                  masterConnectorFound = true;
                } else {
                  if (backupConnector == null) {
                    backupConnector = connector;
                  }
                  if (!connector.isStarted()) {
                    startConnector(connector);
                  }
                }
              }
            }

            if (httpConnector == null && backupConnector != null) {
              httpConnector = backupConnector;
            }
          }

          if (!masterConnectorFound) {
            final Connector connector =
                jettyFactory.createConnector(
                    configuration.getHttpConnectorName(), httpPort, address, useNIO);
            if (httpConnector == null) {
              httpConnector = connector;
            }
            jettyServer.addConnector(connector);
            startConnector(connector);
          }
        } else {
          // remove maybe already configured connectors throuhg
          // jetty.xml, the config-property/config-admin service is
          // master configuration
          Connector[] connectors = jettyServer.getConnectors();
          if (connectors != null) {
            for (Connector connector : connectors) {
              if ((connector instanceof Connector) && !(connector instanceof SslConnector)) {
                jettyServer.removeConnector(connector);
              }
            }
          }
        }
        if (configuration.isHttpSecureEnabled()) {
          final String sslPassword = configuration.getSslPassword();
          final String sslKeyPassword = configuration.getSslKeyPassword();

          Connector[] connectors = jettyServer.getConnectors();
          boolean masterSSLConnectorFound = false;
          if (connectors != null && connectors.length > 0) {
            // Combine the configurations if they do match
            Connector backupConnector = null;

            for (Connector connector : connectors) {
              if (connector instanceof SslConnector) {
                SslConnector sslCon = (SslConnector) connector;
                String[] split = connector.getName().split(":");
                if (httpSecurePort == Integer.valueOf(split[1]).intValue()
                    && address.equalsIgnoreCase(split[0])) {
                  httpSecureConnector = sslCon;

                  if (!sslCon.isStarted()) {
                    startConnector(sslCon);
                  }
                  masterSSLConnectorFound = true;

                } else {
                  // default behavior
                  if (backupConnector == null) {
                    backupConnector = connector;
                  }

                  if (!connector.isStarted()) {
                    startConnector(connector);
                  }
                }
              }
            }
            if (httpSecureConnector == null && backupConnector != null) {
              httpSecureConnector = backupConnector;
            }
          }

          if (!masterSSLConnectorFound) {
            // no combination of jetty.xml and
            // config-admin/properties needed
            if (sslPassword != null && sslKeyPassword != null) {
              final Connector secureConnector =
                  jettyFactory.createSecureConnector(
                      configuration.getHttpSecureConnectorName(),
                      httpSecurePort,
                      configuration.getSslKeystore(),
                      sslPassword,
                      sslKeyPassword,
                      address,
                      configuration.getSslKeystoreType(),
                      configuration.isClientAuthNeeded(),
                      configuration.isClientAuthWanted());
              if (httpSecureConnector == null) {
                httpSecureConnector = secureConnector;
              }
              jettyServer.addConnector(secureConnector);
              startConnector(secureConnector);
            } else {
              LOG.warn(
                  "SSL password and SSL keystore password must be set in order to enable SSL.");
              LOG.warn("SSL connector will not be started");
            }
          }
        } else {
          // remove maybe already configured connectors through
          // jetty.xml, the config-property/config-admin service is
          // master configuration
          Connector[] connectors = jettyServer.getConnectors();
          if (connectors != null) {
            for (Connector connector : connectors) {
              if (connector instanceof SslConnector) {
                jettyServer.removeConnector(connector);
              }
            }
          }
        }
      }
      state = new Started();
      notifyListeners(ServerEvent.STARTED);
    }