Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Fix Resource leaks #128

Open
glassfishrobot opened this issue Mar 17, 2014 · 5 comments
Open

Fix Resource leaks #128

glassfishrobot opened this issue Mar 17, 2014 · 5 comments

Comments

@glassfishrobot
Copy link
Contributor

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
Copy link
Contributor Author

Reported by ahammar

@glassfishrobot
Copy link
Contributor Author

Parent-Task: JAX_WS_COMMONS-124

@glassfishrobot
Copy link
Contributor Author

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
Copy link
Contributor Author

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

@glassfishrobot
Copy link
Contributor Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants