Home / Class/ NativeLibraryLoader Class — netty Architecture

NativeLibraryLoader Class — netty Architecture

Architecture documentation for the NativeLibraryLoader class in NativeLibraryLoader.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  e940cc67_3be6_5ff3_e560_4d68972dc13b["NativeLibraryLoader"]
  34c91fb2_20f7_cd47_7f1f_e75f1425f235["NativeLibraryLoader.java"]
  e940cc67_3be6_5ff3_e560_4d68972dc13b -->|defined in| 34c91fb2_20f7_cd47_7f1f_e75f1425f235
  8ea18f7b_b5e3_b0c6_4ce1_356784300e26["loadFirstAvailable()"]
  e940cc67_3be6_5ff3_e560_4d68972dc13b -->|method| 8ea18f7b_b5e3_b0c6_4ce1_356784300e26
  50a1da9f_331c_2384_d1cd_d997cf54f46f["String()"]
  e940cc67_3be6_5ff3_e560_4d68972dc13b -->|method| 50a1da9f_331c_2384_d1cd_d997cf54f46f
  d92afe83_049b_d0b5_f35c_dd5834c38908["load()"]
  e940cc67_3be6_5ff3_e560_4d68972dc13b -->|method| d92afe83_049b_d0b5_f35c_dd5834c38908
  0e52c8e7_1e67_40f4_2a1c_6c33fc417f6e["URL()"]
  e940cc67_3be6_5ff3_e560_4d68972dc13b -->|method| 0e52c8e7_1e67_40f4_2a1c_6c33fc417f6e
  2c0bfd33_d083_1660_4138_47c853fc9d33["digest()"]
  e940cc67_3be6_5ff3_e560_4d68972dc13b -->|method| 2c0bfd33_d083_1660_4138_47c853fc9d33
  718b0b6c_05e8_e851_bc3f_0a119eac5245["tryPatchShadedLibraryIdAndSign()"]
  e940cc67_3be6_5ff3_e560_4d68972dc13b -->|method| 718b0b6c_05e8_e851_bc3f_0a119eac5245
  b8b7f986_a84f_3a12_d53b_c975f0075ed2["tryExec()"]
  e940cc67_3be6_5ff3_e560_4d68972dc13b -->|method| b8b7f986_a84f_3a12_d53b_c975f0075ed2
  de825732_fc04_e1dd_c5ab_9f862f5d6bca["shouldShadedLibraryIdBePatched()"]
  e940cc67_3be6_5ff3_e560_4d68972dc13b -->|method| de825732_fc04_e1dd_c5ab_9f862f5d6bca
  669386f2_d789_f310_46b6_6942cdcc0600["generateUniqueId()"]
  e940cc67_3be6_5ff3_e560_4d68972dc13b -->|method| 669386f2_d789_f310_46b6_6942cdcc0600
  bafe3f15_b0bf_647a_3a35_cd885f9d3174["loadLibrary()"]
  e940cc67_3be6_5ff3_e560_4d68972dc13b -->|method| bafe3f15_b0bf_647a_3a35_cd885f9d3174
  4596d050_35b6_8fd1_9844_425af82366ca["loadLibraryByHelper()"]
  e940cc67_3be6_5ff3_e560_4d68972dc13b -->|method| 4596d050_35b6_8fd1_9844_425af82366ca
  ad07e32d_733a_fab6_069d_362602f3f820["tryToLoadClass()"]
  e940cc67_3be6_5ff3_e560_4d68972dc13b -->|method| ad07e32d_733a_fab6_069d_362602f3f820
  b457bef5_b901_945e_103b_a802b8d26790["classToByteArray()"]
  e940cc67_3be6_5ff3_e560_4d68972dc13b -->|method| b457bef5_b901_945e_103b_a802b8d26790

Relationship Graph

Source Code

common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java lines 50–545

public final class NativeLibraryLoader {

    private static final InternalLogger logger = InternalLoggerFactory.getInstance(NativeLibraryLoader.class);

    private static final String NATIVE_RESOURCE_HOME = "META-INF/native/";
    private static final File WORKDIR;
    private static final boolean DELETE_NATIVE_LIB_AFTER_LOADING;
    private static final boolean TRY_TO_PATCH_SHADED_ID;
    private static final boolean DETECT_NATIVE_LIBRARY_DUPLICATES;

    // Just use a-Z and numbers as valid ID bytes.
    private static final byte[] UNIQUE_ID_BYTES =
            "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(CharsetUtil.US_ASCII);

    static {
        String workdir = SystemPropertyUtil.get("io.netty.native.workdir");
        if (workdir != null) {
            File f = new File(workdir);
            if (!f.exists() && !f.mkdirs()) {
                throw new ExceptionInInitializerError(
                    new IOException("Custom native workdir mkdirs failed: " + workdir));
            }

            try {
                f = f.getAbsoluteFile();
            } catch (Exception ignored) {
                // Good to have an absolute path, but it's OK.
            }

            WORKDIR = f;
            logger.debug("-Dio.netty.native.workdir: " + WORKDIR);
        } else {
            WORKDIR = PlatformDependent.tmpdir();
            logger.debug("-Dio.netty.native.workdir: " + WORKDIR + " (io.netty.tmpdir)");
        }

        DELETE_NATIVE_LIB_AFTER_LOADING = SystemPropertyUtil.getBoolean(
                "io.netty.native.deleteLibAfterLoading", true);
        logger.debug("-Dio.netty.native.deleteLibAfterLoading: {}", DELETE_NATIVE_LIB_AFTER_LOADING);

        TRY_TO_PATCH_SHADED_ID = SystemPropertyUtil.getBoolean(
                "io.netty.native.tryPatchShadedId", true);
        logger.debug("-Dio.netty.native.tryPatchShadedId: {}", TRY_TO_PATCH_SHADED_ID);

        DETECT_NATIVE_LIBRARY_DUPLICATES = SystemPropertyUtil.getBoolean(
                "io.netty.native.detectNativeLibraryDuplicates", true);
        logger.debug("-Dio.netty.native.detectNativeLibraryDuplicates: {}", DETECT_NATIVE_LIBRARY_DUPLICATES);
    }

    /**
     * Loads the first available library in the collection with the specified
     * {@link ClassLoader}.
     *
     * @throws IllegalArgumentException
     *         if none of the given libraries load successfully.
     */
    public static void loadFirstAvailable(ClassLoader loader, String... names) {
        List<Throwable> suppressed = new ArrayList<Throwable>();
        for (String name : names) {
            try {
                load(name, loader);
                logger.debug("Loaded library with name '{}'", name);
                return;
            } catch (Throwable t) {
                suppressed.add(t);
            }
        }

        IllegalArgumentException iae =
                new IllegalArgumentException("Failed to load any of the given libraries: " + Arrays.toString(names));
        ThrowableUtil.addSuppressedAndClear(iae, suppressed);
        throw iae;
    }

    /**
     * Calculates the mangled shading prefix added to this class's full name.
     *
     * <p>This method mangles the package name as follows, so we can unmangle it back later:
     * <ul>
     *   <li>{@code _} to {@code _1}</li>
     *   <li>{@code .} to {@code _}</li>

Frequently Asked Questions

What is the NativeLibraryLoader class?
NativeLibraryLoader is a class in the netty codebase, defined in common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java.
Where is NativeLibraryLoader defined?
NativeLibraryLoader is defined in common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java at line 50.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free