felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject svn commit: r1792946 - in /felix/trunk/framework/src: main/java/org/apache/felix/framework/ main/java/org/apache/felix/framework/util/manifestparser/ test/java/org/apache/felix/framework/util/manifestparser/
Date Thu, 27 Apr 2017 19:20:15 GMT
Author: pauls
Date: Thu Apr 27 19:20:15 2017
New Revision: 1792946

URL: http://svn.apache.org/viewvc?rev=1792946&view=rev
Log:
Improve native OS version sanitation (FELIX-4696).

Modified:
    felix/trunk/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClause.java
    felix/trunk/framework/src/test/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClauseTest.java

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java?rev=1792946&r1=1792945&r2=1792946&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java Thu
Apr 27 19:20:15 2017
@@ -285,8 +285,7 @@ class ExtensionManager extends URLStream
 
         if( osVersion != null)
         {
-            osVersion = NativeLibraryClause.formatOSVersion(osVersion);
-            attributes.put(NativeNamespace.CAPABILITY_OSVERSION_ATTRIBUTE, Version.parseVersion(osVersion));
+            attributes.put(NativeNamespace.CAPABILITY_OSVERSION_ATTRIBUTE, new Version(NativeLibraryClause.normalizeOSVersion(osVersion)));
         }
 
         if( userLang != null)

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClause.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClause.java?rev=1792946&r1=1792945&r2=1792946&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClause.java
(original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClause.java
Thu Apr 27 19:20:15 2017
@@ -313,7 +313,7 @@ public class NativeLibraryClause
     private boolean checkOSVersions(String osVersion, String[] osversions) 
         throws BundleException
     {
-        Version currentOSVersion = Version.parseVersion(formatOSVersion(osVersion));
+        Version currentOSVersion = Version.parseVersion(normalizeOSVersion(osVersion));
         for (int i = 0; (osversions != null) && (i < osversions.length); i++)
         {
             try
@@ -455,7 +455,7 @@ public class NativeLibraryClause
                     }
                     else if (property.equals(Constants.BUNDLE_NATIVECODE_OSVERSION))
                     {
-                        osVersionList.add(normalizeOSVersion(value));
+                        osVersionList.add(normalizeOSVersionRange(value));
                     }
                     else if (property.equals(Constants.BUNDLE_NATIVECODE_PROCESSOR))
                     {
@@ -497,26 +497,6 @@ public class NativeLibraryClause
         }
     }
 
-    public static String formatOSVersion(String value)
-    {
-        // Header: 'Bundle-NativeCode', Parameter: 'osversion'
-        // Standardized 'osversion': major.minor.micro, only digits
-        try
-        {
-            Pattern versionPattern = Pattern.compile("\\d+\\.?\\d*\\.?\\d*");
-            Matcher matcher = versionPattern.matcher(value);
-            if (matcher.find())
-            {
-                value = matcher.group();
-            }
-            return Version.parseVersion(value).toString();
-        }
-        catch (Exception ex)
-        {
-            return Version.emptyVersion.toString();
-        }
-    }
-
     public static List<String> getOsNameWithAliases(String osName)
     {
         //Can't assume this has been normalized
@@ -691,6 +671,10 @@ public class NativeLibraryClause
         {
             return OS_VXWORKS;
         }
+        else if (value.startsWith(OS_EPOC))
+        {
+            return OS_EPOC;
+        }
         return value;
     }
 
@@ -755,20 +739,109 @@ public class NativeLibraryClause
         {
             return PROC_SPARC;
         }
+
         return value;
     }
 
+    public static String normalizeOSVersionRange(String value)
+    {
+        if (value.indexOf(',') >= 0)
+        {
+            try
+            {
+                String s = value.substring(1, value.length() - 1);
+                String vlo = s.substring(0, s.indexOf(',')).trim();
+                String vhi = s.substring(s.indexOf(',') + 1, s.length()).trim();
+                return new VersionRange(new Version(cleanupVersion(vlo)), (value.charAt(0)
== '['), new Version(
+                    cleanupVersion(vhi)), (value.charAt(value.length() - 1) == ']')).toString();
+            }
+
+            catch (Exception ex)
+            {
+                return Version.emptyVersion.toString();
+            }
+        }
+
+        return normalizeOSVersion(value);
+    }
+    
     public static String normalizeOSVersion(String value)
     {
-        // Header: 'Bundle-NativeCode', Parameter: 'osversion'
-        // Standardized 'osversion': major.minor.micro, only digits
-        try
+        return new Version(cleanupVersion(value)).toString();
+    }
+    
+    private static final Pattern FUZZY_VERSION = Pattern.compile( "(\\d+)(\\.(\\d+)(\\.(\\d+))?)?([^a-zA-Z0-9](.*))?",
+        Pattern.DOTALL );
+
+    private static String cleanupVersion( String version )
+    {
+        StringBuffer result = new StringBuffer();
+        Matcher m = FUZZY_VERSION.matcher( version );
+        if ( m.matches() )
+        {
+            String major = m.group( 1 );
+            String minor = m.group( 3 );
+            String micro = m.group( 5 );
+            String qualifier = m.group( 7 );
+
+            if ( major != null )
+            {
+                result.append( major );
+                if ( minor != null )
+                {
+                    result.append( "." );
+                    result.append( minor );
+                    if ( micro != null )
+                    {
+                        result.append( "." );
+                        result.append( micro );
+                        if ( qualifier != null )
+                        {
+                            result.append( "." );
+                            cleanupModifier( result, qualifier );
+                        }
+                    }
+                    else if ( qualifier != null )
+                    {
+                        result.append( ".0." );
+                        cleanupModifier( result, qualifier );
+                    }
+                    else
+                    {
+                        result.append( ".0" );
+                    }
+                }
+                else if ( qualifier != null )
+                {
+                    result.append( ".0.0." );
+                    cleanupModifier( result, qualifier );
+                }
+                else
+                {
+                    result.append( ".0.0" );
+                }
+            }
+        }
+        else
         {
-            return VersionRange.parse(value).toString();
+            result.append( "0.0.0." );
+            cleanupModifier( result, version );
         }
-        catch (Exception ex)
+        return result.toString();
+    }
+
+
+    private static void cleanupModifier( StringBuffer result, String modifier )
+    {
+        for ( int i = 0; i < modifier.length(); i++ )
         {
-            return Version.emptyVersion.toString();
+            char c = modifier.charAt( i );
+            if ( ( c >= '0' && c <= '9' ) || ( c >= 'a' && c <=
'z' ) || ( c >= 'A' && c <= 'Z' ) || c == '_'
+                || c == '-' )
+                result.append( c );
+            else
+                result.append( '_' );
         }
     }
+
 }

Modified: felix/trunk/framework/src/test/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClauseTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/test/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClauseTest.java?rev=1792946&r1=1792945&r2=1792946&view=diff
==============================================================================
--- felix/trunk/framework/src/test/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClauseTest.java
(original)
+++ felix/trunk/framework/src/test/java/org/apache/felix/framework/util/manifestparser/NativeLibraryClauseTest.java
Thu Apr 27 19:20:15 2017
@@ -149,4 +149,20 @@ public class NativeLibraryClauseTest ext
         assertTrue(NativeLibraryClause.getOsNameWithAliases("windowsxp").contains("windowsxp"));
         assertTrue(NativeLibraryClause.getOsNameWithAliases("win32").contains("win32"));
     }
+    
+    public void testNormalizeOSVersion() {
+        // valid
+        assertEquals("1.0.0", NativeLibraryClause.normalizeOSVersion("1"));
+        assertEquals("1.2.0", NativeLibraryClause.normalizeOSVersion("1.2"));
+        assertEquals("1.2.3", NativeLibraryClause.normalizeOSVersion("1.2.3"));
+        assertEquals("1.2.3.qualifier", NativeLibraryClause.normalizeOSVersion("1.2.3.qualifier"));
+        
+        // to normalize
+        assertEquals("1.0.0.qualifier", NativeLibraryClause.normalizeOSVersion("1.qualifier"));
+        assertEquals("1.2.0.qualifier", NativeLibraryClause.normalizeOSVersion("1.2.qualifier"));
+        
+        assertEquals("3.13.0.39-generic", NativeLibraryClause.normalizeOSVersion("3.13.0-39-generic"));
+
+        assertEquals("3.14.22.100_fc19_i686_PAE", NativeLibraryClause.normalizeOSVersion("3.14.22-100.fc19.i686.PAE"));
+    }
 }



Mime
View raw message