Browse Source

patch submitted by Xavier Hanin, mainly closing HttpURLConnection

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@448452 13f79535-47bb-0310-9956-ffa450edef68
master
Antoine Levy-Lambert 19 years ago
parent
commit
359ac4f967
5 changed files with 55 additions and 12 deletions
  1. +1
    -0
      CONTRIBUTORS
  2. +1
    -1
      WHATSNEW
  3. +4
    -0
      contributors.xml
  4. +4
    -1
      src/etc/testcases/types/resources/build.xml
  5. +45
    -10
      src/main/org/apache/tools/ant/types/resources/URLResource.java

+ 1
- 0
CONTRIBUTORS View File

@@ -255,6 +255,7 @@ Wolf Siberski
Wolfgang Baer Wolfgang Baer
Wolfgang Frech Wolfgang Frech
Wolfgang Werner Wolfgang Werner
Xavier Hanin
Xavier Witdouck Xavier Witdouck
Yohann Roussel Yohann Roussel
Yuji Yamano Yuji Yamano


+ 1
- 1
WHATSNEW View File

@@ -20,7 +20,7 @@ Fixed bugs:
* the ant wrapper script should now correctly locate the java * the ant wrapper script should now correctly locate the java
executable in more recent IBM JDKs for AIX as well. executable in more recent IBM JDKs for AIX as well.


* URLResource did not close jar files.
* URLResource did not close jar files, and also did not disconnect HTTPConnection (s).


* <junittask/> created junitvmwatcher*.properties files but did not close and delete them. * <junittask/> created junitvmwatcher*.properties files but did not close and delete them.




+ 4
- 0
contributors.xml View File

@@ -1014,6 +1014,10 @@
<first>Wolfgang</first> <first>Wolfgang</first>
<last>Werner</last> <last>Werner</last>
</name> </name>
<name>
<first>Xavier</first>
<last>Hanin</last>
</name>
<name> <name>
<first>Xavier</first> <first>Xavier</first>
<last>Witdouck</last> <last>Witdouck</last>


+ 4
- 1
src/etc/testcases/types/resources/build.xml View File

@@ -320,7 +320,10 @@ depends="testfileset,testdirset,testfilelist,testpath,testzipfileset,testpropert
<length property="httpurl.length"> <length property="httpurl.length">
<url refid="httpurl" /> <url refid="httpurl" />
</length> </length>
<fail>
<length property="file.length">
<file file="${file}"/>
</length>
<fail message="length of url ${httpurl.length} length of file ${file.length} file should be twice as big">
<condition> <condition>
<not> <not>
<length file="${file}" when="greater" length="${httpurl.length}" /> <length file="${file}" when="greater" length="${httpurl.length}" />


+ 45
- 10
src/main/org/apache/tools/ant/types/resources/URLResource.java View File

@@ -22,6 +22,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@@ -148,6 +149,28 @@ public class URLResource extends Resource {
if (isReference()) { if (isReference()) {
return ((Resource) getCheckedRef()).isExists(); return ((Resource) getCheckedRef()).isExists();
} }
return isExists(false);
}

/**
* Find out whether the URL exists, and close the connection
* opened to the URL if closeConnection is true.
*
* Note that this method does ensure that if:
* - the resource exists (if it returns true)
* - and if the current object is not a reference
* (isReference() returns false)
* - and if it was called with closeConnection to false,
*
* then the connection to the URL (stored in the conn
* private field) will be opened, and require to be closed
* by the caller.
*
* @param closeConnection true if the connection should be closed
* after the call, false if it should stay open.
* @return true if this resource exists.
*/
private synchronized boolean isExists(boolean closeConnection) {
if (getURL() == null) { if (getURL() == null) {
return false; return false;
} }
@@ -156,9 +179,14 @@ public class URLResource extends Resource {
return true; return true;
} catch (IOException e) { } catch (IOException e) {
return false; return false;
} finally {
if (closeConnection) {
close();
}
} }
} }



/** /**
* Tells the modification time in milliseconds since 01.01.1970 . * Tells the modification time in milliseconds since 01.01.1970 .
* *
@@ -169,15 +197,10 @@ public class URLResource extends Resource {
if (isReference()) { if (isReference()) {
return ((Resource) getCheckedRef()).getLastModified(); return ((Resource) getCheckedRef()).getLastModified();
} }
if (!isExists()) {
return 0L;
}
try {
connect();
return conn.getLastModified();
} catch (IOException e) {
if (!isExists(false)) {
return 0L; return 0L;
} }
return conn.getLastModified();
} }


/** /**
@@ -199,7 +222,7 @@ public class URLResource extends Resource {
if (isReference()) { if (isReference()) {
return ((Resource) getCheckedRef()).getSize(); return ((Resource) getCheckedRef()).getSize();
} }
if (!isExists()) {
if (!isExists(false)) {
return 0L; return 0L;
} }
try { try {
@@ -271,6 +294,7 @@ public class URLResource extends Resource {
* Resource as a stream. * Resource as a stream.
* @throws UnsupportedOperationException if OutputStreams are not * @throws UnsupportedOperationException if OutputStreams are not
* supported for this Resource type. * supported for this Resource type.
* @throws IOException if the URL cannot be opened.
*/ */
public synchronized OutputStream getOutputStream() throws IOException { public synchronized OutputStream getOutputStream() throws IOException {
if (isReference()) { if (isReference()) {
@@ -286,6 +310,7 @@ public class URLResource extends Resource {


/** /**
* Ensure that we have a connection. * Ensure that we have a connection.
* @throws IOException if the connection cannot be established.
*/ */
protected synchronized void connect() throws IOException { protected synchronized void connect() throws IOException {
URL u = getURL(); URL u = getURL();
@@ -304,7 +329,15 @@ public class URLResource extends Resource {
} }
} }


private void close() {
/**
* Closes the URL connection if:
* - it is opened (i.e. the field conn is not null)
* - this type of URLConnection supports some sort of close mechanism
*
* This method ensures the field conn will be null after the call.
*
*/
private synchronized void close() {
if (conn != null) { if (conn != null) {
try { try {
if (conn instanceof JarURLConnection) { if (conn instanceof JarURLConnection) {
@@ -312,9 +345,11 @@ public class URLResource extends Resource {
JarFile jf = juc.getJarFile(); JarFile jf = juc.getJarFile();
jf.close(); jf.close();
jf = null; jf = null;
} else if (conn instanceof HttpURLConnection) {
((HttpURLConnection) conn).disconnect();
} }
} catch (IOException exc) { } catch (IOException exc) {
//ignore
} finally { } finally {
conn = null; conn = null;
} }


Loading…
Cancel
Save