/**
 * @copyright
 * ====================================================================
 * Copyright (c) 2005 CollabNet.  All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://subversion.tigris.org/license-1.html.
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 *
 * This software consists of voluntary contributions made by many
 * individuals.  For exact contribution history, see the revision
 * history and logs, available at http://subversion.tigris.org/.
 * ====================================================================
 * @endcopyright
 */

package org.tigris.subversion.javahl;

/**
 * Handles activities related to management of native resouces
 * (e.g. loading of native libraries).
 */
class NativeResources
{
    /**
     * Version information about the underlying native libraries.
     */
    static Version version;

    /**
     * Load the required native library whose path is specified by the
     * system property <code>subversion.native.library</code> (which
     * can be passed to the JVM on start-up using an argument like
     * <code>-Dsubversion.native.library=/usr/local/lib/libsvnjavahl-1.so</code>).
     * If the system property is not specified or cannot be loaded,
     * attempt to load the library using its expected name, and the
     * platform-dependent loading mechanism.
     *
     * @throws UnsatisfiedLinkError If the native library cannot be
     * loaded.
     * @throws LinkageError If the version of the loaded native
     * library is not compatible with this version of JavaHL's Java
     * APIs.
     * @since 1.3.0
     */
    public static synchronized void loadNativeLibrary()
    {
        // If the user specified the fully qualified path to the
        // native library, try loading that first.
        try
        {
            String specifiedLibraryName =
                System.getProperty("subversion.native.library");
            if (specifiedLibraryName != null)
            {
                System.load(specifiedLibraryName);
                init();
                return;
            }
        }
        catch (UnsatisfiedLinkError ex)
        {
            // ignore that error to try again
        }

        // Try to load the library by its name.  Failing that, try to
        // load it by its old name.
        try
        {
            System.loadLibrary("svnjavahl-1");
            init();
            return;
        }
        catch (UnsatisfiedLinkError ex)
        {
            try
            {
                System.loadLibrary("libsvnjavahl-1");
                init();
                return;
            }
            catch (UnsatisfiedLinkError e)
            {
                System.loadLibrary("svnjavahl");
                init();
                return;
            }
        }
    }

    /**
     * Initializer for native resources to be invoked <em>after</em>
     * the native library has been loaded.  Sets library version
     * information, and initializes the re-entrance hack for native
     * code.
     * @throws LinkageError If the version of the loaded native
     * library is not compatible with this version of JavaHL's Java
     * APIs.
     */
    private static final void init()
    {
        initNativeLibrary();
        version = new Version();
        if (!version.isAtLeast(1, 5, 0))
        {
            throw new LinkageError("Native library version must be at least " +
                                   "1.5.0, but is only " + version);
        }
    }

    /**
     * Initialize the native library layer.
     * @since 1.5.0
     */
    private static native void initNativeLibrary();
}

@@ -1834,32 +1834,84 @@
 Library()%0A    %7B%0A
+        UnsatisfiedLinkError loadException = null;%0A%0A
         // If th
@@ -2391,38 +2391,163 @@
  // 
-ignore that error to try again
+Since the user explicitly specified this path, this is%0A            // the best error to return if no other method succeeds.%0A            loadException = ex;
 %0A   
@@ -2672,69 +2672,231 @@
     
+S
 tr
-y%0A        %7B%0A            System.loadLibrary(%22svnjavahl-1%22);%0A
+ing%5B%5D libraryNames = %7B%22svnjavahl-1%22, %22libsvnjavahl-1%22, %22svnjavahl%22%7D;%0A        for (int i = 0; i %3C libraryNames.length; i++)%0A        %7B%0A            try%0A            %7B%0A                System.loadLibrary(libraryNames%5Bi%5D);%0A    
     
@@ -2915,32 +2915,36 @@
 ();%0A            
+    
 return;%0A        
@@ -2935,34 +2935,42 @@
 return;%0A        
+  
-%7D%0A
+  %7D%0A    
         catch (U
@@ -2993,32 +2993,36 @@
 ror ex)%0A        
+    
 %7B%0A            tr
@@ -3023,78 +3023,95 @@
     
+ 
-try%0A            %7B%0A                System.loadLibrary(%22libsvnjavahl-1%22)
+   if (loadException == null)%0A                %7B%0A                    loadException = ex
 ;%0A  
@@ -3116,39 +3116,47 @@
                 
-init();
+%7D%0A            %7D
 %0A               
@@ -3144,32 +3144,35 @@
   %7D%0A        
+%7D%0A%0A
         
 return;%0A    
@@ -3163,136 +3163,273 @@
     
-return;%0A            %7D%0A            catch (UnsatisfiedLinkError e)%0A            %7B%0A                System.loadLibrary(%22svnjavahl%22);%0A
+// Re-throw the most relevant exception.%0A        if (loadException == null)%0A        %7B%0A            // This could only happen as the result of a programming error.%0A            loadException = new UnsatisfiedLinkError(%22Unable to load JavaHL %22 +%0A                       
     
@@ -3436,32 +3436,24 @@
             
-init();%0A
             
@@ -3454,37 +3454,43 @@
         
-  return;%0A   
+%22native library%22);%0A
         
  %7D%0A     
@@ -3473,33 +3473,32 @@
 rary%22);%0A        
- 
 %7D%0A        %7D%0A    
@@ -3483,33 +3483,52 @@
       %7D%0A        
-%7D
+throw loadException;
 %0A    %7D%0A%0A    /**%0A

Improve accuracy in error reporting when unable to load the JavaHL's native library.