public void setServer(Server server) {
    Service[] findServices = server.findServices();
    for (Service service : findServices) {
      Service existingService = getServer().findService(service.getName());
      if (existingService != null) {
        for (Connector connector : service.findConnectors()) {
          existingService.addConnector(connector);
        }
        for (Executor executor : service.findExecutors()) {
          existingService.addExecutor(executor);
        }
        for (LifecycleListener lifecycleListener : service.findLifecycleListeners()) {
          existingService.addLifecycleListener(lifecycleListener);
        }
        existingService.getContainer().setRealm(service.getContainer().getRealm());
        existingService
            .getContainer()
            .setBackgroundProcessorDelay(service.getContainer().getBackgroundProcessorDelay());
        existingService.getContainer().setCluster(service.getContainer().getCluster());
        // existingService.getContainer().setResources(
        // service.getContainer().getResources());
      } else {
        getServer().addService(service);
      }
    }
    this.setHostname(server.getAddress());

    this.setPort(server.getPort());
  }
 private void addPreviouslyRemovedConnectors() {
   Service[] services = this.tomcat.getServer().findServices();
   for (Service service : services) {
     Connector[] connectors = this.serviceConnectors.get(service);
     if (connectors != null) {
       for (Connector connector : connectors) {
         service.addConnector(connector);
         if (!this.autoStart) {
           stopProtocolHandler(connector);
         }
       }
       this.serviceConnectors.remove(service);
     }
   }
 }
Beispiel #3
0
  /**
   * Get the default http connector. You can set more parameters - the port is already initialized.
   *
   * <p>Alternatively, you can construct a Connector and set any params, then call
   * addConnector(Connector)
   *
   * @return A connector object that can be customized
   */
  public Connector getConnector() {
    getServer();
    if (connector != null) {
      return connector;
    }
    // This will load Apr connector if available,
    // default to nio. I'm having strange problems with apr
    // XXX: jfclere weird... Don't add the AprLifecycleListener then.
    // and for the use case the speed benefit wouldn't matter.

    connector = new Connector("HTTP/1.1");
    // connector = new Connector("org.apache.coyote.http11.Http11Protocol");
    connector.setPort(port);
    service.addConnector(connector);
    return connector;
  }
  /**
   * Start the instance using the ports provided
   *
   * @param port the http port to use
   * @param securePort the secure https port to use
   */
  @SuppressWarnings("unchecked")
  public T start(final Integer port, final Integer securePort) {
    if (port == null && securePort == null)
      throw new IllegalStateException("You must specify a port or a secure port");
    if (isRunning()) throw new IllegalStateException("Server already running");
    final String startedMessage =
        "Started "
            + this.getClass().getSimpleName().replace("Runner", "")
            + " listening on:"
            + (port != null ? " standard port " + port : "")
            + (securePort != null ? " secure port " + securePort : "");

    try {
      String servletContext = "";

      tomcat = new Tomcat();
      tomcat.setBaseDir(
          new File(".").getCanonicalPath()
              + File.separatorChar
              + "tomcat"
              + (servletContext.length() > 0 ? "_" + servletContext : ""));

      // add http port
      tomcat.setPort(port != null ? port : securePort);

      if (securePort != null) {
        // add https connector
        SSLFactory.buildKeyStore();
        Connector httpsConnector = new Connector();
        httpsConnector.setPort(securePort);
        httpsConnector.setSecure(true);
        httpsConnector.setAttribute("keyAlias", SSLFactory.KEY_STORE_ALIAS);
        httpsConnector.setAttribute("keystorePass", SSLFactory.KEY_STORE_PASSWORD);
        logger.trace(
            "Loading key store from file ["
                + new File(SSLFactory.KEY_STORE_FILENAME).getAbsoluteFile()
                + "]");
        httpsConnector.setAttribute(
            "keystoreFile", new File(SSLFactory.KEY_STORE_FILENAME).getAbsoluteFile());
        httpsConnector.setAttribute("clientAuth", "false");
        httpsConnector.setAttribute("sslProtocol", "TLS");
        httpsConnector.setAttribute("SSLEnabled", true);

        Service service = tomcat.getService();
        service.addConnector(httpsConnector);

        Connector defaultConnector = tomcat.getConnector();
        defaultConnector.setRedirectPort(securePort);
      }

      // add servlet
      Context ctx = tomcat.addContext("/" + servletContext, new File(".").getAbsolutePath());
      tomcat.addServlet("/" + servletContext, "mockServerServlet", getServlet());
      ctx.addServletMapping("/*", "mockServerServlet");

      // start server
      tomcat.start();

      // create and start shutdown thread
      shutdownThread = new ShutdownThread(stopPort(port, securePort));
      shutdownThread.start();
      serverStarted(port, securePort);

      logger.info(startedMessage);
      System.out.println(startedMessage);

      join();
    } catch (Throwable t) {
      logger.error("Exception while starting server", t);
    }

    return (T) this;
  }
  public static void main(String[] args) {

    System.setProperty("catalina.base", System.getProperty("user.dir"));
    Connector connector = new HttpConnector();

    Wrapper wrapper1 = new StandardWrapper();
    wrapper1.setName("Primitive");
    wrapper1.setServletClass("PrimitiveServlet");
    Wrapper wrapper2 = new StandardWrapper();
    wrapper2.setName("Modern");
    wrapper2.setServletClass("ModernServlet");

    Context context = new StandardContext();
    // StandardContext's start method adds a default mapper
    context.setPath("/app1");
    context.setDocBase("app1");

    context.addChild(wrapper1);
    context.addChild(wrapper2);

    LifecycleListener listener = new SimpleContextConfig();
    ((Lifecycle) context).addLifecycleListener(listener);

    Host host = new StandardHost();
    host.addChild(context);
    host.setName("localhost");
    host.setAppBase("webapps");

    Loader loader = new WebappLoader();
    context.setLoader(loader);
    // context.addServletMapping(pattern, name);
    context.addServletMapping("/Primitive", "Primitive");
    context.addServletMapping("/Modern", "Modern");

    Engine engine = new StandardEngine();
    engine.addChild(host);
    engine.setDefaultHost("localhost");

    Service service = new StandardService();
    service.setName("Stand-alone Service");
    Server server = new StandardServer();
    server.addService(service);
    service.addConnector(connector);

    // StandardService class's setContainer will call all its connector's setContainer method
    service.setContainer(engine);

    // Start the new server
    if (server instanceof Lifecycle) {
      try {
        server.initialize();
        ((Lifecycle) server).start();
        server.await();
        // the program waits until the await method returns,
        // i.e. until a shutdown command is received.
      } catch (LifecycleException e) {
        e.printStackTrace(System.out);
      }
    }

    // Shut down the server
    if (server instanceof Lifecycle) {
      try {
        ((Lifecycle) server).stop();
      } catch (LifecycleException e) {
        e.printStackTrace(System.out);
      }
    }
  }