From c01b049bd760a36a1952ad8df95cab3a3642df15 Mon Sep 17 00:00:00 2001 From: Team Meerkat Date: Fri, 4 Dec 2015 16:46:56 +0000 Subject: [PATCH 1/6] Olga: adding jetty support for Waitrest --- build/build.dependencies | 3 +++ src/com/googlecode/waitrest/Main.java | 18 ++++++++++++- src/com/googlecode/waitrest/Waitrest.java | 32 ++++++++++++++++++----- 3 files changed, 45 insertions(+), 8 deletions(-) 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..5511b7e 100644 --- a/src/com/googlecode/waitrest/Main.java +++ b/src/com/googlecode/waitrest/Main.java @@ -1,8 +1,11 @@ package com.googlecode.waitrest; +import com.googlecode.utterlyidle.Server; +import com.googlecode.utterlyidle.httpserver.RestServer; + public class Main { public static void main(String[] args) throws Exception { - new Waitrest(port(args)); + new Waitrest(port(args), serverClass(args)); } private static Integer port(String[] args) { @@ -16,4 +19,17 @@ private static Integer port(String[] args) { } return port; } + + private static Class serverClass(String[] args) { + Class serverClass = RestServer.class; + if (args.length > 1) { + try { + serverClass = (Class) Class.forName(args[1]); + } catch (Throwable ex) { + ex.printStackTrace(); + throw new IllegalArgumentException(String.format( "Invalid server class: %s", args[1])); + } + } + return serverClass; + } } diff --git a/src/com/googlecode/waitrest/Waitrest.java b/src/com/googlecode/waitrest/Waitrest.java index d2d7796..f85de6d 100644 --- a/src/com/googlecode/waitrest/Waitrest.java +++ b/src/com/googlecode/waitrest/Waitrest.java @@ -5,27 +5,35 @@ import com.googlecode.utterlyidle.Application; import com.googlecode.utterlyidle.BasePath; import com.googlecode.utterlyidle.Server; +import com.googlecode.utterlyidle.ServerConfiguration; import com.googlecode.utterlyidle.httpserver.RestServer; import java.io.Closeable; -import java.io.IOException; import java.net.URL; import static com.googlecode.utterlyidle.ServerConfiguration.defaultConfiguration; public class Waitrest implements Closeable { - private RestServer restServer; + private Server server; private Restaurant application; - public Waitrest(String basePath, Integer port) { + public Waitrest(String basePath, Integer port, Class serverClass) { try { application = new Restaurant(BasePath.basePath(basePath)); - restServer = new RestServer(application, port == null ? defaultConfiguration() : defaultConfiguration().port(port)); + this.server = initServer(port, serverClass); } catch (Exception e) { throw new RuntimeException("Couldn't start Waitrest: " + e.getMessage()); } } + public Waitrest(Integer port, Class serverClass) { + this("/", port, serverClass); + } + + public Waitrest(String basePath, Integer port) { + this(basePath, port, RestServer.class); + } + public Waitrest(Integer port) { this("/", port); } @@ -36,7 +44,7 @@ public Waitrest() { public void close() { Closeables.safeClose(application); - Closeables.safeClose(restServer); + Closeables.safeClose(server); } public URL getURL() { @@ -44,15 +52,25 @@ public URL getURL() { } public Uri uri() { - return restServer.uri(); + return server.uri(); } public Server server() { - return restServer; + return server; } public Application application() { return application; } + private Server initServer(Integer port, Class serverClass) throws Exception { + ServerConfiguration configuration = (port == null ? defaultConfiguration() : defaultConfiguration().port(port)); + if(serverClass.equals(RestServer.class)){ + return new RestServer(application, configuration); + } else if(serverClass.equals(com.googlecode.utterlyidle.jetty.RestServer.class)){ + return com.googlecode.utterlyidle.jetty.RestServer.restServer(application, configuration); + } + throw new RuntimeException(String.format("The server class %s is currently not supported", serverClass.getName())); + } + } From 72801de2f414a79f56fa70cb13318c3a79e32413 Mon Sep 17 00:00:00 2001 From: Team Meerkat Date: Tue, 8 Dec 2015 11:18:31 +0000 Subject: [PATCH 2/6] Olga: adding jetty support for Waitrest - version2 --- src/com/googlecode/waitrest/Main.java | 38 +++++++---------------- src/com/googlecode/waitrest/Waitrest.java | 28 ++++++----------- 2 files changed, 21 insertions(+), 45 deletions(-) diff --git a/src/com/googlecode/waitrest/Main.java b/src/com/googlecode/waitrest/Main.java index 5511b7e..6fc682d 100644 --- a/src/com/googlecode/waitrest/Main.java +++ b/src/com/googlecode/waitrest/Main.java @@ -1,35 +1,21 @@ package com.googlecode.waitrest; -import com.googlecode.utterlyidle.Server; -import com.googlecode.utterlyidle.httpserver.RestServer; +import com.googlecode.utterlyidle.ServerConfiguration; + +import java.util.Properties; + +import static com.googlecode.utterlyidle.ServerConfiguration.SERVER_CLASS; +import static com.googlecode.utterlyidle.ServerConfiguration.SERVER_PORT; public class Main { public static void main(String[] args) throws Exception { - new Waitrest(port(args), serverClass(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; + new Waitrest(new ServerConfiguration(serverProperties(args))); } - private static Class serverClass(String[] args) { - Class serverClass = RestServer.class; - if (args.length > 1) { - try { - serverClass = (Class) Class.forName(args[1]); - } catch (Throwable ex) { - ex.printStackTrace(); - throw new IllegalArgumentException(String.format( "Invalid server class: %s", args[1])); - } - } - return serverClass; + private static Properties serverProperties(String[] args) { + Properties properties = new Properties(); + if (args.length > 0) properties.setProperty(SERVER_PORT, args[0]); + if (args.length > 1) properties.setProperty(SERVER_CLASS, args[1]); + return properties; } } diff --git a/src/com/googlecode/waitrest/Waitrest.java b/src/com/googlecode/waitrest/Waitrest.java index f85de6d..3252405 100644 --- a/src/com/googlecode/waitrest/Waitrest.java +++ b/src/com/googlecode/waitrest/Waitrest.java @@ -2,11 +2,7 @@ import com.googlecode.totallylazy.Closeables; import com.googlecode.totallylazy.Uri; -import com.googlecode.utterlyidle.Application; -import com.googlecode.utterlyidle.BasePath; -import com.googlecode.utterlyidle.Server; -import com.googlecode.utterlyidle.ServerConfiguration; -import com.googlecode.utterlyidle.httpserver.RestServer; +import com.googlecode.utterlyidle.*; import java.io.Closeable; import java.net.URL; @@ -17,21 +13,21 @@ public class Waitrest implements Closeable { private Server server; private Restaurant application; - public Waitrest(String basePath, Integer port, Class serverClass) { + public Waitrest(String basePath, ServerConfiguration configuration) { try { application = new Restaurant(BasePath.basePath(basePath)); - this.server = initServer(port, serverClass); + this.server = ApplicationBuilder.application(application).start(configuration); } catch (Exception e) { throw new RuntimeException("Couldn't start Waitrest: " + e.getMessage()); } } - public Waitrest(Integer port, Class serverClass) { - this("/", port, serverClass); + public Waitrest(ServerConfiguration serverConfiguration) { + this("/", serverConfiguration); } public Waitrest(String basePath, Integer port) { - this(basePath, port, RestServer.class); + this(basePath, serverConfiguration(port)); } public Waitrest(Integer port) { @@ -39,7 +35,7 @@ public Waitrest(Integer port) { } public Waitrest() { - this("/", null); + this("/", defaultConfiguration()); } public void close() { @@ -63,14 +59,8 @@ public Application application() { return application; } - private Server initServer(Integer port, Class serverClass) throws Exception { - ServerConfiguration configuration = (port == null ? defaultConfiguration() : defaultConfiguration().port(port)); - if(serverClass.equals(RestServer.class)){ - return new RestServer(application, configuration); - } else if(serverClass.equals(com.googlecode.utterlyidle.jetty.RestServer.class)){ - return com.googlecode.utterlyidle.jetty.RestServer.restServer(application, configuration); - } - throw new RuntimeException(String.format("The server class %s is currently not supported", serverClass.getName())); + private static ServerConfiguration serverConfiguration(Integer port) { + return (port == null ? defaultConfiguration() : defaultConfiguration().port(port)); } } From 6af0b8794e49e31accd393b95d1c48873f7b7f4b Mon Sep 17 00:00:00 2001 From: Team Meerkat Date: Wed, 9 Dec 2015 09:39:15 +0000 Subject: [PATCH 3/6] Olga: adding jetty support for Waitrest - version3 --- src/com/googlecode/waitrest/Main.java | 4 +-- src/com/googlecode/waitrest/Waitrest.java | 30 ++++++++++++----------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/com/googlecode/waitrest/Main.java b/src/com/googlecode/waitrest/Main.java index 6fc682d..1eb02cd 100644 --- a/src/com/googlecode/waitrest/Main.java +++ b/src/com/googlecode/waitrest/Main.java @@ -1,7 +1,5 @@ package com.googlecode.waitrest; -import com.googlecode.utterlyidle.ServerConfiguration; - import java.util.Properties; import static com.googlecode.utterlyidle.ServerConfiguration.SERVER_CLASS; @@ -9,7 +7,7 @@ public class Main { public static void main(String[] args) throws Exception { - new Waitrest(new ServerConfiguration(serverProperties(args))); + new Waitrest(serverProperties(args)); } private static Properties serverProperties(String[] args) { diff --git a/src/com/googlecode/waitrest/Waitrest.java b/src/com/googlecode/waitrest/Waitrest.java index 3252405..7c0dc3b 100644 --- a/src/com/googlecode/waitrest/Waitrest.java +++ b/src/com/googlecode/waitrest/Waitrest.java @@ -2,40 +2,39 @@ import com.googlecode.totallylazy.Closeables; import com.googlecode.totallylazy.Uri; -import com.googlecode.utterlyidle.*; +import com.googlecode.utterlyidle.Application; +import com.googlecode.utterlyidle.ApplicationBuilder; +import com.googlecode.utterlyidle.Server; +import com.googlecode.utterlyidle.ServerConfiguration; import java.io.Closeable; import java.net.URL; - -import static com.googlecode.utterlyidle.ServerConfiguration.defaultConfiguration; +import java.util.Properties; public class Waitrest implements Closeable { private Server server; private Restaurant application; - public Waitrest(String basePath, ServerConfiguration configuration) { + public Waitrest(Properties properties) { + ServerConfiguration configuration = new ServerConfiguration(properties); try { - application = new Restaurant(BasePath.basePath(basePath)); + application = new Restaurant(configuration.basePath()); this.server = ApplicationBuilder.application(application).start(configuration); } catch (Exception e) { throw new RuntimeException("Couldn't start Waitrest: " + e.getMessage()); } } - public Waitrest(ServerConfiguration serverConfiguration) { - this("/", serverConfiguration); - } - public Waitrest(String basePath, Integer port) { - this(basePath, serverConfiguration(port)); + this(serverProperties(basePath, port)); } public Waitrest(Integer port) { - this("/", port); + this(null, port); } public Waitrest() { - this("/", defaultConfiguration()); + this(new Properties()); } public void close() { @@ -59,8 +58,11 @@ public Application application() { return application; } - private static ServerConfiguration serverConfiguration(Integer port) { - return (port == null ? defaultConfiguration() : defaultConfiguration().port(port)); + private static Properties serverProperties(String basePath, Integer port) { + Properties properties = new Properties(); + if(basePath != null) properties.setProperty(ServerConfiguration.SERVER_BASE_PATH, basePath); + if(port != null) properties.setProperty(ServerConfiguration.SERVER_PORT, port.toString()); + return properties; } } From 07de284d3facd3e15f18e9687f37cfb1c3321f61 Mon Sep 17 00:00:00 2001 From: Team Meerkat Date: Wed, 9 Dec 2015 15:38:24 +0000 Subject: [PATCH 4/6] Olga: adding jetty support for Waitrest - version4 --- src/com/googlecode/waitrest/Waitrest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/com/googlecode/waitrest/Waitrest.java b/src/com/googlecode/waitrest/Waitrest.java index 7c0dc3b..7518541 100644 --- a/src/com/googlecode/waitrest/Waitrest.java +++ b/src/com/googlecode/waitrest/Waitrest.java @@ -13,13 +13,13 @@ public class Waitrest implements Closeable { private Server server; - private Restaurant application; + private Application application; public Waitrest(Properties properties) { ServerConfiguration configuration = new ServerConfiguration(properties); try { - application = new Restaurant(configuration.basePath()); - this.server = ApplicationBuilder.application(application).start(configuration); + this.server = ApplicationBuilder.application(Restaurant.class).start(configuration); + this.application = server.application(); } catch (Exception e) { throw new RuntimeException("Couldn't start Waitrest: " + e.getMessage()); } From 45fca3bf52ba8a0f8af23fe7f91f56f88411ff48 Mon Sep 17 00:00:00 2001 From: Team Meerkat Date: Wed, 9 Dec 2015 15:59:45 +0000 Subject: [PATCH 5/6] Olga: adding jetty support for Waitrest - version5 --- src/com/googlecode/waitrest/Main.java | 16 ++++------------ src/com/googlecode/waitrest/Waitrest.java | 13 +++++++++---- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/com/googlecode/waitrest/Main.java b/src/com/googlecode/waitrest/Main.java index 1eb02cd..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 java.util.Properties; - -import static com.googlecode.utterlyidle.ServerConfiguration.SERVER_CLASS; -import static com.googlecode.utterlyidle.ServerConfiguration.SERVER_PORT; +import static com.googlecode.waitrest.Waitrest.serverProperties; public class Main { public static void main(String[] args) throws Exception { - new Waitrest(serverProperties(args)); - } - - private static Properties serverProperties(String[] args) { - Properties properties = new Properties(); - if (args.length > 0) properties.setProperty(SERVER_PORT, args[0]); - if (args.length > 1) properties.setProperty(SERVER_CLASS, args[1]); - return properties; + 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 7518541..dfa188e 100644 --- a/src/com/googlecode/waitrest/Waitrest.java +++ b/src/com/googlecode/waitrest/Waitrest.java @@ -11,6 +11,10 @@ import java.net.URL; import java.util.Properties; +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 Server server; private Application application; @@ -26,7 +30,7 @@ public Waitrest(Properties properties) { } public Waitrest(String basePath, Integer port) { - this(serverProperties(basePath, port)); + this(serverProperties(basePath, port == null? null: port.toString(), null)); } public Waitrest(Integer port) { @@ -58,10 +62,11 @@ public Application application() { return application; } - private static Properties serverProperties(String basePath, Integer port) { + public static Properties serverProperties(String basePath, String port, String serverClass) { Properties properties = new Properties(); - if(basePath != null) properties.setProperty(ServerConfiguration.SERVER_BASE_PATH, basePath); - if(port != null) properties.setProperty(ServerConfiguration.SERVER_PORT, port.toString()); + 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; } From 76be8ad7736669f6c8edbe84c6d9bf42301899ea Mon Sep 17 00:00:00 2001 From: Team Meerkat Date: Wed, 9 Dec 2015 16:10:50 +0000 Subject: [PATCH 6/6] Olga: adding jetty support for Waitrest - version6 --- src/com/googlecode/waitrest/Waitrest.java | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/com/googlecode/waitrest/Waitrest.java b/src/com/googlecode/waitrest/Waitrest.java index dfa188e..980e311 100644 --- a/src/com/googlecode/waitrest/Waitrest.java +++ b/src/com/googlecode/waitrest/Waitrest.java @@ -20,17 +20,12 @@ public class Waitrest implements Closeable { private Application application; public Waitrest(Properties properties) { - ServerConfiguration configuration = new ServerConfiguration(properties); - try { - this.server = ApplicationBuilder.application(Restaurant.class).start(configuration); - this.application = server.application(); - } catch (Exception e) { - throw new RuntimeException("Couldn't start Waitrest: " + e.getMessage()); - } + this.server = ApplicationBuilder.application(Restaurant.class).start(new ServerConfiguration(properties)); + this.application = server.application(); } public Waitrest(String basePath, Integer port) { - this(serverProperties(basePath, port == null? null: port.toString(), null)); + this(serverProperties(basePath, port == null ? null : port.toString(), null)); } public Waitrest(Integer port) { @@ -64,9 +59,9 @@ public Application 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); + 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; }