/
TomcatEmbedded.java
68 lines (55 loc) · 2.76 KB
/
TomcatEmbedded.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package info.mgsolutions.tomcat;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.coyote.http11.Http11AprProtocol;
import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.coyote.http2.Http2Protocol;
import java.io.File;
public class TomcatEmbedded {
private static final String TESTBED_HOME = System.getenv("TESTBED_HOME");
private static final String PORT = System.getenv("PORT");
private static final String MAX_THREADS = System.getProperty("tomcat.maxThreads", "200");
public static void main(String[] args) throws Exception {
Tomcat tomcat = new Tomcat();
StandardContext ctx = (StandardContext) tomcat.addContext("", new File(".").getAbsolutePath());
Tomcat.addServlet(ctx, "plaintext", PlainTextServlet.class.getName());
ctx.addServletMappingDecoded("/testbed/plaintext", "plaintext");
final Wrapper async = Tomcat.addServlet(ctx, "asyncplaintext", AsyncPlainTextServlet.class.getName());
async.setAsyncSupported(true);
ctx.addServletMappingDecoded("/testbed/asyncplaintext", "asyncplaintext");
final String protocolName = System.getProperty("tomcat.protocol", Http11NioProtocol.class.getName());
System.out.println("=== Protocol: " + protocolName);
Connector connector = new Connector(protocolName);
if (Http11AprProtocol.class.getName().equals(protocolName)) {
connector.addLifecycleListener(new AprLifecycleListener());
}
tomcat.setConnector(connector);
connector.setPort(Integer.parseInt(PORT, 10));
connector.setProperty("maxThreads", MAX_THREADS);
final boolean h2c = Boolean.getBoolean("tomcat.h2c");
final boolean http2 = Boolean.getBoolean("tomcat.http2");
boolean tls = Boolean.getBoolean("tomcat.tls");
if (h2c || http2) {
connector.addUpgradeProtocol(new Http2Protocol());
if (http2) {
tls = true;
}
}
if (tls) {
connector.setScheme("https");
connector.setSecure(true);
connector.setProperty("sslProtocol", "TLS");
connector.setProperty("SSLEnabled", "true");
connector.setProperty("SSLCertificateFile", TESTBED_HOME + "/etc/tls/server.pem");
connector.setProperty("SSLCertificateKeyFile", TESTBED_HOME + "/etc/tls/server.key");
}
System.out.println("=== Starting on port: " + connector);
tomcat.start();
System.out.println("=== Started");
tomcat.getServer().await();
System.out.println("=== Stopped");
}
}