Class NativeLibraryLoader

java.lang.Object
io.netty.util.internal.NativeLibraryLoader

public final class NativeLibraryLoader extends Object
Helper class to load JNI resources.
  • Field Details

    • logger

      private static final InternalLogger logger
    • NATIVE_RESOURCE_HOME

      private static final String NATIVE_RESOURCE_HOME
      See Also:
    • WORKDIR

      private static final 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
    • DETECT_NATIVE_LIBRARY_DUPLICATES

      private static final boolean DETECT_NATIVE_LIBRARY_DUPLICATES
    • UNIQUE_ID_BYTES

      private static final byte[] UNIQUE_ID_BYTES
  • Constructor Details

    • NativeLibraryLoader

      private NativeLibraryLoader()
  • Method Details

    • loadFirstAvailable

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

      private static String calculateMangledPackagePrefix()
      Calculates the mangled shading prefix added to this class's full name.

      This method mangles the package name as follows, so we can unmangle it back later:

      • _ to _1
      • . to _

      Note that we don't mangle non-ASCII characters here because it's extremely unlikely to have a non-ASCII character in a package name. For more information, see:

      Throws:
      UnsatisfiedLinkError - if the shader used something other than a prefix
    • load

      public static void load(String originalName, ClassLoader loader)
      Load the given library with the specified ClassLoader
    • getResource

      private static URL getResource(String path, ClassLoader loader)
    • digest

      private static byte[] digest(MessageDigest digest, URL url)
    • tryPatchShadedLibraryIdAndSign

      static void tryPatchShadedLibraryIdAndSign(File libraryFile, String originalName)
    • tryExec

      private static boolean tryExec(String cmd)
    • shouldShadedLibraryIdBePatched

      private static boolean shouldShadedLibraryIdBePatched(String packagePrefix)
    • generateUniqueId

      private static byte[] generateUniqueId(int length)
    • loadLibrary

      private static void loadLibrary(ClassLoader loader, 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
    • rethrowWithMoreDetailsIfPossible

      private static void rethrowWithMoreDetailsIfPossible(String name, NoSuchMethodError error)
    • loadLibraryByHelper

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

      private static Class<?> tryToLoadClass(ClassLoader loader, Class<?> helper) throws 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:
      ClassNotFoundException - Helper class not found or loading failed
    • classToByteArray

      private static byte[] classToByteArray(Class<?> clazz) throws 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:
      ClassNotFoundException - Helper class not found or loading failed
    • closeQuietly

      private static void closeQuietly(Closeable c)