On 9/20/06, Matt Benson <[EMAIL PROTECTED]> wrote:

--- Antoine Levy-Lambert <[EMAIL PROTECTED]> wrote:

> Hello Xavier,
>
> the patch is welcome, but I do not see the
> attachment ? maybe it
> comes from my email reader.

Nor did I.  :)


This is weird, I see it in gmail... Is  it using some fancy thing to attach
files?
Anyway, here is the patch in plain text:
Index: src/main/org/apache/tools/ant/types/resources/URLResource.java
===================================================================
--- src/main/org/apache/tools/ant/types/resources/URLResource.java
(revision 448066)
+++ src/main/org/apache/tools/ant/types/resources/URLResource.java
(working copy)
@@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
@@ -145,6 +146,28 @@
     * @return true if this resource exists.
     */
    public synchronized boolean isExists() {
+        return isExists(true);
+    }
+
+    /**
+     * 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 (isReference()) {
            return ((Resource) getCheckedRef()).isExists();
        }
@@ -156,9 +179,14 @@
            return true;
        } catch (IOException e) {
            return false;
+        } finally {
+            if (closeConnection) {
+                close();
+            }
        }
    }

+
    /**
     * Tells the modification time in milliseconds since 01.01.1970 .
     *
@@ -169,10 +197,12 @@
        if (isReference()) {
            return ((Resource) getCheckedRef()).getLastModified();
        }
-        if (!isExists()) {
+        if (!isExists(false)) {
            return 0L;
        }
        try {
+            // note that this call to connect() is not necessary:
+            // isExists returned true and isReference returned false
            connect();
            return conn.getLastModified();
        } catch (IOException e) {
@@ -177,6 +207,8 @@
            return conn.getLastModified();
        } catch (IOException e) {
            return 0L;
+        } finally {
+            close();
        }
    }

@@ -199,16 +231,18 @@
        if (isReference()) {
            return ((Resource) getCheckedRef()).getSize();
        }
-        if (!isExists()) {
+        if (!isExists(false)) {
            return 0L;
        }
        try {
+            // note that this call to connect() is not necessary:
+            // isExists returned true and isReference returned false
            connect();
-            long contentlength = conn.getContentLength();
-            close();
-            return contentlength;
+            return conn.getContentLength();
        } catch (IOException e) {
            return UNKNOWN_SIZE;
+        } finally {
+            close();
        }
    }

@@ -260,7 +294,7 @@
        try {
            return conn.getInputStream();
        } finally {
-            conn = null;
+            close();
        }
    }

@@ -280,7 +314,7 @@
        try {
            return conn.getOutputStream();
        } finally {
-            conn = null;
+            close();
        }
    }

@@ -304,7 +338,15 @@
        }
    }

-    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) {
            try {
                if (conn instanceof JarURLConnection) {
@@ -312,6 +354,8 @@
                    JarFile jf = juc.getJarFile();
                    jf.close();
                    jf = null;
+                } else if (conn instanceof HttpURLConnection) {
+                    ((HttpURLConnection) conn).disconnect();
                }
            } catch (IOException exc) {

@@ -327,7 +371,7 @@
     */
    protected void finalize() throws Throwable {
        close();
-        conn = null;
+        conn = null; // Note: already done by close();
        super.finalize();
    }

Reply via email to