Improve library loading

This commit is contained in:
Andrea Cavalli 2021-10-23 01:25:23 +02:00
parent bc3246913a
commit 1ed9a7739e
3 changed files with 82 additions and 58 deletions

View File

@ -78,8 +78,8 @@
<version>3.8.1</version> <version>3.8.1</version>
<configuration> <configuration>
<encoding>UTF-8</encoding> <encoding>UTF-8</encoding>
<source>8</source> <source>11</source>
<target>8</target> <target>11</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>

18
pom.xml
View File

@ -8,9 +8,9 @@
<name>TDLib Java Parent</name> <name>TDLib Java Parent</name>
<properties> <properties>
<revision>1.0.0.0-SNAPSHOT</revision> <revision>1.0.0.0-SNAPSHOT</revision>
<nativesRevisionNumber>169</nativesRevisionNumber> <nativesRevisionNumber>173</nativesRevisionNumber>
<nativesRevisionSuffix/> <nativesRevisionSuffix/>
<apiRevisionNumber>167</apiRevisionNumber> <apiRevisionNumber>171</apiRevisionNumber>
<apiRevisionSuffix/> <apiRevisionSuffix/>
</properties> </properties>
<repositories> <repositories>
@ -45,20 +45,6 @@
<modules/> <modules/>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles> <profiles>
<profile> <profile>
<id>tdlib</id> <id>tdlib</id>

View File

@ -24,7 +24,11 @@ import java.nio.ByteOrder;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
@ -100,6 +104,20 @@ public final class LoadLibrary {
return true; return true;
} }
private static String removeLastPackageParts(String clazz, int count, String className) {
List<String> parts = new ArrayList<>(Arrays.asList(clazz.split("\\.")));
parts.remove(parts.size() - 1);
for (int i = 0; i < count; i++) {
parts.remove(parts.size() - 1);
}
StringJoiner joiner = new StringJoiner(".");
for (String part : parts) {
joiner.add(part);
}
joiner.add(className);
return joiner.toString();
}
private static void loadJarLibrary(String libname, Arch arch, Os os) throws CantLoadLibrary { private static void loadJarLibrary(String libname, Arch arch, Os os) throws CantLoadLibrary {
Path tempPath; Path tempPath;
try { try {
@ -113,66 +131,34 @@ public final class LoadLibrary {
case LINUX: case LINUX:
switch (arch) { switch (arch) {
case AMD64: case AMD64:
try { classForResource = tryLoadLibraryVersionClass(LibraryVersion.LINUX_AMD64_CLASS);
classForResource = Class.forName(LibraryVersion.LINUX_AMD64_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
break; break;
case I386: case I386:
try { classForResource = tryLoadLibraryVersionClass(LibraryVersion.LINUX_X86_CLASS);
classForResource = Class.forName(LibraryVersion.LINUX_X86_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
break; break;
case AARCH64: case AARCH64:
try { classForResource = tryLoadLibraryVersionClass(LibraryVersion.LINUX_AARCH64_CLASS);
classForResource = Class.forName(LibraryVersion.LINUX_AARCH64_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
break; break;
case ARMHF: case ARMHF:
try { classForResource = tryLoadLibraryVersionClass(LibraryVersion.LINUX_ARMHF_CLASS);
classForResource = Class.forName(LibraryVersion.LINUX_ARMHF_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
break; break;
case S390X: case S390X:
try { classForResource = tryLoadLibraryVersionClass(LibraryVersion.LINUX_S390X_CLASS);
classForResource = Class.forName(LibraryVersion.LINUX_S390X_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
break; break;
case PPC64LE: case PPC64LE:
try { classForResource = tryLoadLibraryVersionClass(LibraryVersion.LINUX_PPC64LE_CLASS);
classForResource = Class.forName(LibraryVersion.LINUX_PPC64LE_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
break; break;
} }
break; break;
case OSX: case OSX:
if (arch == Arch.AMD64) { if (arch == Arch.AMD64) {
try { classForResource = tryLoadLibraryVersionClass(LibraryVersion.OSX_AMD64_CLASS);
classForResource = Class.forName(LibraryVersion.OSX_AMD64_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
} }
break; break;
case WINDOWS: case WINDOWS:
switch (arch) { switch (arch) {
case AMD64: case AMD64:
try { classForResource = tryLoadLibraryVersionClass(LibraryVersion.WINDOWS_AMD64_CLASS);
classForResource = Class.forName(LibraryVersion.WINDOWS_AMD64_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
break; break;
case I386: case I386:
break; break;
@ -205,6 +191,58 @@ public final class LoadLibrary {
System.load(tempFile.toFile().getAbsolutePath()); System.load(tempFile.toFile().getAbsolutePath());
} }
private static Class<?> tryLoadLibraryVersionClass(String classForResource) throws CantLoadLibrary {
try {
return Class.forName(classForResource);
} catch (ClassNotFoundException e1) {
// exact library not found
//Check if a wrong version is installed
try {
Class<?> foundAnotherVersion = Class.forName(removeFromVersion(classForResource));
throw new CantLoadLibrary("Can't load the native libraries."
+ " A different version of the native libraries was found."
+ " Please make sure that you installed the correct one.", e1);
} catch (ClassNotFoundException e2) {
// not found arch
//Check if a wrong arch is installed
try {
Class<?> foundAnotherArch = Class.forName(removeFromArch(classForResource));
throw new CantLoadLibrary("Can't load the native libraries."
+ " A different architecture of the native libraries was found."
+ " Please make sure that you installed the correct one.", e1);
} catch (ClassNotFoundException e3) {
// not found os
//Check if a wrong os is installed
try {
Class<?> foundAnotherOs = Class.forName(removeFromOs(classForResource));
throw new CantLoadLibrary("Can't load the native libraries."
+ " A different OS of the native libraries was found."
+ " Please make sure that you installed the correct one.", e1);
} catch (ClassNotFoundException e4) {
// not found anything
// No library was found, return
return null;
}
}
}
}
}
private static String removeFromVersion(String libraryVersionClass) {
return removeLastPackageParts(libraryVersionClass, 1, "AnyVersion");
}
private static String removeFromArch(String libraryVersionClass) {
return removeLastPackageParts(libraryVersionClass, 2, "AnyArch");
}
private static String removeFromOs(String libraryVersionClass) {
return removeLastPackageParts(libraryVersionClass, 3, "AnyOs");
}
private static Arch getCpuArch() { private static Arch getCpuArch() {
String architecture = System.getProperty("os.arch").trim(); String architecture = System.getProperty("os.arch").trim();