Example usage for java.lang System mapLibraryName

List of usage examples for java.lang System mapLibraryName

Introduction

In this page you can find the example usage for java.lang System mapLibraryName.

Prototype

public static native String mapLibraryName(String libname);

Source Link

Document

Maps a library name into a platform-specific string representing a native library.

Usage

From source file:org.gradle.internal.nativeintegration.jna.JnaBootPathConfigurer.java

/**
 * Attempts to find the jna library and copies it to a specified folder.
 * The copy operation happens only once. Sets the jna-related system property.
 *
 * This hackery is to prevent JNA from creating a shared lib in the tmp dir, as it does not clean things up.
 *
 * @param storageDir - where to store the jna library
 *//* w  ww . j a  v a2  s.  c  o m*/
public void configure(File storageDir) {
    String nativePrefix = OperatingSystem.current().getNativePrefix();
    File tmpDir = new File(storageDir, String.format("jna/%s", nativePrefix));
    tmpDir.mkdirs();
    String jnaLibName = OperatingSystem.current().isMacOsX() ? "libjnidispatch.jnilib"
            : System.mapLibraryName("jnidispatch");
    File libFile = new File(tmpDir, jnaLibName);
    if (!libFile.exists()) {
        String resourceName = "/com/sun/jna/" + nativePrefix + "/" + jnaLibName;
        try {
            InputStream lib = getClass().getResourceAsStream(resourceName);
            if (lib == null) {
                return;
            }
            try {
                FileOutputStream outputStream = new FileOutputStream(libFile);
                try {
                    IOUtils.copy(lib, outputStream);
                } finally {
                    outputStream.close();
                }
            } finally {
                lib.close();
            }
        } catch (IOException e) {
            throw new NativeIntegrationException(
                    String.format("Could not create JNA native library '%s'.", libFile), e);
        }
    }
    System.setProperty("jna.boot.library.path", tmpDir.getAbsolutePath());
}

From source file:org.cryptomator.jni.JniModule.java

@Provides
@Singleton//from  w w w . java  2  s .  c  om
public Optional<WinFunctions> winFunctions(Lazy<WinFunctions> winFunction) {
    if (SystemUtils.IS_OS_WINDOWS) {
        try {
            System.loadLibrary(WinFunctions.LIB_NAME);
            LOG.info("loaded {}", System.mapLibraryName(WinFunctions.LIB_NAME));
            return Optional.of(winFunction.get());
        } catch (UnsatisfiedLinkError e) {
            e.printStackTrace();
            LOG.error("Could not load JNI lib {} from path {}", System.mapLibraryName(WinFunctions.LIB_NAME),
                    System.getProperty("java.library.path"));
        }
    }
    return Optional.empty();
}

From source file:org.gradle.logging.internal.JnaBootPathConfigurer.java

public void configure() {
    File tmpDir = new File(storageDir, "jna");
    tmpDir.mkdirs();//from  ww w .j  av a2  s  .  com
    String jnaLibName = OperatingSystem.current().isMacOsX() ? "libjnidispatch.jnilib"
            : System.mapLibraryName("jnidispatch");
    File libFile = new File(tmpDir, jnaLibName);
    if (!libFile.exists()) {
        String resourceName = "/com/sun/jna/" + OperatingSystem.current().getNativePrefix() + "/" + jnaLibName;
        try {
            InputStream lib = getClass().getResourceAsStream(resourceName);
            if (lib == null) {
                throw new JnaNotAvailableException(
                        String.format("Could not locate JNA native lib resource '%s'.", resourceName));
            }
            try {
                FileOutputStream outputStream = new FileOutputStream(libFile);
                try {
                    IOUtils.copy(lib, outputStream);
                } finally {
                    outputStream.close();
                }
            } finally {
                lib.close();
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    System.setProperty("jna.boot.library.path", tmpDir.getAbsolutePath());
}

From source file:org.gradle.os.jna.JnaBootPathConfigurer.java

public void configure() throws NativeIntegrationUnavailableException {
    File tmpDir = new File(storageDir, "jna");
    tmpDir.mkdirs();// w w  w .  jav a 2  s  .com
    String jnaLibName = OperatingSystem.current().isMacOsX() ? "libjnidispatch.jnilib"
            : System.mapLibraryName("jnidispatch");
    File libFile = new File(tmpDir, jnaLibName);
    if (!libFile.exists()) {
        String resourceName = "/com/sun/jna/" + OperatingSystem.current().getNativePrefix() + "/" + jnaLibName;
        try {
            InputStream lib = getClass().getResourceAsStream(resourceName);
            if (lib == null) {
                throw new NativeIntegrationUnavailableException(
                        String.format("Could not locate JNA native library resource '%s'.", resourceName));
            }
            try {
                FileOutputStream outputStream = new FileOutputStream(libFile);
                try {
                    IOUtils.copy(lib, outputStream);
                } finally {
                    outputStream.close();
                }
            } finally {
                lib.close();
            }
        } catch (IOException e) {
            throw new NativeIntegrationException(
                    String.format("Could not create JNA native library '%s'.", libFile), e);
        }
    }
    System.setProperty("jna.boot.library.path", tmpDir.getAbsolutePath());
}

From source file:jnative.JNativeCodeLoader.java

/**
 * Locates the native library in the jar (loadble by the classloader really),
 * unpacks it in a temp location, and returns that file. If the native library
 * is not found by the classloader, returns null.
 *//* w  ww. j  a va2s .  c  om*/
private static File unpackBinaries() {
    // locate the binaries inside the jar
    String fileName = System.mapLibraryName(LIBRARY_NAME);
    String directory = getDirectoryLocation();
    // use the current defining classloader to load the resource
    InputStream is = JNativeCodeLoader.class.getResourceAsStream(directory + "/" + fileName);
    if (is == null) {
        // specific to mac
        // on mac the filename can be either .dylib or .jnilib: try again with the
        // alternate name
        if (getOsName().contains("Mac")) {
            if (fileName.endsWith(".dylib")) {
                fileName = fileName.replace(".dylib", ".jnilib");
            } else if (fileName.endsWith(".jnilib")) {
                fileName = fileName.replace(".jnilib", ".dylib");
            }
            is = JNativeCodeLoader.class.getResourceAsStream(directory + "/" + fileName);
        }
        // the OS-specific library was not found: fall back on the library path
        if (is == null) {
            return null;
        }
    }

    // write the file
    byte[] buffer = new byte[8192];
    OutputStream os = null;
    try {
        // prepare the unpacked file location
        File unpackedFile = File.createTempFile("unpacked-", "-" + fileName);
        // ensure the file gets cleaned up
        unpackedFile.deleteOnExit();

        os = new FileOutputStream(unpackedFile);
        int read = 0;
        while ((read = is.read(buffer)) != -1) {
            os.write(buffer, 0, read);
        }

        // set the execution permission
        unpackedFile.setExecutable(true, false);
        LOG.debug("temporary unpacked path: " + unpackedFile);
        // return the file
        return unpackedFile;
    } catch (IOException e) {
        LOG.error("could not unpack the binaries", e);
        return null;
    } finally {
        try {
            is.close();
        } catch (IOException ignore) {
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException ignore) {
            }
        }
    }
}

From source file:com.intel.chimera.utils.NativeCodeLoader.java

static File findNativeLibrary() {
    // Try to load the library in chimera.lib.path */
    String nativeLibraryPath = Utils.getLibPath();
    String nativeLibraryName = Utils.getLibName();

    // Resolve the library file name with a suffix (e.g., dll, .so, etc.)
    if (nativeLibraryName == null)
        nativeLibraryName = System.mapLibraryName("chimera");

    if (nativeLibraryPath != null) {
        File nativeLib = new File(nativeLibraryPath, nativeLibraryName);
        if (nativeLib.exists())
            return nativeLib;
    }/*from   w  ww . j a  v a2  s . c om*/

    // Load an OS-dependent native library inside a jar file
    nativeLibraryPath = "/com/intel/chimera/native/" + OSInfo.getNativeLibFolderPathForCurrentOS();
    boolean hasNativeLib = hasResource(nativeLibraryPath + "/" + nativeLibraryName);
    if (!hasNativeLib) {
        if (OSInfo.getOSName().equals("Mac")) {
            // Fix for openjdk7 for Mac
            String altName = "libchimera.jnilib";
            if (hasResource(nativeLibraryPath + "/" + altName)) {
                nativeLibraryName = altName;
                hasNativeLib = true;
            }
        }
    }

    if (!hasNativeLib) {
        String errorMessage = String.format("no native library is found for os.name=%s and os.arch=%s",
                OSInfo.getOSName(), OSInfo.getArchName());
        throw new RuntimeException(errorMessage);
    }

    // Temporary folder for the native lib. Use the value of
    // chimera.tempdir or java.io.tmpdir
    String tempFolder = new File(Utils.getTmpDir()).getAbsolutePath();

    // Extract and load a native library inside the jar file
    return extractLibraryFile(nativeLibraryPath, nativeLibraryName, tempFolder);
}

From source file:com.hadoop.compression.lzo.GPLNativeCodeLoader.java

/**
 * Locates the native library in the jar (loadble by the classloader really),
 * unpacks it in a temp location, and returns that file. If the native library
 * is not found by the classloader, returns null.
 *//*from ww  w. j a va 2 s .com*/
private static File unpackBinaries() {
    // locate the binaries inside the jar
    String fileName = System.mapLibraryName(LIBRARY_NAME);
    String directory = getDirectoryLocation();
    // use the current defining classloader to load the resource
    InputStream is = GPLNativeCodeLoader.class.getResourceAsStream(directory + "/" + fileName);
    if (is == null) {
        // specific to mac
        // on mac the filename can be either .dylib or .jnilib: try again with the
        // alternate name
        if (getOsName().contains("Mac")) {
            if (fileName.endsWith(".dylib")) {
                fileName = fileName.replace(".dylib", ".jnilib");
            } else if (fileName.endsWith(".jnilib")) {
                fileName = fileName.replace(".jnilib", ".dylib");
            }
            is = GPLNativeCodeLoader.class.getResourceAsStream(directory + "/" + fileName);
        }
        // the OS-specific library was not found: fall back on the library path
        if (is == null) {
            return null;
        }
    }

    // write the file
    byte[] buffer = new byte[8192];
    OutputStream os = null;
    try {
        // prepare the unpacked file location
        File unpackedFile = File.createTempFile("unpacked-", "-" + fileName);
        // ensure the file gets cleaned up
        unpackedFile.deleteOnExit();

        os = new FileOutputStream(unpackedFile);
        int read = 0;
        while ((read = is.read(buffer)) != -1) {
            os.write(buffer, 0, read);
        }

        // set the execution permission
        unpackedFile.setExecutable(true, false);
        LOG.debug("temporary unpacked path: " + unpackedFile);
        // return the file
        return unpackedFile;
    } catch (IOException e) {
        LOG.error("could not unpack the binaries", e);
        return null;
    } finally {
        try {
            is.close();
        } catch (IOException ignore) {
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException ignore) {
            }
        }
    }
}

From source file:org.mule.module.launcher.nativelib.PerAppCopyNativeLibraryFinder.java

private File copyNativeLibrary(String name, String libraryPath) {
    final String nativeLibName = System.mapLibraryName(name);
    final File tempLibrary = new File(perAppNativeLibs, nativeLibName + System.currentTimeMillis());

    try {/*from   w ww .ja v  a  2s.c  o m*/
        final File library = new File(libraryPath);
        FileUtils.copyFile(library, tempLibrary);

        return tempLibrary;
    } catch (IOException e) {
        throw new IllegalStateException(String.format("Unable to generate copy for native library '%s' at '%s'",
                nativeLibName, tempLibrary.getAbsolutePath()), e);
    }
}

From source file:com.intel.cryptostream.utils.NativeCodeLoader.java

static File findNativeLibrary() {
    // Try to load the library in com.intel.cryptostream.lib.path */
    String cryptostreamNativeLibraryPath = CryptoStreamUtils.getCryptoStreamLibPath();
    String cryptostreamNativeLibraryName = CryptoStreamUtils.getCryptoStreamLibName();

    // Resolve the library file name with a suffix (e.g., dll, .so, etc.)
    if (cryptostreamNativeLibraryName == null)
        cryptostreamNativeLibraryName = System.mapLibraryName("cryptostream");

    if (cryptostreamNativeLibraryPath != null) {
        File nativeLib = new File(cryptostreamNativeLibraryPath, cryptostreamNativeLibraryName);
        if (nativeLib.exists())
            return nativeLib;
    }//from   w w  w  .j  av a 2  s  . c  om

    // Load an OS-dependent native library inside a jar file
    cryptostreamNativeLibraryPath = "/com/intel/cryptostream/native/"
            + OSInfo.getNativeLibFolderPathForCurrentOS();
    boolean hasNativeLib = hasResource(cryptostreamNativeLibraryPath + "/" + cryptostreamNativeLibraryName);
    if (!hasNativeLib) {
        if (OSInfo.getOSName().equals("Mac")) {
            // Fix for openjdk7 for Mac
            String altName = "libcryptostream.jnilib";
            if (hasResource(cryptostreamNativeLibraryPath + "/" + altName)) {
                cryptostreamNativeLibraryName = altName;
                hasNativeLib = true;
            }
        }
    }

    if (!hasNativeLib) {
        String errorMessage = String.format("no native library is found for os.name=%s and os.arch=%s",
                OSInfo.getOSName(), OSInfo.getArchName());
        throw new RuntimeException(errorMessage);
    }

    // Temporary folder for the native lib. Use the value of
    // com.intel.cryptostream.tempdir or java.io.tmpdir
    String tempFolder = new File(CryptoStreamUtils.getCryptoStreamTmpDir()).getAbsolutePath();

    // Extract and load a native library inside the jar file
    return extractLibraryFile(cryptostreamNativeLibraryPath, cryptostreamNativeLibraryName, tempFolder);
}

From source file:org.esa.s2tbx.dataio.gdal.GDALInstaller.java

/**
 * Install the GDAL library if missing.//  w w  w .j av a  2s.  c  om
 *
 * @throws IOException
 */
public void install() throws IOException {
    if (!org.apache.commons.lang.SystemUtils.IS_OS_WINDOWS) {
        logger.log(Level.SEVERE, "The GDAL library is available only on Windows operation system.");
        return;
    }
    OSCategory osCategory = OSCategory.getOSCategory();
    if (osCategory.getDirectory() == null) {
        logger.log(Level.SEVERE, "No folder found.");
        return;
    }
    if (osCategory.getZipFileName() == null) {
        logger.log(Level.SEVERE, "No zip file name found.");
        return;
    }

    Path gdalFolderPath = getGDALFolderPath();
    if (gdalFolderPath == null) {
        logger.log(Level.SEVERE, "Failed to retrieve the GDAL folder path on the local disk.");
        return;
    }
    if (!Files.exists(gdalFolderPath)) {
        Files.createDirectories(gdalFolderPath);
    }

    String mapLibraryName = System.mapLibraryName("gdal201");
    installDistribution(gdalFolderPath, osCategory, mapLibraryName);
}