tdlight-java/src/main/java/it/tdlight/common/utils/LoadLibrary.java

264 lines
7.4 KiB
Java
Raw Normal View History

2018-07-18 12:24:37 +02:00
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.common.utils;
2018-07-18 12:24:37 +02:00
import java.io.IOException;
import java.io.InputStream;
2018-11-26 16:17:57 +01:00
import java.nio.ByteOrder;
2020-04-19 16:32:49 +02:00
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
2018-07-18 12:24:37 +02:00
import java.util.concurrent.ConcurrentHashMap;
import java.lang.reflect.InvocationTargetException;
2021-04-07 19:48:10 +02:00
import it.tdlight.tdnative.ObjectsUtils;
2018-07-18 12:24:37 +02:00
/**
* The class to load the libraries needed to run Tdlib
*/
public class LoadLibrary {
2021-01-10 19:06:47 +01:00
private static final ConcurrentHashMap<String, Boolean> libraryLoaded = new ConcurrentHashMap<>();
private static final Path librariesPath = Paths.get(".cache");
private static final String libsVersion = LibraryVersion.IMPLEMENTATION_NAME
+ "-" + LibraryVersion.VERSION + "-" + LibraryVersion.NATIVES_VERSION;
2020-04-19 16:32:49 +02:00
static {
if (Files.notExists(librariesPath)) {
try {
Files.createDirectories(librariesPath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Load a library installed in the system (priority choice) or a library included in the jar.
*
* @param libname The name of the library.
* @throws CantLoadLibrary An exception that is thrown when the LoadLibrary class fails to load the library.
*/
2020-08-30 00:46:28 +02:00
public static void load(String libname) throws CantLoadLibrary {
2020-04-19 16:32:49 +02:00
if (libname == null || libname.trim().isEmpty()) {
throw new IllegalArgumentException();
}
if (libraryLoaded.containsKey(libname)) {
if (libraryLoaded.get(libname)) {
return;
}
}
loadLibrary(libname);
libraryLoaded.put(libname, true);
}
2020-08-30 00:46:28 +02:00
private static void loadLibrary(String libname) throws CantLoadLibrary {
Arch arch = getCpuArch();
Os os = getOs();
2020-04-19 16:32:49 +02:00
if (arch == Arch.unknown) {
2020-08-30 00:46:28 +02:00
throw (CantLoadLibrary) new CantLoadLibrary().initCause(new IllegalStateException("Arch: \"" + System.getProperty("os.arch") + "\" is unknown"));
2020-04-19 16:32:49 +02:00
}
if (os == Os.unknown) {
2020-08-30 00:46:28 +02:00
throw (CantLoadLibrary) new CantLoadLibrary().initCause(new IllegalStateException("Os: \"" + System.getProperty("os.name") + "\" is unknown"));
2020-04-19 16:32:49 +02:00
}
try {
loadJarLibrary(libname, arch, os);
} catch (IOException | CantLoadLibrary | UnsatisfiedLinkError e) {
2020-08-23 15:36:45 +02:00
if (loadSysLibrary(libname)) {
return;
}
2020-08-30 00:46:28 +02:00
throw (CantLoadLibrary) new CantLoadLibrary().initCause(e);
2020-04-19 16:32:49 +02:00
}
}
private static boolean loadSysLibrary(String libname) {
try {
System.loadLibrary(libname);
} catch (UnsatisfiedLinkError e) {
return false;
}
return true;
}
private static void loadJarLibrary(String libname, Arch arch, Os os) throws IOException, CantLoadLibrary {
Path tempPath = Files.createDirectories(librariesPath.resolve("version-" + libsVersion).resolve(libname));
Path tempFile = Paths.get(tempPath.toString(), libname + getExt(os));
2020-08-21 00:01:01 +02:00
Class<?> classForResource = null;
switch (os) {
case linux:
switch (arch) {
case amd64:
try {
classForResource = Class.forName(LibraryVersion.LINUX_AMD64_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
2020-08-21 00:01:01 +02:00
break;
2020-12-29 23:38:50 +01:00
case x86:
try {
classForResource = Class.forName(LibraryVersion.LINUX_X86_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
break;
2020-08-21 00:01:01 +02:00
case aarch64:
try {
classForResource = Class.forName(LibraryVersion.LINUX_AARCH64_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
2020-08-21 00:01:01 +02:00
break;
2020-12-29 23:38:50 +01:00
case armv7:
try {
classForResource = Class.forName(LibraryVersion.LINUX_ARMV7_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
break;
case armv6:
try {
classForResource = Class.forName(LibraryVersion.LINUX_ARMV6_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
break;
case ppc64le:
try {
classForResource = Class.forName(LibraryVersion.LINUX_PPC64LE_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
break;
2020-08-21 00:01:01 +02:00
}
break;
2020-08-23 19:48:42 +02:00
case osx:
if (arch == Arch.amd64) {
try {
classForResource = Class.forName(LibraryVersion.OSX_AMD64_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
2020-08-23 19:48:42 +02:00
}
break;
2020-08-21 00:01:01 +02:00
case win:
2020-12-29 23:38:50 +01:00
switch (arch) {
case amd64:
try {
classForResource = Class.forName(LibraryVersion.WINDOWS_AMD64_CLASS);
} catch (ClassNotFoundException e) {
// not found
}
break;
case x86:
break;
2020-08-21 00:01:01 +02:00
}
break;
}
if (classForResource == null) {
throw new IOException("Native libraries for platform " + os + "-" + arch + " not found!");
}
InputStream libInputStream;
try {
libInputStream = ObjectsUtils.requireNonNull((InputStream) classForResource.getDeclaredMethod("getLibraryAsStream").invoke(InputStream.class));
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | NullPointerException e) {
throw new IOException("Native libraries for platform " + os + "-" + arch + " not found!", e);
}
2020-04-19 16:32:49 +02:00
if (Files.notExists(tempFile)) {
Files.copy(libInputStream, tempFile);
}
2020-09-01 23:18:19 +02:00
if (libInputStream != null) libInputStream.close();
2020-04-19 16:32:49 +02:00
System.load(tempFile.toFile().getAbsolutePath());
}
private static Arch getCpuArch() {
String architecture = System.getProperty("os.arch").trim();
2020-04-19 16:32:49 +02:00
switch (architecture) {
case "amd64":
case "x86_64":
return Arch.amd64;
case "i386":
case "x86":
2020-12-29 23:38:50 +01:00
return Arch.x86;
case "armv6":
return Arch.armv6;
2020-04-19 16:32:49 +02:00
case "arm":
2020-12-29 23:38:50 +01:00
case "aarch32":
case "armv7":
case "armv7l":
return Arch.armv7;
2020-04-19 16:32:49 +02:00
case "arm64":
case "aarch64":
return Arch.aarch64;
2020-12-29 23:38:50 +01:00
case "powerpc":
2020-04-19 16:32:49 +02:00
case "ppc64":
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) // Java always returns ppc64 for all 64-bit powerpc but
return Arch.ppc64le; // powerpc64le (our target) is very different, it uses this condition to accurately identify the architecture
else
return Arch.unknown;
default:
return Arch.unknown;
}
}
public static Os getOs() {
String os = System.getProperty("os.name").toLowerCase().trim();
2020-04-19 16:32:49 +02:00
if (os.contains("linux"))
return Os.linux;
if (os.contains("windows"))
return Os.win;
if (os.contains("mac"))
2020-08-23 19:34:08 +02:00
return Os.osx;
2020-04-19 16:32:49 +02:00
if (os.contains("darwin"))
2020-08-23 19:34:08 +02:00
return Os.osx;
2020-04-19 16:32:49 +02:00
return Os.unknown;
}
private static String getExt(Os os) {
2020-08-23 19:34:08 +02:00
switch (os) {
case win:
return ".dll";
case osx:
return ".dylib";
case linux:
case unknown:
default:
return ".so";
}
2020-04-19 16:32:49 +02:00
}
private static String createPath(String... path) {
StringBuilder stringBuilder = new StringBuilder();
2020-04-19 16:32:49 +02:00
stringBuilder.append("/");
for (int i = 0; i < path.length; i++) {
stringBuilder.append(path[i]);
if (i < path.length - 1) {
stringBuilder.append("/");
}
}
return stringBuilder.toString();
}
2018-07-18 12:24:37 +02:00
}