-
Notifications
You must be signed in to change notification settings - Fork 62
/
Library.java
executable file
·457 lines (418 loc) · 17.2 KB
/
Library.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*******************************************************************************
* Copyright (C) 2009-2011 FuseSource Corp.
* Copyright (c) 2000, 2009 IBM Corporation and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.fusesource.hawtjni.runtime;
import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
/**
* Used to find and load a JNI library, eventually after having extracted it.
*
* It will search for the library in order at the following locations:
* <ol>
* <li> in the custom library path: If the "<code>library.${name}.path</code>" System property is set to a directory,
* subdirectories are searched:
* <ol>
* <li> "<code>${platform}/${arch}</code>"
* <li> "<code>${platform}</code>"
* <li> "<code>${os}</code>"
* <li> "<code></code>"
* </ol>
* for 2 namings of the library:
* <ol>
* <li> as "<code>${name}-${version}</code>" library name if the version can be determined.
* <li> as "<code>${name}</code>" library name
* </ol>
* <li> system library path: This is where the JVM looks for JNI libraries by default.
* <ol>
* <li> as "<code>${name}${bit-model}-${version}</code>" library name if the version can be determined.
* <li> as "<code>${name}-${version}</code>" library name if the version can be determined.
* <li> as "<code>${name}</code>" library name
* </ol>
* <li> classpath path: If the JNI library can be found on the classpath, it will get extracted
* and then loaded. This way you can embed your JNI libraries into your packaged JAR files.
* They are looked up as resources in this order:
* <ol>
* <li> "<code>META-INF/native/${platform}/${arch}/${library[-version]}</code>": Store your library here if you want to embed
* more than one platform JNI library on different processor archs in the jar.
* <li> "<code>META-INF/native/${platform}/${library[-version]}</code>": Store your library here if you want to embed more
* than one platform JNI library in the jar.
* <li> "<code>META-INF/native/${os}/${library[-version]}</code>": Store your library here if you want to embed more
* than one platform JNI library in the jar but don't want to take bit model into account.
* <li> "<code>META-INF/native/${library[-version]}</code>": Store your library here if your JAR is only going to embedding one
* platform library.
* </ol>
* The file extraction is attempted until it succeeds in the following directories.
* <ol>
* <li> The directory pointed to by the "<code>library.${name}.path</code>" System property (if set)
* <li> a temporary directory (uses the "<code>java.io.tmpdir</code>" System property)
* </ol>
* </ol>
*
* where:
* <ul>
* <li>"<code>${name}</code>" is the name of library
* <li>"<code>${version}</code>" is the value of "<code>library.${name}.version</code>" System property if set.
* Otherwise it is set to the ImplementationVersion property of the JAR's Manifest</li>
* <li>"<code>${os}</code>" is your operating system, for example "<code>osx</code>", "<code>linux</code>", or "<code>windows</code>"</li>
* <li>"<code>${bit-model}</code>" is "<code>64</code>" if the JVM process is a 64 bit process, otherwise it's "<code>32</code>" if the
* JVM is a 32 bit process</li>
* <li>"<code>${arch}</code>" is the architecture for the processor, for example "<code>amd64</code>" or "<code>sparcv9</code>"</li>
* <li>"<code>${platform}</code>" is "<code>${os}${bit-model}</code>", for example "<code>linux32</code>" or "<code>osx64</code>" </li>
* <li>"<code>${library[-version]}</code>": is the normal jni library name for the platform (eventually with <code>-${version}</code>) suffix.
* For example "<code>${name}.dll</code>" on
* windows, "<code>lib${name}.jnilib</code>" on OS X, and "<code>lib${name}.so</code>" on linux</li>
* </ul>
*
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
* @see System#mapLibraryName(String)
*/
public class Library {
static final String SLASH = System.getProperty("file.separator");
final private String name;
final private String version;
final private ClassLoader classLoader;
private boolean loaded;
private String nativeLibraryPath;
private URL nativeLibrarySourceUrl;
public Library(String name) {
this(name, null, null);
}
public Library(String name, Class<?> clazz) {
this(name, version(clazz), clazz.getClassLoader());
}
public Library(String name, String version) {
this(name, version, null);
}
public Library(String name, String version, ClassLoader classLoader) {
if( name == null ) {
throw new IllegalArgumentException("name cannot be null");
}
this.name = name;
this.version = version;
this.classLoader= classLoader;
}
private static String version(Class<?> clazz) {
try {
return clazz.getPackage().getImplementationVersion();
} catch (Throwable e) {
}
return null;
}
/**
* Get the path to the native library loaded.
* @return the path (should not be null once the library is loaded)
* @since 1.16
*/
public String getNativeLibraryPath() {
return nativeLibraryPath;
}
/**
* Get the URL to the native library source that has been extracted (if it was extracted).
* @return the url to the source (in classpath)
* @since 1.16
*/
public URL getNativeLibrarySourceUrl() {
return nativeLibrarySourceUrl;
}
public static String getOperatingSystem() {
String name = System.getProperty("os.name").toLowerCase().trim();
if( name.startsWith("linux") ) {
return "linux";
}
if( name.startsWith("mac os x") ) {
return "osx";
}
if( name.startsWith("win") ) {
return "windows";
}
return name.replaceAll("\\W+", "_");
}
public static String getPlatform() {
return getOperatingSystem()+getBitModel();
}
public static int getBitModel() {
String prop = System.getProperty("sun.arch.data.model");
if (prop == null) {
prop = System.getProperty("com.ibm.vm.bitmode");
}
if( prop!=null ) {
return Integer.parseInt(prop);
}
return -1; // we don't know..
}
/**
* Load the native library.
*/
synchronized public void load() {
if( loaded ) {
return;
}
doLoad();
loaded = true;
}
private void doLoad() {
/* Perhaps a custom version is specified */
String version = System.getProperty("library."+name+".version");
if (version == null) {
version = this.version;
}
ArrayList<Throwable> errors = new ArrayList<Throwable>();
String[] specificDirs = getSpecificSearchDirs();
String libFilename = map(name);
String versionlibFilename = (version == null) ? null : map(name + "-" + version);
/* Try loading library from a custom library path */
String customPath = System.getProperty("library."+name+".path");
if (customPath != null) {
for ( String dir: specificDirs ) {
if( version!=null && load(errors, file(customPath, dir, versionlibFilename)) )
return;
if( load(errors, file(customPath, dir, libFilename)) )
return;
}
}
/* Try loading library from java library path */
if( version!=null && loadLibrary(errors, name + getBitModel() + "-" + version) )
return;
if( version!=null && loadLibrary(errors, name + "-" + version) )
return;
if( loadLibrary(errors, name) )
return;
/* Try extracting the library from the jar */
if( classLoader!=null ) {
String targetLibName = version != null ? versionlibFilename : libFilename;
for ( String dir: specificDirs ) {
if( version!=null && extractAndLoad(errors, customPath, dir, versionlibFilename, targetLibName) )
return;
if( extractAndLoad(errors, customPath, dir, libFilename, targetLibName) )
return;
}
}
/* Failed to find the library */
UnsatisfiedLinkError e = new UnsatisfiedLinkError("Could not load library. Reasons: " + errors.toString());
try {
Method method = Throwable.class.getMethod("addSuppressed", Throwable.class);
for (Throwable t : errors) {
method.invoke(e, t);
}
} catch (Throwable ignore) {
}
throw e;
}
@Deprecated
final public String getArchSpecifcResourcePath() {
return getArchSpecificResourcePath();
}
final public String getArchSpecificResourcePath() {
return "META-INF/native/"+ getPlatform() + "/" + System.getProperty("os.arch") + "/" +map(name);
}
@Deprecated
final public String getOperatingSystemSpecifcResourcePath() {
return getOperatingSystemSpecificResourcePath();
}
final public String getOperatingSystemSpecificResourcePath() {
return getPlatformSpecificResourcePath(getOperatingSystem());
}
@Deprecated
final public String getPlatformSpecifcResourcePath() {
return getPlatformSpecificResourcePath();
}
final public String getPlatformSpecificResourcePath() {
return getPlatformSpecificResourcePath(getPlatform());
}
@Deprecated
final public String getPlatformSpecifcResourcePath(String platform) {
return getPlatformSpecificResourcePath(platform);
}
final public String getPlatformSpecificResourcePath(String platform) {
return "META-INF/native/"+platform+"/"+map(name);
}
@Deprecated
final public String getResorucePath() {
return getResourcePath();
}
final public String getResourcePath() {
return "META-INF/native/"+map(name);
}
final public String getLibraryFileName() {
return map(name);
}
/**
* Search directories for library:<ul>
* <li><code>${platform}/${arch}</code> to enable platform JNI library for different processor archs</li>
* <li><code>${platform}</code> to enable platform JNI library</li>
* <li><code>${os}</code> to enable OS JNI library</li>
* <li>no directory</li>
* </ul>
* @return the list
* @since 1.15
*/
final public String[] getSpecificSearchDirs() {
return new String[] {
getPlatform() + "/" + System.getProperty("os.arch"),
getPlatform(),
getOperatingSystem()
};
}
private boolean extractAndLoad(ArrayList<Throwable> errors, String customPath, String dir, String libName, String targetLibName) {
String resourcePath = "META-INF/native/" + ( dir == null ? "" : (dir + '/')) + libName;
URL resource = classLoader.getResource(resourcePath);
if( resource !=null ) {
int idx = targetLibName.lastIndexOf('.');
String prefix = targetLibName.substring(0, idx)+"-";
String suffix = targetLibName.substring(idx);
// Use the user provided path,
// then fallback to the java temp directory,
// and last, use the user home folder
for (File path : Arrays.asList(
customPath != null ? file(customPath) : null,
file(System.getProperty("java.io.tmpdir")),
file(System.getProperty("user.home"), ".hawtjni", name))) {
if( path!=null ) {
// Try to extract it to the custom path...
File target = extract(errors, resource, prefix, suffix, path);
if( target!=null ) {
if( load(errors, target) ) {
nativeLibrarySourceUrl = resource;
return true;
}
}
}
}
}
return false;
}
private File file(String ...paths) {
File rc = null ;
for (String path : paths) {
if( rc == null ) {
rc = new File(path);
} else if( path != null ) {
rc = new File(rc, path);
}
}
return rc;
}
private String map(String libName) {
/*
* libraries in the Macintosh use the extension .jnilib but the some
* VMs map to .dylib.
*/
libName = System.mapLibraryName(libName);
String ext = ".dylib";
if (libName.endsWith(ext)) {
libName = libName.substring(0, libName.length() - ext.length()) + ".jnilib";
}
return libName;
}
private File extract(ArrayList<Throwable> errors, URL source, String prefix, String suffix, File directory) {
File target = null;
directory = directory.getAbsoluteFile();
if (!directory.exists()) {
if (!directory.mkdirs()) {
errors.add(new IOException("Unable to create directory: " + directory));
return null;
}
}
try {
FileOutputStream os = null;
InputStream is = null;
try {
target = File.createTempFile(prefix, suffix, directory);
is = source.openStream();
if (is != null) {
byte[] buffer = new byte[4096];
os = new FileOutputStream(target);
int read;
while ((read = is.read(buffer)) != -1) {
os.write(buffer, 0, read);
}
chmod755(target);
}
target.deleteOnExit();
return target;
} finally {
close(os);
close(is);
}
} catch (Throwable e) {
IOException io;
if( target!=null ) {
target.delete();
io = new IOException("Unable to extract library from " + source + " to " + target);
} else {
io = new IOException("Unable to create temporary file in " + directory);
}
io.initCause(e);
errors.add(io);
}
return null;
}
static private void close(Closeable file) {
if(file!=null) {
try {
file.close();
} catch (Exception ignore) {
}
}
}
private void chmod755(File file) {
if (getPlatform().startsWith("windows"))
return;
// Use Files.setPosixFilePermissions if we are running Java 7+ to avoid forking the JVM for executing chmod
try {
ClassLoader classLoader = getClass().getClassLoader();
// Check if the PosixFilePermissions exists in the JVM, if not this will throw a ClassNotFoundException
Class<?> posixFilePermissionsClass = classLoader.loadClass("java.nio.file.attribute.PosixFilePermissions");
// Set <PosixFilePermission> permissionSet = PosixFilePermissions.fromString("rwxr-xr-x")
Method fromStringMethod = posixFilePermissionsClass.getMethod("fromString", String.class);
Object permissionSet = fromStringMethod.invoke(null, "rwxr-xr-x");
// Path path = file.toPath()
Object path = file.getClass().getMethod("toPath").invoke(file);
// Files.setPosixFilePermissions(path, permissionSet)
Class<?> pathClass = classLoader.loadClass("java.nio.file.Path");
Class<?> filesClass = classLoader.loadClass("java.nio.file.Files");
Method setPosixFilePermissionsMethod = filesClass.getMethod("setPosixFilePermissions", pathClass, Set.class);
setPosixFilePermissionsMethod.invoke(null, path, permissionSet);
} catch (Throwable ignored) {
// Fallback to starting a new process
try {
Runtime.getRuntime().exec(new String[]{"chmod", "755", file.getCanonicalPath()}).waitFor();
} catch (Throwable e) {
}
}
}
private boolean load(ArrayList<Throwable> errors, File lib) {
try {
System.load(lib.getPath());
nativeLibraryPath = lib.getPath();
return true;
} catch (UnsatisfiedLinkError e) {
LinkageError le = new LinkageError("Unable to load library from " + lib);
le.initCause(e);
errors.add(le);
}
return false;
}
private boolean loadLibrary(ArrayList<Throwable> errors, String lib) {
try {
System.loadLibrary(lib);
nativeLibraryPath = "java.library.path,sun.boot.library.pathlib:" + lib;
return true;
} catch (UnsatisfiedLinkError e) {
LinkageError le = new LinkageError("Unable to load library " + lib);
le.initCause(e);
errors.add(le);
}
return false;
}
}