Author: bdelacretaz
Date: Mon Dec 21 15:48:12 2015
New Revision: 1721199
URL: http://svn.apache.org/viewvc?rev=1721199&view=rev
Log:
SLING-5379 - make BundleFileProcessor more generic
Added:
sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/BundleFileProcessor.java
- copied, changed from r1721192,
sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/BundleUtil.java
sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/BundleFileProcessorTest.java
- copied, changed from r1721192,
sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/BundleUtilTest.java
Removed:
sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/BundleUtil.java
sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/BundleUtilTest.java
Copied:
sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/BundleFileProcessor.java
(from r1721192,
sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/BundleUtil.java)
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/BundleFileProcessor.java?p2=sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/BundleFileProcessor.java&p1=sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/BundleUtil.java&r1=1721192&r2=1721199&rev=1721199&view=diff
==============================================================================
---
sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/BundleUtil.java
(original)
+++
sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/BundleFileProcessor.java
Mon Dec 21 15:48:12 2015
@@ -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 {
Copied:
sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/BundleFileProcessorTest.java
(from r1721192,
sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/BundleUtilTest.java)
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/BundleFileProcessorTest.java?p2=sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/BundleFileProcessorTest.java&p1=sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/BundleUtilTest.java&r1=1721192&r2=1721199&rev=1721199&view=diff
==============================================================================
---
sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/BundleUtilTest.java
(original)
+++
sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/BundleFileProcessorTest.java
Mon Dec 21 15:48:12 2015
@@ -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();