Skip to content

Commit

Permalink
[BZ-1035338] add timeout for UrlConnection in UrlResource
Browse files Browse the repository at this point in the history
(cherry picked from commit 470a882)
  • Loading branch information
mariofusco committed Jan 15, 2014
1 parent e34bcc1 commit 8f6cfd6
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions drools-core/src/main/java/org/drools/core/io/impl/UrlResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public class UrlResource extends BaseResource
private String username = "";
private String password = "";

private static final String DROOLS_RESOURCE_URLTIMEOUT = "drools.resource.urltimeout";
private static final int DEFAULT_TIMEOUT = 10000; // 10 seconds
private static final int TIMEOUT = initTimeout();

public UrlResource() {

}
Expand Down Expand Up @@ -210,8 +214,15 @@ private void cacheStream() {
}
}

private URLConnection openURLConnection(URL url) throws IOException {
URLConnection con = url.openConnection();
con.setConnectTimeout(TIMEOUT);
con.setReadTimeout(TIMEOUT);
return con;
}

private InputStream grabStream() throws IOException {
URLConnection con = this.url.openConnection();
URLConnection con = openURLConnection(this.url);
con.setUseCaches(false);

if (con instanceof HttpURLConnection) {
Expand Down Expand Up @@ -293,7 +304,7 @@ private long grabLastMod() throws IOException {
File file = getFile();
return file.lastModified();
} else {
URLConnection conn = getURL().openConnection();
URLConnection conn = openURLConnection(getURL());
if (conn instanceof HttpURLConnection) {
((HttpURLConnection) conn).setRequestMethod("HEAD");
if ("enabled".equalsIgnoreCase(basicAuthentication)) {
Expand Down Expand Up @@ -386,4 +397,12 @@ private static File getCacheDir() {
return new File(root);
}
}

private static int initTimeout() {
try {
return Integer.parseInt(System.getProperty( DROOLS_RESOURCE_URLTIMEOUT ));
} catch (Exception e) {
return DEFAULT_TIMEOUT;
}
}
}

0 comments on commit 8f6cfd6

Please sign in to comment.