$OpenBSD: patch-jdk_src_share_classes_java_lang_ClassLoader_java,v 1.7 2012/04/12 22:27:20 matthew Exp $
--- jdk/src/share/classes/java/lang/ClassLoader.java.orig	Thu Nov 17 21:57:34 2011
+++ jdk/src/share/classes/java/lang/ClassLoader.java	Wed Apr 11 09:10:38 2012
@@ -27,6 +27,7 @@ package java.lang;
 import java.io.InputStream;
 import java.io.IOException;
 import java.io.File;
+import java.io.FilenameFilter;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.net.MalformedURLException;
@@ -40,7 +41,9 @@ import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.security.ProtectionDomain;
 import java.security.cert.Certificate;
+import java.util.Arrays;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -1861,18 +1864,61 @@ public abstract class ClassLoader {
     }
 
     private static boolean loadLibrary0(Class fromClass, final File file) {
-        boolean exists = AccessController.doPrivileged(
-            new PrivilegedAction<Object>() {
-                public Object run() {
-                    return file.exists() ? Boolean.TRUE : null;
-                }})
-            != null;
-        if (!exists) {
+        File libfile = AccessController.doPrivileged(
+            new PrivilegedAction<File>() {
+                class LibraryFileFilter implements FilenameFilter {
+                    final String libname;
+
+                    LibraryFileFilter(String libname) {
+                        this.libname = libname;
+                    }
+
+                    public boolean accept(File dir, String name) {
+                        if (name.startsWith(libname)) {
+                            return name.substring(libname.length()).matches("\\.[0-9]{1,20}\\.[0-9]{1,20}$");
+                        }
+                        return false;
+                    }
+                }
+
+                class LibraryFileVersionComparator implements Comparator<String> {
+                    public int compare(String s1, String s2) {
+                        String[] f1 = s1.split("\\."), f2 = s2.split("\\.");
+                        int res = compareComponents(f1[f1.length - 2], f2[f2.length - 2]);
+                        if (res == 0) {
+                            res = compareComponents(f1[f1.length - 1], f2[f2.length - 1]);
+                        }
+                        return res;
+                    }
+
+                    int compareComponents(String s1, String s2) {
+                        return Long.valueOf(s1).compareTo(Long.valueOf(s2));
+                    }
+                }
+
+                public File run() {
+                    if (file.exists())
+                        return file;
+                    // if file is unversioned, check for a versioned one in same dir
+                    if (file.getName().endsWith(".so")) {
+                        File dir = file.getParentFile();
+                        if (dir != null) {
+                            String liblist[] = dir.list(new LibraryFileFilter(file.getName()));
+                            if (liblist != null && liblist.length > 0) {
+                                // return the highest versioned lib
+                                String highest = Collections.max(Arrays.asList(liblist), new LibraryFileVersionComparator());
+                                return new File(dir, highest);
+                            }
+                        }
+                    }
+                    return null;
+                }});
+        if (libfile == null) {
             return false;
         }
         String name;
         try {
-            name = file.getCanonicalPath();
+            name = libfile.getCanonicalPath();
         } catch (IOException e) {
             return false;
         }
