Skip to content

Commit

Permalink
Added protocol to ServerConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbodart committed Oct 24, 2015
1 parent 10f9922 commit 3992cbf
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/com/googlecode/utterlyidle/ServerConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,29 @@
import static java.lang.String.format;

public class ServerConfiguration {

public static final String SERVER_PROTOCOL = "server.protocol";
public static final String SERVER_BASE_PATH = "server.base.path";
public static final String MAX_THREAD_NUM = "server.max.thread.number";
public static final String SERVER_BIND_ADDRESS = "server.bind.address";
public static final String SERVER_PORT = "server.port";
public static final String SERVER_CLASS = "server.class";

public static final String DEFAULT_PROTOCOL = Protocol.HTTP;
public static final String DEFAULT_BASE_PATH = "/";
public static final String DEFAULT_THREAD_NUMBER = "50";
public static final String DEFAULT_BIND_ADDRESS = "0.0.0.0";
public static final String DEFAULT_PORT = "0";
public static final String DEFAULT_CLASS = RestServer.class.getCanonicalName();

private final String protocol;
private final BasePath basePath;
private final int maxThreadNumber;
private final InetAddress bindAddress;
private final int port;
private final Class<? extends Server> serverClass;

public ServerConfiguration(BasePath basePath, int maxThreadNumber, InetAddress bindAddress, int port, Class<? extends Server> serverClass) {
public ServerConfiguration(String protocol, BasePath basePath, int maxThreadNumber, InetAddress bindAddress, int port, Class<? extends Server> serverClass) {
this.protocol = protocol;
this.basePath = basePath;
this.maxThreadNumber = maxThreadNumber;
this.bindAddress = bindAddress;
Expand All @@ -41,11 +44,12 @@ public ServerConfiguration(BasePath basePath, int maxThreadNumber, InetAddress b
}

public ServerConfiguration() {
this( new BasePath(DEFAULT_BASE_PATH), Integer.parseInt(DEFAULT_THREAD_NUMBER), toInetAddress(DEFAULT_BIND_ADDRESS), Integer.parseInt(DEFAULT_PORT), toServer(DEFAULT_CLASS));
this(DEFAULT_PROTOCOL, new BasePath(DEFAULT_BASE_PATH), Integer.parseInt(DEFAULT_THREAD_NUMBER), toInetAddress(DEFAULT_BIND_ADDRESS), Integer.parseInt(DEFAULT_PORT), toServer(DEFAULT_CLASS));
}

public ServerConfiguration(Properties properties) {
this(new BasePath(properties.getProperty(SERVER_BASE_PATH, DEFAULT_BASE_PATH)),
this(properties.getProperty(SERVER_PROTOCOL, DEFAULT_PROTOCOL),
new BasePath(properties.getProperty(SERVER_BASE_PATH, DEFAULT_BASE_PATH)),
valueOf(properties.getProperty(MAX_THREAD_NUM, DEFAULT_THREAD_NUMBER)),
toInetAddress(properties.getProperty(SERVER_BIND_ADDRESS, DEFAULT_BIND_ADDRESS)),
valueOf(properties.getProperty(SERVER_PORT, DEFAULT_PORT)),
Expand All @@ -61,39 +65,47 @@ public BasePath basePath() {
}

public ServerConfiguration basePath(BasePath basePath) {
return new ServerConfiguration(basePath, maxThreadNumber, bindAddress, port, serverClass);
return new ServerConfiguration(protocol, basePath, maxThreadNumber, bindAddress, port, serverClass);
}

public int maxThreadNumber() {
return maxThreadNumber;
}

public ServerConfiguration maxThreadNumber(int maxThreadNumber) {
return new ServerConfiguration(basePath, maxThreadNumber, bindAddress, port, serverClass);
return new ServerConfiguration(protocol, basePath, maxThreadNumber, bindAddress, port, serverClass);
}

public Class<? extends Server> serverClass() {
return serverClass;
}

public ServerConfiguration serverClass(Class<? extends Server> serverClass) {
return new ServerConfiguration(basePath, maxThreadNumber, bindAddress, port, serverClass);
return new ServerConfiguration(protocol, basePath, maxThreadNumber, bindAddress, port, serverClass);
}

public InetAddress bindAddress() {
return bindAddress;
}

public ServerConfiguration bindAddress(InetAddress bindAddress) {
return new ServerConfiguration(basePath, maxThreadNumber, bindAddress, port, serverClass);
return new ServerConfiguration(protocol, basePath, maxThreadNumber, bindAddress, port, serverClass);
}

public int port() {
return port;
}

public ServerConfiguration port(int bindPort) {
return new ServerConfiguration(basePath, maxThreadNumber, bindAddress, bindPort, serverClass);
return new ServerConfiguration(protocol, basePath, maxThreadNumber, bindAddress, bindPort, serverClass);
}

public int port() {
return port;
public String protocol() {
return protocol;
}

public ServerConfiguration protocol(final String protocol) {
return new ServerConfiguration(protocol, basePath, maxThreadNumber, bindAddress, port, serverClass);
}

private static InetAddress toInetAddress(final String address) {
Expand All @@ -112,8 +124,7 @@ private static Class<? extends Server> toServer(String className) {
}
}


public Uri toUrl() {
return uri(format("http://localhost:%s%s", port(), basePath()));
return uri(format("%s://localhost:%s%s", protocol(), port(), basePath()));
}
}
}

0 comments on commit 3992cbf

Please sign in to comment.