Added code to check for duplicate imports/exports. This was mainly to aid
debugging for bundle developers, but still will require some work for
specification compliance because exports can be duplicated in some situations.


git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@379144 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Felix.java b/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Felix.java
index 49f189b..e6e4d0b 100644
--- a/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Felix.java
+++ b/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Felix.java
@@ -2936,14 +2936,30 @@
             throw new BundleException("Unknown 'Bundle-ManifestVersion' value: " + version);
         }
 
+        // Create map to check for duplicate imports/exports.
+        Map dupeMap = new HashMap();
+
         // Get import packages from bundle manifest.
         R4Package[] pkgs = R4Package.parseImportOrExportHeader(
             (String) headerMap.get(Constants.IMPORT_PACKAGE));
-        R4Import[] imports = new R4Import[pkgs.length];
+
+        // Create non-duplicated import array.
+        dupeMap.clear();
         for (int i = 0; i < pkgs.length; i++)
         {
-            imports[i] = new R4Import(pkgs[i]);
+            if (dupeMap.get(pkgs[i].getName()) == null)
+            {
+                dupeMap.put(pkgs[i].getName(), new R4Import(pkgs[i]));
+            }
+            else
+            {
+                // TODO: FRAMEWORK - Determine if we should error here.
+                m_logger.log(Logger.LOG_WARNING,
+                    "Duplicate import - " + pkgs[i].getName());
+            }
         }
+        R4Import[] imports =
+            (R4Import[]) dupeMap.values().toArray(new R4Import[dupeMap.size()]);
 
         // Check to make sure that R3 bundles have only specified
         // the 'specification-version' attribute and no directives.
@@ -2973,11 +2989,25 @@
         // Get export packages from bundle manifest.
         pkgs = R4Package.parseImportOrExportHeader(
             (String) headerMap.get(Constants.EXPORT_PACKAGE));
-        R4Export[] exports = new R4Export[pkgs.length];
+
+        // Create non-duplicated export array.
+        dupeMap.clear();
         for (int i = 0; i < pkgs.length; i++)
         {
-            exports[i] = new R4Export(pkgs[i]);
+            if (dupeMap.get(pkgs[i].getName()) == null)
+            {
+                dupeMap.put(pkgs[i].getName(), new R4Export(pkgs[i]));
+            }
+            else
+            {
+                // TODO: FRAMEWORK - Exports can be duplicated, so fix this.
+                m_logger.log(Logger.LOG_WARNING,
+                    "Duplicate export - " + pkgs[i].getName());
+            }
         }
+        R4Export[] exports =
+            (R4Export[]) dupeMap.values().toArray(new R4Export[dupeMap.size()]);
+
 
         // Check to make sure that R3 bundles have only specified
         // the 'specification-version' attribute and no directives.
@@ -3038,15 +3068,27 @@
             }
         }
 
-// TODO: CHECK FOR DUPLICATE IMPORTS/EXPORTS HERE.
         // Get dynamic import packages from bundle manifest.
         pkgs = R4Package.parseImportOrExportHeader(
             (String) headerMap.get(Constants.DYNAMICIMPORT_PACKAGE));
-        R4Import[] dynamics = new R4Import[pkgs.length];
+
+        // Create non-duplicated dynamic import array.
+        dupeMap.clear();
         for (int i = 0; i < pkgs.length; i++)
         {
-            dynamics[i] = new R4Import(pkgs[i]);
+            if (dupeMap.get(pkgs[i].getName()) == null)
+            {
+                dupeMap.put(pkgs[i].getName(), new R4Import(pkgs[i]));
+            }
+            else
+            {
+                // TODO: FRAMEWORK - Determine if we should error here.
+                m_logger.log(Logger.LOG_WARNING,
+                    "Duplicate import - " + pkgs[i].getName());
+            }
         }
+        R4Import[] dynamics =
+            (R4Import[]) dupeMap.values().toArray(new R4Import[dupeMap.size()]);
 
         // Check to make sure that R3 bundles have no attributes or
         // directives.