From b1ac43ba6ac7085b666328e7f94937bb6401cdf5 Mon Sep 17 00:00:00 2001 From: Remi Vankeisbelck Date: Tue, 26 Jan 2016 19:01:59 +0100 Subject: [PATCH] #37 : rollback to Java7 (no more lambdas). Still needs java8 to run webtests on jetty9... --- .../examples/async/AsyncActionBean.java | 67 ++++++++++--------- .../examples/async/AsyncHttpClient.java | 5 +- pom.xml | 4 +- .../controller/NameBasedActionResolver.java | 7 +- .../stripes/mock/MockAsyncContext.java | 2 +- .../stripes/mock/TestMockAsync.java | 34 ++++++---- 6 files changed, 65 insertions(+), 54 deletions(-) diff --git a/examples/src/main/java/net/sourceforge/stripes/examples/async/AsyncActionBean.java b/examples/src/main/java/net/sourceforge/stripes/examples/async/AsyncActionBean.java index 072e7eee6..a201a3d25 100644 --- a/examples/src/main/java/net/sourceforge/stripes/examples/async/AsyncActionBean.java +++ b/examples/src/main/java/net/sourceforge/stripes/examples/async/AsyncActionBean.java @@ -4,8 +4,8 @@ import net.sourceforge.stripes.examples.bugzooky.ext.Public; import net.sourceforge.stripes.validation.Validate; import org.apache.http.HttpHost; +import org.apache.http.HttpResponse; -import javax.servlet.AsyncContext; import java.io.ByteArrayOutputStream; @Public @@ -43,7 +43,7 @@ public Resolution display() { * asynchronously fetch data from a remote web service (github) * and set instance fields for use in the view. */ - public void asyncEvent(AsyncResolution async) { + public void asyncEvent(final AsyncResolution async) { // we use an Async Http Client in order to call the github web service as a demo. // the async http client calls back one of the lambdas when it's done, and @@ -53,41 +53,44 @@ public void asyncEvent(AsyncResolution async) { HttpHost host = new HttpHost("api.github.com", 443, "https"); new AsyncHttpClient(host) .buildRequest("/repos/StripesFramework/stripes/commits") - .completed(result -> { - - // response is returned, deserialize result - status = result.getStatusLine().getStatusCode(); - if (status == 200) { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try { - result.getEntity().writeTo(bos); - bos.close(); - ghResponse = bos.toString("UTF-8"); - } catch (Exception e) { - clientException = e; + .completed(new AsyncHttpClient.Consumer() { + @Override + public void accept(HttpResponse result) { + // response is returned, deserialize result + status = result.getStatusLine().getStatusCode(); + if (status == 200) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try { + result.getEntity().writeTo(bos); + bos.close(); + ghResponse = bos.toString("UTF-8"); + } catch (Exception e) { + clientException = e; + } + async.complete(forwardResolution); + } else { + ghResponse = result.getStatusLine().getReasonPhrase(); + async.complete(forwardResolution); } - async.complete(forwardResolution); - } else { - ghResponse = result.getStatusLine().getReasonPhrase(); - async.complete(forwardResolution); } - }) - .failed(ex -> { - - // http client failure - clientException = ex; - async.complete(forwardResolution); - + .failed(new AsyncHttpClient.Consumer() { + @Override + public void accept(Exception e) { + // http client failure + clientException = e; + async.complete(forwardResolution); + } }) - .cancelled(() -> { - - // just for demo, we never call it... - cancelled = true; - async.complete(forwardResolution); + .cancelled(new Runnable() { + @Override + public void run() { + // just for demo, we never call it... + cancelled = true; + async.complete(forwardResolution); - }) - .get(); // trigger async request + } + }).get(); // trigger async request } @DontValidate diff --git a/examples/src/main/java/net/sourceforge/stripes/examples/async/AsyncHttpClient.java b/examples/src/main/java/net/sourceforge/stripes/examples/async/AsyncHttpClient.java index 15dd295c0..bcd52ab3d 100644 --- a/examples/src/main/java/net/sourceforge/stripes/examples/async/AsyncHttpClient.java +++ b/examples/src/main/java/net/sourceforge/stripes/examples/async/AsyncHttpClient.java @@ -13,7 +13,6 @@ import org.apache.http.nio.reactor.ConnectingIOReactor; import java.io.IOException; -import java.util.function.Consumer; /** * Wrapper for non-blocking http client example. Avoids cluttering the action bean's code... @@ -54,6 +53,10 @@ public AsyncRequest buildRequest(String uri) { return new AsyncRequest(uri); } + public interface Consumer { + void accept(T t); + } + public class AsyncRequest { private final String uri; diff --git a/pom.xml b/pom.xml index 78ccaf5bc..ded12d238 100644 --- a/pom.xml +++ b/pom.xml @@ -191,8 +191,8 @@ maven-compiler-plugin 3.1 - 1.8 - 1.8 + 1.6 + 1.6 diff --git a/stripes/src/main/java/net/sourceforge/stripes/controller/NameBasedActionResolver.java b/stripes/src/main/java/net/sourceforge/stripes/controller/NameBasedActionResolver.java index 7aece56e7..9c8789773 100644 --- a/stripes/src/main/java/net/sourceforge/stripes/controller/NameBasedActionResolver.java +++ b/stripes/src/main/java/net/sourceforge/stripes/controller/NameBasedActionResolver.java @@ -23,7 +23,6 @@ import javax.servlet.ServletContext; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.lang.reflect.Parameter; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Collections; @@ -251,10 +250,10 @@ public String getHandledEvent(Method handler) { public static boolean isAsyncEventHandler(Method handler) { if (!Modifier.isAbstract(handler.getModifiers()) && handler.getReturnType().equals(Void.TYPE) - && handler.getParameterCount() == 1) { + && handler.getParameterTypes().length == 1) { // look at arg type - Parameter p = handler.getParameters()[0]; - return AsyncResolution.class.isAssignableFrom(p.getType()); + Class pType = handler.getParameterTypes()[0]; + return AsyncResolution.class.isAssignableFrom(pType); } return false; } diff --git a/stripes/src/main/java/net/sourceforge/stripes/mock/MockAsyncContext.java b/stripes/src/main/java/net/sourceforge/stripes/mock/MockAsyncContext.java index 7a4bbdf5b..5013b3fdc 100644 --- a/stripes/src/main/java/net/sourceforge/stripes/mock/MockAsyncContext.java +++ b/stripes/src/main/java/net/sourceforge/stripes/mock/MockAsyncContext.java @@ -13,7 +13,7 @@ public class MockAsyncContext implements AsyncContext { private final ServletRequest request; private final ServletResponse response; - private final List listeners = new ArrayList<>(); + private final List listeners = new ArrayList(); private boolean completed = false; private boolean timedOut = false; diff --git a/stripes/src/test/java/net/sourceforge/stripes/mock/TestMockAsync.java b/stripes/src/test/java/net/sourceforge/stripes/mock/TestMockAsync.java index 91176869c..31d4426e8 100644 --- a/stripes/src/test/java/net/sourceforge/stripes/mock/TestMockAsync.java +++ b/stripes/src/test/java/net/sourceforge/stripes/mock/TestMockAsync.java @@ -111,16 +111,19 @@ public void doAsync(AsyncResolution r) throws Exception { r.complete(); } - public void doReallyAsync(AsyncResolution r) throws Exception { - new Thread(() -> { - System.out.println("Really Async !"); - try { - r.getResponse().getWriter().write("DONE"); - completed = true; - r.complete(); - } catch (IOException e) { - // will timeout... - e.printStackTrace(); + public void doReallyAsync(final AsyncResolution r) throws Exception { + new Thread(new Runnable() { + @Override + public void run() { + System.out.println("Really Async !"); + try { + r.getResponse().getWriter().write("DONE"); + completed = true; + r.complete(); + } catch (IOException e) { + // will timeout... + e.printStackTrace(); + } } }).start(); } @@ -144,10 +147,13 @@ public void doAsyncAndCompleteWithForwardResolution(AsyncResolution r) { r.complete(new ForwardResolution("/foo/bar.jsp")); } - public void doAsyncClassy(AsyncResolution callback) { - new Thread(() -> { - completed = true; - callback.complete(new ForwardResolution("/foo/bar")); + public void doAsyncClassy(final AsyncResolution callback) { + new Thread(new Runnable() { + @Override + public void run() { + completed = true; + callback.complete(new ForwardResolution("/foo/bar")); + } }).start(); }