You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by va...@apache.org on 2008/01/25 14:53:08 UTC

svn commit: r615225 - in /geronimo/server: branches/2.0/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/util/ trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/util/

Author: vamsic007
Date: Fri Jan 25 05:53:07 2008
New Revision: 615225

URL: http://svn.apache.org/viewvc?rev=615225&view=rev
Log:
GERONIMO-3764 Deployer fails to cleanup the temp files created during deployment process
 o When url.openStream() is used to read an individual file from an archive i.e. when protocol is "jar", the archive file is locked even after the stream is closed and it prevents immediate deletion of temporary files created during the deployment process.  This can be avoided by using JarFile.getInputStream() instead.

Modified:
    geronimo/server/branches/2.0/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/util/DeploymentUtil.java
    geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/util/DeploymentUtil.java

Modified: geronimo/server/branches/2.0/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/util/DeploymentUtil.java
URL: http://svn.apache.org/viewvc/geronimo/server/branches/2.0/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/util/DeploymentUtil.java?rev=615225&r1=615224&r2=615225&view=diff
==============================================================================
--- geronimo/server/branches/2.0/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/util/DeploymentUtil.java (original)
+++ geronimo/server/branches/2.0/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/util/DeploymentUtil.java Fri Jan 25 05:53:07 2008
@@ -19,6 +19,7 @@
 import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -32,6 +33,7 @@
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.LinkedList;
+import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
@@ -112,9 +114,23 @@
     public static File toTempFile(URL url) throws IOException {
         InputStream in = null;
         OutputStream out = null;
+        JarFile jarFile = null;
         try {
-            in = url.openStream();
-
+            if(url.getProtocol().equalsIgnoreCase("jar")) {
+                // url.openStream() locks the jar file and does not release the lock even after the stream is closed.
+                // This problem is avoided by using JarFile APIs.
+                File file = new File(url.getFile().substring(5, url.getFile().indexOf("!/")));
+                String path = url.getFile().substring(url.getFile().indexOf("!/")+2);
+                jarFile = new JarFile(file);
+                JarEntry jarEntry = jarFile.getJarEntry(path);
+                if(jarEntry != null) {
+                    in = jarFile.getInputStream(jarEntry);
+                } else {
+                    throw new FileNotFoundException("JarEntry "+path+" not found in "+file);
+                }
+            } else {
+                in = url.openStream();
+            }
             int index = url.getPath().lastIndexOf(".");
             String extension = null;
             if (index > 0) {
@@ -129,14 +145,29 @@
         } finally {
             close(out);
             close(in);
+            close(jarFile);
         }
     }
 
     public static String readAll(URL url) throws IOException {
         Reader reader = null;
+        JarFile jarFile = null;
         try {
-            reader = new InputStreamReader(url.openStream());
-
+            if(url.getProtocol().equalsIgnoreCase("jar")) {
+                // url.openStream() locks the jar file and does not release the lock even after the stream is closed.
+                // This problem is avoided by using JarFile APIs.
+                File file = new File(url.getFile().substring(5, url.getFile().indexOf("!/")));
+                String path = url.getFile().substring(url.getFile().indexOf("!/")+2);
+                jarFile = new JarFile(file);
+                JarEntry jarEntry = jarFile.getJarEntry(path);
+                if(jarEntry != null) {
+                    reader = new InputStreamReader(jarFile.getInputStream(jarEntry));
+                } else {
+                    throw new FileNotFoundException("JarEntry "+path+" not found in "+file);
+                }
+            } else {
+                reader = new InputStreamReader(url.openStream());
+            }
             char[] buffer = new char[4000];
             StringBuffer out = new StringBuffer();
             for(int count = reader.read(buffer); count >= 0; count = reader.read(buffer)) {
@@ -145,6 +176,7 @@
             return out.toString();
         } finally {
             close(reader);
+            close(jarFile);
         }
     }
 

Modified: geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/util/DeploymentUtil.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/util/DeploymentUtil.java?rev=615225&r1=615224&r2=615225&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/util/DeploymentUtil.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-deployment/src/main/java/org/apache/geronimo/deployment/util/DeploymentUtil.java Fri Jan 25 05:53:07 2008
@@ -19,6 +19,7 @@
 import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -32,6 +33,7 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
+import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
@@ -113,9 +115,23 @@
     public static File toTempFile(URL url) throws IOException {
         InputStream in = null;
         OutputStream out = null;
+        JarFile jarFile = null;
         try {
-            in = url.openStream();
-
+            if(url.getProtocol().equalsIgnoreCase("jar")) {
+                // url.openStream() locks the jar file and does not release the lock even after the stream is closed.
+                // This problem is avoided by using JarFile APIs.
+                File file = new File(url.getFile().substring(5, url.getFile().indexOf("!/")));
+                String path = url.getFile().substring(url.getFile().indexOf("!/")+2);
+                jarFile = new JarFile(file);
+                JarEntry jarEntry = jarFile.getJarEntry(path);
+                if(jarEntry != null) {
+                    in = jarFile.getInputStream(jarEntry);
+                } else {
+                    throw new FileNotFoundException("JarEntry "+path+" not found in "+file);
+                }
+            } else {
+                in = url.openStream();
+            }
             int index = url.getPath().lastIndexOf(".");
             String extension = null;
             if (index > 0) {
@@ -130,14 +146,29 @@
         } finally {
             close(out);
             close(in);
+            close(jarFile);
         }
     }
 
     public static String readAll(URL url) throws IOException {
         Reader reader = null;
+        JarFile jarFile = null;
         try {
-            reader = new InputStreamReader(url.openStream());
-
+            if(url.getProtocol().equalsIgnoreCase("jar")) {
+                // url.openStream() locks the jar file and does not release the lock even after the stream is closed.
+                // This problem is avoided by using JarFile APIs.
+                File file = new File(url.getFile().substring(5, url.getFile().indexOf("!/")));
+                String path = url.getFile().substring(url.getFile().indexOf("!/")+2);
+                jarFile = new JarFile(file);
+                JarEntry jarEntry = jarFile.getJarEntry(path);
+                if(jarEntry != null) {
+                    reader = new InputStreamReader(jarFile.getInputStream(jarEntry));
+                } else {
+                    throw new FileNotFoundException("JarEntry "+path+" not found in "+file);
+                }
+            } else {
+                reader = new InputStreamReader(url.openStream());
+            }
             char[] buffer = new char[4000];
             StringBuffer out = new StringBuffer();
             for(int count = reader.read(buffer); count >= 0; count = reader.read(buffer)) {
@@ -146,6 +177,7 @@
             return out.toString();
         } finally {
             close(reader);
+            close(jarFile);
         }
     }