Java Code Examples for io.netty.util.internal.PlatformDependent#getClassLoader()

The following examples show how to use io.netty.util.internal.PlatformDependent#getClassLoader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Native.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private static void loadNativeLibrary() {
    String name = SystemPropertyUtil.get("os.name").toLowerCase(Locale.UK).trim();
    if (!name.startsWith("mac") && !name.contains("bsd") && !name.startsWith("darwin")) {
        throw new IllegalStateException("Only supported on BSD");
    }
    String staticLibName = "netty_transport_native_kqueue";
    String sharedLibName = staticLibName + '_' + PlatformDependent.normalizedArch();
    ClassLoader cl = PlatformDependent.getClassLoader(Native.class);
    try {
        NativeLibraryLoader.load(sharedLibName, cl);
    } catch (UnsatisfiedLinkError e1) {
        try {
            NativeLibraryLoader.load(staticLibName, cl);
            logger.debug("Failed to load {}", sharedLibName, e1);
        } catch (UnsatisfiedLinkError e2) {
            ThrowableUtil.addSuppressed(e1, e2);
            throw e1;
        }
    }
}
 
Example 2
Source File: Native.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private static void loadNativeLibrary() {
    String name = SystemPropertyUtil.get("os.name").toLowerCase(Locale.UK).trim();
    if (!name.startsWith("linux")) {
        throw new IllegalStateException("Only supported on Linux");
    }
    String staticLibName = "netty_transport_native_epoll";
    String sharedLibName = staticLibName + '_' + PlatformDependent.normalizedArch();
    ClassLoader cl = PlatformDependent.getClassLoader(Native.class);
    try {
        NativeLibraryLoader.load(sharedLibName, cl);
    } catch (UnsatisfiedLinkError e1) {
        try {
            NativeLibraryLoader.load(staticLibName, cl);
            logger.debug("Failed to load {}", sharedLibName, e1);
        } catch (UnsatisfiedLinkError e2) {
            ThrowableUtil.addSuppressed(e1, e2);
            throw e1;
        }
    }
}
 
Example 3
Source File: ClassResolvers.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
static ClassLoader defaultClassLoader(ClassLoader classLoader) {
    if (classLoader != null) {
        return classLoader;
    }

    final ClassLoader contextClassLoader = PlatformDependent.getContextClassLoader();
    if (contextClassLoader != null) {
        return contextClassLoader;
    }

    return PlatformDependent.getClassLoader(ClassResolvers.class);
}
 
Example 4
Source File: ClassResolvers.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
static ClassLoader defaultClassLoader(ClassLoader classLoader) {
    if (classLoader != null) {
        return classLoader;
    }

    final ClassLoader contextClassLoader = PlatformDependent.getContextClassLoader();
    if (contextClassLoader != null) {
        return contextClassLoader;
    }

    return PlatformDependent.getClassLoader(ClassResolvers.class);
}
 
Example 5
Source File: DefaultChannelId.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private static int defaultProcessId() {
    ClassLoader loader = null;
    String value;
    try {
        loader = PlatformDependent.getClassLoader(DefaultChannelId.class);
        // Invoke java.lang.management.ManagementFactory.getRuntimeMXBean().getName()
        Class<?> mgmtFactoryType = Class.forName("java.lang.management.ManagementFactory", true, loader);
        Class<?> runtimeMxBeanType = Class.forName("java.lang.management.RuntimeMXBean", true, loader);

        Method getRuntimeMXBean = mgmtFactoryType.getMethod("getRuntimeMXBean", EmptyArrays.EMPTY_CLASSES);
        Object bean = getRuntimeMXBean.invoke(null, EmptyArrays.EMPTY_OBJECTS);
        Method getName = runtimeMxBeanType.getMethod("getName", EmptyArrays.EMPTY_CLASSES);
        value = (String) getName.invoke(bean, EmptyArrays.EMPTY_OBJECTS);
    } catch (Throwable t) {
        logger.debug("Could not invoke ManagementFactory.getRuntimeMXBean().getName(); Android?", t);
        try {
            // Invoke android.os.Process.myPid()
            Class<?> processType = Class.forName("android.os.Process", true, loader);
            Method myPid = processType.getMethod("myPid", EmptyArrays.EMPTY_CLASSES);
            value = myPid.invoke(null, EmptyArrays.EMPTY_OBJECTS).toString();
        } catch (Throwable t2) {
            logger.debug("Could not invoke Process.myPid(); not Android?", t2);
            value = "";
        }
    }

    int atIndex = value.indexOf('@');
    if (atIndex >= 0) {
        value = value.substring(0, atIndex);
    }

    int pid;
    try {
        pid = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        // value did not contain an integer.
        pid = -1;
    }

    if (pid < 0) {
        pid = PlatformDependent.threadLocalRandom().nextInt();
        logger.warn("Failed to find the current process ID from '{}'; using a random value: {}",  value, pid);
    }

    return pid;
}