-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathPlatform.java
More file actions
845 lines (755 loc) · 30.5 KB
/
Platform.java
File metadata and controls
845 lines (755 loc) · 30.5 KB
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/*
* BridJ - Dynamic and blazing-fast native interop for Java.
* http://bridj.googlecode.com/
*
* Copyright (c) 2010-2015, Olivier Chafik (http://ochafik.com/)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Olivier Chafik nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY OLIVIER CHAFIK AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.bridj;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import org.bridj.util.ProcessUtils;
import org.bridj.util.StringUtils;
/**
* Information about the execution platform (OS, architecture, native sizes...)
* and platform-specific actions.
* <ul>
* <li>To know if the JVM platform is 32 bits or 64 bits, use
* {@link Platform#is64Bits()}
* </li><li>To know if the OS is an Unix-like system, use
* {@link Platform#isUnix()}
* </li><li>To open files and URLs in a platform-specific way, use
* {@link Platform#open(File)}, {@link Platform#open(URL)}, {@link Platform#show(File)}
* </li></ul>
*
* @author ochafik
*/
public class Platform {
static final String osName = System.getProperty("os.name", "");
private static boolean inited;
static final String BridJLibraryName = "bridj";
public static final int POINTER_SIZE,
WCHAR_T_SIZE,
SIZE_T_SIZE,
TIME_T_SIZE,
CLONG_SIZE;
/*interface FunInt {
int apply();
}
static int tryInt(FunInt f, int defaultValue) {
try {
return f.apply();
} catch (Throwable th) {
return defaultValue;
}
}*/
static final ClassLoader systemClassLoader;
public static ClassLoader getClassLoader() {
return getClassLoader(BridJ.class);
}
public static ClassLoader getClassLoader(Class<?> cl) {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (contextClassLoader != null) {
return contextClassLoader;
}
ClassLoader classLoader = cl == null ? null : cl.getClassLoader();
return classLoader == null ? systemClassLoader : classLoader;
}
public static InputStream getResourceAsStream(String path) {
URL url = getResource(path);
try {
return url != null ? url.openStream() : null;
} catch (IOException ex) {
if (BridJ.verbose) {
BridJ.warning("Failed to get resource '" + path + "'", ex);
}
return null;
}
}
public static URL getResource(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
URL in = BridJ.class.getResource(path);
if (in != null) {
return in;
}
path = path.substring(1);
ClassLoader[] cls = {
BridJ.class.getClassLoader(),
Thread.currentThread().getContextClassLoader(),
systemClassLoader
};
for (ClassLoader cl : cls) {
if (cl != null && (in = cl.getResource(path)) != null) {
return in;
}
}
return null;
}
/*
public static class utsname {
public final String sysname, nodename, release, version, machine;
public utsname(String sysname, String nodename, String release, String version, String machine) {
this.sysname = sysname;
this.nodename = nodename;
this.release = release;
this.version = version;
this.machine = machine;
}
public String toString() {
StringBuilder b = new StringBuilder("{\n");
b.append("\tsysname: \"").append(sysname).append("\",\n");
b.append("\tnodename: \"").append(nodename).append("\",\n");
b.append("\trelease: \"").append(release).append("\",\n");
b.append("\tversion: \"").append(version).append("\",\n");
b.append("\tmachine: \"").append(machine).append("\"\n");
return b.append("}").toString();
}
}
public static native utsname uname();
*/
static final List<String> embeddedLibraryResourceRoots = new ArrayList<String>();
/**
* BridJ is able to automatically extract native binaries bundled in the
* application's JARs, using a customizable root path and a predefined
* architecture-dependent subpath. This method adds an alternative root path
* to the search list.<br>
* For instance, if you want to load library "mylib" and call
* <code>addEmbeddedLibraryResourceRoot("my/company/lib/")</code>, BridJ
* will look for library in the following locations :
* <ul>
* <li>"my/company/lib/darwin_universal/libmylib.dylib" on MacOS X (or
* darwin_x86, darwin_x64, darwin_ppc if the binary is not universal)</li>
* <li>"my/company/lib/win32/mylib.dll" on Windows (use win64 on 64 bits
* architectures)</li>
* <li>"my/company/lib/linux_x86/libmylib.so" on Linux (use linux_x64 on 64
* bits architectures)</li>
* <li>"my/company/lib/sunos_x86/libmylib.so" on Solaris (use sunos_x64 /
* sunos_sparc on other architectures)</li>
* <li>"lib/armeabi/libmylib.so" on Android (for Android-specific reasons,
* only the "lib" sub-path can effectively contain loadable binaries)</li>
* </ul>
* For other platforms or for an updated list of supported platforms, please
* have a look at BridJ's JAR contents (under "org/bridj/lib") and/or to its
* source tree, browsable online.
*/
public static synchronized void addEmbeddedLibraryResourceRoot(String root) {
embeddedLibraryResourceRoots.add(0, root);
}
static Set<File> temporaryExtractedLibraryCanonicalFiles = Collections.synchronizedSet(new LinkedHashSet<File>());
static void addTemporaryExtractedLibraryFileToDeleteOnExit(File file) throws IOException {
File canonicalFile = file.getCanonicalFile();
// Give a chance to NativeLibrary.release() to delete the file :
temporaryExtractedLibraryCanonicalFiles.add(canonicalFile);
// Ask Java to delete the file upon exit if it still exists
canonicalFile.deleteOnExit();
}
private static final String arch;
private static boolean is64Bits;
private static File extractedLibrariesTempDir;
static {
arch = System.getProperty("os.arch");
{
String dataModel = System.getProperty("sun.arch.data.model", System.getProperty("com.ibm.vm.bitmode"));
// TODO(ochafik): Look at sun.cpu.endian=little for ppc64le
// System.err.println("# os.arch = " + arch);
// System.err.println("# sun.arch.data.model = " + dataModel);
// try {
// System.getProperties().store(System.out, "");
// } catch (Exception ex) {
// throw new RuntimeException(ex);
// }
if ("32".equals(dataModel)) {
is64Bits = false;
} else if ("64".equals(dataModel)) {
is64Bits = true;
} else {
is64Bits =
arch.contains("64")
|| arch.equalsIgnoreCase("sparcv9");
}
}
systemClassLoader = createClassLoader();
addEmbeddedLibraryResourceRoot("libs/");
if (!isAndroid()) {
addEmbeddedLibraryResourceRoot("lib/");
addEmbeddedLibraryResourceRoot("org/bridj/lib/");
if (!Version.VERSION_SPECIFIC_SUB_PACKAGE.equals("")) {
addEmbeddedLibraryResourceRoot("org/bridj/" + Version.VERSION_SPECIFIC_SUB_PACKAGE + "/lib/");
}
}
try {
extractedLibrariesTempDir = createTempDir("BridJExtractedLibraries");
initLibrary();
} catch (Throwable th) {
th.printStackTrace();
}
POINTER_SIZE = sizeOf_ptrdiff_t();
WCHAR_T_SIZE = sizeOf_wchar_t();
SIZE_T_SIZE = sizeOf_size_t();
TIME_T_SIZE = sizeOf_time_t();
CLONG_SIZE = sizeOf_long();
is64Bits = POINTER_SIZE == 8;
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
shutdown();
}
});
}
private static List<NativeLibrary> nativeLibraries = new ArrayList<NativeLibrary>();
static void addNativeLibrary(NativeLibrary library) {
synchronized (nativeLibraries) {
nativeLibraries.add(library);
}
}
private static void shutdown() {
//releaseNativeLibraries();
deleteTemporaryExtractedLibraryFiles();
}
private static void releaseNativeLibraries() {
synchronized (nativeLibraries) {
// Release libraries in reverse order :
for (int iLibrary = nativeLibraries.size(); iLibrary-- != 0;) {
NativeLibrary lib = nativeLibraries.get(iLibrary);
try {
lib.release();
} catch (Throwable th) {
BridJ.error("Failed to release library '" + lib.path + "' : " + th, th);
}
}
}
}
private static void deleteTemporaryExtractedLibraryFiles() {
synchronized (temporaryExtractedLibraryCanonicalFiles) {
temporaryExtractedLibraryCanonicalFiles.add(extractedLibrariesTempDir);
// Release libraries in reverse order :
List<File> filesToDeleteAfterExit = new ArrayList<File>();
for (File tempFile : Platform.temporaryExtractedLibraryCanonicalFiles) {
if (tempFile.delete()) {
if (BridJ.verbose) {
BridJ.info("Deleted temporary library file '" + tempFile + "'");
}
} else {
filesToDeleteAfterExit.add(tempFile);
}
}
if (!filesToDeleteAfterExit.isEmpty()) {
if (BridJ.verbose) {
BridJ.info("Attempting to delete " + filesToDeleteAfterExit.size() + " files after JVM exit : " + StringUtils.implode(filesToDeleteAfterExit, ", "));
}
try {
ProcessUtils.startJavaProcess(DeleteFiles.class, filesToDeleteAfterExit);
} catch (Throwable ex) {
BridJ.error("Failed to launch process to delete files after JVM exit : " + ex, ex);
}
}
}
}
public static class DeleteFiles {
static boolean delete(List<File> files) {
for (Iterator<File> it = files.iterator(); it.hasNext();) {
File file = it.next();
if (file.delete()) {
it.remove();
}
}
return files.isEmpty();
}
final static long TRY_DELETE_EVERY_MILLIS = 50,
FAIL_AFTER_MILLIS = 10000;
public static void main(String[] args) {
try {
List<File> files = new LinkedList<File>();
for (String arg : args) {
files.add(new File(arg));
}
long start = System.currentTimeMillis();
while (!delete(files)) {
long elapsed = System.currentTimeMillis() - start;
if (elapsed > FAIL_AFTER_MILLIS) {
BridJ.error("Failed to delete the following files : " + StringUtils.implode(files));
System.exit(1);
}
Thread.sleep(TRY_DELETE_EVERY_MILLIS);
}
} catch (Throwable th) {
th.printStackTrace();
} finally {
System.exit(0);
}
}
}
static ClassLoader createClassLoader() {
List<URL> urls = new ArrayList<URL>();
for (String propName : new String[]{"java.class.path", "sun.boot.class.path"}) {
String prop = System.getProperty(propName);
if (prop == null) {
continue;
}
for (String path : prop.split(File.pathSeparator)) {
path = path.trim();
if (path.length() == 0) {
continue;
}
URL url;
try {
url = new URL(path);
} catch (MalformedURLException ex) {
try {
url = new File(path).toURI().toURL();
} catch (MalformedURLException ex2) {
url = null;
}
}
if (url != null) {
urls.add(url);
}
}
}
//System.out.println("URLs for synthetic class loader :");
//for (URL url : urls)
// System.out.println("\t" + url);
return new URLClassLoader(urls.toArray(new URL[urls.size()]));
}
static String getenvOrProperty(String envName, String javaName, String defaultValue) {
String value = System.getenv(envName);
if (value == null) {
value = System.getProperty(javaName);
}
if (value == null) {
value = defaultValue;
}
return value;
}
public static synchronized void initLibrary() {
if (inited) {
return;
}
inited = true;
try {
boolean loaded = false;
String forceLibFile = getenvOrProperty("BRIDJ_LIBRARY", "bridj.library", null);
String lib = null;
if (forceLibFile != null) {
try {
System.load(lib = forceLibFile);
loaded = true;
} catch (Throwable ex) {
BridJ.error("Failed to load forced library " + forceLibFile, ex);
}
}
if (!loaded) {
if (!Platform.isAndroid()) {
try {
File libFile = extractEmbeddedLibraryResource(BridJLibraryName);
if (libFile == null) {
throw new FileNotFoundException("Failed to extract embedded library '" + BridJLibraryName + "' (could be a classloader issue, or missing binary in resource path " + StringUtils.implode(embeddedLibraryResourceRoots, ", ") + ")");
}
if (BridJ.veryVerbose) {
BridJ.info("Loading library " + libFile);
}
System.load(lib = libFile.toString());
BridJ.setNativeLibraryFile(BridJLibraryName, libFile);
loaded = true;
} catch (IOException ex) {
BridJ.error("Failed to load '" + BridJLibraryName + "'", ex);
}
}
if (!loaded) {
System.loadLibrary("bridj");
}
}
if (BridJ.veryVerbose) {
BridJ.info("Loaded library " + lib);
}
init();
//if (BridJ.protectedMode)
// BridJ.info("Protected mode enabled");
if (BridJ.logCalls) {
BridJ.info("Calls logs enabled");
}
} catch (Throwable ex) {
throw new RuntimeException("Failed to initialize " + BridJ.class.getSimpleName() + " (" + ex + ")", ex);
}
}
private static native void init();
public static boolean isLinux() {
return isUnix() && osName.toLowerCase().contains("linux");
}
public static boolean isMacOSX() {
return isUnix() && (osName.startsWith("Mac") || osName.startsWith("Darwin"));
}
public static boolean isSolaris() {
return isUnix() && (osName.startsWith("SunOS") || osName.startsWith("Solaris"));
}
public static boolean isBSD() {
return isUnix() && (osName.contains("BSD") || isMacOSX());
}
public static boolean isUnix() {
return File.separatorChar == '/';
}
public static boolean isWindows() {
return File.separatorChar == '\\';
}
public static boolean isWindows7() {
return osName.equals("Windows 7");
}
/**
* Whether to use Unicode versions of Windows APIs rather than ANSI versions
* (for functions that haven't been bound yet : has no effect on functions
* that have already been bound).<br>
* Some Windows APIs such as SendMessage have two versions :
* <ul>
* <li>one that uses single-byte character strings (SendMessageA, with 'A'
* for ANSI strings)</li>
* <li>one that uses unicode character strings (SendMessageW, with 'W' for
* Wide strings).</li>
* </ul>
* <br>
* In a C/C++ program, this behaviour is controlled by the UNICODE macro
* definition.<br>
* By default, BridJ will use the Unicode versions. Set this field to false,
* set the bridj.useUnicodeVersionOfWindowsAPIs property to "false" or the
* BRIDJ_USE_UNICODE_VERSION_OF_WINDOWS_APIS environment variable to "0" to
* use the ANSI string version instead.
*/
public static boolean useUnicodeVersionOfWindowsAPIs = !("false".equals(System.getProperty("bridj.useUnicodeVersionOfWindowsAPIs"))
|| "0".equals(System.getenv("BRIDJ_USE_UNICODE_VERSION_OF_WINDOWS_APIS")));
private static String getArch() {
return arch;
}
/**
* Machine (as returned by `uname -m`, except for i686 which is actually
* i386), adjusted to the JVM platform (32 or 64 bits)
*/
public static String getMachine() {
String arch = getArch();
if (arch.equals("amd64") || arch.equals("x86_64")) {
if (is64Bits()) {
return "x86_64";
} else {
return "i386"; // we are running a 32 bits JVM on a 64 bits platform
}
}
if (arch.equals("aarch64")) {
return "arm64";
}
return arch;
}
public static boolean isAndroid() {
return "dalvik".equalsIgnoreCase(System.getProperty("java.vm.name")) && isLinux();
}
public static boolean isArm() {
String arch = getArch();
return "arm".equals(arch) || "arm64".equals(arch) || "aarch64".equals(arch);
}
public static boolean isSparc() {
String arch = getArch();
return "sparc".equals(arch)
|| "sparcv9".equals(arch);
}
public static boolean is64Bits() {
return is64Bits;
}
public static boolean isAmd64Arch() {
String arch = getArch();
return arch.equals("x86_64");
}
static List<String> getPossibleFileNames(String name) {
List<String> fileNames = new ArrayList<String>(1);
if (isWindows()) {
fileNames.add(name + ".dll");
fileNames.add(name + ".drv");
} else {
String jniName = "lib" + name + ".jnilib";
if (isMacOSX()) {
fileNames.add("lib" + name + ".dylib");
fileNames.add(jniName);
} else {
fileNames.add("lib" + name + ".so");
fileNames.add(name + ".so");
fileNames.add(jniName);
}
}
assert !fileNames.isEmpty();
if (name.contains(".")) {
fileNames.add(name);
}
return fileNames;
}
static synchronized List<String> getEmbeddedLibraryPaths(String name) {
String os = isWindows() ? "windows"
: isMacOSX() ? "darwin"
: isLinux() ? "linux"
: isSolaris() ? "sunos"
: isAndroid() ? "android"
: null;
List<String> paths = new ArrayList<String>();
if (os != null) {
List<String> archs = new ArrayList<String>();
if (isArm()) {
// Note: to discriminate between hard-float (armhf) and soft-float (armel), we used to test .
archs.add(
is64Bits()
? "arm64" :
new File("/lib/arm-linux-gnueabihf").isDirectory()
? "armhf" :
"armel");
} else if (isSparc()) {
archs.add(is64Bits() ? "sparc64" : "sparc");
} else {
if (isMacOSX()) {
// Note: used to handle jailbroken iphones here but whyyyy? iphoneos_arm32_arm
archs.add("universal");
}
archs.add(is64Bits() ? "x64" : "x86");
}
for (String root : embeddedLibraryResourceRoots) {
if (root == null) {
continue;
}
for (String arch : archs) {
paths.add(root + os + "_" + arch);
}
}
}
if (paths.isEmpty()) {
throw new RuntimeException("Platform not supported ! (os.name='" + osName + "', os.arch='" + System.getProperty("os.arch") + "')");
}
return paths;
}
static synchronized List<String> getEmbeddedLibraryResource(String name) {
List<String> paths = getEmbeddedLibraryPaths(name);
List<String> fileNames = getPossibleFileNames(name);
List<String> ret = new ArrayList<String>(paths.size() * fileNames.size());
for (String path : paths) {
for (String fileName : fileNames) {
ret.add(path + "/" + fileName);
}
}
if (BridJ.veryVerbose) {
BridJ.info("Embedded resource paths for library '" + name + "': " + ret);
}
return ret;
}
static void tryDeleteFilesInSameDirectory(final File legitFile, final Pattern fileNamePattern, long atLeastOlderThanMillis) {
final long maxModifiedDateForDeletion = System.currentTimeMillis() - atLeastOlderThanMillis;
new Thread(new Runnable() {
public void run() {
File dir = legitFile.getParentFile();
String legitFileName = legitFile.getName();
try {
for (String name : dir.list()) {
if (name.equals(legitFileName)) {
continue;
}
if (!fileNamePattern.matcher(name).matches()) {
continue;
}
File file = new File(dir, name);
if (file.lastModified() > maxModifiedDateForDeletion) {
continue;
}
if (file.delete() && BridJ.verbose) {
BridJ.info("Deleted old binary file '" + file + "'");
}
}
} catch (SecurityException ex) {
// no right to delete files in that directory
BridJ.warning("Failed to delete files matching '" + fileNamePattern + "' in directory '" + dir + "'", ex);
} catch (Throwable ex) {
BridJ.error("Unexpected error : " + ex, ex);
}
}
}).start();
}
static final long DELETE_OLD_BINARIES_AFTER_MILLIS = 24 * 60 * 60 * 1000; // 24 hours
static File extractEmbeddedLibraryResource(String name) throws IOException {
String firstLibraryResource = null;
List<String> libraryResources = getEmbeddedLibraryResource(name);
if (BridJ.veryVerbose) {
BridJ.info("Library resources for " + name + ": " + libraryResources);
}
for (String libraryResource : libraryResources) {
if (firstLibraryResource == null) {
firstLibraryResource = libraryResource;
}
// int i = libraryResource.lastIndexOf('.');
int len;
byte[] b = new byte[8196];
InputStream in = getResourceAsStream(libraryResource);
if (in == null) {
File f = new File(libraryResource);
if (!f.exists()) {
f = new File(f.getName());
}
if (f.exists()) {
return f.getCanonicalFile();
}
continue;
}
String fileName = new File(libraryResource).getName();
File libFile = new File(extractedLibrariesTempDir, fileName);
OutputStream out = new BufferedOutputStream(new FileOutputStream(libFile));
while ((len = in.read(b)) > 0) {
out.write(b, 0, len);
}
out.close();
in.close();
addTemporaryExtractedLibraryFileToDeleteOnExit(libFile);
addTemporaryExtractedLibraryFileToDeleteOnExit(libFile.getParentFile());
return libFile;
}
return null;
}
static final int maxTempFileAttempts = 20;
static File createTempDir(String prefix) throws IOException {
File dir;
for (int i = 0; i < maxTempFileAttempts; i++) {
dir = Files.createTempDirectory(prefix).toFile();
if (true) {
return dir;
}
}
throw new RuntimeException("Failed to create temp dir with prefix '" + prefix + "' despite " + maxTempFileAttempts + " attempts!");
}
/**
* Opens an URL with the default system action.
*
* @param url url to open
* @throws NoSuchMethodException if opening an URL on the current platform
* is not supported
*/
public static final void open(URL url) throws NoSuchMethodException {
if (url.getProtocol().equals("file")) {
open(new File(url.getFile()));
} else {
if (Platform.isMacOSX()) {
execArgs("open", url.toString());
} else if (Platform.isWindows()) {
execArgs("rundll32", "url.dll,FileProtocolHandler", url.toString());
} else if (Platform.isUnix() && hasUnixCommand("gnome-open")) {
execArgs("gnome-open", url.toString());
} else if (Platform.isUnix() && hasUnixCommand("konqueror")) {
execArgs("konqueror", url.toString());
} else if (Platform.isUnix() && hasUnixCommand("mozilla")) {
execArgs("mozilla", url.toString());
} else {
throw new NoSuchMethodException("Cannot open urls on this platform");
}
}
}
/**
* Opens a file with the default system action.
*
* @param file file to open
* @throws NoSuchMethodException if opening a file on the current platform
* is not supported
*/
public static final void open(File file) throws NoSuchMethodException {
if (Platform.isMacOSX()) {
execArgs("open", file.getAbsolutePath());
} else if (Platform.isWindows()) {
if (file.isDirectory()) {
execArgs("explorer", file.getAbsolutePath());
} else {
execArgs("start", file.getAbsolutePath());
}
} else if (Platform.isUnix() && hasUnixCommand("gnome-open")) {
execArgs("gnome-open", file.toString());
} else if (Platform.isUnix() && hasUnixCommand("konqueror")) {
execArgs("konqueror", file.toString());
} else if (Platform.isSolaris() && file.isDirectory()) {
execArgs("/usr/dt/bin/dtfile", "-folder", file.getAbsolutePath());
} else {
throw new NoSuchMethodException("Cannot open files on this platform");
}
}
/**
* Show a file in its parent directory, if possible selecting the file (not
* possible on all platforms).
*
* @param file file to show in the system's default file navigator
* @throws NoSuchMethodException if showing a file on the current platform
* is not supported
*/
public static final void show(File file) throws NoSuchMethodException, IOException {
if (Platform.isWindows()) {
exec("explorer /e,/select,\"" + file.getCanonicalPath() + "\"");
} else {
open(file.getAbsoluteFile().getParentFile());
}
}
static final void execArgs(String... cmd) throws NoSuchMethodException {
try {
Runtime.getRuntime().exec(cmd);
} catch (Exception ex) {
ex.printStackTrace();
throw new NoSuchMethodException(ex.toString());
}
}
static final void exec(String cmd) throws NoSuchMethodException {
try {
Runtime.getRuntime().exec(cmd).waitFor();
} catch (Exception ex) {
ex.printStackTrace();
throw new NoSuchMethodException(ex.toString());
}
}
static final boolean hasUnixCommand(String name) {
try {
Process p = Runtime.getRuntime().exec(new String[]{"which", name});
return p.waitFor() == 0;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
static native int sizeOf_size_t();
static native int sizeOf_time_t();
static native int sizeOf_wchar_t();
static native int sizeOf_ptrdiff_t();
static native int sizeOf_long();
static native int getMaxDirectMappingArgCount();
}