Skip to content

Commit

Permalink
servers - introduced common WebServerBase superclass intended to shar…
Browse files Browse the repository at this point in the history
…e common WebServer code.

This fixes #152
  • Loading branch information
fcamblor committed Sep 9, 2017
1 parent 6772f7e commit 6b20ac6
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 280 deletions.
108 changes: 108 additions & 0 deletions restx-core/src/main/java/restx/server/WebServerBase.java
@@ -0,0 +1,108 @@
package restx.server;

import restx.common.Version;

import java.util.concurrent.atomic.AtomicLong;

import static com.google.common.base.Preconditions.checkNotNull;
import static restx.common.MoreFiles.checkFileExists;
import static restx.common.MoreIO.checkCanOpenSocket;

public abstract class WebServerBase implements WebServer {
protected static final AtomicLong SERVER_ID = new AtomicLong();

protected final int port;
protected final String bindInterface;
protected final String appBase;
protected String serverId;

protected final String serverTypeName;
protected final String serverGroupId;
protected final String serverModule;

protected WebServerBase(String appBase, int port, String bindInterface, String serverTypeName, String serverGroupId, String serverModule) {
if(appBase != null) {
checkFileExists(appBase);
}

this.port = port;
this.bindInterface = bindInterface;
this.appBase = appBase;
this.serverId = serverTypeName + "#" + SERVER_ID.incrementAndGet();

this.serverTypeName = serverTypeName;
this.serverGroupId = serverGroupId;
this.serverModule = serverModule;
}

/**
* Sets the serverId used by this server.
*
* Must not be called when server is started.
*
* The serverId is used to uniquely identify the main Factory used by REST main router in this server.
* It allows to access the Factory with Factory.getInstance(serverId).
*
* @param serverId the server id to set. Must be unique in the JVM.
*
* @return current server
*/
public synchronized WebServerBase setServerId(final String serverId) {
if (isStarted()) {
throw new IllegalStateException("can't set server id when server is started");
}
this.serverId = serverId;
return this;
}

@Override
public String getServerType() {
return serverTypeName + " " + Version.getVersion(serverGroupId, serverModule) + ", embedded";
}

@Override
public String getServerId() {
return serverId;
}

@Override
public int getPort() {
return port;
}

@Override
public String baseUrl() {
// Dunno why, but if I use bindInterface here, some test will fail in ContextParamsTest
return WebServers.baseUri("127.0.0.1", port);
}

@Override
public synchronized void start() throws Exception {
checkCanOpenSocket(port);
WebServers.register(this);

this._start();
}

@Override
public void startAndAwait() throws Exception {
start();
await();
}

@Override
public synchronized void stop() throws Exception {
this._stop();

WebServers.unregister(serverId);
}

@Override
public synchronized boolean isStarted() {
return WebServers.getServerById(serverId).isPresent();
}

public abstract void await() throws InterruptedException;
protected abstract void _start() throws Exception;
protected abstract void _stop() throws Exception;
}
79 changes: 4 additions & 75 deletions restx-server-jetty7/src/main/java/restx/server/JettyWebServer.java
Expand Up @@ -15,91 +15,32 @@
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import restx.common.Version;

import java.util.concurrent.atomic.AtomicLong;

import static com.google.common.base.Preconditions.checkNotNull;
import static restx.common.MoreFiles.checkFileExists;
import static restx.common.MoreIO.checkCanOpenSocket;

public class JettyWebServer implements WebServer {
private static final AtomicLong SERVER_ID = new AtomicLong();

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

private Server server;
private int port;
private String bindInterface;
private String appBase;
private String webInfLocation;
private String serverId;


public JettyWebServer(String appBase, int aPort) {
this(null, appBase, aPort, null);
}

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

if (webInfLocation != null) {
checkFileExists(webInfLocation);
}

this.port = port;
this.bindInterface = bindInterface;
this.appBase = appBase;
this.webInfLocation = webInfLocation;
this.serverId = "Jetty#" + SERVER_ID.incrementAndGet();
}

/**
* Sets the serverId used by this server.
*
* Must not be called when server is started.
*
* The serverId is used to uniquely identify the main Factory used by REST main router in this server.
* It allows to access the Factory with Factory.getInstance(serverId).
*
* @param serverId the server id to set. Must be unique in the JVM.
*
* @return current server
*/
public synchronized JettyWebServer setServerId(final String serverId) {
if (isStarted()) {
throw new IllegalStateException("can't set server id when server is started");
}
this.serverId = serverId;
return this;
}

@Override
public String getServerId() {
return serverId;
}

@Override
public int getPort() {
return port;
}

@Override
public String baseUrl() {
return WebServers.baseUri("127.0.0.1", port);
}

@Override
public String getServerType() {
return "Jetty " + Version.getVersion("org.eclipse.jetty", "jetty-server") + ", embedded";
}

@Override
public synchronized void start() throws Exception {
checkCanOpenSocket(port);

protected void _start() throws Exception {
server = new Server();
WebServers.register(this);

server.setThreadPool(createThreadPool());
server.addConnector(createConnector());
Expand All @@ -109,27 +50,15 @@ public synchronized void start() throws Exception {
server.start();
}

@Override
public void startAndAwait() throws Exception {
start();
await();
}

@Override
public void await() throws InterruptedException {
server.join();
}

@Override
public synchronized void stop() throws Exception {
protected void _stop() throws Exception {
server.stop();
server = null;
WebServers.unregister(serverId);
}

@Override
public synchronized boolean isStarted() {
return server != null;
}

protected ThreadPool createThreadPool() {
Expand Down
79 changes: 4 additions & 75 deletions restx-server-jetty8/src/main/java/restx/server/JettyWebServer.java
Expand Up @@ -15,91 +15,32 @@
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import restx.common.Version;

import java.util.concurrent.atomic.AtomicLong;

import static com.google.common.base.Preconditions.checkNotNull;
import static restx.common.MoreFiles.checkFileExists;
import static restx.common.MoreIO.checkCanOpenSocket;

public class JettyWebServer implements WebServer {
private static final AtomicLong SERVER_ID = new AtomicLong();

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

private Server server;
private int port;
private String bindInterface;
private String appBase;
private String webInfLocation;
private String serverId;


public JettyWebServer(String appBase, int aPort) {
this(null, appBase, aPort, null);
}

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

if (webInfLocation != null) {
checkFileExists(webInfLocation);
}

this.port = port;
this.bindInterface = bindInterface;
this.appBase = appBase;
this.webInfLocation = webInfLocation;
this.serverId = "Jetty#" + SERVER_ID.incrementAndGet();
}

/**
* Sets the serverId used by this server.
*
* Must not be called when server is started.
*
* The serverId is used to uniquely identify the main Factory used by REST main router in this server.
* It allows to access the Factory with Factory.getInstance(serverId).
*
* @param serverId the server id to set. Must be unique in the JVM.
*
* @return current server
*/
public synchronized JettyWebServer setServerId(final String serverId) {
if (isStarted()) {
throw new IllegalStateException("can't set server id when server is started");
}
this.serverId = serverId;
return this;
}

@Override
public String getServerId() {
return serverId;
}

@Override
public int getPort() {
return port;
}

@Override
public String baseUrl() {
return WebServers.baseUri("127.0.0.1", port);
}

@Override
public String getServerType() {
return "Jetty " + Version.getVersion("org.eclipse.jetty", "jetty-server") + ", embedded";
}

@Override
public synchronized void start() throws Exception {
checkCanOpenSocket(port);

protected void _start() throws Exception {
server = new Server();
WebServers.register(this);

server.setThreadPool(createThreadPool());
server.addConnector(createConnector());
Expand All @@ -109,27 +50,15 @@ public synchronized void start() throws Exception {
server.start();
}

@Override
public void startAndAwait() throws Exception {
start();
await();
}

@Override
public void await() throws InterruptedException {
server.join();
}

@Override
public synchronized void stop() throws Exception {
protected void _stop() throws Exception {
server.stop();
server = null;
WebServers.unregister(serverId);
}

@Override
public synchronized boolean isStarted() {
return server != null;
}

protected ThreadPool createThreadPool() {
Expand Down

0 comments on commit 6b20ac6

Please sign in to comment.