Example #1
0
  /**
   * Enables JNDI naming which is disabled by default. Server must implement {@link Lifecycle} in
   * order for the {@link NamingContextListener} to be used.
   */
  public void enableNaming() {
    // Make sure getServer() has been called as that is where naming is
    // disabled
    getServer();
    server.addLifecycleListener(new NamingContextListener());

    System.setProperty("catalina.useNaming", "true");

    String value = "org.apache.naming";
    String oldValue = System.getProperty(javax.naming.Context.URL_PKG_PREFIXES);
    if (oldValue != null) {
      if (oldValue.contains(value)) {
        value = oldValue;
      } else {
        value = value + ":" + oldValue;
      }
    }
    System.setProperty(javax.naming.Context.URL_PKG_PREFIXES, value);

    value = System.getProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY);
    if (value == null) {
      System.setProperty(
          javax.naming.Context.INITIAL_CONTEXT_FACTORY,
          "org.apache.naming.java.javaURLContextFactory");
    }
  }
Example #2
0
  private void start(boolean await) {

    // try to shutdown a previous Tomcat
    sendShutdownCommand();

    try {
      final ServerSocket srv = new ServerSocket(this.httpPort);
      srv.close();
    } catch (IOException e) {
      log.error("PORT " + this.httpPort + " ALREADY IN USE");
      return;
    }

    // Read a dummy value. This triggers loading of the catalina.properties
    // file
    CatalinaProperties.getProperty("dummy");

    appendSkipJars("tomcat.util.scan.DefaultJarScanner.jarsToSkip", this.skipJarsDefaultJarScanner);
    appendSkipJars(
        "org.apache.catalina.startup.ContextConfig.jarsToSkip", this.skipJarsContextConfig);
    appendSkipJars("org.apache.catalina.startup.TldConfig.jarsToSkip", this.skipJarsTldConfig);

    this.tomcat = new Tomcat();

    if (this.tempDirectory == null) {
      this.tempDirectory = new File(".", "/target/tomcat." + this.httpPort).getAbsolutePath();
    }

    this.tomcat.setBaseDir(this.tempDirectory);

    if (this.silent) {
      this.tomcat.setSilent(true);
    }

    if (this.addDefaultListeners) {
      this.tomcat.getServer().addLifecycleListener(new AprLifecycleListener());
    }

    if (this.useNio) {
      Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
      connector.setPort(this.httpPort);
      connector.setMaxPostSize(this.maxPostSize);
      connector.setURIEncoding("UTF-8");
      this.tomcat.setConnector(connector);
      this.tomcat.getService().addConnector(connector);
    } else {
      this.tomcat.setPort(this.httpPort);
      this.tomcat.getConnector().setURIEncoding("UTF-8");
      this.tomcat.getConnector().setMaxPostSize(this.maxPostSize);
    }

    if (this.compressionMinSize >= 0) {
      this.tomcat
          .getConnector()
          .setProperty("compression", String.valueOf(this.compressionMinSize));
      this.tomcat.getConnector().setProperty("compressableMimeType", this.compressableMimeType);
    }

    if (this.httpsPort != 0) {
      final Connector httpsConnector;
      if (this.useNio) {
        httpsConnector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
      } else {
        httpsConnector = new Connector("HTTP/1.1");
      }
      httpsConnector.setSecure(true);
      httpsConnector.setPort(this.httpsPort);
      httpsConnector.setMaxPostSize(this.maxPostSize);
      httpsConnector.setScheme("https");
      httpsConnector.setURIEncoding("UTF-8");

      httpsConnector.setProperty("SSLEnabled", "true");
      httpsConnector.setProperty("keyAlias", this.keyAlias);
      httpsConnector.setProperty("keystoreFile", this.keyStoreFile);
      httpsConnector.setProperty("keystorePass", this.keyStorePass);
      httpsConnector.setProperty("sslProtocol", this.sslProtocol);

      if (this.compressionMinSize >= 0) {
        httpsConnector.setProperty("compression", String.valueOf(this.compressionMinSize));
        httpsConnector.setProperty("compressableMimeType", this.compressableMimeType);
      }

      this.tomcat.getEngine().setDefaultHost("localhost");
      this.tomcat.getService().addConnector(httpsConnector);
    }

    if (this.shutdownPort != null) {
      this.tomcat.getServer().setPort(this.shutdownPort);
    }

    String contextDir = this.contextDirectory;
    if (contextDir == null) {
      contextDir = new File(".").getAbsolutePath() + "/src/main/webapp";
    }

    final Context ctx;
    try {

      if (!this.contextPath.equals("")) {
        File rootCtxDir = new File("./target/tcroot");
        if (!rootCtxDir.exists()) {
          rootCtxDir.mkdirs();
        }
        Context rootCtx = this.tomcat.addWebapp("", rootCtxDir.getAbsolutePath());
        rootCtx.setPrivileged(true);
        Tomcat.addServlet(rootCtx, "listContexts", new ListContextsServlet(rootCtx))
            .addMapping("/");
      }

      ctx = this.tomcat.addWebapp(this.contextPath, contextDir);
      ctx.setResources(new TargetClassesContext());
    } catch (ServletException e) {
      throw new RuntimeException(e);
    }

    if (this.privileged) {
      ctx.setPrivileged(true);
    }

    if (this.enableNaming
        || !this.contextEnvironments.isEmpty()
        || !this.contextResources.isEmpty()
        || this.contextFileURL != null) {
      this.tomcat.enableNaming();

      if (this.addDefaultListeners) {
        this.tomcat.getServer().addLifecycleListener(new GlobalResourcesLifecycleListener());
      }
    }

    if (this.addDefaultListeners) {
      Server server = this.tomcat.getServer();
      server.addLifecycleListener(new JasperListener());
      server.addLifecycleListener(new JreMemoryLeakPreventionListener());
      server.addLifecycleListener(new ThreadLocalLeakPreventionListener());
    }

    for (ContextEnvironment env : this.contextEnvironments) {
      ctx.getNamingResources().addEnvironment(env);
    }

    for (ContextResource res : this.contextResources) {
      ctx.getNamingResources().addResource(res);
    }

    for (ApplicationParameter param : this.contextInitializationParameters) {
      ctx.addApplicationParameter(param);
    }

    if (this.contextFileURL != null) {
      ctx.setConfigFile(this.contextFileURL);
    }

    // Shutdown tomcat if a failure occurs during startup
    ctx.addLifecycleListener(
        new LifecycleListener() {
          @Override
          public void lifecycleEvent(LifecycleEvent event) {
            if (event.getLifecycle().getState() == LifecycleState.FAILED) {
              ((StandardServer) EmbeddedTomcat.this.tomcat.getServer()).stopAwait();
            }
          }
        });

    try {
      this.tomcat.start();
    } catch (LifecycleException e) {
      throw new RuntimeException(e);
    }

    ((StandardManager) ctx.getManager()).setPathname(null);

    installSlf4jBridge();

    if (await) {
      this.tomcat.getServer().await();
      stop();
    }
  }