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:34 UTC

[sling-org-apache-sling-commons-osgi] 08/19: SLING-5379 - make BundleFileProcessor more generic

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 669bde42eeb2b9e77d210c2265ea32ec929a0213
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Mon Dec 21 15:48:12 2015 +0000

    SLING-5379 - make BundleFileProcessor more generic
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/osgi@1721199 13f79535-47bb-0310-9956-ffa450edef68
---
 .../{BundleUtil.java => BundleFileProcessor.java}  | 55 +++++++++++-----------
 ...eUtilTest.java => BundleFileProcessorTest.java} | 35 ++++++++++++--
 2 files changed, 58 insertions(+), 32 deletions(-)

diff --git a/src/main/java/org/apache/sling/commons/osgi/BundleUtil.java b/src/main/java/org/apache/sling/commons/osgi/BundleFileProcessor.java
similarity index 63%
rename from src/main/java/org/apache/sling/commons/osgi/BundleUtil.java
rename to src/main/java/org/apache/sling/commons/osgi/BundleFileProcessor.java
index 5470bac..2804952 100644
--- a/src/main/java/org/apache/sling/commons/osgi/BundleUtil.java
+++ b/src/main/java/org/apache/sling/commons/osgi/BundleFileProcessor.java
@@ -24,47 +24,46 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.jar.Attributes;
 import java.util.jar.JarEntry;
 import java.util.jar.JarInputStream;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
 
 /**
- * The <code>BundleUtil</code> is a utility class providing some
- * useful utility methods for bundle handling.
+ * The <code>BundleFileProcessor</code> can transform a bundle Manifest
+ * by creating a modified copy of the bundle file. 
  * @since 2.4
  */
-public class BundleUtil {
+public abstract class BundleFileProcessor {
+    
+    private final File input;
+    private final File outputFolder;
+    
+    public BundleFileProcessor(File input, File outputFolder) {
+        this.input = input;
+        this.outputFolder = outputFolder;
+    }
+    
+    /** Process the bundle Manifest. Can return the original
+     *  one if no changes are needed */
+    protected abstract Manifest processManifest(Manifest originalManifest);
+
+    /** Return the filename to use for the newly created bundle file */
+    protected abstract String getTargetFilename(Manifest inputJarManifest);
+    
     /**
-     * Creates a new OSGi Bundle from a given bundle with the only difference that the
-     * symbolic name is changed. The original symbolic name is recorded in the Manifest
-     * using the {@code X-Original-Bundle-SymbolicName} header.
-     * @param bundleFile The original bundle file. This file will not be modified.
-     * @param newBSN The new Bundle-SymbolicName
-     * @param tempDir The temporary directory to use. This is where the new bundle will be
-     * written. This directory must exist.
-     * @return The new bundle with the altered Symbolic Name.
+     * Creates a new OSGi Bundle from a given bundle, processing its manifest
+     * using the processManifest method.
+     * @return The new bundle file
      * @throws IOException If something goes wrong reading or writing.
      */
-    public static File renameBSN(File bundleFile, String newBSN, File tempDir) throws IOException {
+    public File process() throws IOException {
         JarInputStream jis = null;
         try {
-            jis = new JarInputStream(new FileInputStream(bundleFile));
-            Manifest inputMF = jis.getManifest();
-
-            Attributes inputAttrs = inputMF.getMainAttributes();
-            String bver = inputAttrs.getValue("Bundle-Version");
-            String orgBSN = inputAttrs.getValue("Bundle-SymbolicName");
-            if (bver == null)
-                bver = "0.0.0";
-
-            File newBundle = new File(tempDir, newBSN + "-" + bver + ".jar");
-
-            Manifest newMF = new Manifest(inputMF);
-            Attributes outputAttrs = newMF.getMainAttributes();
-            outputAttrs.putValue("Bundle-SymbolicName", newBSN);
-            outputAttrs.putValue("X-Original-Bundle-SymbolicName", orgBSN);
+            jis = new JarInputStream(new FileInputStream(input));
+            Manifest oldMF = jis.getManifest();
+            Manifest newMF = processManifest(oldMF);
+            File newBundle = new File(outputFolder, getTargetFilename(oldMF));
 
             JarOutputStream jos = null;
             try {
diff --git a/src/test/java/org/apache/sling/commons/osgi/BundleUtilTest.java b/src/test/java/org/apache/sling/commons/osgi/BundleFileProcessorTest.java
similarity index 80%
rename from src/test/java/org/apache/sling/commons/osgi/BundleUtilTest.java
rename to src/test/java/org/apache/sling/commons/osgi/BundleFileProcessorTest.java
index abb77fe..c8d67b4 100644
--- a/src/test/java/org/apache/sling/commons/osgi/BundleUtilTest.java
+++ b/src/test/java/org/apache/sling/commons/osgi/BundleFileProcessorTest.java
@@ -37,7 +37,7 @@ import java.util.jar.Manifest;
 
 import org.junit.Test;
 
-public class BundleUtilTest {
+public class BundleFileProcessorTest {
     
     private static void closeQuietly(Closeable c) {
         try {
@@ -46,6 +46,33 @@ public class BundleUtilTest {
         }
     }
     
+    static class BSNRenamer extends BundleFileProcessor {
+        private final String newBSN;
+        
+        BSNRenamer(File input, File outputFolder, String newBSN) {
+            super(input, outputFolder);
+            this.newBSN = newBSN;
+        }
+        
+        protected Manifest processManifest(Manifest inputMF) {
+            Attributes inputAttrs = inputMF.getMainAttributes();
+            String orgBSN = inputAttrs.getValue("Bundle-SymbolicName");
+            Manifest newMF = new Manifest(inputMF);
+            Attributes outputAttrs = newMF.getMainAttributes();
+            outputAttrs.putValue("Bundle-SymbolicName", newBSN);
+            outputAttrs.putValue("X-Original-Bundle-SymbolicName", orgBSN);
+            return newMF;
+        }
+        
+        protected String getTargetFilename(Manifest inputJarManifest) {
+            String bver = inputJarManifest.getMainAttributes().getValue("Bundle-Version");
+            if (bver == null) {
+                bver = "0.0.0";
+            }
+            return newBSN + "-" + bver + ".jar";
+        }
+    }
+    
     @Test
     public void testBSNRenaming() throws IOException {
         File tempDir = new File(System.getProperty("java.io.tmpdir"));
@@ -53,7 +80,7 @@ public class BundleUtilTest {
         // Just take any bundle from the maven deps as an example...
         File originalFile = getMavenArtifactFile(getMavenRepoRoot(), "com.google.guava", "guava", "15.0");
 
-        File generatedFile = BundleUtil.renameBSN(originalFile, "org.acme.baklava.guava", tempDir);
+        File generatedFile = new BSNRenamer(originalFile, tempDir, "org.acme.baklava.guava").process();
 
         try {
             compareJarContents(originalFile, generatedFile);
@@ -129,7 +156,7 @@ public class BundleUtilTest {
 
     private static byte [] streamToByteArray(InputStream is) throws IOException {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        BundleUtil.pumpStream(is, baos);
+        BundleFileProcessor.pumpStream(is, baos);
         return baos.toByteArray();
     }
 
@@ -138,7 +165,7 @@ public class BundleUtilTest {
     }
 
     private static File getMavenRepoRoot() throws IOException {
-        URL res = BundleUtilTest.class.getClassLoader().getResource(
+        URL res = BundleFileProcessorTest.class.getClassLoader().getResource(
                 Test.class.getName().replace('.', '/') + ".class");
 
         String u = res.toExternalForm();

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