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/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