FELIX-3300: fix calculateExportsFromContents to avoid exporting empty packages, also only use calculateExportsFromContents when analyzing attached files

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1243107 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
index 733e006..3136d89 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
@@ -40,6 +40,7 @@
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.TreeSet;
 import java.util.jar.Attributes;
 import java.util.jar.Manifest;
 
@@ -1239,7 +1240,7 @@
 
     private static void addLocalPackages( File outputDirectory, Analyzer analyzer )
     {
-        Collection packages = new LinkedHashSet();
+        Collection packages = new TreeSet();
 
         if ( outputDirectory != null && outputDirectory.isDirectory() )
         {
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/ManifestPlugin.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/ManifestPlugin.java
index c7a6544..9f0e892 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/ManifestPlugin.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/ManifestPlugin.java
@@ -24,6 +24,7 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -178,11 +179,16 @@
 
         analyzer.setJar( file );
 
+        // calculateExportsFromContents when we have no explicit instructions defining
+        // the contents of the bundle *and* we are not analyzing the output directory,
+        // otherwise fall-back to addMavenInstructions approach
+
         if ( analyzer.getProperty( Analyzer.EXPORT_PACKAGE ) == null
             && analyzer.getProperty( Analyzer.EXPORT_CONTENTS ) == null
-            && analyzer.getProperty( Analyzer.PRIVATE_PACKAGE ) == null )
+            && analyzer.getProperty( Analyzer.PRIVATE_PACKAGE ) == null
+            && !file.equals( getOutputDirectory() ) )
         {
-            String export = analyzer.calculateExportsFromContents( analyzer.getJar() );
+            String export = calculateExportsFromContents( analyzer.getJar() );
             analyzer.setProperty( Analyzer.EXPORT_PACKAGE, export );
         }
 
@@ -219,4 +225,41 @@
             }
         }
     }
+
+
+    /*
+     * Patched version of bnd's Analyzer.calculateExportsFromContents
+     */
+    public static String calculateExportsFromContents( Jar bundle )
+    {
+        String ddel = "";
+        StringBuffer sb = new StringBuffer();
+        Map<String, Map<String, Resource>> map = bundle.getDirectories();
+        for ( Iterator<Entry<String, Map<String, Resource>>> i = map.entrySet().iterator(); i.hasNext(); )
+        {
+            //----------------------------------------------------
+            // should also ignore directories with no resources
+            //----------------------------------------------------
+            Entry<String, Map<String, Resource>> entry = i.next();
+            if ( entry.getValue() == null || entry.getValue().isEmpty() )
+                continue;
+            //----------------------------------------------------
+            String directory = entry.getKey();
+            if ( directory.equals( "META-INF" ) || directory.startsWith( "META-INF/" ) )
+                continue;
+            if ( directory.equals( "OSGI-OPT" ) || directory.startsWith( "OSGI-OPT/" ) )
+                continue;
+            if ( directory.equals( "/" ) )
+                continue;
+
+            if ( directory.endsWith( "/" ) )
+                directory = directory.substring( 0, directory.length() - 1 );
+
+            directory = directory.replace( '/', '.' );
+            sb.append( ddel );
+            sb.append( directory );
+            ddel = ",";
+        }
+        return sb.toString();
+    }
 }