/
SunHttpJaxrsServer.java
180 lines (160 loc) · 4.82 KB
/
SunHttpJaxrsServer.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package org.jboss.resteasy.plugins.server.sun.http;
import com.sun.net.httpserver.HttpServer;
import org.jboss.resteasy.plugins.server.embedded.EmbeddedJaxrsServer;
import org.jboss.resteasy.plugins.server.embedded.SecurityDomain;
import org.jboss.resteasy.spi.ResteasyDeployment;
import org.jboss.resteasy.util.EmbeddedServerHelper;
import org.jboss.resteasy.util.PortProvider;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
/**
* com.sun.net.httpserver.HttpServer adapter for Resteasy. You may instead want to create and manage your own HttpServer.
* Use the HttpContextBuilder class in this case to build and register a specific HttpContext.
*
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public class SunHttpJaxrsServer implements EmbeddedJaxrsServer<SunHttpJaxrsServer>
{
protected HttpContextBuilder context = new HttpContextBuilder();
protected HttpServer httpServer;
protected int configuredPort = PortProvider.getPort();
protected int runtimePort = -1;
protected ResteasyDeployment deployment;
private EmbeddedServerHelper serverHelper = new EmbeddedServerHelper();
private final ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override
public Thread newThread(final Runnable r) {
final Thread result = new Thread(r);
final ClassLoader cl;
if (deployment == null) {
cl = SunHttpJaxrsServer.class.getClassLoader();
} else {
cl = deployment.getClass().getClassLoader();
}
// TODO (jrp) think about this and if it should be privileged
result.setContextClassLoader(cl);
result.setName("EmbeddedJaxrsServer");
result.setDaemon(true);
return result;
}
});
@Override
public SunHttpJaxrsServer deploy() {
// no-op
return this;
}
@Override
public SunHttpJaxrsServer start()
{
serverHelper.checkDeployment(deployment);
String aPath = serverHelper.checkAppDeployment(deployment);
if (aPath == null) {
aPath = context.getPath();
}
setRootResourcePath(serverHelper.checkContextPath(aPath));
if (httpServer == null)
{
try
{
httpServer = HttpServer.create(new InetSocketAddress(configuredPort), 10);
httpServer.setExecutor(executor);
runtimePort = httpServer.getAddress().getPort();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
context.bind(httpServer);
httpServer.start();
return this;
}
@Override
public void stop()
{
runtimePort = -1;
httpServer.stop(0);
executor.shutdown();
try {
// TODO (jrp) is 10 correct? That is what the VertxJaxrsServer uses
executor.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException ignore) {
Thread.currentThread().interrupt();
}
context.cleanup();
if (deployment != null) {
deployment.stop();
}
}
@Override
public ResteasyDeployment getDeployment() {
if(deployment == null) {
deployment = context.getDeployment();
}
return deployment;
}
@Override
public SunHttpJaxrsServer setDeployment(ResteasyDeployment deployment)
{
this.deployment = deployment;
this.context.setDeployment(deployment);
return this;
}
/**
* Value is ignored if HttpServer property is set. Default value is 8080
*
* @param port
*/
@Override
public SunHttpJaxrsServer setPort(int port)
{
this.configuredPort = port;
return this;
}
/**
* Gets port number of this HttpServer.
*
* @return port number.
*/
public int getPort()
{
return runtimePort > 0 ? runtimePort : configuredPort;
}
@Override
public SunHttpJaxrsServer setHostname(String hostname) {
// no-op
return this;
}
/**
* Setting a security domain will turn on Basic Authentication
*
* @param securityDomain
*/
@Override
public SunHttpJaxrsServer setSecurityDomain(SecurityDomain securityDomain)
{
this.context.setSecurityDomain(securityDomain);
return this;
}
@Override
public SunHttpJaxrsServer setRootResourcePath(String rootResourcePath)
{
context.setPath(rootResourcePath);
return this;
}
/**
* If you do not provide an HttpServer instance, one will be created on startup
*
* @param httpServer
*/
public SunHttpJaxrsServer setHttpServer(HttpServer httpServer)
{
this.httpServer = httpServer;
return this;
}
}