Support JAR-wide manifest package metadata. (FELIX-682)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@690313 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/Felix.java b/framework/src/main/java/org/apache/felix/framework/Felix.java
index a6c6c25..70616c6 100644
--- a/framework/src/main/java/org/apache/felix/framework/Felix.java
+++ b/framework/src/main/java/org/apache/felix/framework/Felix.java
@@ -3445,6 +3445,7 @@
         // Note, in case this is an extension bundle it's exports are removed -
         // they will be added to the system bundle directly later on.
         IModuleDefinition md = new ModuleDefinition(
+            headerMap,
             (isExtensionBundle) ? null : mp.getCapabilities(),
             mp.getRequirements(),
             mp.getDynamicRequirements(),
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/DirectoryRevision.java b/framework/src/main/java/org/apache/felix/framework/cache/DirectoryRevision.java
index 94ee30d..918f3e2 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/DirectoryRevision.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/DirectoryRevision.java
@@ -21,18 +21,12 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.List;
 import java.util.Map;
 import java.util.jar.Manifest;
 
 import org.apache.felix.framework.Logger;
-import org.apache.felix.framework.util.FelixConstants;
 import org.apache.felix.framework.util.StringMap;
-import org.apache.felix.framework.util.manifestparser.ManifestParser;
-import org.apache.felix.framework.cache.DirectoryContent;
 import org.apache.felix.moduleloader.IContent;
-import org.apache.felix.framework.cache.JarContent;
 
 /**
  * <p>
@@ -44,7 +38,6 @@
 class DirectoryRevision extends BundleRevision
 {
     private File m_refDir = null;
-    private Map m_header = null;
 
     public DirectoryRevision(
         Logger logger, File revisionRootDir, String location) throws Exception
@@ -76,11 +69,6 @@
     public synchronized Map getManifestHeader()
         throws Exception
     {
-        if (m_header != null)
-        {
-            return m_header;
-        }
-
         // Read the header file from the reference directory.
         InputStream is = null;
 
@@ -98,8 +86,7 @@
             // Get manifest.
             Manifest mf = new Manifest(is);
             // Create a case insensitive map of manifest attributes.
-            m_header = new StringMap(mf.getMainAttributes(), false);
-            return m_header;
+            return new StringMap(mf.getMainAttributes(), false);
         }
         finally
         {
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/JarRevision.java b/framework/src/main/java/org/apache/felix/framework/cache/JarRevision.java
index 84c3ebb..cf9cc7e 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/JarRevision.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/JarRevision.java
@@ -48,7 +48,6 @@
     private static final transient String BUNDLE_JAR_FILE = "bundle.jar";
 
     private File m_bundleFile = null;
-    private Map m_header = null;
 
     public JarRevision(
         Logger logger, File revisionRootDir, String location, boolean byReference)
@@ -81,11 +80,6 @@
 
     public synchronized Map getManifestHeader() throws Exception
     {
-        if (m_header != null)
-        {
-            return m_header;
-        }
-
         // Get the embedded resource.
         JarFile jarFile = null;
 
@@ -101,9 +95,7 @@
             // Get manifest.
             Manifest mf = jarFile.getManifest();
             // Create a case insensitive map of manifest attributes.
-            m_header = new StringMap(mf.getMainAttributes(), false);
-            return m_header;
-
+            return new StringMap(mf.getMainAttributes(), false);
         }
         finally
         {
diff --git a/framework/src/main/java/org/apache/felix/framework/searchpolicy/ContentLoaderImpl.java b/framework/src/main/java/org/apache/felix/framework/searchpolicy/ContentLoaderImpl.java
index 27211a8..d99f872 100644
--- a/framework/src/main/java/org/apache/felix/framework/searchpolicy/ContentLoaderImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/searchpolicy/ContentLoaderImpl.java
@@ -332,6 +332,10 @@
         Map headers = null;
         try
         {
+// TODO: REFACTOR - It seems that we shouldn't have to get the manifest
+//       here since we already have it in our module definition, but we
+//       don't have access to the module definition here. This is something
+//       to be considered when we refactor the module layer.
             is = content.getEntryAsStream("META-INF/MANIFEST.MF");
             headers = new StringMap(new Manifest(is).getMainAttributes(), false);
         }
diff --git a/framework/src/main/java/org/apache/felix/framework/searchpolicy/ModuleDefinition.java b/framework/src/main/java/org/apache/felix/framework/searchpolicy/ModuleDefinition.java
index 9139325..73eb14d 100644
--- a/framework/src/main/java/org/apache/felix/framework/searchpolicy/ModuleDefinition.java
+++ b/framework/src/main/java/org/apache/felix/framework/searchpolicy/ModuleDefinition.java
@@ -18,26 +18,34 @@
  */
 package org.apache.felix.framework.searchpolicy;
 
+import java.util.Map;
 import org.apache.felix.framework.util.manifestparser.R4Library;
 import org.apache.felix.moduleloader.*;
 
 public class ModuleDefinition implements IModuleDefinition
 {
-    public ICapability[] m_capabilities = null;
-    public IRequirement[] m_requirements = null;
-    public IRequirement[] m_dynamicRequirements = null;
+    private Map m_headerMap = null;
+    private ICapability[] m_capabilities = null;
+    private IRequirement[] m_requirements = null;
+    private IRequirement[] m_dynamicRequirements = null;
     private R4Library[] m_libraries = null;
 
     public ModuleDefinition(
-        ICapability[] capabilities, IRequirement[] requirements,
+        Map headerMap, ICapability[] capabilities, IRequirement[] requirements,
         IRequirement[] dynamicRequirements, R4Library[] libraries)
     {
+        m_headerMap = headerMap;
         m_capabilities = capabilities;
         m_requirements = requirements;
         m_dynamicRequirements = dynamicRequirements;
         m_libraries = libraries;
     }
 
+    public Map getHeaders()
+    {
+        return m_headerMap;
+    }
+
     public ICapability[] getCapabilities()
     {
 // TODO: RB - These should probably all return copies of the array.
diff --git a/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicyCore.java b/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicyCore.java
index e4786fe..ec22542 100755
--- a/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicyCore.java
+++ b/framework/src/main/java/org/apache/felix/framework/searchpolicy/R4SearchPolicyCore.java
@@ -33,7 +33,6 @@
 import org.apache.felix.framework.BundleProtectionDomain;
 import org.apache.felix.framework.Logger;
 import org.apache.felix.framework.util.CompoundEnumeration;
-import org.apache.felix.framework.util.FelixConstants;
 import org.apache.felix.framework.util.SecurityManagerEx;
 import org.apache.felix.framework.util.Util;
 import org.apache.felix.framework.util.manifestparser.Capability;
@@ -43,7 +42,6 @@
 import org.apache.felix.framework.util.manifestparser.R4Library;
 import org.apache.felix.framework.util.manifestparser.Requirement;
 import org.apache.felix.moduleloader.ICapability;
-import org.apache.felix.moduleloader.IContent;
 import org.apache.felix.moduleloader.IModule;
 import org.apache.felix.moduleloader.IModuleFactory;
 import org.apache.felix.moduleloader.IRequirement;
@@ -158,25 +156,23 @@
 
     public Object[] definePackage(IModule module, String pkgName)
     {
-        try
+        Map headerMap = ((ModuleDefinition) module.getDefinition()).getHeaders();
+        String spectitle = (String) headerMap.get("Specification-Title");
+        String specversion = (String) headerMap.get("Specification-Version");
+        String specvendor = (String) headerMap.get("Specification-Vendor");
+        String impltitle = (String) headerMap.get("Implementation-Title");
+        String implversion = (String) headerMap.get("Implementation-Version");
+        String implvendor = (String) headerMap.get("Implementation-Vendor");
+        if ((spectitle != null)
+            || (specversion != null)
+            || (specvendor != null)
+            || (impltitle != null)
+            || (implversion != null)
+            || (implvendor != null))
         {
-            ICapability cap = Util.getSatisfyingCapability(module,
-                new Requirement(ICapability.PACKAGE_NAMESPACE, "(package=" + pkgName + ")"));
-            if (cap != null)
-            {
-                return new Object[] {
-                    pkgName, // Spec title.
-                    cap.getProperties().get(ICapability.VERSION_PROPERTY).toString(), // Spec version.
-                    "", // Spec vendor.
-                    "", // Impl title.
-                    "", // Impl version.
-                    "" // Impl vendor.
-                };
-            }
-        }
-        catch (InvalidSyntaxException ex)
-        {
-            // This should never happen.
+            return new Object[] {
+                spectitle, specversion, specvendor, impltitle, implversion, implvendor
+            };
         }
         return null;
     }