You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2022/08/14 14:19:45 UTC

[sling-maven-plugin] branch master updated: releng: fix SonarQube issue

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-maven-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 9d23112  releng: fix SonarQube issue
9d23112 is described below

commit 9d23112e875bdc651ad2bb2354a5b4dcdf2fe8bd
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Sun Aug 14 16:19:41 2022 +0200

    releng: fix SonarQube issue
---
 .../bundlesupport/AbstractBundleDeployMojo.java    | 71 ++++++++--------------
 1 file changed, 24 insertions(+), 47 deletions(-)

diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleDeployMojo.java b/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleDeployMojo.java
index 3476d9b..5e0960c 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleDeployMojo.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleDeployMojo.java
@@ -18,16 +18,17 @@
 package org.apache.sling.maven.bundlesupport;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.Enumeration;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
-import java.util.jar.JarInputStream;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
 import java.util.zip.Deflater;
+import java.util.zip.ZipOutputStream;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.hc.client5.http.classic.methods.HttpPost;
@@ -36,6 +37,7 @@ import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler;
 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugins.annotations.Parameter;
+import org.osgi.framework.Constants;
 
 @Deprecated
 abstract class AbstractBundleDeployMojo extends AbstractBundleRequestMojo {
@@ -125,57 +127,32 @@ abstract class AbstractBundleDeployMojo extends AbstractBundleRequestMojo {
         int pos = fileName.indexOf(oldVersion);
         fileName = fileName.substring(0, pos) + newVersion
             + fileName.substring(pos + oldVersion.length());
-
-        JarInputStream jis = null;
-        JarOutputStream jos;
-        OutputStream out = null;
-        JarFile sourceJar = null;
-        try {
-            // now create a temporary file and update the version
-            sourceJar = new JarFile(file);
+        final File destJar = new File(file.getParentFile(), fileName);
+        
+        // now create a temporary file and update the version
+        try (JarFile sourceJar = new JarFile(file)) {
             final Manifest manifest = sourceJar.getManifest();
-            manifest.getMainAttributes().putValue("Bundle-Version", newVersion);
-
-            jis = new JarInputStream(new FileInputStream(file));
-            final File destJar = new File(file.getParentFile(), fileName);
-            out = new FileOutputStream(destJar);
-            jos = new JarOutputStream(out, manifest);
-
-            jos.setMethod(JarOutputStream.DEFLATED);
-            jos.setLevel(Deflater.BEST_COMPRESSION);
-
-            JarEntry entryIn = jis.getNextJarEntry();
-            while (entryIn != null) {
-                JarEntry entryOut = new JarEntry(entryIn.getName());
-                entryOut.setTime(entryIn.getTime());
-                entryOut.setComment(entryIn.getComment());
-                jos.putNextEntry(entryOut);
-                if (!entryIn.isDirectory()) {
-                    IOUtils.copy(jis, jos);
+            manifest.getMainAttributes().putValue(Constants.BUNDLE_VERSION, newVersion);
+            try (OutputStream out = new FileOutputStream(destJar);
+                 JarOutputStream jos = new JarOutputStream(out, manifest)) {
+                jos.setMethod(ZipOutputStream.DEFLATED);
+                jos.setLevel(Deflater.BEST_COMPRESSION);
+                Enumeration<JarEntry> entries = sourceJar.entries();
+                while (entries.hasMoreElements()) {
+                    JarEntry entryIn = entries.nextElement();
+                    JarEntry entryOut = new JarEntry(entryIn);
+                    jos.putNextEntry(entryOut);
+                    if (!entryIn.isDirectory()) {
+                        try (InputStream jis = sourceJar.getInputStream(entryOut)) {
+                            IOUtils.copy(jis, jos);
+                        }
+                    }
                 }
-                jos.closeEntry();
-                jis.closeEntry();
-                entryIn = jis.getNextJarEntry();
+                return destJar;
             }
-
-            // close the JAR file now to force writing
-            jos.close();
-            return destJar;
         } catch (IOException ioe) {
             throw new MojoExecutionException(
                 "Unable to update version in jar file.", ioe);
-        } finally {
-            if (sourceJar != null) {
-                try {
-                    sourceJar.close();
-                }
-                catch (IOException ex) {
-                    // close
-                }
-            }
-            IOUtils.closeQuietly(jis);
-            IOUtils.closeQuietly(out);
         }
-
     }
 }
\ No newline at end of file