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() {
try {
Init.start();
} catch (CantLoadLibrary cantLoadLibrary) {
throw new MissingTdlibLibrary();
} catch (Throwable throwable) {
throwable.printStackTrace();
System.exit(1);
}
this.clientId = createNativeClient();
}

View File

@ -30,8 +30,9 @@ public class Log {
static {
try {
Init.start();
} catch (CantLoadLibrary cantLoadLibrary) {
throw new MissingTdlibLibrary();
} catch (Throwable throwable) {
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.
*/
public class CantLoadLibrary extends Exception {
public class CantLoadLibrary extends RuntimeException {
/**
* Creates a new CantLoadLibrary exception.
*/

View File

@ -27,7 +27,7 @@ public class Init {
* Initialize Tdlib
* @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();
if (os == Os.win) {

View File

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