Navigation Menu

Skip to content

Commit

Permalink
[#247] Let 3rd party devs add their own WS implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
erwan committed Oct 11, 2010
1 parent 5547250 commit 17be28a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 39 deletions.
19 changes: 13 additions & 6 deletions framework/src/play/libs/WS.java
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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();
}

Expand Down
20 changes: 3 additions & 17 deletions framework/src/play/libs/ws/WSAsync.java
Expand Up @@ -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"));
Expand All @@ -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;
Expand Down
18 changes: 2 additions & 16 deletions framework/src/play/libs/ws/WSUrlFetch.java
Expand Up @@ -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;
Expand Down

0 comments on commit 17be28a

Please sign in to comment.