forked from eclipse-efx/efxclipse-drift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NativeLoader.java
89 lines (70 loc) · 2.27 KB
/
NativeLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package org.eclipse.fx.drift.internal;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class NativeLoader {
private static String OS = null;
public static String getOsName() {
if (OS == null) {
OS = System.getProperty("os.name");
}
return OS;
}
public static boolean isWindows() {
return getOsName().toLowerCase().contains("windows");
}
public static boolean isLinux() {
return getOsName().toLowerCase().contains("linux");
}
public static boolean isMacOs() {
return getOsName().toLowerCase().contains("mac");
}
/*package*/ static boolean osgi = false;
private static boolean useJavaLibraryPath = Boolean.getBoolean("driftfx.use.java.library.path");
public static void loadLibrary(Class<?> context, String libname) {
if (useJavaLibraryPath || osgi) {
// osgi will take care of it
System.loadLibrary(libname);
}
else {
// we need to make it happen
String filename = getFilename(libname);
Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir"));
Path extractPath = tmpDir.resolve("driftfx").resolve(filename);
String resourceName = "/native/" + filename;
URL url = context.getResource(resourceName);
System.err.println("Resource Lookup: name: " + resourceName + ", context: " + context + " => " + url);
try (InputStream in = context.getResourceAsStream("/native/" + filename)) {
extract(in, extractPath);
} catch (IOException e) {
e.printStackTrace();
}
System.load(extractPath.toString());
}
}
private static Path extract(InputStream in, Path extractPath) throws IOException {
Files.createDirectories(extractPath.getParent());
if (!Files.exists(extractPath)) {
long bytes = Files.copy(in, extractPath);
if (bytes == 0) {
throw new IOException("library has no size!");
}
}
return extractPath;
}
private static String getFilename(String libname) {
if (isWindows()) {
return libname + ".dll";
}
if (isLinux()) {
return "lib" + libname + ".so";
}
if (isMacOs()) {
return "lib" + libname + ".dylib";
}
throw new RuntimeException("os not supported: " + getOsName());
}
}