FELIX-912: set default Export-Package based on local source files

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@744869 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 ca4565a..942dc23 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
@@ -386,10 +386,7 @@
             }
             else
             {
-                String bsn = builder.getProperty( Analyzer.BUNDLE_SYMBOLICNAME );
-                String namespace = bsn.replaceAll( "\\W", "." );
-
-                builder.setProperty( Analyzer.EXPORT_PACKAGE, namespace + ".*" );
+                addLocalPackages( builder, currentProject );
             }
         }
 
@@ -915,6 +912,52 @@
     }
 
 
+    private static void addLocalPackages( Analyzer analyzer, MavenProject project )
+    {
+        DirectoryScanner scanner = new DirectoryScanner();
+        scanner.setBasedir( project.getBuild().getSourceDirectory() );
+        scanner.setIncludes( new String[]
+            { "**/*.java" } );
+
+        scanner.addDefaultExcludes();
+        scanner.scan();
+
+        Collection packages = new HashSet();
+
+        String[] paths = scanner.getIncludedFiles();
+        for ( int i = 0; i < paths.length; i++ )
+        {
+            packages.add( getPackageName( paths[i] ) );
+        }
+
+        StringBuffer exportPackage = new StringBuffer();
+        StringBuffer privatePackage = new StringBuffer();
+
+        for ( Iterator i = packages.iterator(); i.hasNext(); )
+        {
+            String pkg = ( String ) i.next();
+            if ( ".".equals( pkg ) || pkg.contains( ".internal" ) || pkg.contains( ".impl" ) )
+            {
+                privatePackage.append( pkg ).append( ',' );
+            }
+            else
+            {
+                exportPackage.append( pkg ).append( ',' );
+            }
+        }
+
+        analyzer.setProperty( Analyzer.EXPORT_PACKAGE, exportPackage.toString() );
+        analyzer.setProperty( Analyzer.PRIVATE_PACKAGE, privatePackage.toString() );
+    }
+
+
+    public static String getPackageName( String filename )
+    {
+        int n = filename.lastIndexOf( File.separatorChar );
+        return n < 0 ? "." : filename.substring( 0, n ).replace( File.separatorChar, '.' );
+    }
+
+
     private static String getMavenResourcePaths( MavenProject project )
     {
         final String basePath = project.getBasedir().getAbsolutePath();