Skip to content

Commit 6b20ac6

Browse files
committed
servers - introduced common WebServerBase superclass intended to share common WebServer code.
This fixes #152
1 parent 6772f7e commit 6b20ac6

File tree

5 files changed

+131
-280
lines changed

5 files changed

+131
-280
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package restx.server;
2+
3+
import restx.common.Version;
4+
5+
import java.util.concurrent.atomic.AtomicLong;
6+
7+
import static com.google.common.base.Preconditions.checkNotNull;
8+
import static restx.common.MoreFiles.checkFileExists;
9+
import static restx.common.MoreIO.checkCanOpenSocket;
10+
11+
public abstract class WebServerBase implements WebServer {
12+
protected static final AtomicLong SERVER_ID = new AtomicLong();
13+
14+
protected final int port;
15+
protected final String bindInterface;
16+
protected final String appBase;
17+
protected String serverId;
18+
19+
protected final String serverTypeName;
20+
protected final String serverGroupId;
21+
protected final String serverModule;
22+
23+
protected WebServerBase(String appBase, int port, String bindInterface, String serverTypeName, String serverGroupId, String serverModule) {
24+
if(appBase != null) {
25+
checkFileExists(appBase);
26+
}
27+
28+
this.port = port;
29+
this.bindInterface = bindInterface;
30+
this.appBase = appBase;
31+
this.serverId = serverTypeName + "#" + SERVER_ID.incrementAndGet();
32+
33+
this.serverTypeName = serverTypeName;
34+
this.serverGroupId = serverGroupId;
35+
this.serverModule = serverModule;
36+
}
37+
38+
/**
39+
* Sets the serverId used by this server.
40+
*
41+
* Must not be called when server is started.
42+
*
43+
* The serverId is used to uniquely identify the main Factory used by REST main router in this server.
44+
* It allows to access the Factory with Factory.getInstance(serverId).
45+
*
46+
* @param serverId the server id to set. Must be unique in the JVM.
47+
*
48+
* @return current server
49+
*/
50+
public synchronized WebServerBase setServerId(final String serverId) {
51+
if (isStarted()) {
52+
throw new IllegalStateException("can't set server id when server is started");
53+
}
54+
this.serverId = serverId;
55+
return this;
56+
}
57+
58+
@Override
59+
public String getServerType() {
60+
return serverTypeName + " " + Version.getVersion(serverGroupId, serverModule) + ", embedded";
61+
}
62+
63+
@Override
64+
public String getServerId() {
65+
return serverId;
66+
}
67+
68+
@Override
69+
public int getPort() {
70+
return port;
71+
}
72+
73+
@Override
74+
public String baseUrl() {
75+
// Dunno why, but if I use bindInterface here, some test will fail in ContextParamsTest
76+
return WebServers.baseUri("127.0.0.1", port);
77+
}
78+
79+
@Override
80+
public synchronized void start() throws Exception {
81+
checkCanOpenSocket(port);
82+
WebServers.register(this);
83+
84+
this._start();
85+
}
86+
87+
@Override
88+
public void startAndAwait() throws Exception {
89+
start();
90+
await();
91+
}
92+
93+
@Override
94+
public synchronized void stop() throws Exception {
95+
this._stop();
96+
97+
WebServers.unregister(serverId);
98+
}
99+
100+
@Override
101+
public synchronized boolean isStarted() {
102+
return WebServers.getServerById(serverId).isPresent();
103+
}
104+
105+
public abstract void await() throws InterruptedException;
106+
protected abstract void _start() throws Exception;
107+
protected abstract void _stop() throws Exception;
108+
}

restx-server-jetty7/src/main/java/restx/server/JettyWebServer.java

+4-75
Original file line numberDiff line numberDiff line change
@@ -15,91 +15,32 @@
1515
import org.eclipse.jetty.webapp.WebAppContext;
1616
import org.slf4j.Logger;
1717
import org.slf4j.LoggerFactory;
18-
import restx.common.Version;
19-
20-
import java.util.concurrent.atomic.AtomicLong;
2118

2219
import static com.google.common.base.Preconditions.checkNotNull;
2320
import static restx.common.MoreFiles.checkFileExists;
24-
import static restx.common.MoreIO.checkCanOpenSocket;
25-
26-
public class JettyWebServer implements WebServer {
27-
private static final AtomicLong SERVER_ID = new AtomicLong();
2821

22+
public class JettyWebServer extends WebServerBase {
2923
private static final Logger logger = LoggerFactory.getLogger(JettyWebServer.class);
3024

3125
private Server server;
32-
private int port;
33-
private String bindInterface;
34-
private String appBase;
3526
private String webInfLocation;
36-
private String serverId;
37-
3827

3928
public JettyWebServer(String appBase, int aPort) {
4029
this(null, appBase, aPort, null);
4130
}
4231

4332
public JettyWebServer(String webInfLocation, String appBase, int port, String bindInterface) {
44-
checkFileExists(checkNotNull(appBase));
33+
super(checkNotNull(appBase), port, bindInterface, "Jetty7", "org.eclipse.jetty", "jetty-server");
4534

4635
if (webInfLocation != null) {
4736
checkFileExists(webInfLocation);
4837
}
49-
50-
this.port = port;
51-
this.bindInterface = bindInterface;
52-
this.appBase = appBase;
5338
this.webInfLocation = webInfLocation;
54-
this.serverId = "Jetty#" + SERVER_ID.incrementAndGet();
55-
}
56-
57-
/**
58-
* Sets the serverId used by this server.
59-
*
60-
* Must not be called when server is started.
61-
*
62-
* The serverId is used to uniquely identify the main Factory used by REST main router in this server.
63-
* It allows to access the Factory with Factory.getInstance(serverId).
64-
*
65-
* @param serverId the server id to set. Must be unique in the JVM.
66-
*
67-
* @return current server
68-
*/
69-
public synchronized JettyWebServer setServerId(final String serverId) {
70-
if (isStarted()) {
71-
throw new IllegalStateException("can't set server id when server is started");
72-
}
73-
this.serverId = serverId;
74-
return this;
75-
}
76-
77-
@Override
78-
public String getServerId() {
79-
return serverId;
80-
}
81-
82-
@Override
83-
public int getPort() {
84-
return port;
8539
}
8640

8741
@Override
88-
public String baseUrl() {
89-
return WebServers.baseUri("127.0.0.1", port);
90-
}
91-
92-
@Override
93-
public String getServerType() {
94-
return "Jetty " + Version.getVersion("org.eclipse.jetty", "jetty-server") + ", embedded";
95-
}
96-
97-
@Override
98-
public synchronized void start() throws Exception {
99-
checkCanOpenSocket(port);
100-
42+
protected void _start() throws Exception {
10143
server = new Server();
102-
WebServers.register(this);
10344

10445
server.setThreadPool(createThreadPool());
10546
server.addConnector(createConnector());
@@ -109,27 +50,15 @@ public synchronized void start() throws Exception {
10950
server.start();
11051
}
11152

112-
@Override
113-
public void startAndAwait() throws Exception {
114-
start();
115-
await();
116-
}
117-
11853
@Override
11954
public void await() throws InterruptedException {
12055
server.join();
12156
}
12257

12358
@Override
124-
public synchronized void stop() throws Exception {
59+
protected void _stop() throws Exception {
12560
server.stop();
12661
server = null;
127-
WebServers.unregister(serverId);
128-
}
129-
130-
@Override
131-
public synchronized boolean isStarted() {
132-
return server != null;
13362
}
13463

13564
protected ThreadPool createThreadPool() {

restx-server-jetty8/src/main/java/restx/server/JettyWebServer.java

+4-75
Original file line numberDiff line numberDiff line change
@@ -15,91 +15,32 @@
1515
import org.eclipse.jetty.webapp.WebAppContext;
1616
import org.slf4j.Logger;
1717
import org.slf4j.LoggerFactory;
18-
import restx.common.Version;
19-
20-
import java.util.concurrent.atomic.AtomicLong;
2118

2219
import static com.google.common.base.Preconditions.checkNotNull;
2320
import static restx.common.MoreFiles.checkFileExists;
24-
import static restx.common.MoreIO.checkCanOpenSocket;
25-
26-
public class JettyWebServer implements WebServer {
27-
private static final AtomicLong SERVER_ID = new AtomicLong();
2821

22+
public class JettyWebServer extends WebServerBase {
2923
private static final Logger logger = LoggerFactory.getLogger(JettyWebServer.class);
3024

3125
private Server server;
32-
private int port;
33-
private String bindInterface;
34-
private String appBase;
3526
private String webInfLocation;
36-
private String serverId;
37-
3827

3928
public JettyWebServer(String appBase, int aPort) {
4029
this(null, appBase, aPort, null);
4130
}
4231

4332
public JettyWebServer(String webInfLocation, String appBase, int port, String bindInterface) {
44-
checkFileExists(checkNotNull(appBase));
33+
super(checkNotNull(appBase), port, bindInterface, "Jetty8", "org.eclipse.jetty", "jetty-server");
4534

4635
if (webInfLocation != null) {
4736
checkFileExists(webInfLocation);
4837
}
49-
50-
this.port = port;
51-
this.bindInterface = bindInterface;
52-
this.appBase = appBase;
5338
this.webInfLocation = webInfLocation;
54-
this.serverId = "Jetty#" + SERVER_ID.incrementAndGet();
55-
}
56-
57-
/**
58-
* Sets the serverId used by this server.
59-
*
60-
* Must not be called when server is started.
61-
*
62-
* The serverId is used to uniquely identify the main Factory used by REST main router in this server.
63-
* It allows to access the Factory with Factory.getInstance(serverId).
64-
*
65-
* @param serverId the server id to set. Must be unique in the JVM.
66-
*
67-
* @return current server
68-
*/
69-
public synchronized JettyWebServer setServerId(final String serverId) {
70-
if (isStarted()) {
71-
throw new IllegalStateException("can't set server id when server is started");
72-
}
73-
this.serverId = serverId;
74-
return this;
75-
}
76-
77-
@Override
78-
public String getServerId() {
79-
return serverId;
80-
}
81-
82-
@Override
83-
public int getPort() {
84-
return port;
8539
}
8640

8741
@Override
88-
public String baseUrl() {
89-
return WebServers.baseUri("127.0.0.1", port);
90-
}
91-
92-
@Override
93-
public String getServerType() {
94-
return "Jetty " + Version.getVersion("org.eclipse.jetty", "jetty-server") + ", embedded";
95-
}
96-
97-
@Override
98-
public synchronized void start() throws Exception {
99-
checkCanOpenSocket(port);
100-
42+
protected void _start() throws Exception {
10143
server = new Server();
102-
WebServers.register(this);
10344

10445
server.setThreadPool(createThreadPool());
10546
server.addConnector(createConnector());
@@ -109,27 +50,15 @@ public synchronized void start() throws Exception {
10950
server.start();
11051
}
11152

112-
@Override
113-
public void startAndAwait() throws Exception {
114-
start();
115-
await();
116-
}
117-
11853
@Override
11954
public void await() throws InterruptedException {
12055
server.join();
12156
}
12257

12358
@Override
124-
public synchronized void stop() throws Exception {
59+
protected void _stop() throws Exception {
12560
server.stop();
12661
server = null;
127-
WebServers.unregister(serverId);
128-
}
129-
130-
@Override
131-
public synchronized boolean isStarted() {
132-
return server != null;
13362
}
13463

13564
protected ThreadPool createThreadPool() {

0 commit comments

Comments
 (0)