@Test public void testFileNotFound() { try { NativeLibraryLoader.load(UUID.randomUUID().toString(), NativeLibraryLoaderTest.class.getClassLoader()); fail(); } catch (UnsatisfiedLinkError error) { assertTrue(error.getCause() instanceof FileNotFoundException); if (PlatformDependent.javaVersion() >= 7) { verifySuppressedException(error, UnsatisfiedLinkError.class); } } }
public static void load(String originalName, ClassLoader loader) { String name = calculatePackagePrefix().replace('.', '_') + originalName; List<Throwable> suppressed = new ArrayList<Throwable>(); try { loadLibrary(loader, name, false); return; } catch (Throwable ex) { suppressed.add(ex); logger.debug( "{} cannot be loaded from java.libary.path, " + "now trying export to -Dio.netty.native.workdir: {}", name, WORKDIR, ex); } String libname = System.mapLibraryName(name); String path = NATIVE_RESOURCE_HOME + libname; InputStream in = null; OutputStream out = null; File tmpFile = null; URL url; if (loader == null) { url = ClassLoader.getSystemResource(path); } else { url = loader.getResource(path); } try { if (url == null) { if (PlatformDependent.isOsx()) { String fileName = path.endsWith(".jnilib") ? NATIVE_RESOURCE_HOME + "lib" + name + ".dynlib" : NATIVE_RESOURCE_HOME + "lib" + name + ".jnilib"; if (loader == null) { url = ClassLoader.getSystemResource(fileName); } else { url = loader.getResource(fileName); } if (url == null) { FileNotFoundException fnf = new FileNotFoundException(fileName); ThrowableUtil.addSuppressedAndClear(fnf, suppressed); throw fnf; } } else { FileNotFoundException fnf = new FileNotFoundException(path); ThrowableUtil.addSuppressedAndClear(fnf, suppressed); throw fnf; } } int index = libname.lastIndexOf('.'); String prefix = libname.substring(0, index); String suffix = libname.substring(index, libname.length()); tmpFile = File.createTempFile(prefix, suffix, WORKDIR); in = url.openStream(); out = new FileOutputStream(tmpFile); byte[] buffer = new byte[8192]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } out.flush(); closeQuietly(out); out = null; loadLibrary(loader, tmpFile.getPath(), true); } catch (UnsatisfiedLinkError e) { try { if (tmpFile != null && tmpFile.isFile() && tmpFile.canRead() && !NoexecVolumeDetector.canExecuteExecutable(tmpFile)) { logger.info("{} exists but cannot be executed even when execute permissions set; " + "check volume for \"noexec\" flag; use -Dio.netty.native.workdir=[path] " + "to set native working directory separately.", tmpFile.getPath()); } } catch (Throwable t) { suppressed.add(t); logger.debug("Error checking if {} is on a file store mounted with noexec", tmpFile, t); } ThrowableUtil.addSuppressedAndClear(e, suppressed); throw e; } catch (Exception e) { UnsatisfiedLinkError ule = new UnsatisfiedLinkError("could not load a native library: " + name); ule.initCause(e); ThrowableUtil.addSuppressedAndClear(ule, suppressed); throw ule; } finally { closeQuietly(in); closeQuietly(out); if (tmpFile != null && (!DELETE_NATIVE_LIB_AFTER_LOADING || !tmpFile.delete())) { tmpFile.deleteOnExit(); } } }
NativeLibraryLoader { public static void load(String originalName, ClassLoader loader) { String name = calculatePackagePrefix().replace('.', '_') + originalName; List<Throwable> suppressed = new ArrayList<Throwable>(); try { loadLibrary(loader, name, false); return; } catch (Throwable ex) { suppressed.add(ex); logger.debug( "{} cannot be loaded from java.libary.path, " + "now trying export to -Dio.netty.native.workdir: {}", name, WORKDIR, ex); } String libname = System.mapLibraryName(name); String path = NATIVE_RESOURCE_HOME + libname; InputStream in = null; OutputStream out = null; File tmpFile = null; URL url; if (loader == null) { url = ClassLoader.getSystemResource(path); } else { url = loader.getResource(path); } try { if (url == null) { if (PlatformDependent.isOsx()) { String fileName = path.endsWith(".jnilib") ? NATIVE_RESOURCE_HOME + "lib" + name + ".dynlib" : NATIVE_RESOURCE_HOME + "lib" + name + ".jnilib"; if (loader == null) { url = ClassLoader.getSystemResource(fileName); } else { url = loader.getResource(fileName); } if (url == null) { FileNotFoundException fnf = new FileNotFoundException(fileName); ThrowableUtil.addSuppressedAndClear(fnf, suppressed); throw fnf; } } else { FileNotFoundException fnf = new FileNotFoundException(path); ThrowableUtil.addSuppressedAndClear(fnf, suppressed); throw fnf; } } int index = libname.lastIndexOf('.'); String prefix = libname.substring(0, index); String suffix = libname.substring(index, libname.length()); tmpFile = File.createTempFile(prefix, suffix, WORKDIR); in = url.openStream(); out = new FileOutputStream(tmpFile); byte[] buffer = new byte[8192]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } out.flush(); closeQuietly(out); out = null; loadLibrary(loader, tmpFile.getPath(), true); } catch (UnsatisfiedLinkError e) { try { if (tmpFile != null && tmpFile.isFile() && tmpFile.canRead() && !NoexecVolumeDetector.canExecuteExecutable(tmpFile)) { logger.info("{} exists but cannot be executed even when execute permissions set; " + "check volume for \"noexec\" flag; use -Dio.netty.native.workdir=[path] " + "to set native working directory separately.", tmpFile.getPath()); } } catch (Throwable t) { suppressed.add(t); logger.debug("Error checking if {} is on a file store mounted with noexec", tmpFile, t); } ThrowableUtil.addSuppressedAndClear(e, suppressed); throw e; } catch (Exception e) { UnsatisfiedLinkError ule = new UnsatisfiedLinkError("could not load a native library: " + name); ule.initCause(e); ThrowableUtil.addSuppressedAndClear(ule, suppressed); throw ule; } finally { closeQuietly(in); closeQuietly(out); if (tmpFile != null && (!DELETE_NATIVE_LIB_AFTER_LOADING || !tmpFile.delete())) { tmpFile.deleteOnExit(); } } } }
NativeLibraryLoader { public static void load(String originalName, ClassLoader loader) { String name = calculatePackagePrefix().replace('.', '_') + originalName; List<Throwable> suppressed = new ArrayList<Throwable>(); try { loadLibrary(loader, name, false); return; } catch (Throwable ex) { suppressed.add(ex); logger.debug( "{} cannot be loaded from java.libary.path, " + "now trying export to -Dio.netty.native.workdir: {}", name, WORKDIR, ex); } String libname = System.mapLibraryName(name); String path = NATIVE_RESOURCE_HOME + libname; InputStream in = null; OutputStream out = null; File tmpFile = null; URL url; if (loader == null) { url = ClassLoader.getSystemResource(path); } else { url = loader.getResource(path); } try { if (url == null) { if (PlatformDependent.isOsx()) { String fileName = path.endsWith(".jnilib") ? NATIVE_RESOURCE_HOME + "lib" + name + ".dynlib" : NATIVE_RESOURCE_HOME + "lib" + name + ".jnilib"; if (loader == null) { url = ClassLoader.getSystemResource(fileName); } else { url = loader.getResource(fileName); } if (url == null) { FileNotFoundException fnf = new FileNotFoundException(fileName); ThrowableUtil.addSuppressedAndClear(fnf, suppressed); throw fnf; } } else { FileNotFoundException fnf = new FileNotFoundException(path); ThrowableUtil.addSuppressedAndClear(fnf, suppressed); throw fnf; } } int index = libname.lastIndexOf('.'); String prefix = libname.substring(0, index); String suffix = libname.substring(index, libname.length()); tmpFile = File.createTempFile(prefix, suffix, WORKDIR); in = url.openStream(); out = new FileOutputStream(tmpFile); byte[] buffer = new byte[8192]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } out.flush(); closeQuietly(out); out = null; loadLibrary(loader, tmpFile.getPath(), true); } catch (UnsatisfiedLinkError e) { try { if (tmpFile != null && tmpFile.isFile() && tmpFile.canRead() && !NoexecVolumeDetector.canExecuteExecutable(tmpFile)) { logger.info("{} exists but cannot be executed even when execute permissions set; " + "check volume for \"noexec\" flag; use -Dio.netty.native.workdir=[path] " + "to set native working directory separately.", tmpFile.getPath()); } } catch (Throwable t) { suppressed.add(t); logger.debug("Error checking if {} is on a file store mounted with noexec", tmpFile, t); } ThrowableUtil.addSuppressedAndClear(e, suppressed); throw e; } catch (Exception e) { UnsatisfiedLinkError ule = new UnsatisfiedLinkError("could not load a native library: " + name); ule.initCause(e); ThrowableUtil.addSuppressedAndClear(ule, suppressed); throw ule; } finally { closeQuietly(in); closeQuietly(out); if (tmpFile != null && (!DELETE_NATIVE_LIB_AFTER_LOADING || !tmpFile.delete())) { tmpFile.deleteOnExit(); } } } private  NativeLibraryLoader(); }
NativeLibraryLoader { public static void load(String originalName, ClassLoader loader) { String name = calculatePackagePrefix().replace('.', '_') + originalName; List<Throwable> suppressed = new ArrayList<Throwable>(); try { loadLibrary(loader, name, false); return; } catch (Throwable ex) { suppressed.add(ex); logger.debug( "{} cannot be loaded from java.libary.path, " + "now trying export to -Dio.netty.native.workdir: {}", name, WORKDIR, ex); } String libname = System.mapLibraryName(name); String path = NATIVE_RESOURCE_HOME + libname; InputStream in = null; OutputStream out = null; File tmpFile = null; URL url; if (loader == null) { url = ClassLoader.getSystemResource(path); } else { url = loader.getResource(path); } try { if (url == null) { if (PlatformDependent.isOsx()) { String fileName = path.endsWith(".jnilib") ? NATIVE_RESOURCE_HOME + "lib" + name + ".dynlib" : NATIVE_RESOURCE_HOME + "lib" + name + ".jnilib"; if (loader == null) { url = ClassLoader.getSystemResource(fileName); } else { url = loader.getResource(fileName); } if (url == null) { FileNotFoundException fnf = new FileNotFoundException(fileName); ThrowableUtil.addSuppressedAndClear(fnf, suppressed); throw fnf; } } else { FileNotFoundException fnf = new FileNotFoundException(path); ThrowableUtil.addSuppressedAndClear(fnf, suppressed); throw fnf; } } int index = libname.lastIndexOf('.'); String prefix = libname.substring(0, index); String suffix = libname.substring(index, libname.length()); tmpFile = File.createTempFile(prefix, suffix, WORKDIR); in = url.openStream(); out = new FileOutputStream(tmpFile); byte[] buffer = new byte[8192]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } out.flush(); closeQuietly(out); out = null; loadLibrary(loader, tmpFile.getPath(), true); } catch (UnsatisfiedLinkError e) { try { if (tmpFile != null && tmpFile.isFile() && tmpFile.canRead() && !NoexecVolumeDetector.canExecuteExecutable(tmpFile)) { logger.info("{} exists but cannot be executed even when execute permissions set; " + "check volume for \"noexec\" flag; use -Dio.netty.native.workdir=[path] " + "to set native working directory separately.", tmpFile.getPath()); } } catch (Throwable t) { suppressed.add(t); logger.debug("Error checking if {} is on a file store mounted with noexec", tmpFile, t); } ThrowableUtil.addSuppressedAndClear(e, suppressed); throw e; } catch (Exception e) { UnsatisfiedLinkError ule = new UnsatisfiedLinkError("could not load a native library: " + name); ule.initCause(e); ThrowableUtil.addSuppressedAndClear(ule, suppressed); throw ule; } finally { closeQuietly(in); closeQuietly(out); if (tmpFile != null && (!DELETE_NATIVE_LIB_AFTER_LOADING || !tmpFile.delete())) { tmpFile.deleteOnExit(); } } } private  NativeLibraryLoader(); static void loadFirstAvailable(ClassLoader loader, String... names); static void load(String originalName, ClassLoader loader); }
NativeLibraryLoader { public static void load(String originalName, ClassLoader loader) { String name = calculatePackagePrefix().replace('.', '_') + originalName; List<Throwable> suppressed = new ArrayList<Throwable>(); try { loadLibrary(loader, name, false); return; } catch (Throwable ex) { suppressed.add(ex); logger.debug( "{} cannot be loaded from java.libary.path, " + "now trying export to -Dio.netty.native.workdir: {}", name, WORKDIR, ex); } String libname = System.mapLibraryName(name); String path = NATIVE_RESOURCE_HOME + libname; InputStream in = null; OutputStream out = null; File tmpFile = null; URL url; if (loader == null) { url = ClassLoader.getSystemResource(path); } else { url = loader.getResource(path); } try { if (url == null) { if (PlatformDependent.isOsx()) { String fileName = path.endsWith(".jnilib") ? NATIVE_RESOURCE_HOME + "lib" + name + ".dynlib" : NATIVE_RESOURCE_HOME + "lib" + name + ".jnilib"; if (loader == null) { url = ClassLoader.getSystemResource(fileName); } else { url = loader.getResource(fileName); } if (url == null) { FileNotFoundException fnf = new FileNotFoundException(fileName); ThrowableUtil.addSuppressedAndClear(fnf, suppressed); throw fnf; } } else { FileNotFoundException fnf = new FileNotFoundException(path); ThrowableUtil.addSuppressedAndClear(fnf, suppressed); throw fnf; } } int index = libname.lastIndexOf('.'); String prefix = libname.substring(0, index); String suffix = libname.substring(index, libname.length()); tmpFile = File.createTempFile(prefix, suffix, WORKDIR); in = url.openStream(); out = new FileOutputStream(tmpFile); byte[] buffer = new byte[8192]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } out.flush(); closeQuietly(out); out = null; loadLibrary(loader, tmpFile.getPath(), true); } catch (UnsatisfiedLinkError e) { try { if (tmpFile != null && tmpFile.isFile() && tmpFile.canRead() && !NoexecVolumeDetector.canExecuteExecutable(tmpFile)) { logger.info("{} exists but cannot be executed even when execute permissions set; " + "check volume for \"noexec\" flag; use -Dio.netty.native.workdir=[path] " + "to set native working directory separately.", tmpFile.getPath()); } } catch (Throwable t) { suppressed.add(t); logger.debug("Error checking if {} is on a file store mounted with noexec", tmpFile, t); } ThrowableUtil.addSuppressedAndClear(e, suppressed); throw e; } catch (Exception e) { UnsatisfiedLinkError ule = new UnsatisfiedLinkError("could not load a native library: " + name); ule.initCause(e); ThrowableUtil.addSuppressedAndClear(ule, suppressed); throw ule; } finally { closeQuietly(in); closeQuietly(out); if (tmpFile != null && (!DELETE_NATIVE_LIB_AFTER_LOADING || !tmpFile.delete())) { tmpFile.deleteOnExit(); } } } private  NativeLibraryLoader(); static void loadFirstAvailable(ClassLoader loader, String... names); static void load(String originalName, ClassLoader loader);  }