diff --git a/build/build.dependencies b/build/build.dependencies index c332e33..337dff9 100644 --- a/build/build.dependencies +++ b/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 diff --git a/src/com/googlecode/waitrest/Main.java b/src/com/googlecode/waitrest/Main.java index e238bbc..52beab2 100644 --- a/src/com/googlecode/waitrest/Main.java +++ b/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)); } } diff --git a/src/com/googlecode/waitrest/Waitrest.java b/src/com/googlecode/waitrest/Waitrest.java index d2d7796..980e311 100644 --- a/src/com/googlecode/waitrest/Waitrest.java +++ b/src/com/googlecode/waitrest/Waitrest.java @@ -3,40 +3,42 @@ 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() { @@ -44,15 +46,23 @@ public URL getURL() { } 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; + } + }