Fix native library extension matching; previously it was skipping
the last extension. (FELIX-2789)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1060887 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/util/manifestparser/ManifestParser.java b/framework/src/main/java/org/apache/felix/framework/util/manifestparser/ManifestParser.java
index 5601754..f718c0e 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/manifestparser/ManifestParser.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/manifestparser/ManifestParser.java
@@ -1516,7 +1516,7 @@
      * included in the set of delimiter characters.
      * @param value the delimited string to parse.
      * @param delim the characters delimiting the tokens.
-     * @return an array of string tokens or null if there were no tokens.
+     * @return a list of string or an empty list if there are none.
     **/
     public static List<String> parseDelimitedString(String value, String delim)
     {
diff --git a/framework/src/main/java/org/apache/felix/framework/util/manifestparser/R4Library.java b/framework/src/main/java/org/apache/felix/framework/util/manifestparser/R4Library.java
index 214fe9c..8ced497 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/manifestparser/R4Library.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/manifestparser/R4Library.java
@@ -18,6 +18,7 @@
  */
 package org.apache.felix.framework.util.manifestparser;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import org.osgi.framework.Constants;
@@ -84,43 +85,50 @@
     **/
     public boolean match(Map configMap, String name)
     {
+        // First, check for an exact match.
+        boolean matched = false;
+        if (m_libraryFile.equals(name) || m_libraryFile.endsWith("/" + name))
+        {
+            matched = true;
+        }
+
+        // Then check the mapped name.
         String libname = System.mapLibraryName(name);
+        // As well as any additional library file extensions.
         List<String> exts = ManifestParser.parseDelimitedString(
             (String) configMap.get(Constants.FRAMEWORK_LIBRARY_EXTENSIONS), ",");
-        int extIdx = 0;
-
-        // First try to match the default name, then try to match any additionally
-        // specified library extensions.
-        do
+        if (exts == null)
         {
+            exts = new ArrayList<String>();
+        }
+        // For Mac OSX, try dylib too.
+        if (libname.endsWith(".jnilib") && m_libraryFile.endsWith(".dylib"))
+        {
+            exts.add("dylib");
+        }
+        // Loop until we find a match or not.
+        int extIdx = -1;
+        while (!matched && (extIdx < exts.size()))
+        {
+            // Check if the current name matches.
             if (m_libraryFile.equals(libname) || m_libraryFile.endsWith("/" + libname))
             {
-                return true;
+                matched = true;
             }
-            else if (libname.endsWith(".jnilib") && m_libraryFile.endsWith(".dylib"))
-            {
-                libname = libname.substring(0, libname.length() - 6) + "dylib";
-                if (m_libraryFile.equals(libname) || m_libraryFile.endsWith("/" + libname))
-                {
-                    return true;
-                }
-            }
-            else if (m_libraryFile.equals(name) || m_libraryFile.endsWith("/" + name))
-            {
-                return true;
-            }
+
+            // Increment extension index.
+            extIdx++;
 
             // If we have other native library extensions to try, then
             // calculate the new native library name.
-            if ((exts != null) && (extIdx < exts.size()))
+            if (!matched && (extIdx < exts.size()))
             {
                 int idx = libname.lastIndexOf(".");
                 libname = (idx < 0)
-                    ? libname + "." + exts.get(extIdx++)
-                    : libname.substring(0, idx) + "." + exts.get(extIdx++);
+                    ? libname + "." + exts.get(extIdx)
+                    : libname.substring(0, idx + 1) + exts.get(extIdx);
             }
         }
-        while ((exts != null) && (extIdx < exts.size()));
 
         return false;
     }