You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:25:33 UTC

[sling-org-apache-sling-commons-osgi] 07/19: SLING-5379 - remove java 7 requirement, does not seem worth it just for try-with-resources

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

rombert pushed a commit to annotated tag org.apache.sling.commons.osgi-2.4.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-osgi.git

commit a64bf63eb3b67744f1b5fc4bfe381186bd987351
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Mon Dec 21 15:25:28 2015 +0000

    SLING-5379 - remove java 7 requirement, does not seem worth it just for try-with-resources
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/osgi@1721192 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  1 -
 .../org/apache/sling/commons/osgi/BundleUtil.java  | 17 ++++++++--
 .../apache/sling/commons/osgi/BundleUtilTest.java  | 37 +++++++++++++++++-----
 3 files changed, 43 insertions(+), 12 deletions(-)

diff --git a/pom.xml b/pom.xml
index 35beb9d..aac2dc4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -41,7 +41,6 @@
     </scm>
     
     <properties>
-        <sling.java.version>7</sling.java.version>
         <test.jars.folder>${project.build.directory}/testjars</test.jars.folder>
     </properties>
 
diff --git a/src/main/java/org/apache/sling/commons/osgi/BundleUtil.java b/src/main/java/org/apache/sling/commons/osgi/BundleUtil.java
index a1bcd95..5470bac 100644
--- a/src/main/java/org/apache/sling/commons/osgi/BundleUtil.java
+++ b/src/main/java/org/apache/sling/commons/osgi/BundleUtil.java
@@ -48,7 +48,9 @@ public class BundleUtil {
      * @throws IOException If something goes wrong reading or writing.
      */
     public static File renameBSN(File bundleFile, String newBSN, File tempDir) throws IOException {
-        try (JarInputStream jis = new JarInputStream(new FileInputStream(bundleFile))) {
+        JarInputStream jis = null;
+        try {
+            jis = new JarInputStream(new FileInputStream(bundleFile));
             Manifest inputMF = jis.getManifest();
 
             Attributes inputAttrs = inputMF.getMainAttributes();
@@ -64,7 +66,9 @@ public class BundleUtil {
             outputAttrs.putValue("Bundle-SymbolicName", newBSN);
             outputAttrs.putValue("X-Original-Bundle-SymbolicName", orgBSN);
 
-            try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(newBundle), newMF)) {
+            JarOutputStream jos = null;
+            try {
+                jos = new JarOutputStream(new FileOutputStream(newBundle), newMF);
                 JarEntry je = null;
                 while ((je = jis.getNextJarEntry()) != null) {
                     try {
@@ -76,9 +80,16 @@ public class BundleUtil {
                         jis.closeEntry();;
                     }
                 }
+            } finally {
+                if(jos != null) {
+                    jos.close();
+                }
             }
-
             return newBundle;
+        } finally {
+            if(jis != null) {
+                jis.close();
+            }
         }
     }
 
diff --git a/src/test/java/org/apache/sling/commons/osgi/BundleUtilTest.java b/src/test/java/org/apache/sling/commons/osgi/BundleUtilTest.java
index 9e0fa5c..abb77fe 100644
--- a/src/test/java/org/apache/sling/commons/osgi/BundleUtilTest.java
+++ b/src/test/java/org/apache/sling/commons/osgi/BundleUtilTest.java
@@ -18,7 +18,12 @@
  */
 package org.apache.sling.commons.osgi;
 
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
 import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -32,11 +37,15 @@ import java.util.jar.Manifest;
 
 import org.junit.Test;
 
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
 public class BundleUtilTest {
+    
+    private static void closeQuietly(Closeable c) {
+        try {
+            c.close();
+        } catch(IOException ignore) {
+        }
+    }
+    
     @Test
     public void testBSNRenaming() throws IOException {
         File tempDir = new File(System.getProperty("java.io.tmpdir"));
@@ -49,8 +58,11 @@ public class BundleUtilTest {
         try {
             compareJarContents(originalFile, generatedFile);
 
-            try (JarFile jfOrg = new JarFile(originalFile);
-                    JarFile jfNew = new JarFile(generatedFile)) {
+            JarFile jfOrg = null;
+            JarFile jfNew = null;
+            try {
+                    jfOrg = new JarFile(originalFile);
+                    jfNew = new JarFile(generatedFile);
                     Manifest mfOrg = jfOrg.getManifest();
                     Manifest mfNew = jfNew.getManifest();
 
@@ -69,6 +81,9 @@ public class BundleUtilTest {
                             assertEquals("Different keys: " + key, orgVal, newVal);
                         }
                     }
+                } finally {
+                    closeQuietly(jfOrg);
+                    closeQuietly(jfNew);
                 }
 
         } finally {
@@ -77,8 +92,11 @@ public class BundleUtilTest {
     }
 
     private static void compareJarContents(File orgJar, File actualJar) throws IOException {
-        try (JarInputStream jis1 = new JarInputStream(new FileInputStream(orgJar));
-            JarInputStream jis2 = new JarInputStream(new FileInputStream(actualJar))) {
+        JarInputStream jis1 = null;
+        JarInputStream jis2 = null;
+        try {
+            jis1 = new JarInputStream(new FileInputStream(orgJar));
+            jis2 = new JarInputStream(new FileInputStream(actualJar));
             JarEntry je1 = null;
             while ((je1 = jis1.getNextJarEntry()) != null) {
                 if (je1.isDirectory())
@@ -103,6 +121,9 @@ public class BundleUtilTest {
                     jis2.closeEntry();
                 }
             }
+        } finally {
+            closeQuietly(jis1);
+            closeQuietly(jis2);
         }
     }
 

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.