Class NativeLibraryLoader


  • public final class NativeLibraryLoader
    extends java.lang.Object
    Helper class to load JNI resources.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private NativeLibraryLoader()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.String calculatePackagePrefix()
      The shading prefix added to this class's full name.
      private static byte[] classToByteArray​(java.lang.Class<?> clazz)
      Load the helper Class as a byte array, to be redefined in specified ClassLoader.
      private static void closeQuietly​(java.io.Closeable c)  
      static void load​(java.lang.String originalName, java.lang.ClassLoader loader)
      Load the given library with the specified ClassLoader
      static void loadFirstAvailable​(java.lang.ClassLoader loader, java.lang.String... names)
      Loads the first available library in the collection with the specified ClassLoader.
      private static void loadLibrary​(java.lang.ClassLoader loader, java.lang.String name, boolean absolute)
      Loading the native library into the specified ClassLoader.
      private static void loadLibraryByHelper​(java.lang.Class<?> helper, java.lang.String name, boolean absolute)  
      private static boolean patchShadedLibraryId​(byte[] bytes, java.lang.String originalName, java.lang.String name)
      Try to patch shaded library to ensure it uses a unique ID.
      (package private) static boolean patchShadedLibraryId​(java.io.InputStream in, java.io.OutputStream out, java.lang.String originalName, java.lang.String name)  
      private static boolean shouldShadedLibraryIdBePatched​(java.lang.String packagePrefix)  
      private static java.lang.Class<?> tryToLoadClass​(java.lang.ClassLoader loader, java.lang.Class<?> helper)
      Try to load the helper Class into specified ClassLoader.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • NATIVE_RESOURCE_HOME

        private static final java.lang.String NATIVE_RESOURCE_HOME
        See Also:
        Constant Field Values
      • WORKDIR

        private static final java.io.File WORKDIR
      • DELETE_NATIVE_LIB_AFTER_LOADING

        private static final boolean DELETE_NATIVE_LIB_AFTER_LOADING
      • TRY_TO_PATCH_SHADED_ID

        private static final boolean TRY_TO_PATCH_SHADED_ID
      • UNIQUE_ID_BYTES

        private static final byte[] UNIQUE_ID_BYTES
    • Constructor Detail

      • NativeLibraryLoader

        private NativeLibraryLoader()
    • Method Detail

      • loadFirstAvailable

        public static void loadFirstAvailable​(java.lang.ClassLoader loader,
                                              java.lang.String... names)
        Loads the first available library in the collection with the specified ClassLoader.
        Throws:
        java.lang.IllegalArgumentException - if none of the given libraries load successfully.
      • calculatePackagePrefix

        private static java.lang.String calculatePackagePrefix()
        The shading prefix added to this class's full name.
        Throws:
        java.lang.UnsatisfiedLinkError - if the shader used something other than a prefix
      • load

        public static void load​(java.lang.String originalName,
                                java.lang.ClassLoader loader)
        Load the given library with the specified ClassLoader
      • patchShadedLibraryId

        static boolean patchShadedLibraryId​(java.io.InputStream in,
                                            java.io.OutputStream out,
                                            java.lang.String originalName,
                                            java.lang.String name)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • shouldShadedLibraryIdBePatched

        private static boolean shouldShadedLibraryIdBePatched​(java.lang.String packagePrefix)
      • patchShadedLibraryId

        private static boolean patchShadedLibraryId​(byte[] bytes,
                                                    java.lang.String originalName,
                                                    java.lang.String name)
        Try to patch shaded library to ensure it uses a unique ID.
      • loadLibrary

        private static void loadLibrary​(java.lang.ClassLoader loader,
                                        java.lang.String name,
                                        boolean absolute)
        Loading the native library into the specified ClassLoader.
        Parameters:
        loader - - The ClassLoader where the native library will be loaded into
        name - - The native library path or name
        absolute - - Whether the native library will be loaded by path or by name
      • loadLibraryByHelper

        private static void loadLibraryByHelper​(java.lang.Class<?> helper,
                                                java.lang.String name,
                                                boolean absolute)
                                         throws java.lang.UnsatisfiedLinkError
        Throws:
        java.lang.UnsatisfiedLinkError
      • tryToLoadClass

        private static java.lang.Class<?> tryToLoadClass​(java.lang.ClassLoader loader,
                                                         java.lang.Class<?> helper)
                                                  throws java.lang.ClassNotFoundException
        Try to load the helper Class into specified ClassLoader.
        Parameters:
        loader - - The ClassLoader where to load the helper Class
        helper - - The helper Class
        Returns:
        A new helper Class defined in the specified ClassLoader.
        Throws:
        java.lang.ClassNotFoundException - Helper class not found or loading failed
      • classToByteArray

        private static byte[] classToByteArray​(java.lang.Class<?> clazz)
                                        throws java.lang.ClassNotFoundException
        Load the helper Class as a byte array, to be redefined in specified ClassLoader.
        Parameters:
        clazz - - The helper Class provided by this bundle
        Returns:
        The binary content of helper Class.
        Throws:
        java.lang.ClassNotFoundException - Helper class not found or loading failed
      • closeQuietly

        private static void closeQuietly​(java.io.Closeable c)