-
Notifications
You must be signed in to change notification settings - Fork 218
/
JLineNativeLoader.java
409 lines (368 loc) · 14.7 KB
/
JLineNativeLoader.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
/*
* Copyright (c) 2023, the original author(s).
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.jline.nativ;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Set the system properties, library.jline.path, library.jline.name,
* appropriately so that jline can find *.dll, *.jnilib and
* *.so files, according to the current OS (win, linux, mac).
* <p>
* The library files are automatically extracted from this project's package
* (JAR).
* <p>
* usage: call {@link #initialize()} before using jline.
*/
public class JLineNativeLoader {
private static final Logger logger = Logger.getLogger("org.jline");
private static boolean loaded = false;
private static String nativeLibraryPath;
private static String nativeLibrarySourceUrl;
/**
* Loads jline native library.
*
* @return True if jline native library is successfully loaded; false
* otherwise.
*/
public static synchronized boolean initialize() {
// only cleanup before the first extract
if (!loaded) {
Thread cleanup = new Thread(JLineNativeLoader::cleanup, "cleanup");
cleanup.setPriority(Thread.MIN_PRIORITY);
cleanup.setDaemon(true);
cleanup.start();
}
try {
loadJLineNativeLibrary();
} catch (Exception e) {
throw new RuntimeException("Unable to load jline native library: " + e.getMessage(), e);
}
return loaded;
}
public static String getNativeLibraryPath() {
return nativeLibraryPath;
}
public static String getNativeLibrarySourceUrl() {
return nativeLibrarySourceUrl;
}
private static File getTempDir() {
return new File(System.getProperty("jline.tmpdir", System.getProperty("java.io.tmpdir")));
}
/**
* Deleted old native libraries e.g. on Windows the DLL file is not removed
* on VM-Exit (bug #80)
*/
static void cleanup() {
String tempFolder = getTempDir().getAbsolutePath();
File dir = new File(tempFolder);
File[] nativeLibFiles = dir.listFiles(new FilenameFilter() {
private final String searchPattern = "jlinenative-" + getVersion();
public boolean accept(File dir, String name) {
return name.startsWith(searchPattern) && !name.endsWith(".lck");
}
});
if (nativeLibFiles != null) {
for (File nativeLibFile : nativeLibFiles) {
File lckFile = new File(nativeLibFile.getAbsolutePath() + ".lck");
if (!lckFile.exists()) {
try {
nativeLibFile.delete();
} catch (SecurityException e) {
logger.log(Level.INFO, "Failed to delete old native lib" + e.getMessage(), e);
}
}
}
}
}
private static int readNBytes(InputStream in, byte[] b) throws IOException {
int n = 0;
int len = b.length;
while (n < len) {
int count = in.read(b, n, len - n);
if (count <= 0) break;
n += count;
}
return n;
}
private static String contentsEquals(InputStream in1, InputStream in2) throws IOException {
byte[] buffer1 = new byte[8192];
byte[] buffer2 = new byte[8192];
int numRead1;
int numRead2;
while (true) {
numRead1 = readNBytes(in1, buffer1);
numRead2 = readNBytes(in2, buffer2);
if (numRead1 > 0) {
if (numRead2 <= 0) {
return "EOF on second stream but not first";
}
if (numRead2 != numRead1) {
return "Read size different (" + numRead1 + " vs " + numRead2 + ")";
}
// Otherwise same number of bytes read
if (!Arrays.equals(buffer1, buffer2)) {
return "Content differs";
}
// Otherwise same bytes read, so continue ...
} else {
// Nothing more in stream 1 ...
if (numRead2 > 0) {
return "EOF on first stream but not second";
} else {
return null;
}
}
}
}
/**
* Extracts and loads the specified library file to the target folder
*
* @param libFolderForCurrentOS Library path.
* @param libraryFileName Library name.
* @param targetFolder Target folder.
* @return
*/
private static boolean extractAndLoadLibraryFile(
String libFolderForCurrentOS, String libraryFileName, String targetFolder) {
String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName;
// Include architecture name in temporary filename in order to avoid conflicts
// when multiple JVMs with different architectures running at the same time
String uuid = randomUUID();
String extractedLibFileName = String.format("jlinenative-%s-%s-%s", getVersion(), uuid, libraryFileName);
String extractedLckFileName = extractedLibFileName + ".lck";
File extractedLibFile = new File(targetFolder, extractedLibFileName);
File extractedLckFile = new File(targetFolder, extractedLckFileName);
try {
// Extract a native library file into the target directory
try (InputStream in = JLineNativeLoader.class.getResourceAsStream(nativeLibraryFilePath)) {
if (!extractedLckFile.exists()) {
new FileOutputStream(extractedLckFile).close();
}
try (OutputStream out = new FileOutputStream(extractedLibFile)) {
copy(in, out);
}
} finally {
// Delete the extracted lib file on JVM exit.
extractedLibFile.deleteOnExit();
extractedLckFile.deleteOnExit();
}
// Set executable (x) flag to enable Java to load the native library
extractedLibFile.setReadable(true);
extractedLibFile.setWritable(true);
extractedLibFile.setExecutable(true);
// Check whether the contents are properly copied from the resource folder
try (InputStream nativeIn = JLineNativeLoader.class.getResourceAsStream(nativeLibraryFilePath)) {
try (InputStream extractedLibIn = new FileInputStream(extractedLibFile)) {
String eq = contentsEquals(nativeIn, extractedLibIn);
if (eq != null) {
throw new RuntimeException(String.format(
"Failed to write a native library file at %s because %s", extractedLibFile, eq));
}
}
}
// Load library
if (loadNativeLibrary(extractedLibFile)) {
nativeLibrarySourceUrl = JLineNativeLoader.class
.getResource(nativeLibraryFilePath)
.toExternalForm();
return true;
}
} catch (IOException e) {
log(Level.WARNING, "Unable to load JLine's native library", e);
}
return false;
}
private static String randomUUID() {
return Long.toHexString(new Random().nextLong());
}
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int n;
while ((n = in.read(buf)) > 0) {
out.write(buf, 0, n);
}
}
/**
* Loads native library using the given path and name of the library.
*
* @param libPath Path of the native library.
* @return True for successfully loading; false otherwise.
*/
private static boolean loadNativeLibrary(File libPath) {
if (libPath.exists()) {
try {
String path = libPath.getAbsolutePath();
System.load(path);
nativeLibraryPath = path;
return true;
} catch (UnsatisfiedLinkError e) {
log(
Level.WARNING,
"Failed to load native library:" + libPath.getName() + ". osinfo: "
+ OSInfo.getNativeLibFolderPathForCurrentOS(),
e);
return false;
}
} else {
return false;
}
}
/**
* Loads jline library using given path and name of the library.
*
* @throws
*/
private static void loadJLineNativeLibrary() throws Exception {
if (loaded) {
return;
}
List<String> triedPaths = new ArrayList<String>();
// Try loading library from library.jline.path library path */
String jlineNativeLibraryPath = System.getProperty("library.jline.path");
String jlineNativeLibraryName = System.getProperty("library.jline.name");
if (jlineNativeLibraryName == null) {
jlineNativeLibraryName = System.mapLibraryName("jlinenative");
assert jlineNativeLibraryName != null;
if (jlineNativeLibraryName.endsWith(".dylib")) {
jlineNativeLibraryName = jlineNativeLibraryName.replace(".dylib", ".jnilib");
}
}
if (jlineNativeLibraryPath != null) {
String withOs = jlineNativeLibraryPath + "/" + OSInfo.getNativeLibFolderPathForCurrentOS();
if (loadNativeLibrary(new File(withOs, jlineNativeLibraryName))) {
loaded = true;
return;
} else {
triedPaths.add(withOs);
}
if (loadNativeLibrary(new File(jlineNativeLibraryPath, jlineNativeLibraryName))) {
loaded = true;
return;
} else {
triedPaths.add(jlineNativeLibraryPath);
}
}
// Load the os-dependent library from the jar file
String packagePath = JLineNativeLoader.class.getPackage().getName().replace('.', '/');
jlineNativeLibraryPath = String.format("/%s/%s", packagePath, OSInfo.getNativeLibFolderPathForCurrentOS());
boolean hasNativeLib = hasResource(jlineNativeLibraryPath + "/" + jlineNativeLibraryName);
if (hasNativeLib) {
// temporary library folder
String tempFolder = getTempDir().getAbsolutePath();
// Try extracting the library from jar
if (extractAndLoadLibraryFile(jlineNativeLibraryPath, jlineNativeLibraryName, tempFolder)) {
loaded = true;
return;
} else {
triedPaths.add(jlineNativeLibraryPath);
}
}
// As a last resort try from java.library.path
String javaLibraryPath = System.getProperty("java.library.path", "");
for (String ldPath : javaLibraryPath.split(File.pathSeparator)) {
if (ldPath.isEmpty()) {
continue;
}
if (loadNativeLibrary(new File(ldPath, jlineNativeLibraryName))) {
loaded = true;
return;
} else {
triedPaths.add(ldPath);
}
}
throw new Exception(String.format(
"No native library found for os.name=%s, os.arch=%s, paths=[%s]",
OSInfo.getOSName(), OSInfo.getArchName(), join(triedPaths, File.pathSeparator)));
}
private static boolean hasResource(String path) {
return JLineNativeLoader.class.getResource(path) != null;
}
/**
* @return The major version of the jline library.
*/
public static int getMajorVersion() {
String[] c = getVersion().split("\\.");
return (c.length > 0) ? Integer.parseInt(c[0]) : 1;
}
/**
* @return The minor version of the jline library.
*/
public static int getMinorVersion() {
String[] c = getVersion().split("\\.");
return (c.length > 1) ? Integer.parseInt(c[1]) : 0;
}
/**
* @return The version of the jline library.
*/
public static String getVersion() {
URL versionFile = JLineNativeLoader.class.getResource("/META-INF/maven/org.jline/jline-native/pom.properties");
String version = "unknown";
try {
if (versionFile != null) {
Properties versionData = new Properties();
versionData.load(versionFile.openStream());
version = versionData.getProperty("version", version);
version = version.trim().replaceAll("[^0-9.]", "");
}
} catch (IOException e) {
log(Level.WARNING, "Unable to load jline-native version", e);
}
return version;
}
private static String join(List<String> list, String separator) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String item : list) {
if (first) first = false;
else sb.append(separator);
sb.append(item);
}
return sb.toString();
}
private static void log(Level level, String message, Throwable t) {
if (logger.isLoggable(level)) {
if (logger.isLoggable(Level.FINE)) {
logger.log(level, message, t);
} else {
logger.log(level, message + " (caused by: " + t + ", enable debug logging for stacktrace)");
}
}
}
}