From 17be28a8339f78c1f2bf321927d9a77fd50ee621 Mon Sep 17 00:00:00 2001 From: Erwan Loisant Date: Mon, 11 Oct 2010 16:40:54 +0200 Subject: [PATCH] [#247] Let 3rd party devs add their own WS implementation --- framework/src/play/libs/WS.java | 19 +++++++++++++------ framework/src/play/libs/ws/WSAsync.java | 20 +++----------------- framework/src/play/libs/ws/WSUrlFetch.java | 18 ++---------------- 3 files changed, 18 insertions(+), 39 deletions(-) diff --git a/framework/src/play/libs/WS.java b/framework/src/play/libs/WS.java index bcd3537320..7b2e3a5156 100644 --- a/framework/src/play/libs/WS.java +++ b/framework/src/play/libs/WS.java @@ -64,14 +64,22 @@ public void onApplicationStop() { static void init() { if (wsImpl != null) return; - if (Play.configuration.getProperty("webservice", "async").equals("urlfetch")) { - wsImpl = WSUrlFetch.getInstance(); + String implementation = Play.configuration.getProperty("webservice", "async"); + if (implementation.equals("urlfetch")) { + wsImpl = new WSUrlFetch(); Logger.info("Using URLFetch for web service"); - } else { + } else if (implementation.equals("async")) { Logger.info("Using Async for web service"); - wsImpl = WSAsync.getInstance(); + wsImpl = new WSAsync(); + } else { + try { + wsImpl = (WSImpl)Play.classloader.loadClass(implementation).newInstance(); + Logger.info("Using the class:" + implementation + " for web service"); + } catch (Exception e) { + Logger.error("Cannot load class " + implementation + ", using async instead"); + wsImpl = new WSAsync(); + } } - wsImpl.init(); } /** @@ -116,7 +124,6 @@ public static WSRequest url(String url, String... params) { public interface WSImpl { public WSRequest newRequest(String url); - public void init(); public void stop(); } diff --git a/framework/src/play/libs/ws/WSAsync.java b/framework/src/play/libs/ws/WSAsync.java index fd1b66f413..ff7a48a114 100644 --- a/framework/src/play/libs/ws/WSAsync.java +++ b/framework/src/play/libs/ws/WSAsync.java @@ -59,21 +59,9 @@ */ public class WSAsync implements WSImpl { - private static AsyncHttpClient httpClient; + private AsyncHttpClient httpClient; - private static WSAsync uniqueInstance; - - private WSAsync() {} - - public static WSAsync getInstance() { - if (uniqueInstance == null) { - uniqueInstance = new WSAsync(); - } - return uniqueInstance; - } - - @Override - public void init() { + public WSAsync() { String proxyHost = Play.configuration.getProperty("http.proxyHost", System.getProperty("http.proxyHost")); String proxyPort = Play.configuration.getProperty("http.proxyPort", System.getProperty("http.proxyPort")); String proxyUser = Play.configuration.getProperty("http.proxyUser", System.getProperty("http.proxyUser")); @@ -95,18 +83,16 @@ public void init() { httpClient = new AsyncHttpClient(confBuilder.build()); } - @Override public void stop() { Logger.trace("Releasing http client connections..."); httpClient.close(); } - @Override public WSRequest newRequest(String url) { return new WSAsyncRequest(url); } - public static class WSAsyncRequest extends WSRequest { + public class WSAsyncRequest extends WSRequest { private WSAsyncRequest(String url) { this.url = url; diff --git a/framework/src/play/libs/ws/WSUrlFetch.java b/framework/src/play/libs/ws/WSUrlFetch.java index de510531da..397dc5ffb4 100644 --- a/framework/src/play/libs/ws/WSUrlFetch.java +++ b/framework/src/play/libs/ws/WSUrlFetch.java @@ -26,29 +26,15 @@ */ public class WSUrlFetch implements WSImpl { - private static WSUrlFetch uniqueInstance; + public WSUrlFetch() {} - private WSUrlFetch() {} - - public static WSUrlFetch getInstance() { - if (uniqueInstance == null) { - uniqueInstance = new WSUrlFetch(); - } - return uniqueInstance; - } - - @Override - public void init() {} - - @Override public void stop() {} - @Override public play.libs.WS.WSRequest newRequest(String url) { return new WSUrlfetchRequest(url); } - public static class WSUrlfetchRequest extends WSRequest { + public class WSUrlfetchRequest extends WSRequest { private WSUrlfetchRequest(String url) { this.url = url;