Class NativeLibResolver


  • public final class NativeLibResolver
    extends Object
    This class resolves a native lib. The resoler expects a native lib with a name generated by libtool. The resolver scans first the java.library.path for the lib. If the lib is not found it will be searched in the resources of the Consumers class. The search directoy is /lib/. If its found it will be copied to the temp dir and can be loaded with the class loader that actually needs the lib. This makes sure that OSGi can find, load and use the native lib, because each bundle has its own classloder. So a lib loded from this bundle cant be used in the Consumers bundle. This is needed because System.load(java.lang.String) uses reflection to find the correct classloder which is the one that System.load is called.

    Usage

    Single Library

     
     public final class LibJnhwCommonLoader {
    
         public final static String LIB_JNHW_COMMON = "jnhw-common";
         public final static int LIB_JNHW_COMMON_VERSION = 2;
         private static LoadResult LIB_JNHW_COMMON_LOAD_RESULT;
         private final static Object loadLock = new Object();
         private static LoadState state = LoadState.INIT;
    
         protected static void doSystemLoad(String absoluteLibName) {
             System.load(absoluteLibName);
         }
    
         private LibJnhwCommonLoader() {
         }
    
         public static LoadResult getLoadResult() {
             return LIB_JNHW_COMMON_LOAD_RESULT;
         }
    
         public static LoadState touch() {
             synchronized (loadLock) {
                 if (state != LoadState.INIT) {
                     return state;
                 }
                 state = LoadState.LOADING;
             }
             LIB_JNHW_COMMON_LOAD_RESULT = NativeLibResolver.loadNativeLib(LIB_JNHW_COMMON, LIB_JNHW_COMMON_VERSION, LibJnhwCommonLoader::doSystemLoad);
             synchronized (loadLock) {
                 if (LIB_JNHW_COMMON_LOAD_RESULT.isLoaded()) {
                     state = LoadState.SUCCESS;
                 } else {
                     state = LoadState.FAILURE;
                 }
             }
             return state;
         }
    
     }

    Library with dependency

     
     public final class LibJnhwPosixLoader {
    
         public final static String LIB_JNHW_POSIX = "jnhw-posix";
         private static LoadResult LIB_JNHW_POSIX_LOAD_RESULT;
         public final static int LIB_JNHW_POSIX_VERSION = 2;
         private final static Object loadLock = new Object();
         private static LoadState state = LoadState.INIT;
    
         protected static void doSystemLoad(String absoluteLibName) {
             System.load(absoluteLibName);
         }
    
         public static LoadResult getLoadResult() {
             return LIB_JNHW_POSIX_LOAD_RESULT;
         }
    
         public static LoadState touch() {
             synchronized (loadLock) {
                 if (state != LoadState.INIT) {
                     return state;
                 }
                 state = LoadState.LOADING;
             }
             if (LoadState.SUCCESS == LibJnhwCommonLoader.touch()) {
                 LIB_JNHW_POSIX_LOAD_RESULT = NativeLibResolver.loadNativeLib(LIB_JNHW_POSIX, LIB_JNHW_POSIX_VERSION, LibJnhwPosixLoader::doSystemLoad);
             } else {
                 //Just mark the error a dependant lib was not properly loaded
                 LIB_JNHW_POSIX_LOAD_RESULT = LibJnhwCommonLoader.getLoadResult();
             }
             synchronized (loadLock) {
                 if (LIB_JNHW_POSIX_LOAD_RESULT.isLoaded()) {
                     state = LoadState.SUCCESS;
                 } else {
                     state = LoadState.FAILURE;
                 }
             }
             return state;
         }
    
         private LibJnhwPosixLoader() {
         }
    
     }
    Author:
    aploese
    • Method Detail

      • loadNativeLib

        public static LoadResult loadNativeLib​(String libName,
                                               int libToolInterfaceVersion,
                                               Consumer<String> consumer)
        Only call System.load(absLibName); in the subclass - so OSGi can pick the right classloader...
        Parameters:
        libName - the name of the lib without any pre or posfix. @see OS for the naming templates.
        libToolInterfaceVersion - the libtool version.
        consumer - the consumer which really load the lib.
        Returns:
        the result of this attempt to load the native lib.
      • getOS

        public static OS getOS()
      • getArch

        public static Arch getArch()
      • getSizeOfPointer

        public static SizeInBit getSizeOfPointer()
      • getSizeOfLong

        public static SizeInBit getSizeOfLong()
      • getEndianess

        public static Endianess getEndianess()