Incorporated changes from Niclas Hedhman as well as fixed a bug that was
including "." on the class path twice. (FELIX-73)


git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@409396 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiJarMojo.java b/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiJarMojo.java
index 354b6d8..8213898 100644
--- a/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiJarMojo.java
+++ b/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiJarMojo.java
@@ -1,5 +1,5 @@
 /*
- *   Copyright 2005 The Apache Software Foundation
+ *   Copyright 2006 The Apache Software Foundation
  *
  *   Licensed under the Apache License, Version 2.0 (the "License");
  *   you may not use this file except in compliance with the License.
@@ -39,6 +39,8 @@
  * @description build an OSGi bundle jar
  */
 public class OsgiJarMojo extends AbstractMojo {
+    public static final String OSGI_REFERENCES = "osgi.references";
+
 	private static final String[]		EMPTY_STRING_ARRAY		= {};
 
 	int									bundleManifestVersion	= 1;
@@ -191,7 +193,16 @@
 			analyzeJar(jar, contained, referred, uses);
 		}
 
-		referred.removeAll(contained);
+        referred.removeAll(contained);
+
+        // Check if the osgi.references property is set and display result
+        // if necessary.
+		checkReferencesProperty(uses);
+
+        // Dump referenced package information if debug logging is enabled.
+		if (getLog().isDebugEnabled()) {
+		    printReferencedPackages(uses);
+		}
 
 		Map exports = parseHeader(osgiManifest.getExportPackage());
 		Map imports = parseHeader(osgiManifest.getImportPackage());
@@ -208,6 +219,10 @@
 			createExportHeader(exports, uses);
 		}
 
+        // Remove any ignored packages from the referred set.
+        Set ignorePackage = parseIgnorePackage();
+        referred.removeAll(ignorePackage);
+
 		// If the POM file contains an import declaration,
 		// we verify its validity. Otherwise, we generate the
 		// import package header from the referred. Exports
@@ -246,11 +261,11 @@
 
 	private void createBundleClasspathHeader(List bundleClassPath) {
 		StringBuffer sb = new StringBuffer();
-		String del = ".,";
-		for (Iterator i = bundleClassPath.iterator(); i.hasNext();) {
-			sb.append(del);
+		for (Iterator i = bundleClassPath.iterator(); i.hasNext(); ) {
 			sb.append(i.next());
-			del = ",";
+            if (i.hasNext()) {
+                sb.append(",");
+            }
 		}
 		if (sb.length() > 0)
 			archiveConfig.addManifestEntry("Bundle-Classpath", sb.toString());
@@ -263,7 +278,6 @@
 	 * 
 	 * @param contained Set of contained packages
 	 * @param exports Map with the export clauses from the POM
-	 * @param uses Map with use clauses
 	 * @throws MojoExecutionException
 	 */
 	void verifyDeclaredExports(Map exports, Set contained)
@@ -335,7 +349,17 @@
 		test.removeAll(declaredImports);
 		for (Iterator m = test.iterator(); m.hasNext();) {
 			Object o = m.next();
-			getLog().warn("[OSGi] MISSING IMPORT: " + o);
+			// For backwards compatibility with existing builds, only
+            // issue a warning for missing imports. Other the other hand,
+            // if packages are being ignored, then this is a new build
+            // so be more strict and throw an error.
+            if (osgiManifest.getIgnorePackage() == null) {
+                getLog().warn("[OSGi] MISSING IMPORT: " + o);
+            }
+            else {
+                getLog().error("[OSGi] MISSING IMPORT: " + o);
+                throw new MojoExecutionException("Missing Import " + o);
+            }
 		}
 
 		test = new HashSet(declaredImports);
@@ -430,7 +454,6 @@
 	 * changed TODO
 	 * 
 	 * @param mainJar
-	 * @param sb
 	 * @return
 	 */
 	List getBundleClassPath(Jar mainJar) {
@@ -484,7 +507,6 @@
 	 * @param exports map { name => Map { attribute|directive => value } }
 	 * @return the clauses
 	 */
-
 	String printClauses(Map exports) {
 		StringBuffer sb = new StringBuffer();
 		String del = "";
@@ -559,7 +581,7 @@
 		if (osgiManifest != null && osgiManifest.getEntries().size() > 0) {
 			Map entries = osgiManifest.getEntries();
 
-			getLog().info(
+			getLog().debug(
 					"Bundle manifest will be modified with the following entries: "
 							+ entries.toString());
 			archiveConfig.addManifestEntries(entries);
@@ -642,4 +664,73 @@
 
 		return (String[]) excludeList.toArray(EMPTY_STRING_ARRAY);
 	}
-}
+
+    private Set parseIgnorePackage() {
+        HashSet result = new HashSet();
+        String pkgs = osgiManifest.getIgnorePackage();
+        if (pkgs != null) {
+            StringTokenizer st = new StringTokenizer(pkgs, ",", false);
+            while (st.hasMoreTokens()) {
+                result.add(st.nextToken().trim());
+            }
+        }
+        return result;
+    }
+
+    private void checkReferencesProperty(Map uses) {
+        String interestedIn = System.getProperty(OSGI_REFERENCES);
+        if (interestedIn == null) {
+            return;
+        }
+        StringBuffer buf = new StringBuffer();
+        Iterator it1 = uses.entrySet().iterator();
+        while (it1.hasNext()) {
+            Map.Entry entry = (Map.Entry) it1.next();
+            String pack = (String) entry.getKey();
+            Set references = (Set) entry.getValue();
+            Iterator it2 = references.iterator();
+            while (it2.hasNext()) {
+                String packReferred = (String) it2.next();
+                if (interestedIn.equals(packReferred)) {
+                    buf.append("          |-- ");
+                    buf.append(pack);
+                    buf.append('\n');
+                    break;
+                }
+            }
+        }
+        if (buf.length() == 0) {
+            getLog().info("Noone uses " + interestedIn);
+        }
+        else {
+            int changePos = buf.lastIndexOf("|");
+            buf.setCharAt(changePos, '+');
+            getLog().info(interestedIn + " used by;\n" + buf);
+        }
+    }
+
+    private void printReferencedPackages(Map uses) {
+        StringBuffer buf = new StringBuffer();
+        Iterator it1 = uses.entrySet().iterator();
+        while (it1.hasNext()) {
+            Map.Entry entry = (Map.Entry) it1.next();
+            String pack = (String) entry.getKey();
+            buf.append(pack);
+            buf.append('\n');
+            Set references = (Set) entry.getValue();
+            Iterator it2 = references.iterator();
+            while (it2.hasNext()) {
+                String packReferred = (String) it2.next();
+                buf.append("          |-- ");
+                buf.append(packReferred);
+                buf.append('\n');
+            }
+            int changePos = buf.lastIndexOf("|");
+            if (changePos >= 0) {
+                buf.setCharAt(changePos, '+');
+            }
+            buf.append('\n');
+        }
+        getLog().debug("Referenced package list: \n" + buf.toString());
+    }
+}
\ No newline at end of file
diff --git a/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiManifest.java b/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiManifest.java
index 277c590..41205bd 100644
--- a/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiManifest.java
+++ b/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiManifest.java
@@ -1,5 +1,5 @@
 /*
- *   Copyright 2005 The Apache Software Foundation
+ *   Copyright 2006 The Apache Software Foundation
  *
  *   Licensed under the Apache License, Version 2.0 (the "License");
  *   you may not use this file except in compliance with the License.
@@ -53,7 +53,6 @@
     private static final String REQUIRE_BUNDLE = "Require-Bundle";
     private static final String FRAGMENT_HOST = "Fragment-Host";
     private static final String BUNDLE_MANIFESTVERSION = "Bundle-ManifestVersion";
-
     private static final String BUNDLE_URL = "Bundle-URL";
     private static final String BUNDLE_SOURCE = "Bundle-Source";
     private static final String BUNDLE_DATE = "Bundle-Date";
@@ -95,151 +94,151 @@
     private String bundleDate;
     private String metadataLocation;
     private String serviceComponent;
+    private String ignorePackage;
 
     private Properties entries = new Properties();
 
     public Properties getEntries()
     {
-        if ( getBundleCategory() != null )
+        if (getBundleCategory() != null)
         {
-            entries.put( BUNDLE_CATEGORY, getBundleCategory() );
+            entries.put(BUNDLE_CATEGORY, getBundleCategory());
         }
 
         /*
-         if (getBundleClassPath() != null)
-         {
-         entries.put(BUNDLE_CLASSPATH, getBundleClassPath());
-         }
+         * if (getBundleClassPath() != null) { entries.put(BUNDLE_CLASSPATH,
+         * getBundleClassPath()); }
          */
 
-        if ( getBundleCopyright() != null )
+        if (getBundleCopyright() != null)
         {
-            entries.put( BUNDLE_COPYRIGHT, getBundleCopyright() );
+            entries.put(BUNDLE_COPYRIGHT, getBundleCopyright());
         }
 
-        if ( getBundleDescription() != null )
+        if (getBundleDescription() != null)
         {
-            entries.put( BUNDLE_DESCRIPTION, getBundleDescription() );
+            entries.put(BUNDLE_DESCRIPTION, getBundleDescription());
         }
 
-        if ( getBundleName() != null )
+        if (getBundleName() != null)
         {
-            entries.put( BUNDLE_NAME, getBundleName() );
+            entries.put(BUNDLE_NAME, getBundleName());
         }
 
-        if ( getBundleNativeCode() != null )
+        if (getBundleNativeCode() != null)
         {
-            entries.put( BUNDLE_NATIVECODE, getBundleNativeCode() );
+            entries.put(BUNDLE_NATIVECODE, getBundleNativeCode());
         }
 
-        if ( getExportPackage() != null )
+        if (getExportPackage() != null)
         {
-            entries.put( EXPORT_PACKAGE, getExportPackage() );
+            entries.put(EXPORT_PACKAGE, getExportPackage());
         }
 
-        if ( getExportService() != null )
+        if (getExportService() != null)
         {
-            entries.put( EXPORT_SERVICE, getExportService() );
+            entries.put(EXPORT_SERVICE, getExportService());
         }
 
-        if ( getImportPackage() != null )
+        if (getImportPackage() != null)
         {
-            entries.put( IMPORT_PACKAGE, getImportPackage() );
+            entries.put(IMPORT_PACKAGE, getImportPackage());
         }
 
-        if ( getDynamicImportPackage() != null )
+        if (getDynamicImportPackage() != null)
         {
-            entries.put( DYNAMICIMPORT_PACKAGE, getDynamicImportPackage() );
+            entries.put(DYNAMICIMPORT_PACKAGE, getDynamicImportPackage());
         }
 
-        if ( getImportService() != null )
+        if (getImportService() != null)
         {
-            entries.put( IMPORT_SERVICE, getImportService() );
+            entries.put(IMPORT_SERVICE, getImportService());
         }
 
-        if ( getBundleVendor() != null )
+        if (getBundleVendor() != null)
         {
-            entries.put( BUNDLE_VENDOR, getBundleVendor() );
+            entries.put(BUNDLE_VENDOR, getBundleVendor());
         }
 
-        if ( getBundleVersion() != null )
+        if (getBundleVersion() != null)
         {
-            entries.put( BUNDLE_VERSION, getBundleVersion() );
+            entries.put(BUNDLE_VERSION, getBundleVersion());
         }
 
-        if ( getBundleDocUrl() != null )
+        if (getBundleDocUrl() != null)
         {
-            entries.put( BUNDLE_DOCURL, getBundleDocUrl() );
+            entries.put(BUNDLE_DOCURL, getBundleDocUrl());
         }
 
-        if ( getBundleContactAddress() != null )
+        if (getBundleContactAddress() != null)
         {
-            entries.put( BUNDLE_CONTACTADDRESS, getBundleContactAddress() );
+            entries.put(BUNDLE_CONTACTADDRESS, getBundleContactAddress());
         }
 
-        if ( getBundleActivator() != null )
+        if (getBundleActivator() != null)
         {
-            entries.put( BUNDLE_ACTIVATOR, getBundleActivator() );
+            entries.put(BUNDLE_ACTIVATOR, getBundleActivator());
         }
 
-        if ( getBundleUpdateLocation() != null )
+        if (getBundleUpdateLocation() != null)
         {
-            entries.put( BUNDLE_UPDATELOCATION, getBundleUpdateLocation() );
+            entries.put(BUNDLE_UPDATELOCATION, getBundleUpdateLocation());
         }
 
-        if ( getBundleRequiredExecutionEnvironment() != null )
+        if (getBundleRequiredExecutionEnvironment() != null)
         {
-            entries.put( BUNDLE_REQUIREDEXECUTIONENVIRONMENT, getBundleRequiredExecutionEnvironment() );
+            entries.put(BUNDLE_REQUIREDEXECUTIONENVIRONMENT,
+                getBundleRequiredExecutionEnvironment());
         }
 
-        if ( getBundleSymbolicName() != null )
+        if (getBundleSymbolicName() != null)
         {
-            entries.put( BUNDLE_SYMBOLICNAME, getBundleSymbolicName() );
+            entries.put(BUNDLE_SYMBOLICNAME, getBundleSymbolicName());
         }
 
-        if ( getBundleLocalization() != null )
+        if (getBundleLocalization() != null)
         {
-            entries.put( BUNDLE_LOCALIZATION, getBundleLocalization() );
+            entries.put(BUNDLE_LOCALIZATION, getBundleLocalization());
         }
 
-        if ( getRequireBundle() != null )
+        if (getRequireBundle() != null)
         {
-            entries.put( REQUIRE_BUNDLE, getRequireBundle() );
+            entries.put(REQUIRE_BUNDLE, getRequireBundle());
         }
 
-        if ( getFragmentHost() != null )
+        if (getFragmentHost() != null)
         {
-            entries.put( FRAGMENT_HOST, getFragmentHost() );
+            entries.put(FRAGMENT_HOST, getFragmentHost());
         }
 
-        if ( getBundleManifestVersion() != null )
+        if (getBundleManifestVersion() != null)
         {
-            entries.put( BUNDLE_MANIFESTVERSION, getBundleManifestVersion() );
+            entries.put(BUNDLE_MANIFESTVERSION, getBundleManifestVersion());
         }
 
-        if ( getBundleUrl() != null )
+        if (getBundleUrl() != null)
         {
-            entries.put( BUNDLE_URL, getBundleUrl() );
+            entries.put(BUNDLE_URL, getBundleUrl());
         }
 
-        if ( getBundleSource() != null )
+        if (getBundleSource() != null)
         {
-            entries.put( BUNDLE_SOURCE, getBundleSource() );
+            entries.put(BUNDLE_SOURCE, getBundleSource());
         }
 
-        if ( getBundleDate() != null )
+        if (getBundleDate() != null)
         {
-            entries.put( BUNDLE_DATE, getBundleDate() );
+            entries.put(BUNDLE_DATE, getBundleDate());
         }
 
-        if ( getMetadataLocation() != null )
+        if (getMetadataLocation() != null)
         {
-            entries.put( METADATA_LOCATION, getMetadataLocation() );
+            entries.put(METADATA_LOCATION, getMetadataLocation());
         }
 
-        if ( getServiceComponent() != null )
+        if (getServiceComponent() != null)
         {
-            entries.put( SERVICE_COMPONENT, getServiceComponent() );
+            entries.put(SERVICE_COMPONENT, getServiceComponent());
         }
 
         return entries;
@@ -250,21 +249,16 @@
         return bundleCategory;
     }
 
-    public void setBundleCategory( String bundleCategory )
+    public void setBundleCategory(String bundleCategory)
     {
         this.bundleCategory = bundleCategory;
     }
 
     /*
-     public String getBundleClasspath()
-     {
-     return bundleClasspath;
-     }
-
-     public void setBundleClasspath(String bundleClasspath)
-     {
-     this.bundleClasspath = bundleClasspath;
-     }
+     * public String getBundleClasspath() { return bundleClasspath; }
+     * 
+     * public void setBundleClasspath(String bundleClasspath) {
+     * this.bundleClasspath = bundleClasspath; }
      */
 
     public String getBundleCopyright()
@@ -272,7 +266,7 @@
         return bundleCopyright;
     }
 
-    public void setBundleCopyright( String bundleCopyright )
+    public void setBundleCopyright(String bundleCopyright)
     {
         this.bundleCopyright = bundleCopyright;
     }
@@ -282,7 +276,7 @@
         return bundleDescription;
     }
 
-    public void setBundleDescription( String bundleDescription )
+    public void setBundleDescription(String bundleDescription)
     {
         this.bundleDescription = bundleDescription;
     }
@@ -292,7 +286,7 @@
         return bundleName;
     }
 
-    public void setBundleName( String bundleName )
+    public void setBundleName(String bundleName)
     {
         this.bundleName = bundleName;
     }
@@ -302,7 +296,7 @@
         return bundleNativeCode;
     }
 
-    public void setBundleNativeCode( String bundleNativeCode )
+    public void setBundleNativeCode(String bundleNativeCode)
     {
         this.bundleNativeCode = bundleNativeCode;
     }
@@ -312,9 +306,9 @@
         return exportPackage;
     }
 
-    public void setExportPackage( String exportPackage )
+    public void setExportPackage(String exportPackage)
     {
-        this.exportPackage = exportPackage;
+        this.exportPackage = trim(exportPackage);
     }
 
     public String getExportService()
@@ -322,9 +316,9 @@
         return exportService;
     }
 
-    public void setExportService( String exportService )
+    public void setExportService(String exportService)
     {
-        this.exportService = exportService;
+        this.exportService = trim(exportService);
     }
 
     public String getImportPackage()
@@ -332,9 +326,9 @@
         return importPackage;
     }
 
-    public void setImportPackage( String importPackage )
+    public void setImportPackage(String importPackage)
     {
-        this.importPackage = importPackage;
+        this.importPackage = trim(importPackage);
     }
 
     public String getDynamicImportPackage()
@@ -342,9 +336,9 @@
         return dynamicImportPackage;
     }
 
-    public void setDynamicImportPackage( String dynamicImportPackage )
+    public void setDynamicImportPackage(String dynamicImportPackage)
     {
-        this.dynamicImportPackage = dynamicImportPackage;
+        this.dynamicImportPackage = trim(dynamicImportPackage);
     }
 
     public String getImportService()
@@ -352,7 +346,7 @@
         return importService;
     }
 
-    public void setImportService( String importService )
+    public void setImportService(String importService)
     {
         this.importService = importService;
     }
@@ -362,7 +356,7 @@
         return bundleVendor;
     }
 
-    public void setBundleVendor( String bundleVendor )
+    public void setBundleVendor(String bundleVendor)
     {
         this.bundleVendor = bundleVendor;
     }
@@ -372,7 +366,7 @@
         return bundleVersion;
     }
 
-    public void setBundleVersion( String bundleVersion )
+    public void setBundleVersion(String bundleVersion)
     {
         this.bundleVersion = bundleVersion;
     }
@@ -382,7 +376,7 @@
         return bundleDocUrl;
     }
 
-    public void setBundleDocUrl( String bundleDocUrl )
+    public void setBundleDocUrl(String bundleDocUrl)
     {
         this.bundleDocUrl = bundleDocUrl;
     }
@@ -392,7 +386,7 @@
         return bundleContactAddress;
     }
 
-    public void setBundleContactAddress( String bundleContactAddress )
+    public void setBundleContactAddress(String bundleContactAddress)
     {
         this.bundleContactAddress = bundleContactAddress;
     }
@@ -402,9 +396,9 @@
         return bundleActivator;
     }
 
-    public void setBundleActivator( String bundleActivator )
+    public void setBundleActivator(String bundleActivator)
     {
-        this.bundleActivator = bundleActivator;
+        this.bundleActivator = trim(bundleActivator);
     }
 
     public String getBundleUpdateLocation()
@@ -412,7 +406,7 @@
         return bundleUpdateLocation;
     }
 
-    public void setBundleUpdateLocation( String bundleUpdateLocation )
+    public void setBundleUpdateLocation(String bundleUpdateLocation)
     {
         this.bundleUpdateLocation = bundleUpdateLocation;
     }
@@ -422,7 +416,8 @@
         return bundleRequiredExecutionEnvironment;
     }
 
-    public void setBundleRequiredExecutionEnvironment( String bundleRequiredExecutionEnvironment )
+    public void setBundleRequiredExecutionEnvironment(
+        String bundleRequiredExecutionEnvironment)
     {
         this.bundleRequiredExecutionEnvironment = bundleRequiredExecutionEnvironment;
     }
@@ -432,9 +427,9 @@
         return bundleSymbolicName;
     }
 
-    public void setBundleSymbolicName( String bundleSymbolicName )
+    public void setBundleSymbolicName(String bundleSymbolicName)
     {
-        this.bundleSymbolicName = bundleSymbolicName;
+        this.bundleSymbolicName = trim(bundleSymbolicName);
     }
 
     public String getBundleLocalization()
@@ -442,7 +437,7 @@
         return bundleLocalization;
     }
 
-    public void setBundleLocalization( String bundleLocalization )
+    public void setBundleLocalization(String bundleLocalization)
     {
         this.bundleLocalization = bundleLocalization;
     }
@@ -452,9 +447,9 @@
         return requireBundle;
     }
 
-    public void setRequireBundle( String requireBundle )
+    public void setRequireBundle(String requireBundle)
     {
-        this.requireBundle = requireBundle;
+        this.requireBundle = trim(requireBundle);
     }
 
     public String getFragmentHost()
@@ -462,9 +457,9 @@
         return fragmentHost;
     }
 
-    public void setFragmentHost( String fragmentHost )
+    public void setFragmentHost(String fragmentHost)
     {
-        this.fragmentHost = fragmentHost;
+        this.fragmentHost = trim(fragmentHost);
     }
 
     public String getBundleManifestVersion()
@@ -472,7 +467,7 @@
         return bundleManifestVersion;
     }
 
-    public void setBundleManifestVersion( String bundleManifestVersion )
+    public void setBundleManifestVersion(String bundleManifestVersion)
     {
         this.bundleManifestVersion = bundleManifestVersion;
     }
@@ -482,7 +477,7 @@
         return bundleUrl;
     }
 
-    public void setBundleUrl( String bundleUrl )
+    public void setBundleUrl(String bundleUrl)
     {
         this.bundleUrl = bundleUrl;
     }
@@ -492,7 +487,7 @@
         return bundleSource;
     }
 
-    public void setBundleSource( String bundleSource )
+    public void setBundleSource(String bundleSource)
     {
         this.bundleSource = bundleSource;
     }
@@ -502,7 +497,7 @@
         return bundleDate;
     }
 
-    public void setBundleDate( String bundleDate )
+    public void setBundleDate(String bundleDate)
     {
         this.bundleDate = bundleDate;
     }
@@ -512,7 +507,7 @@
         return metadataLocation;
     }
 
-    public void setMetadataLocation( String metadataLocation )
+    public void setMetadataLocation(String metadataLocation)
     {
         this.metadataLocation = metadataLocation;
     }
@@ -522,8 +517,38 @@
         return serviceComponent;
     }
 
-    public void setServiceComponent( String serviceComponent )
+    public void setServiceComponent(String serviceComponent)
     {
         this.serviceComponent = serviceComponent;
     }
-}
+
+    public String getIgnorePackage()
+    {
+        return ignorePackage;
+    }
+
+    public void setIgnorePackage(String ignorePackage)
+    {
+        this.ignorePackage = ignorePackage;
+    }
+
+    /**
+     * Removes all whitespace in the entry.
+     *
+     * @param entry The entry to be cleaned up.
+     * @return A copy of the entry string without any whitespace.
+     */
+    private String trim(String entry)
+    {
+        StringBuffer buf = new StringBuffer(entry.length());
+        for (int i = 0; i < entry.length(); i++)
+        {
+            char ch = entry.charAt(i);
+            if (ch > 32)
+            {
+                buf.append(ch);
+            }
+        }
+        return buf.toString();
+    }
+}
\ No newline at end of file