use shared util method to read Manifest from ZipFiles (FELIX-2527)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@982975 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/sigil/common/core/sigil.properties b/sigil/common/core/sigil.properties
index 801ce44..508cfcc 100644
--- a/sigil/common/core/sigil.properties
+++ b/sigil/common/core/sigil.properties
@@ -16,6 +16,7 @@
 	org.apache.felix.sigil.common.core*, \
 	org.apache.felix.sigil.common.model*, \
 	org.apache.felix.sigil.common.repository*, \
+	org.apache.felix.sigil.common.util*, \
 	org.apache.felix.sigil.common.progress*,
 
 -resources: \
@@ -40,8 +41,9 @@
 	org.apache.felix.sigil.common.model, \
 	org.apache.felix.sigil.common.model.eclipse, \
 	org.apache.felix.sigil.common.model.osgi, \
-	org.apache.felix.sigil.common.repository, \
 	org.apache.felix.sigil.common.progress, \
+	org.apache.felix.sigil.common.repository, \
+	org.apache.felix.sigil.common.util, \
 
 -imports: \
 	aQute.lib.osgi;resolve=compile, \
@@ -55,6 +57,7 @@
 	org.apache.felix.sigil.common.model.osgi, \
 	org.apache.felix.sigil.common.osgi, \
 	org.apache.felix.sigil.common.repository, \
+	org.apache.felix.sigil.common.util, \
 	org.osgi.framework, \
 
 header;Bundle-ActivationPolicy: lazy
diff --git a/sigil/common/core/src/org/apache/felix/sigil/common/config/BldConfig.java b/sigil/common/core/src/org/apache/felix/sigil/common/config/BldConfig.java
index f12dd4a..499af0d 100644
--- a/sigil/common/core/src/org/apache/felix/sigil/common/config/BldConfig.java
+++ b/sigil/common/core/src/org/apache/felix/sigil/common/config/BldConfig.java
@@ -29,7 +29,7 @@
 import java.util.Properties;
 import java.util.TreeMap;
 
-import org.apache.felix.sigil.common.core.util.QuoteUtil;
+import org.apache.felix.sigil.common.util.QuoteUtil;
 
 public class BldConfig
 {
diff --git a/sigil/common/core/src/org/apache/felix/sigil/common/core/internal/model/eclipse/SigilBundle.java b/sigil/common/core/src/org/apache/felix/sigil/common/core/internal/model/eclipse/SigilBundle.java
index cd6b3f6..11d7053 100644
--- a/sigil/common/core/src/org/apache/felix/sigil/common/core/internal/model/eclipse/SigilBundle.java
+++ b/sigil/common/core/src/org/apache/felix/sigil/common/core/internal/model/eclipse/SigilBundle.java
@@ -36,7 +36,6 @@
 
 import org.apache.felix.sigil.common.config.Resource;
 import org.apache.felix.sigil.common.core.BldCore;
-import org.apache.felix.sigil.common.core.util.ManifestUtil;
 import org.apache.felix.sigil.common.model.AbstractCompoundModelElement;
 import org.apache.felix.sigil.common.model.eclipse.IBundleCapability;
 import org.apache.felix.sigil.common.model.eclipse.ISigilBundle;
@@ -44,6 +43,7 @@
 import org.apache.felix.sigil.common.model.osgi.IPackageExport;
 import org.apache.felix.sigil.common.model.osgi.IPackageImport;
 import org.apache.felix.sigil.common.progress.IProgress;
+import org.apache.felix.sigil.common.util.ManifestUtil;
 import org.osgi.framework.Version;
 
 /**
@@ -115,10 +115,10 @@
     {
         if (location != null && location.exists())
         {
-            JarFile f = new JarFile(location);
+            JarFile f = new JarFile(location, false);
             try
             {
-                setBundleInfo(ManifestUtil.buildBundleModelElement(f.getManifest()));
+                setBundleInfo(ManifestUtil.buildBundleModelElement(f));
             }
             finally
             {
diff --git a/sigil/common/core/src/org/apache/felix/sigil/common/core/repository/DirectoryHelper.java b/sigil/common/core/src/org/apache/felix/sigil/common/core/repository/DirectoryHelper.java
index 4eaaea1..ec6179d 100644
--- a/sigil/common/core/src/org/apache/felix/sigil/common/core/repository/DirectoryHelper.java
+++ b/sigil/common/core/src/org/apache/felix/sigil/common/core/repository/DirectoryHelper.java
@@ -23,7 +23,6 @@
 import java.io.IOException;
 import java.util.List;
 import java.util.jar.JarFile;
-import java.util.jar.Manifest;
 
 import org.apache.felix.sigil.common.core.BldCore;
 import org.apache.felix.sigil.common.model.ModelElementFactory;
@@ -31,6 +30,7 @@
 import org.apache.felix.sigil.common.model.eclipse.ISigilBundle;
 import org.apache.felix.sigil.common.model.osgi.IBundleModelElement;
 import org.apache.felix.sigil.common.repository.AbstractBundleRepository;
+import org.apache.felix.sigil.common.util.ManifestUtil;
 
 public class DirectoryHelper
 {
@@ -54,7 +54,7 @@
                     try
                     {
                         jar = new JarFile(f, false);
-                        ISigilBundle bundle = buildBundle(repository, jar.getManifest(),
+                        ISigilBundle bundle = buildBundle(repository, jar,
                             f);
                         if (bundle != null)
                         {
@@ -96,9 +96,9 @@
     }
 
     private static ISigilBundle buildBundle(AbstractBundleRepository repository,
-        Manifest manifest, File f)
+        JarFile jar, File f) throws IOException
     {
-        IBundleModelElement info = repository.buildBundleModelElement(manifest);
+        IBundleModelElement info = ManifestUtil.buildBundleModelElement(jar);
 
         ISigilBundle bundle = null;
 
diff --git a/sigil/common/core/src/org/apache/felix/sigil/common/core/repository/SystemRepository.java b/sigil/common/core/src/org/apache/felix/sigil/common/core/repository/SystemRepository.java
index cfd0b77..6796b45 100644
--- a/sigil/common/core/src/org/apache/felix/sigil/common/core/repository/SystemRepository.java
+++ b/sigil/common/core/src/org/apache/felix/sigil/common/core/repository/SystemRepository.java
@@ -31,6 +31,7 @@
 import org.apache.felix.sigil.common.model.osgi.IPackageExport;
 import org.apache.felix.sigil.common.repository.AbstractBundleRepository;
 import org.apache.felix.sigil.common.repository.IRepositoryVisitor;
+import org.apache.felix.sigil.common.util.ManifestUtil;
 
 public class SystemRepository extends AbstractBundleRepository
 {
@@ -73,8 +74,8 @@
                 if (frameworkPath != null)
                 {
                     systemBundle.setLocation(frameworkPath);
-                    jar = new JarFile(frameworkPath);
-                    info = buildBundleModelElement(jar.getManifest());
+                    jar = new JarFile(frameworkPath,false);
+                    info = ManifestUtil.buildBundleModelElement(jar);
                 }
                 else
                 {
diff --git a/sigil/common/core/src/org/apache/felix/sigil/common/repository/AbstractBundleRepository.java b/sigil/common/core/src/org/apache/felix/sigil/common/repository/AbstractBundleRepository.java
index c55e73b..d0d1e80 100644
--- a/sigil/common/core/src/org/apache/felix/sigil/common/repository/AbstractBundleRepository.java
+++ b/sigil/common/core/src/org/apache/felix/sigil/common/repository/AbstractBundleRepository.java
@@ -24,12 +24,10 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
-import java.util.jar.Manifest;
 
 import org.apache.felix.sigil.common.core.BldCore;
 import org.apache.felix.sigil.common.core.licence.ILicenseManager;
 import org.apache.felix.sigil.common.core.licence.ILicensePolicy;
-import org.apache.felix.sigil.common.core.util.ManifestUtil;
 import org.apache.felix.sigil.common.model.IModelElement;
 import org.apache.felix.sigil.common.model.eclipse.ILibrary;
 import org.apache.felix.sigil.common.model.eclipse.ISigilBundle;
@@ -257,11 +255,6 @@
         return found.isEmpty() ? null : found.iterator().next();
     }
 
-    public IBundleModelElement buildBundleModelElement(Manifest mf)
-    {
-        return ManifestUtil.buildBundleModelElement(mf);
-    }
-
     protected ILicensePolicy findPolicy(IModelElement elem)
     {
         ILicenseManager man = BldCore.getLicenseManager();
diff --git a/sigil/common/core/src/org/apache/felix/sigil/common/core/util/ManifestUtil.java b/sigil/common/core/src/org/apache/felix/sigil/common/util/ManifestUtil.java
similarity index 88%
rename from sigil/common/core/src/org/apache/felix/sigil/common/core/util/ManifestUtil.java
rename to sigil/common/core/src/org/apache/felix/sigil/common/util/ManifestUtil.java
index 750c426..9048a77 100644
--- a/sigil/common/core/src/org/apache/felix/sigil/common/core/util/ManifestUtil.java
+++ b/sigil/common/core/src/org/apache/felix/sigil/common/util/ManifestUtil.java
@@ -1,9 +1,13 @@
-package org.apache.felix.sigil.common.core.util;
+package org.apache.felix.sigil.common.util;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.jar.Attributes;
 import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
 
 import org.apache.felix.sigil.common.core.BldCore;
 import org.apache.felix.sigil.common.model.ModelElementFactory;
@@ -18,10 +22,34 @@
 
 public class ManifestUtil
 {
-    public static IBundleModelElement buildBundleModelElement(Manifest mf)
-    {
+    /**
+     * Use this utility to read Manifest from a JarFile or ZipFile
+     * due to http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6735255
+     * 
+     * @param zip
+     * @return
+     * @throws IOException
+     */
+    public static Manifest getManifest(ZipFile zip) throws IOException {
+        ZipEntry entry = zip.getEntry("META-INF/MANIFEST.MF");
+        if ( entry == null ) return null;
+        
+        InputStream in = zip.getInputStream(entry);
+        try {
+            return new Manifest(in);
+        }
+        finally {
+            // explicitly close due to 
+            // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6735255
+            in.close();
+        }
+    }
+    
+    public static IBundleModelElement buildBundleModelElement(ZipFile zip) throws IOException {
         IBundleModelElement info = null;
 
+        Manifest mf = getManifest(zip);
+        
         if (mf != null)
         {
             Attributes attrs = mf.getMainAttributes();
diff --git a/sigil/common/core/src/org/apache/felix/sigil/common/core/util/QuoteUtil.java b/sigil/common/core/src/org/apache/felix/sigil/common/util/QuoteUtil.java
similarity index 97%
rename from sigil/common/core/src/org/apache/felix/sigil/common/core/util/QuoteUtil.java
rename to sigil/common/core/src/org/apache/felix/sigil/common/util/QuoteUtil.java
index dc88889..4bab43e 100644
--- a/sigil/common/core/src/org/apache/felix/sigil/common/core/util/QuoteUtil.java
+++ b/sigil/common/core/src/org/apache/felix/sigil/common/util/QuoteUtil.java
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-package org.apache.felix.sigil.common.core.util;
+package org.apache.felix.sigil.common.util;
 
 import java.util.ArrayList;
 
diff --git a/sigil/eclipse/core/sigil.properties b/sigil/eclipse/core/sigil.properties
index e9689e1..42e5b89 100644
--- a/sigil/eclipse/core/sigil.properties
+++ b/sigil/eclipse/core/sigil.properties
@@ -38,8 +38,9 @@
 	org.apache.felix.sigil.common.model.eclipse, \
 	org.apache.felix.sigil.common.model.osgi, \
 	org.apache.felix.sigil.common.osgi, \
-	org.apache.felix.sigil.common.progress;version=0.9.0, \
+	org.apache.felix.sigil.common.progress, \
 	org.apache.felix.sigil.common.repository, \
+	org.apache.felix.sigil.common.util, \
 	org.apache.felix.sigil.eclipse, \
 	org.apache.felix.sigil.eclipse.install, \
 	org.apache.felix.sigil.eclipse.job, \
diff --git a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/internal/repository/eclipse/OSGiInstallRepository.java b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/internal/repository/eclipse/OSGiInstallRepository.java
index 79c4f12..c479fa2 100644
--- a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/internal/repository/eclipse/OSGiInstallRepository.java
+++ b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/internal/repository/eclipse/OSGiInstallRepository.java
@@ -26,7 +26,6 @@
 import java.util.List;
 import java.util.Map;
 import java.util.jar.JarFile;
-import java.util.jar.Manifest;
 
 import org.apache.felix.sigil.common.core.BldCore;
 import org.apache.felix.sigil.common.model.ModelElementFactory;
@@ -35,6 +34,7 @@
 import org.apache.felix.sigil.common.model.osgi.IBundleModelElement;
 import org.apache.felix.sigil.common.repository.AbstractBundleRepository;
 import org.apache.felix.sigil.common.repository.IRepositoryVisitor;
+import org.apache.felix.sigil.common.util.ManifestUtil;
 import org.apache.felix.sigil.eclipse.SigilCore;
 import org.apache.felix.sigil.eclipse.install.IOSGiInstall;
 import org.eclipse.core.runtime.IPath;
@@ -106,8 +106,8 @@
         JarFile jar = null;
         try
         {
-            jar = new JarFile(f);
-            ISigilBundle bundle = buildBundle(jar.getManifest(), f);
+            jar = new JarFile(f, false);
+            ISigilBundle bundle = buildBundle(jar, f);
             if (bundle != null)
             {
                 bundle.setLocation(f);
@@ -145,9 +145,9 @@
         }
     }
 
-    private ISigilBundle buildBundle(Manifest manifest, File f)
+    private ISigilBundle buildBundle(JarFile jar, File f) throws IOException
     {
-        IBundleModelElement info = buildBundleModelElement(manifest);
+        IBundleModelElement info = ManifestUtil.buildBundleModelElement(jar);
 
         ISigilBundle bundle = null;