package org.libgit2.jagged.core;

import java.io.File;

import org.libgit2.jagged.core.Platform.OperatingSystem;

/**
 * A wrapper around the system library loader to provide improved semantics for
 * installations containing libraries for multiple platforms.
 */
public class NativeLoader
{
    private static String nativeLibraryPath;

    public static void setNativeLibraryPath(String libraryPath)
    {
        NativeLoader.nativeLibraryPath = libraryPath;
    }

    /**
     * Loads the given shared library, looking in the platform-specific library
     * directory relative to the current working directory.
     * 
     * @param libraryName
     *        the name of the library to load (must not be {@code null})
     */
    public static void load(final String libraryName)
    {
        if (nativeLibraryPath == null)
        {
            nativeLibraryPath = System.getProperty("org.libgit2.jagged.nativeLibraryPath");
        }

        /* Provide a mediocre default when the system property is unset. */
        if (nativeLibraryPath == null)
        {
            nativeLibraryPath = "native";
        }

        final File operatingSystemPath =
            new File(nativeLibraryPath, Platform.getCurrentPlatform().getOperatingSystem().getOsgiName());

        final File architecturePath =
            new File(operatingSystemPath, Platform.getCurrentPlatform().getArchitecture().getName());

        /*
         * There's indecision as to whether the file extension of a JNI library
         * on Mac OS should be ".jnilib" (Java 6 and prior) or ".dylib" (Java
         * 7). So we need to hardcode this for compatibility.
         */
        final String mappedLibraryName;

        if (Platform.getCurrentPlatform().getOperatingSystem() == OperatingSystem.MAC_OS_X)
        {
            mappedLibraryName = "lib" + libraryName + ".dylib";
        }
        else
        {
            mappedLibraryName = System.mapLibraryName(libraryName);
        }

        /*
         * Try the operating system path first - some platforms (eg, Mac OS) can
         * support "fat" binaries that are archives of the libraries for the
         * multiple architectures.
         */
        File libraryPath = new File(operatingSystemPath, mappedLibraryName);

        if (!libraryPath.exists())
        {
            libraryPath = new File(architecturePath, mappedLibraryName);
        }

        System.load(libraryPath.getAbsolutePath());
    }
}
@@ -334,100 +334,608 @@
 th;%0A
-%0A    public static void setNativeLibraryPath(String libraryPath)%0A    %7B%0A        NativeLoader.
+    private static boolean nativeLibraryLoaded = false;%0A%0A    /**%0A     * Sets the native library path programmatically.%0A     * This must be called before the first call to %7B@link #load(String)%7D.%0A     * %0A     * @param libraryPath the path to the native library location (must not be %7B@code null%7D)%0A     */%0A    public static void setNativeLibraryPath(String libraryPath)%0A    %7B%0A    %09Ensure.argumentNotNull(libraryPath, %22libraryPath%22);%0A%0A    %09if (nativeLibraryLoaded)%0A    %09%7B%0A    %09%09throw new IllegalStateException(%22Native library path cannot be configured after libraries have been loaded%22);%0A    %09%7D%0A%0A        
 nati
@@ -2950,15 +2950,51 @@
 ath());%0A
+        nativeLibraryLoaded = true;%0A
     %7D%0A%7D

add javadoc, limitations on setter