-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
SimpleServerFactory.java
131 lines (113 loc) · 4.83 KB
/
SimpleServerFactory.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package io.dropwizard.server;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.collect.ImmutableMap;
import io.dropwizard.jetty.ConnectorFactory;
import io.dropwizard.jetty.ContextRoutingHandler;
import io.dropwizard.jetty.HttpConnectorFactory;
import io.dropwizard.setup.Environment;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
/**
* A single-connector implementation of {@link ServerFactory}, suitable for PaaS deployments
* (e.g., Heroku) where applications are limited to a single, runtime-defined port. A startup script
* can override the port via {@code -Ddw.server.connector.port=$PORT}.
* <p/>
* <b>Configuration Parameters:</b>
* <table>
* <tr>
* <td>Name</td>
* <td>Default</td>
* <td>Description</td>
* </tr>
* <tr>
* <td>{@code connector}</td>
* <td>An {@link HttpConnectorFactory HTTP connector} listening on port {@code 8080}.</td>
* <td>The {@link ConnectorFactory connector} which will handle both application and admin requests.</td>
* </tr>
* <tr>
* <td>{@code applicationContextPath}</td>
* <td>{@code /application}</td>
* <td>The context path of the application servlets, including Jersey.</td>
* </tr>
* <tr>
* <td>{@code adminContextPath}</td>
* <td>{@code /admin}</td>
* <td>The context path of the admin servlets, including metrics and tasks.</td>
* </tr>
* </table>
* <p/>
* For more configuration parameters, see {@link AbstractServerFactory}.
*
* @see ServerFactory
* @see AbstractServerFactory
*/
@JsonTypeName("simple")
public class SimpleServerFactory extends AbstractServerFactory {
@Valid
@NotNull
private ConnectorFactory connector = HttpConnectorFactory.application();
@NotEmpty
private String applicationContextPath = "/application";
@NotEmpty
private String adminContextPath = "/admin";
@JsonProperty
public ConnectorFactory getConnector() {
return connector;
}
@JsonProperty
public void setConnector(ConnectorFactory factory) {
this.connector = factory;
}
@JsonProperty
public String getApplicationContextPath() {
return applicationContextPath;
}
@JsonProperty
public void setApplicationContextPath(String contextPath) {
this.applicationContextPath = contextPath;
}
@JsonProperty
public String getAdminContextPath() {
return adminContextPath;
}
@JsonProperty
public void setAdminContextPath(String contextPath) {
this.adminContextPath = contextPath;
}
@Override
public Server build(Environment environment) {
printBanner(environment.getName());
final ThreadPool threadPool = createThreadPool(environment.metrics());
final Server server = buildServer(environment.lifecycle(), threadPool);
environment.getApplicationContext().setContextPath(applicationContextPath);
final Handler applicationHandler = createAppServlet(server,
environment.jersey(),
environment.getObjectMapper(),
environment.getValidator(),
environment.getApplicationContext(),
environment.getJerseyServletContainer(),
environment.metrics());
environment.getAdminContext().setContextPath(adminContextPath);
final Handler adminHandler = createAdminServlet(server,
environment.getAdminContext(),
environment.metrics(),
environment.healthChecks());
final Connector conn = connector.build(server,
environment.metrics(),
environment.getName(),
null);
server.addConnector(conn);
final ContextRoutingHandler routingHandler = new ContextRoutingHandler(ImmutableMap.of(
applicationContextPath, applicationHandler,
adminContextPath, adminHandler
));
server.setHandler(addStatsHandler(addRequestLog(server, routingHandler, environment.getName())));
return server;
}
}