/** * Find the right shared library, depending on the operating system and architecture. * * @throws UnsupportedOperationException Throw an exception if no native library for this platform * was found. */ private static URL findLibraryResource(final String libraryFileName) { String osName = System.getProperty("os.name").toLowerCase(); String osArch = System.getProperty("os.arch").toLowerCase(); String resourceName = null; if (osName.startsWith(OS_WIN)) { resourceName = determineWindowsLibrary(libraryFileName, osName, osArch); } else if (osName.startsWith(OS_LINUX)) { resourceName = determineLinuxLibrary(libraryFileName, osName, osArch); } else if (osName.startsWith(OS_FREEBSD)) { resourceName = determineFreebsdLibrary(libraryFileName, osName, osArch); } else if (osName.startsWith(OS_MAC)) { resourceName = determineMacLibrary(libraryFileName); } else { unsupportedPlatform(osName, osArch); } URL resource = NativeLoader.class.getResource(resourceName); if (null == resource) { unsupportedPlatform(osName, osArch, resourceName); } return resource; }
/** * Load the shared libraries. */ static void loadLibrary() { try { File dir = Files.createTempDirectory("libjsass-").toFile(); dir.deleteOnExit(); if (System.getProperty("os.name").toLowerCase().startsWith("win")) { System.load(saveLibrary(dir, "sass")); } System.load(saveLibrary(dir, "jsass")); } catch (Exception exception) { LOG.warn(exception.getMessage(), exception); throw new LoaderException(exception); } }
/** * Save the shared library in the given temporary directory. */ static String saveLibrary(final File dir, final String libraryName) throws IOException { String libraryFileName = "lib" + libraryName; URL libraryResource = findLibraryResource(libraryFileName); String basename = FilenameUtils.getName(libraryResource.getPath()); File file = new File(dir, basename); file.deleteOnExit(); try ( InputStream in = libraryResource.openStream(); OutputStream out = new FileOutputStream(file) ) { IOUtils.copy(in, out); } return file.getAbsolutePath(); }
/** * Determine the right FreeBSD library depending on the architecture. * * @param library The library name. * @param osName The operating system name. * @param osArch The system architecture. * @return The library resource. * @throws UnsupportedOperationException Throw an exception if no native library for this platform * was found. */ private static String determineFreebsdLibrary( final String library, final String osName, final String osArch ) { String resourceName; String platform = null; String fileExtension = "so"; switch (osArch) { case ARCH_AMD64: case ARCH_X86_64: platform = "freebsd-x64"; break; default: unsupportedPlatform(osName, osArch); } resourceName = "/" + platform + "/" + library + "." + fileExtension; return resourceName; }
/** * Load the shared libraries. */ public static void loadLibrary() { try { File tmpDir = new File(System.getProperty("java.io.tmpdir")); File dir = File.createTempFile("libjsass-", ".d", tmpDir); dir.delete(); dir.mkdir(); dir.deleteOnExit(); if (System.getProperty("os.name").toLowerCase().startsWith("win")) { System.load(saveLibrary(dir, "sass")); } System.load(saveLibrary(dir, "jsass")); } catch (Exception exception) { System.err.println(exception.getMessage()); exception.printStackTrace(System.err); throw new RuntimeException(exception); } }
/** * Save the right shared library in the given temporary directory. */ static String saveLibrary(File dir, String library) throws IOException { library = "lib" + library; URL libraryResource = findLibraryResource(library); String basename = FilenameUtils.getName(libraryResource.getPath()); File file = new File(dir, basename); file.deleteOnExit(); try ( InputStream in = libraryResource.openStream(); OutputStream out = new FileOutputStream(file) ) { IOUtils.copy(in, out); } return file.getAbsolutePath(); } }
unsupportedPlatform(osName, osArch);