4
4
import org .slf4j .Logger ;
5
5
import org .slf4j .LoggerFactory ;
6
6
7
+ import java .io .File ;
8
+ import java .io .IOException ;
7
9
import java .lang .foreign .*;
8
10
import java .lang .invoke .MethodHandle ;
9
11
import java .lang .invoke .MethodHandles ;
10
12
import java .lang .invoke .MethodType ;
13
+ import java .nio .file .Files ;
14
+ import java .nio .file .Path ;
15
+ import java .util .LinkedList ;
16
+ import java .util .List ;
17
+ import java .util .stream .Collectors ;
18
+ import java .util .stream .Stream ;
11
19
12
20
import static java .lang .foreign .ValueLayout .*;
13
21
@@ -19,19 +27,37 @@ final class RuntimeHelper {
19
27
private static final SymbolLookup SYMBOL_LOOKUP ;
20
28
private static final SegmentAllocator THROWING_ALLOCATOR = (x , y ) -> { throw new AssertionError ("should not reach here" ); };
21
29
private static boolean isLoaded = false ;
30
+ private static final String LD_CONFIG = "/etc/ld.so.conf.d/" ;
31
+ private static final String LIB_NAME_VERSION = "libappindicator3.so.1" ;
32
+ private static List <String > allPath = new LinkedList <>();
22
33
private static final Logger LOG = LoggerFactory .getLogger (RuntimeHelper .class );
23
34
24
35
final static SegmentAllocator CONSTANT_ALLOCATOR =
25
36
(size , align ) -> MemorySegment .allocateNative (size , align , SegmentScope .auto ());
26
37
27
38
static {
28
- try {
29
- System .loadLibrary ("appindicator3" );
30
- LOG .debug ("Native code library appindicator3 successfully loaded" );
31
- isLoaded = true ;
32
- } catch (UnsatisfiedLinkError e ) {
33
- LOG .info ("Native code library appindicator3 failed to load" );
39
+ try (Stream <Path > paths = Files .list (Path .of (LD_CONFIG ))) {
40
+ paths .forEach ((file ) -> {
41
+ try (Stream <String > lines = Files .lines (file )) {
42
+ List <String > collection = lines .filter (line -> line .startsWith ("/" )).toList ();
43
+ allPath .addAll (collection );
44
+ } catch (IOException e ) {
45
+ LOG .error ("File '{}' could not be loaded" , file );
46
+ }
47
+ });
48
+ } catch (IOException e ) {
49
+ LOG .error ("Directory '{}' does not exist" , LD_CONFIG );
50
+ }
51
+
52
+ allPath .add ("/usr/lib" ); // for systems, that don't implement multiarch
53
+ for (String path : allPath ) {
54
+ try {
55
+ System .load (path + File .separator + LIB_NAME_VERSION );
56
+ isLoaded = true ;
57
+ break ;
58
+ } catch (UnsatisfiedLinkError ignored ) { }
34
59
}
60
+ LOG .info (isLoaded ? "Native code library " + LIB_NAME_VERSION + " successfully loaded" : "Native code library " + LIB_NAME_VERSION + " failed to load" );
35
61
SymbolLookup loaderLookup = SymbolLookup .loaderLookup ();
36
62
SYMBOL_LOOKUP = name -> loaderLookup .find (name ).or (() -> LINKER .defaultLookup ().find (name ));
37
63
}
0 commit comments