Skip to content

Commit

Permalink
Merge pull request #3 from olyabobra/master
Browse files Browse the repository at this point in the history
Olga: adding different RestServer support for Waitrest
  • Loading branch information
danielbodart committed Dec 9, 2015
2 parents 40d14e0 + 76be8ad commit 8845c5c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
3 changes: 3 additions & 0 deletions build/build.dependencies
@@ -1,6 +1,9 @@
mvn:org.hamcrest:hamcrest-core:jar:1.2
mvn:org.hamcrest:hamcrest-library:jar:1.2
mvn:junit:junit-dep:jar:4.8.2
mvn:org.mortbay.jetty:jetty-util:jar:6.1.26
mvn:org.mortbay.jetty:jetty:jar:6.1.26
mvn:javax.servlet:javax.servlet-api:jar:3.1.0

mvn://repo.bodar.com/com.googlecode.utterlyidle:utterlyidle:pack|sources:1.13
mvn://repo.bodar.com/com.googlecode.totallylazy:totallylazy:pack|sources:1.22
Expand Down
18 changes: 5 additions & 13 deletions src/com/googlecode/waitrest/Main.java
@@ -1,19 +1,11 @@
package com.googlecode.waitrest;

import static com.googlecode.waitrest.Waitrest.serverProperties;

public class Main {
public static void main(String[] args) throws Exception {
new Waitrest(port(args));
}

private static Integer port(String[] args) {
Integer port = null;
if (args.length > 0) {
try {
port = Integer.parseInt(args[0]);
} catch (Exception ex) {
throw new IllegalArgumentException("The port " + args[0] + " should be an integer");
}
}
return port;
String serverPort = (args.length > 0)? args[0]: null;
String serverClass = (args.length > 1)? args[1]: null;
new Waitrest(serverProperties(null, serverPort, serverClass));
}
}
44 changes: 27 additions & 17 deletions src/com/googlecode/waitrest/Waitrest.java
Expand Up @@ -3,56 +3,66 @@
import com.googlecode.totallylazy.Closeables;
import com.googlecode.totallylazy.Uri;
import com.googlecode.utterlyidle.Application;
import com.googlecode.utterlyidle.BasePath;
import com.googlecode.utterlyidle.ApplicationBuilder;
import com.googlecode.utterlyidle.Server;
import com.googlecode.utterlyidle.httpserver.RestServer;
import com.googlecode.utterlyidle.ServerConfiguration;

import java.io.Closeable;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

import static com.googlecode.utterlyidle.ServerConfiguration.defaultConfiguration;
import static com.googlecode.utterlyidle.ServerConfiguration.SERVER_BASE_PATH;
import static com.googlecode.utterlyidle.ServerConfiguration.SERVER_CLASS;
import static com.googlecode.utterlyidle.ServerConfiguration.SERVER_PORT;

public class Waitrest implements Closeable {
private RestServer restServer;
private Restaurant application;
private Server server;
private Application application;

public Waitrest(Properties properties) {
this.server = ApplicationBuilder.application(Restaurant.class).start(new ServerConfiguration(properties));
this.application = server.application();
}

public Waitrest(String basePath, Integer port) {
try {
application = new Restaurant(BasePath.basePath(basePath));
restServer = new RestServer(application, port == null ? defaultConfiguration() : defaultConfiguration().port(port));
} catch (Exception e) {
throw new RuntimeException("Couldn't start Waitrest: " + e.getMessage());
}
this(serverProperties(basePath, port == null ? null : port.toString(), null));
}

public Waitrest(Integer port) {
this("/", port);
this(null, port);
}

public Waitrest() {
this("/", null);
this(new Properties());
}

public void close() {
Closeables.safeClose(application);
Closeables.safeClose(restServer);
Closeables.safeClose(server);
}

public URL getURL() {
return uri().toURL();
}

public Uri uri() {
return restServer.uri();
return server.uri();
}

public Server server() {
return restServer;
return server;
}

public Application application() {
return application;
}

public static Properties serverProperties(String basePath, String port, String serverClass) {
Properties properties = new Properties();
if (basePath != null) properties.setProperty(SERVER_BASE_PATH, basePath);
if (port != null) properties.setProperty(SERVER_PORT, port);
if (serverClass != null) properties.setProperty(SERVER_CLASS, serverClass);
return properties;
}

}

0 comments on commit 8845c5c

Please sign in to comment.