| 1 |
package org.zeromq.tools; |
| 2 |
|
| 3 |
import org.scijava.nativelib.NativeLibraryUtil; |
| 4 |
import org.scijava.nativelib.NativeLoader; |
| 5 |
|
| 6 |
import java.io.File; |
| 7 |
import java.io.IOException; |
| 8 |
import java.net.URL; |
| 9 |
import java.net.URLDecoder; |
| 10 |
import java.util.Enumeration; |
| 11 |
import java.util.HashSet; |
| 12 |
import java.util.Map; |
| 13 |
import java.util.Set; |
| 14 |
import java.util.jar.JarEntry; |
| 15 |
import java.util.jar.JarFile; |
| 16 |
|
| 17 |
public class ZmqNativeLoader { |
| 18 |
|
| 19 |
private static final Set<String> loadedLibraries = new HashSet<>(); |
| 20 |
|
| 21 |
private static final Set<String> potentialDlls = new HashSet<>(); |
| 22 |
|
| 23 |
static { |
| 24 |
if (System.getProperty("os.name").toLowerCase().contains("windows")) { |
| 25 |
// Collect potential DLLs to be match when loading the libraries |
| 26 |
|
| 27 |
// 1. Lookup the java.library.path |
| 28 |
String javaLibraryPath = System.getProperty("java.library.path"); |
| 29 |
if (javaLibraryPath != null) { |
| 30 |
final String[] paths = javaLibraryPath.split(File.pathSeparator); |
| 31 |
for (String path : paths) { |
| 32 |
File folder = new File(path); |
| 33 |
if (folder.exists()) { |
| 34 |
for (final File fileEntry : folder.listFiles()) { |
| 35 |
if (fileEntry.isFile() && fileEntry.getName().indexOf(".dll") != -1) { |
| 36 |
potentialDlls.add(fileEntry.getName().substring(0, fileEntry.getName().indexOf("."))); |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// 2. Lookup the jar files |
| 44 |
try { |
| 45 |
String path = "natives/" + NativeLibraryUtil.getArchitecture().name().toLowerCase() + "/"; |
| 46 |
URL url = ZmqNativeLoader.class.getClassLoader().getResource(path); |
| 47 |
if (url != null && url.getProtocol().equals("jar")) { |
| 48 |
String jarPath = url.getPath().substring(5, url.getPath().indexOf("!")); // Strip out only the JAR file |
| 49 |
JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8")); |
| 50 |
Enumeration<JarEntry> entries = jar.entries(); // Gives ALL entries in jar |
| 51 |
while (entries.hasMoreElements()) { |
| 52 |
String name = entries.nextElement().getName(); |
| 53 |
// Filter DLLs according to the path |
| 54 |
if (name.startsWith(path) && name.indexOf(".dll") != -1) { |
| 55 |
potentialDlls.add(name.substring(path.length(), name.length() - 4)); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} catch (IOException e) { |
| 60 |
// Ignore! |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public static void loadLibraries(Map<String, Boolean> libraries) { |
| 66 |
for (String libraryName : libraries.keySet()) { |
| 67 |
boolean libraryLoaded = false; |
| 68 |
if (System.getProperty("os.name").toLowerCase().contains("windows")) { |
| 69 |
// Windows DLL names are not as standardized as dynamic library names are under linux or osx. |
| 70 |
// Therefore we inspect potential DLLs to find a match. |
| 71 |
|
| 72 |
// Find all other libraries that contains this library's name |
| 73 |
Set<String> conflictingLibrariesNames = new HashSet<>(); |
| 74 |
for (String otherLibraryName : libraries.keySet()) { |
| 75 |
if (otherLibraryName.contains(libraryName) && !otherLibraryName.equals(libraryName)) { |
| 76 |
conflictingLibrariesNames.add(otherLibraryName); |
| 77 |
} |
| 78 |
} |
| 79 |
// Build regexes to exclude any prefixes and suffixes of libraries that contain this library's name |
| 80 |
String regexForbiddenPrefix = ""; |
| 81 |
String regexForbiddenSuffix = ""; |
| 82 |
if (conflictingLibrariesNames.size() > 0) { |
| 83 |
for (String conflictingLibraryName : conflictingLibrariesNames) { |
| 84 |
String prefix = conflictingLibraryName.substring(0, conflictingLibraryName.indexOf(libraryName)); |
| 85 |
if (!"".equals(prefix)) { |
| 86 |
regexForbiddenPrefix += prefix + "|"; |
| 87 |
} |
| 88 |
} |
| 89 |
if (!"".equals(regexForbiddenPrefix)) { |
| 90 |
regexForbiddenPrefix = regexForbiddenPrefix.substring(0, regexForbiddenPrefix.length() - 1); |
| 91 |
regexForbiddenPrefix = "(?<!" + regexForbiddenPrefix + ")"; |
| 92 |
} |
| 93 |
|
| 94 |
for (String conflictingLibraryName : conflictingLibrariesNames) { |
| 95 |
String suffix = conflictingLibraryName.substring(conflictingLibraryName.indexOf(libraryName) + libraryName.length()); |
| 96 |
if (!"".equals(suffix)) { |
| 97 |
regexForbiddenSuffix += suffix + "|"; |
| 98 |
} |
| 99 |
} |
| 100 |
if (!"".equals(regexForbiddenSuffix)) { |
| 101 |
regexForbiddenSuffix = regexForbiddenSuffix.substring(0, regexForbiddenSuffix.length() - 1); |
| 102 |
regexForbiddenSuffix = "(?!" + regexForbiddenSuffix + ")"; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
for (String fileName : potentialDlls) { |
| 107 |
String regex = "^(?:lib)?.*" + regexForbiddenPrefix + libraryName + regexForbiddenSuffix + ".*$"; |
| 108 |
if (fileName.matches(regex)) { |
| 109 |
loadLibrary(fileName, libraries.get(libraryName)); |
| 110 |
libraryLoaded = true; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
if (!libraryLoaded) { |
| 115 |
loadLibrary(libraryName, libraries.get(libraryName)); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
public static void loadLibrary(String libname, boolean optional) { |
| 121 |
if (!loadedLibraries.contains(libname)) { |
| 122 |
try { |
| 123 |
NativeLoader.loadLibrary(libname); |
| 124 |
} catch (IOException e) { |
| 125 |
if (optional) { |
| 126 |
System.err |
| 127 |
.println("[WARN] " + e.getMessage() + " from jar. Assuming it is installed on the system."); |
| 128 |
} else { |
| 129 |
System.err.println("[FATAL] " + e.getMessage() + " from jar. Dependency is required!"); |
| 130 |
System.exit(-1); |
| 131 |
} |
| 132 |
} |
| 133 |
loadedLibraries.add(libname); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
} |