Skip to content

Commit c644fed

Browse files
committed
Load library via its filename
Lookup paths in /etc/ld.so.conf.d/
1 parent 4837ca1 commit c644fed

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

src/main/java/org/purejava/appindicator/RuntimeHelper.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66

7+
import java.io.File;
8+
import java.io.IOException;
79
import java.lang.foreign.*;
810
import java.lang.invoke.MethodHandle;
911
import java.lang.invoke.MethodHandles;
1012
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;
1119

1220
import static java.lang.foreign.ValueLayout.*;
1321

@@ -19,19 +27,37 @@ final class RuntimeHelper {
1927
private static final SymbolLookup SYMBOL_LOOKUP;
2028
private static final SegmentAllocator THROWING_ALLOCATOR = (x, y) -> { throw new AssertionError("should not reach here"); };
2129
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<>();
2233
private static final Logger LOG = LoggerFactory.getLogger(RuntimeHelper.class);
2334

2435
final static SegmentAllocator CONSTANT_ALLOCATOR =
2536
(size, align) -> MemorySegment.allocateNative(size, align, SegmentScope.auto());
2637

2738
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) { }
3459
}
60+
LOG.info(isLoaded ? "Native code library " + LIB_NAME_VERSION + " successfully loaded" : "Native code library " + LIB_NAME_VERSION + " failed to load");
3561
SymbolLookup loaderLookup = SymbolLookup.loaderLookup();
3662
SYMBOL_LOOKUP = name -> loaderLookup.find(name).or(() -> LINKER.defaultLookup().find(name));
3763
}
Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package org.purejava.appindicator;
2+
13
import org.junit.jupiter.api.DisplayName;
24
import org.junit.jupiter.api.Order;
35
import org.junit.jupiter.api.Test;
@@ -6,20 +8,47 @@
68
import org.slf4j.Logger;
79
import org.slf4j.LoggerFactory;
810

9-
import static org.junit.jupiter.api.Assertions.*;
11+
import java.io.File;
12+
import java.io.IOException;
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.Stream;
1018

1119
class AppIndicatorTest {
1220
private static final Logger LOG = LoggerFactory.getLogger(AppIndicatorTest.class);
21+
private static boolean isLoaded = false;
22+
private static final String LD_CONFIG = "/etc/ld.so.conf.d/";
23+
private static final String LIB_NAME_VERSION = "libappindicator3.so.1";
24+
private static List<String> allPath = new LinkedList<>();
25+
1326
@Test
1427
@Order(1)
1528
@DisplayName("Testing availability of native lib")
1629
@EnabledOnOs(OS.LINUX)
1730
public void shouldHaveNoErrors() {
18-
try {
19-
System.loadLibrary("appindicator3");
20-
LOG.debug("Native code library appindicator3 successfully loaded");
21-
} catch (UnsatisfiedLinkError e) {
22-
LOG.info("Native code library appindicator3 failed to load");
31+
try (Stream<Path> paths = Files.list(Path.of(LD_CONFIG))) {
32+
paths.forEach((file) -> {
33+
try (Stream<String> lines = Files.lines(file)) {
34+
List<String> collection = lines.filter(line -> line.startsWith("/")).toList();
35+
allPath.addAll(collection);
36+
} catch (IOException e) {
37+
LOG.error("File '{}' could not be loaded", file);
38+
}
39+
});
40+
} catch (IOException e) {
41+
LOG.error("Directory '{}' does not exist", LD_CONFIG);
42+
}
43+
44+
allPath.add("/usr/lib"); // for systems, that don't implement multiarch
45+
for (String path : allPath) {
46+
try {
47+
System.load(path + File.separator + LIB_NAME_VERSION);
48+
isLoaded = true;
49+
break;
50+
} catch (UnsatisfiedLinkError ignored) { }
2351
}
52+
LOG.info(isLoaded ? "Native code library " + LIB_NAME_VERSION + " successfully loaded" : "Native code library " + LIB_NAME_VERSION + " failed to load");
2453
}
2554
}

0 commit comments

Comments
 (0)