Use initCause when throw exceptions in Init process to pass the real reason for failure when loading TdLib

Fix https://github.com/ErnyTech/JTDLib/issues/2

Signed-off-by: Ernesto Castellotti <erny.castell@gmail.com>
This commit is contained in:
Ernesto Castellotti 2018-11-18 12:17:43 +01:00
parent 4092659f24
commit fbf8a495bb
6 changed files with 25 additions and 80 deletions

View File

@ -44,10 +44,10 @@ public class Client {
public Client() { public Client() {
try { try {
Init.start(); Init.start();
} catch (CantLoadLibrary cantLoadLibrary) { } catch (Throwable throwable) {
throw new MissingTdlibLibrary(); throwable.printStackTrace();
System.exit(1);
} }
this.clientId = createNativeClient(); this.clientId = createNativeClient();
} }

View File

@ -30,8 +30,9 @@ public class Log {
static { static {
try { try {
Init.start(); Init.start();
} catch (CantLoadLibrary cantLoadLibrary) { } catch (Throwable throwable) {
throw new MissingTdlibLibrary(); throwable.printStackTrace();
System.exit(0);
} }
} }

View File

@ -20,7 +20,7 @@ package it.ernytech.tdlib.utils;
/** /**
* An exception that is thrown when the LoadLibrary class fails to load the library. * An exception that is thrown when the LoadLibrary class fails to load the library.
*/ */
public class CantLoadLibrary extends Exception { public class CantLoadLibrary extends RuntimeException {
/** /**
* Creates a new CantLoadLibrary exception. * Creates a new CantLoadLibrary exception.
*/ */

View File

@ -27,7 +27,7 @@ public class Init {
* Initialize Tdlib * Initialize Tdlib
* @throws CantLoadLibrary An exception that is thrown when the LoadLibrary class fails to load the library. * @throws CantLoadLibrary An exception that is thrown when the LoadLibrary class fails to load the library.
*/ */
public static void start() throws CantLoadLibrary { public static void start() throws Throwable {
var os = LoadLibrary.getOs(); var os = LoadLibrary.getOs();
if (os == Os.win) { if (os == Os.win) {

View File

@ -32,9 +32,9 @@ public class LoadLibrary {
* @param libname The name of the library. * @param libname The name of the library.
* @throws CantLoadLibrary An exception that is thrown when the LoadLibrary class fails to load the library. * @throws CantLoadLibrary An exception that is thrown when the LoadLibrary class fails to load the library.
*/ */
public static void load(String libname) throws CantLoadLibrary { public static void load(String libname) throws Throwable {
if (libname == null || libname.trim().isEmpty()) { if (libname == null || libname.trim().isEmpty()) {
throw new CantLoadLibrary(); throw new IllegalArgumentException();
} }
if (libraryLoaded.containsKey(libname)) { if (libraryLoaded.containsKey(libname)) {
@ -47,8 +47,8 @@ public class LoadLibrary {
libraryLoaded.put(libname, true); libraryLoaded.put(libname, true);
} }
private static void loadLibrary(String libname) throws CantLoadLibrary { private static void loadLibrary(String libname) throws Throwable {
if (loadSysLibrary(libname) == 0) { if (loadSysLibrary(libname)) {
return; return;
} }
@ -56,66 +56,40 @@ public class LoadLibrary {
var os = getOs(); var os = getOs();
if (arch == Arch.unknown) { if (arch == Arch.unknown) {
throw new CantLoadLibrary(); throw new CantLoadLibrary().initCause(new IllegalStateException("Arch: \"" + System.getProperty("os.arch") + "\" is unknown"));
} }
if (os == Os.unknown) { if (os == Os.unknown) {
throw new CantLoadLibrary(); throw new CantLoadLibrary().initCause(new IllegalStateException("Os: \"" + System.getProperty("os.name") + "\" is unknown"));
} }
if (loadJarLibrary(libname, arch, os) == 0) { try {
return; loadJarLibrary(libname, arch, os);
} catch (IOException | CantLoadLibrary | UnsatisfiedLinkError e) {
throw new CantLoadLibrary().initCause(e);
} }
throw new CantLoadLibrary();
} }
private static int loadSysLibrary(String libname) { private static boolean loadSysLibrary(String libname) {
try { try {
System.loadLibrary(libname); System.loadLibrary(libname);
} catch (UnsatisfiedLinkError e) { } catch (UnsatisfiedLinkError e) {
return 1; return false;
} }
return 0; return true;
} }
private static int loadJarLibrary(String libname, Arch arch, Os os) { private static void loadJarLibrary(String libname, Arch arch, Os os) throws IOException, CantLoadLibrary {
Path tempPath; Path tempPath = Files.createTempDirectory(libname);
try {
tempPath = Files.createTempDirectory(libname);
} catch (IOException e) {
return 1;
}
deleteOnExit(tempPath); deleteOnExit(tempPath);
Path tempFile = Paths.get(tempPath.toString(), libname + getExt(os)); Path tempFile = Paths.get(tempPath.toString(), libname + getExt(os));
deleteOnExit(tempPath); deleteOnExit(tempPath);
var libInputStream = LoadLibrary.class.getResourceAsStream(createPath("libs", os.name(), arch.name(), libname) + getExt(os)); var libInputStream = LoadLibrary.class.getResourceAsStream(createPath("libs", os.name(), arch.name(), libname) + getExt(os));
Files.copy(libInputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
try { System.load(tempFile.toFile().getAbsolutePath());
Files.copy(libInputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
return 1;
}
try {
loadLibraryPath(tempFile);
} catch (CantLoadLibrary cantLoadLibrary) {
return 1;
}
return 0;
} }
private static void loadLibraryPath(Path path) throws CantLoadLibrary {
try {
System.load(path.toFile().getAbsolutePath());
} catch (UnsatisfiedLinkError e) {
throw new CantLoadLibrary();
}
}
private static Arch getCpuArch() { private static Arch getCpuArch() {
var arch = System.getProperty("os.arch").trim(); var arch = System.getProperty("os.arch").trim();

View File

@ -1,30 +0,0 @@
/*
* 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.ernytech.tdlib.utils;
/**
* An runtime exception that is thrown when the libraries needed for Tdlib have not been loaded.
*/
public class MissingTdlibLibrary extends RuntimeException {
/**
* Creates a new MissingTdlibLibrary runtime exception.
*/
public MissingTdlibLibrary() {
super("Tdlib library has not been loaded, so execution can't continue");
}
}