FELIX-518 - support localization and signature files:

- ignore signature files (for now);
- allow localization files to precede bundle files.



git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1716193 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/deploymentadmin/deploymentadmin/src/main/java/org/apache/felix/deploymentadmin/AbstractDeploymentPackage.java b/deploymentadmin/deploymentadmin/src/main/java/org/apache/felix/deploymentadmin/AbstractDeploymentPackage.java
index 1d3ec68..89f4ce9 100644
--- a/deploymentadmin/deploymentadmin/src/main/java/org/apache/felix/deploymentadmin/AbstractDeploymentPackage.java
+++ b/deploymentadmin/deploymentadmin/src/main/java/org/apache/felix/deploymentadmin/AbstractDeploymentPackage.java
@@ -24,6 +24,7 @@
 import java.net.URL;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.jar.Manifest;
 
@@ -160,7 +161,7 @@
      * @param manifest The manifest of the deployment package.
      * @param bundleContext The bundle context.
      * @throws DeploymentException Thrown if the specified manifest does not
-     *         describe a valid deployment package.
+     *             describe a valid deployment package.
      */
     public AbstractDeploymentPackage(Manifest manifest, BundleContext bundleContext, DeploymentAdminImpl deploymentAdmin) throws DeploymentException {
         m_manifest = new DeploymentPackageManifest(manifest);
@@ -279,7 +280,7 @@
      * @return Stream to the bundle identified by the specified symbolic name or
      *         null if no such bundle exists in this deployment package.
      * @throws IOException If the bundle can not be properly offered as an
-     *         inputstream
+     *             inputstream
      */
     public abstract InputStream getBundleStream(String symbolicName) throws IOException;
 
@@ -491,7 +492,7 @@
      * it's path/resource-id.
      *
      * @param path String containing a resource path (either bundle or processed
-     *        resource)
+     *            resource)
      * @return <code>AbstractInfoImpl</code> for the resource identified by the
      *         specified path or null if the path is unknown
      */
@@ -499,4 +500,17 @@
         return (AbstractInfo) m_pathToEntry.get(path);
     }
 
+    /**
+     * Returns whether the given name (which is expected to be the name of a
+     * JarEntry) is a signature file or the JAR index file.
+     * 
+     * @param name the name of the JAR entry to test, cannot be
+     *            <code>null</code>.
+     * @return <code>true</code> if the given JAR entry name is a signature file
+     *         or JAR index file, <code>false</code> otherwise.
+     */
+    protected boolean isMetaInfFile(String name) {
+        name = name.toUpperCase(Locale.US);
+        return name.startsWith("META-INF/") && (name.endsWith("/INDEX.LIST") || name.endsWith(".SF") || name.endsWith(".DSA") || name.endsWith(".RS"));
+    }
 }
diff --git a/deploymentadmin/deploymentadmin/src/main/java/org/apache/felix/deploymentadmin/StreamDeploymentPackage.java b/deploymentadmin/deploymentadmin/src/main/java/org/apache/felix/deploymentadmin/StreamDeploymentPackage.java
index 8279739..4c88d9c 100644
--- a/deploymentadmin/deploymentadmin/src/main/java/org/apache/felix/deploymentadmin/StreamDeploymentPackage.java
+++ b/deploymentadmin/deploymentadmin/src/main/java/org/apache/felix/deploymentadmin/StreamDeploymentPackage.java
@@ -35,6 +35,7 @@
 public class StreamDeploymentPackage extends AbstractDeploymentPackage {
     private final JarInputStream m_input;
     private final List m_names = new ArrayList();
+    private boolean m_inMetaInf = true;
 
     /**
      * Creates an instance of this class.
@@ -57,17 +58,37 @@
     }
 
     public AbstractInfo getNextEntry() throws IOException {
-        ZipEntry nextEntry = m_input.getNextJarEntry();
-        if (nextEntry == null) {
-            return null;
+        String name;
+
+        boolean metaInfFile = true;
+        do {
+            ZipEntry nextEntry = m_input.getNextJarEntry();
+            if (nextEntry == null) {
+                return null;
+            }
+            name = nextEntry.getName();
+
+            // FELIX-518: do not try to process signature or localization files...
+            metaInfFile = isMetaInfFile(name);
+            if (metaInfFile) {
+                if (!m_inMetaInf) {
+                    throw new IOException("Unexpected signature file found after manifest files: " + name);
+                }
+                else {
+                    continue;
+                }
+            }
         }
-        String name = nextEntry.getName();
+        while (metaInfFile);
+
+        m_inMetaInf = false;
         m_names.add(name);
-        AbstractInfo abstractInfoByPath = getAbstractInfoByPath(name);
-        return abstractInfoByPath;
+
+        return getAbstractInfoByPath(name);
     }
 
-    // This only works for those resources that have been read from the stream already, no guarantees for remainder of stream
+    // This only works for those resources that have been read from the stream already, no guarantees for remainder of
+    // stream
     public BundleInfoImpl[] getOrderedBundleInfos() {
         List result = new ArrayList();
 
diff --git a/deploymentadmin/deploymentadmin/src/main/java/org/apache/felix/deploymentadmin/spi/UpdateCommand.java b/deploymentadmin/deploymentadmin/src/main/java/org/apache/felix/deploymentadmin/spi/UpdateCommand.java
index 4e96879..bda0449 100644
--- a/deploymentadmin/deploymentadmin/src/main/java/org/apache/felix/deploymentadmin/spi/UpdateCommand.java
+++ b/deploymentadmin/deploymentadmin/src/main/java/org/apache/felix/deploymentadmin/spi/UpdateCommand.java
@@ -65,6 +65,10 @@
                 String name = entry.getPath();
                 BundleInfoImpl bundleInfo = (BundleInfoImpl) expectedBundles.remove(name);
                 if (bundleInfo == null) {
+                    if (isLocalizationFile(name)) {
+                        // FELIX-518: do not try to process signature or localization files...
+                        continue;
+                    }
                     throw new DeploymentException(CODE_OTHER_ERROR, "Resource '" + name + "' is not described in the manifest.");
                 }
 
@@ -100,8 +104,8 @@
 
                 Version targetVersion = getVersion(bundle);
                 if (!sourceVersion.equals(targetVersion)) {
-                    throw new DeploymentException(CODE_OTHER_ERROR, "Installed/updated bundle version (" + targetVersion + ") do not match what was installed/updated: " + sourceVersion
-                        + ", offending bundle = " + bsn);
+                    throw new DeploymentException(CODE_OTHER_ERROR,
+                        "Installed/updated bundle version (" + targetVersion + ") do not match what was installed/updated: " + sourceVersion + ", offending bundle = " + bsn);
                 }
             }
         }
@@ -114,6 +118,10 @@
         return Version.parseVersion((String) bundle.getHeaders().get(BUNDLE_VERSION));
     }
 
+    private boolean isLocalizationFile(String name) {
+        return name.startsWith("OSGI-INF/l10n/");
+    }
+
     private static class UninstallBundleRunnable extends AbstractAction {
         private final Bundle m_bundle;
         private final LogService m_log;
diff --git a/deploymentadmin/itest/pom.xml b/deploymentadmin/itest/pom.xml
index f1421ac..4a45f59 100644
--- a/deploymentadmin/itest/pom.xml
+++ b/deploymentadmin/itest/pom.xml
@@ -20,7 +20,7 @@
 	</parent>
 	<properties>
 		<osgi.version>4.2.0</osgi.version>
-		<pax.exam.version>3.4.0</pax.exam.version>
+		<pax.exam.version>3.6.0</pax.exam.version>
 		<pax.url.version>1.6.0</pax.url.version>
 	</properties>
 	<name>Apache Felix DeploymentAdmin Integration Tests</name>
@@ -46,7 +46,7 @@
 		<dependency>
 			<groupId>org.apache.felix</groupId>
 			<artifactId>org.apache.felix.dependencymanager</artifactId>
-			<version>3.1.0</version>
+			<version>4.1.1</version>
 			<scope>test</scope>
 		</dependency>
 		<dependency>
@@ -58,7 +58,7 @@
 		<dependency>
 			<groupId>org.apache.felix</groupId>
 			<artifactId>org.apache.felix.metatype</artifactId>
-			<version>1.0.6</version>
+			<version>1.1.2</version>
 			<scope>test</scope>
 		</dependency>
 		<dependency>
@@ -82,7 +82,12 @@
 		<dependency>
 			<groupId>junit</groupId>
 			<artifactId>junit</artifactId>
-			<version>4.11</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>commons-codec</groupId>
+			<artifactId>commons-codec</artifactId>
+			<version>1.10</version>
 			<scope>test</scope>
 		</dependency>
 
@@ -114,13 +119,13 @@
 		<dependency>
 			<groupId>ch.qos.logback</groupId>
 			<artifactId>logback-core</artifactId>
-			<version>1.1.2</version>
+			<version>1.1.3</version>
 			<scope>test</scope>
 		</dependency>
 		<dependency>
 			<groupId>ch.qos.logback</groupId>
 			<artifactId>logback-classic</artifactId>
-			<version>1.1.2</version>
+			<version>1.1.3</version>
 			<scope>test</scope>
 		</dependency>
 
@@ -147,6 +152,14 @@
 					<source>1.6</source>
 				</configuration>
 			</plugin>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<configuration>
+					<source>1.7</source>
+					<target>1.7</target>
+				</configuration>
+			</plugin>
 		</plugins>
 		<pluginManagement>
 			<plugins>
diff --git a/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/BaseIntegrationTest.java b/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/BaseIntegrationTest.java
index d6020ce..f13df3f 100644
--- a/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/BaseIntegrationTest.java
+++ b/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/BaseIntegrationTest.java
@@ -88,6 +88,7 @@
             mavenBundle("org.apache.felix", "org.apache.felix.deploymentadmin").versionAsInProject(),
             mavenBundle("org.apache.felix", "org.apache.felix.eventadmin").versionAsInProject(),
             mavenBundle("org.apache.felix", "org.apache.felix.configadmin").versionAsInProject(),
+            mavenBundle("commons-codec", "commons-codec").versionAsInProject(),
             
             junitBundles()
         );
diff --git a/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/InstallDeploymentPackageTest.java b/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/InstallDeploymentPackageTest.java
index 3651018..2b0ff9a 100644
--- a/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/InstallDeploymentPackageTest.java
+++ b/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/InstallDeploymentPackageTest.java
@@ -19,6 +19,7 @@
 package org.apache.felix.deploymentadmin.itest;
 
 import java.io.File;
+import java.net.URL;
 
 import org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder;
 import org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder.JarManifestManipulatingFilter;
@@ -37,6 +38,24 @@
 public class InstallDeploymentPackageTest extends BaseIntegrationTest
 {
     /**
+     * FELIX-518 - Test that DP with localization and signature files are properly deployed.
+     */
+    @Test
+    public void testInstallDeploymentPackageWithLocalizationAndSignatureFilesOk() throws Exception {
+        URL dpProps = getClass().getResource("/dp.properties");
+        assertNotNull(dpProps);
+        
+        DeploymentPackageBuilder dpBuilder = createNewDeploymentPackageBuilder("1.0.0");
+        dpBuilder
+            .addSignatures()
+            .add(dpBuilder.createLocalizationResource().setUrl(dpProps).setResourceProcessorPID(TEST_FAILING_BUNDLE_RP1).setFilename("dp.properties"))
+            .add(dpBuilder.createResourceProcessorResource().setUrl(getTestBundleURL("rp1")));
+
+        installDeploymentPackage(dpBuilder);
+    }
+    
+    
+    /**
      * FELIX-4409/4410/4463 - test the installation of an invalid deployment package.
      */
     @Test
diff --git a/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/util/ArtifactData.java b/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/util/ArtifactData.java
index d3cdd9e..a4a6939 100644
--- a/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/util/ArtifactData.java
+++ b/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/util/ArtifactData.java
@@ -66,6 +66,10 @@
         return m_isBundle;
     }
 
+    public boolean isLocalizationFile() {
+        return m_filename.startsWith("OSGI-INF/l10n/");
+    }
+
     public boolean isCustomizer() {
         return m_isCustomizer;
     }
diff --git a/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/util/DeploymentPackageBuilder.java b/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/util/DeploymentPackageBuilder.java
index 1d67ddb..727f20d 100644
--- a/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/util/DeploymentPackageBuilder.java
+++ b/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/util/DeploymentPackageBuilder.java
@@ -20,6 +20,7 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -99,23 +100,6 @@
 
     private static final int BUFFER_SIZE = 32 * 1024;
 
-    private final String m_symbolicName;
-    private final String m_version;
-    private final List<ArtifactData> m_bundles = new ArrayList<ArtifactData>();
-    private final List<ArtifactData> m_processors = new ArrayList<ArtifactData>();
-
-    private final List<ArtifactData> m_artifacts = new ArrayList<ArtifactData>();
-    private String m_fixPackageVersion;
-
-    private boolean m_verification;
-
-    private DeploymentPackageBuilder(String symbolicName, String version) {
-        m_symbolicName = symbolicName;
-        m_version = version;
-
-        m_verification = true;
-    }
-
     /**
      * Creates a new deployment package builder.
      * 
@@ -127,6 +111,27 @@
         return new DeploymentPackageBuilder(name, version);
     }
 
+    private final String m_symbolicName;
+    private final String m_version;
+    private final List<ArtifactData> m_localizationFiles = new ArrayList<ArtifactData>();
+    private final List<ArtifactData> m_bundles = new ArrayList<ArtifactData>();
+    private final List<ArtifactData> m_processors = new ArrayList<ArtifactData>();
+
+    private final List<ArtifactData> m_artifacts = new ArrayList<ArtifactData>();
+
+    private String m_fixPackageVersion;
+
+    private boolean m_addSignatures;
+    private boolean m_verification;
+
+    private DeploymentPackageBuilder(String symbolicName, String version) {
+        m_symbolicName = symbolicName;
+        m_version = version;
+
+        m_addSignatures = false;
+        m_verification = true;
+    }
+
     /**
      * Adds an artifact to the deployment package.
      * 
@@ -142,12 +147,20 @@
         else if (artifactData.isBundle()) {
             m_bundles.add(artifactData);
         }
+        else if (artifactData.isLocalizationFile()) {
+            m_localizationFiles.add(artifactData);
+        }
         else {
             m_artifacts.add(artifactData);
         }
         return this;
     }
 
+    public DeploymentPackageBuilder addSignatures() {
+        m_addSignatures = true;
+        return this;
+    }
+
     /**
      * Creates a new deployment package builder with the same symbolic name as this builder.
      * 
@@ -163,6 +176,10 @@
         return new BundleDataBuilder();
     }
 
+    public LocalizationResourceDataBuilder createLocalizationResource() {
+        return new LocalizationResourceDataBuilder();
+    }
+
     public ResourceDataBuilder createResource() {
         return new ResourceDataBuilder();
     }
@@ -207,6 +224,7 @@
      */
     public void generate(OutputStream output) throws Exception {
         List<ArtifactData> artifacts = new ArrayList<ArtifactData>();
+        artifacts.addAll(m_localizationFiles);
         artifacts.addAll(m_bundles);
         artifacts.addAll(m_processors);
         artifacts.addAll(m_artifacts);
@@ -217,6 +235,14 @@
         }
 
         Manifest m = createManifest(artifacts);
+
+        // The order in which the actual entries are added to the JAR is different than we're using for the manifest...
+        artifacts.clear();
+        artifacts.addAll(m_bundles);
+        artifacts.addAll(m_processors);
+        artifacts.addAll(m_localizationFiles);
+        artifacts.addAll(m_artifacts);
+
         writeStream(artifacts, m, output);
     }
 
@@ -293,6 +319,10 @@
                 a.putValue("DeploymentPackage-Missing", "true");
             }
 
+            if (m_addSignatures) {
+                a.putValue("SHA-256-Digest", "bogusdata=");
+            }
+
             entries.put(file.getFilename(), a);
         }
 
@@ -337,12 +367,40 @@
         }
     }
 
+    private InputStream getArtifactDataInputStream(ArtifactData file) throws IOException {
+        ResourceFilter filter = file.getFilter();
+        if (filter != null) {
+            return filter.createInputStream(file.getURL());
+        }
+        return file.getURL().openStream();
+    }
+
     private void writeStream(List<ArtifactData> files, Manifest manifest, OutputStream outputStream) throws Exception {
+        byte[] buffer = new byte[BUFFER_SIZE];
         JarOutputStream output = null;
-        InputStream fis = null;
+        InputStream is = null;
         try {
             output = new JarOutputStream(outputStream, manifest);
-            byte[] buffer = new byte[BUFFER_SIZE];
+
+            if (m_addSignatures) {
+                // Empty file index...
+                output.putNextEntry(new JarEntry("META-INF/INDEX.LIST"));
+                output.write(new byte[0]);
+                output.closeEntry();
+                
+                // Create a signature file + signature block
+                Manifest mf = new Manifest();
+                mf.getMainAttributes().put(Attributes.Name.SIGNATURE_VERSION, "1.0");
+                mf.getMainAttributes().putValue("SHA-256-Digest-Manifest", "bogusdata=");
+
+                output.putNextEntry(new JarEntry("META-INF/DP.SF"));
+                mf.write(output);
+                output.closeEntry();
+
+                output.putNextEntry(new JarEntry("META-INF/DP.DSA"));
+                output.write(new byte[] { 1, 2, 3, 4 });
+                output.closeEntry();
+            }
 
             Iterator<ArtifactData> filesIter = files.iterator();
             while (filesIter.hasNext()) {
@@ -354,35 +412,33 @@
 
                 output.putNextEntry(new JarEntry(file.getFilename()));
 
-                ResourceFilter filter = file.getFilter();
-                if (filter != null) {
-                    fis = filter.createInputStream(file.getURL());
-                }
-                else {
-                    fis = file.getURL().openStream();
-                }
-
+                is = getArtifactDataInputStream(file);
                 try {
-                    int bytes = fis.read(buffer);
-                    while (bytes != -1) {
+                    int bytes;
+                    while ((bytes = is.read(buffer)) != -1) {
                         output.write(buffer, 0, bytes);
-                        bytes = fis.read(buffer);
                     }
                 }
                 finally {
-                    fis.close();
-                    fis = null;
+                    closeSilently(is);
 
                     output.closeEntry();
                 }
             }
         }
         finally {
-            if (fis != null) {
-                fis.close();
+            closeSilently(is);
+            closeSilently(output);
+        }
+    }
+
+    static void closeSilently(Closeable resource) {
+        if (resource != null) {
+            try {
+                resource.close();
             }
-            if (output != null) {
-                output.close();
+            catch (IOException e) {
+                // Ignore...
             }
         }
     }
diff --git a/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/util/LocalizationResourceDataBuilder.java b/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/util/LocalizationResourceDataBuilder.java
new file mode 100644
index 0000000..a04cea9
--- /dev/null
+++ b/deploymentadmin/itest/src/test/java/org/apache/felix/deploymentadmin/itest/util/LocalizationResourceDataBuilder.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.deploymentadmin.itest.util;
+
+/**
+ * Provides a resource data builder.
+ */
+public class LocalizationResourceDataBuilder extends ResourceDataBuilder {
+
+    public LocalizationResourceDataBuilder() {
+        // Nop
+    }
+
+    @Override
+    public LocalizationResourceDataBuilder setFilename(String filename) {
+        String prefix = "OSGI-INF/l10n/";
+        if (!filename.startsWith(prefix)) {
+            filename = prefix.concat(filename);
+        }
+        return (LocalizationResourceDataBuilder) super.setFilename(filename);
+    }
+
+    @Override
+    LocalizationResourceDataBuilder getThis() {
+        return this;
+    }
+}
diff --git a/deploymentadmin/itest/src/test/resources/dp.properties b/deploymentadmin/itest/src/test/resources/dp.properties
new file mode 100644
index 0000000..687d015
--- /dev/null
+++ b/deploymentadmin/itest/src/test/resources/dp.properties
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+# no additional properties
\ No newline at end of file