javaee / metro-jaxws-commons

Metro has been contributed to Eclipse Foundation. This repository is for legacy review only. Please refer to the Eclipse EE4J Metro project for the very latest
https://eclipse-ee4j.github.io/metro-wsit/
Other
10 stars 9 forks source link

Fix Resource leaks #128

Open glassfishrobot opened 10 years ago

glassfishrobot commented 10 years ago

There are some resource leaks in the plugin that should be fixed. When the plugin is integrated into an IDE those could be a problem.

Environment

n/a

Affected Versions

[2.3]

glassfishrobot commented 10 years ago

Reported by ahammar

glassfishrobot commented 10 years ago

ahammar said: Here's a patch for trunk:

Index: src/test/java/org/jvnet/jax_ws_commons/jaxws/Assertions.java
===================================================================
--- src/test/java/org/jvnet/jax_ws_commons/jaxws/Assertions.java    (revision 1184)
+++ src/test/java/org/jvnet/jax_ws_commons/jaxws/Assertions.java    (working copy)
@@ -46,11 +46,15 @@
         Assert.assertTrue(f.exists(), f.getAbsolutePath() + " does not exist");
         Assert.assertTrue(f.isFile(), f.getAbsolutePath() + " is not a file");
         BufferedReader r = new BufferedReader(new FileReader(f));
+        try {
         String line;
-        while ((line = r.readLine()) != null) {
-            if (line.contains(s)) {
-return;
+            while ((line = r.readLine()) != null) {
+if (line.contains(s)) {
+    return;
+}
             }
+        } finally {
+            r.close();
         }
         Assert.fail("'" + s + "' is missing in:" + f.getAbsolutePath());
     }
@@ -61,6 +65,7 @@
         Assert.assertTrue(f.isFile(), f.getAbsolutePath() + " is not a file");
         ZipFile zf = new ZipFile(f);
         Assert.assertNotNull(zf.getEntry(path), "'" + path + "' is missing in: " + jarName);
+        zf.close();
     }

     public static void assertJarNotContains(File project, String jarName, String path) throws ZipException, IOException {
@@ -69,5 +74,6 @@
         Assert.assertTrue(f.isFile(), f.getAbsolutePath() + " is not a file");
         ZipFile zf = new ZipFile(f);
         Assert.assertNull(zf.getEntry(path), "'" + path + "' is in: " + jarName);
+        zf.close();
     }
 }
Index: src/main/java/org/jvnet/jax_ws_commons/jaxws/WsImportMojo.java
===================================================================
--- src/main/java/org/jvnet/jax_ws_commons/jaxws/WsImportMojo.java  (revision 1184)
+++ src/main/java/org/jvnet/jax_ws_commons/jaxws/WsImportMojo.java  (working copy)
@@ -591,9 +591,11 @@
 }
 if (!(u == null || !"jar".equalsIgnoreCase(u.getProtocol()))) {
     String path = u.getPath();
+    JarFile jarFile = null;
     try {
         Pattern p = Pattern.compile(dir.replace(File.separatorChar, '/') + PATTERN, Pattern.CASE_INSENSITIVE);
-        Enumeration<JarEntry> jes = new JarFile(path.substring(5, path.indexOf("!/"))).entries();
+        jarFile = new JarFile(path.substring(5, path.indexOf("!/")));
+        Enumeration<JarEntry> jes = jarFile.entries();
         while (jes.hasMoreElements()) {
             JarEntry je = jes.nextElement();
             Matcher m = p.matcher(je.getName());
@@ -604,6 +606,13 @@
         }
     } catch (IOException ex) {
         Logger.getLogger(WsImportMojo.class.getName()).log(Level.SEVERE, null, ex);
+    } finally {
+        try {
+            if (jarFile != null) {
+jarFile.close();
+            }
+        } catch (Exception ignore) {
+        }
     }
 }
             }
@@ -720,9 +729,9 @@
     }

     private String getHash(String s) {
+        Formatter formatter = new Formatter();
         try {
             MessageDigest md = MessageDigest.getInstance("SHA");
-            Formatter formatter = new Formatter();
             for (byte b : md.digest(s.getBytes("UTF-8"))) {
 formatter.format("%02x", b);
             }
@@ -731,6 +740,8 @@
             getLog().debug(ex.getMessage(), ex);
         } catch (NoSuchAlgorithmException ex) {
             getLog().debug(ex.getMessage(), ex);
+        } finally {
+            formatter.close();
         }
         //fallback to some default
         getLog().warn("Could not compute hash for " + s + ". Using fallback method.");

The patch is a slight modification of the one in #124, just including fix for the resource leaks.

glassfishrobot commented 9 years ago

ahammar said: Any chance to get this fixed to prevent issues in IDE usage scenarios where memory is leaking?

glassfishrobot commented 10 years ago

Parent-Task: JAX_WS_COMMONS-124

glassfishrobot commented 7 years ago

This issue was imported from java.net JIRA JAX_WS_COMMONS-128