Package io.netty.util.internal
Class NativeLibraryLoader
- java.lang.Object
-
- io.netty.util.internal.NativeLibraryLoader
-
public final class NativeLibraryLoader extends java.lang.Object
Helper class to load JNI resources.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
NativeLibraryLoader.NoexecVolumeDetector
-
Field Summary
Fields Modifier and Type Field Description private static boolean
DELETE_NATIVE_LIB_AFTER_LOADING
private static boolean
DETECT_NATIVE_LIBRARY_DUPLICATES
private static InternalLogger
logger
private static java.lang.String
NATIVE_RESOURCE_HOME
private static boolean
TRY_TO_PATCH_SHADED_ID
private static byte[]
UNIQUE_ID_BYTES
private static java.io.File
WORKDIR
-
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
calculateMangledPackagePrefix()
Calculates the mangled shading prefix added to this class's full name.private static byte[]
classToByteArray(java.lang.Class<?> clazz)
Load the helperClass
as a byte array, to be redefined in specifiedClassLoader
.private static void
closeQuietly(java.io.Closeable c)
private static byte[]
digest(java.security.MessageDigest digest, java.net.URL url)
private static byte[]
generateUniqueId(int length)
private static java.net.URL
getResource(java.lang.String path, java.lang.ClassLoader loader)
static void
load(java.lang.String originalName, java.lang.ClassLoader loader)
Load the given library with the specifiedClassLoader
static void
loadFirstAvailable(java.lang.ClassLoader loader, java.lang.String... names)
Loads the first available library in the collection with the specifiedClassLoader
.private static void
loadLibrary(java.lang.ClassLoader loader, java.lang.String name, boolean absolute)
Loading the native library into the specifiedClassLoader
.private static void
loadLibraryByHelper(java.lang.Class<?> helper, java.lang.String name, boolean absolute)
private static void
rethrowWithMoreDetailsIfPossible(java.lang.String name, java.lang.NoSuchMethodError error)
private static boolean
shouldShadedLibraryIdBePatched(java.lang.String packagePrefix)
private static boolean
tryExec(java.lang.String cmd)
(package private) static void
tryPatchShadedLibraryIdAndSign(java.io.File libraryFile, java.lang.String originalName)
private static java.lang.Class<?>
tryToLoadClass(java.lang.ClassLoader loader, java.lang.Class<?> helper)
Try to load the helperClass
into specifiedClassLoader
.
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
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
-
DETECT_NATIVE_LIBRARY_DUPLICATES
private static final boolean DETECT_NATIVE_LIBRARY_DUPLICATES
-
UNIQUE_ID_BYTES
private static final byte[] UNIQUE_ID_BYTES
-
-
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 specifiedClassLoader
.- Throws:
java.lang.IllegalArgumentException
- if none of the given libraries load successfully.
-
calculateMangledPackagePrefix
private static java.lang.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:
- JNI specification
parsePackagePrefix()
innetty_jni_util.c
.
- 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 specifiedClassLoader
-
getResource
private static java.net.URL getResource(java.lang.String path, java.lang.ClassLoader loader)
-
digest
private static byte[] digest(java.security.MessageDigest digest, java.net.URL url)
-
tryPatchShadedLibraryIdAndSign
static void tryPatchShadedLibraryIdAndSign(java.io.File libraryFile, java.lang.String originalName)
-
tryExec
private static boolean tryExec(java.lang.String cmd)
-
shouldShadedLibraryIdBePatched
private static boolean shouldShadedLibraryIdBePatched(java.lang.String packagePrefix)
-
generateUniqueId
private static byte[] generateUniqueId(int length)
-
loadLibrary
private static void loadLibrary(java.lang.ClassLoader loader, java.lang.String name, boolean absolute)
Loading the native library into the specifiedClassLoader
.- Parameters:
loader
- - TheClassLoader
where the native library will be loaded intoname
- - The native library path or nameabsolute
- - Whether the native library will be loaded by path or by name
-
rethrowWithMoreDetailsIfPossible
private static void rethrowWithMoreDetailsIfPossible(java.lang.String name, java.lang.NoSuchMethodError error)
-
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 helperClass
into specifiedClassLoader
.- Parameters:
loader
- - TheClassLoader
where to load the helperClass
helper
- - The helperClass
- 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 helperClass
as a byte array, to be redefined in specifiedClassLoader
.- Parameters:
clazz
- - The helperClass
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)
-
-