diff --git a/src/main/java/it/ernytech/tdlib/Client.java b/src/main/java/it/ernytech/tdlib/Client.java index b16e7a4..e3b0004 100644 --- a/src/main/java/it/ernytech/tdlib/Client.java +++ b/src/main/java/it/ernytech/tdlib/Client.java @@ -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(); } diff --git a/src/main/java/it/ernytech/tdlib/Log.java b/src/main/java/it/ernytech/tdlib/Log.java index 0529917..9b2a98d 100644 --- a/src/main/java/it/ernytech/tdlib/Log.java +++ b/src/main/java/it/ernytech/tdlib/Log.java @@ -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); } } diff --git a/src/main/java/it/ernytech/tdlib/utils/CantLoadLibrary.java b/src/main/java/it/ernytech/tdlib/utils/CantLoadLibrary.java index a0fbbdb..34bbe82 100644 --- a/src/main/java/it/ernytech/tdlib/utils/CantLoadLibrary.java +++ b/src/main/java/it/ernytech/tdlib/utils/CantLoadLibrary.java @@ -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. */ diff --git a/src/main/java/it/ernytech/tdlib/utils/Init.java b/src/main/java/it/ernytech/tdlib/utils/Init.java index 02c895a..aad95cf 100644 --- a/src/main/java/it/ernytech/tdlib/utils/Init.java +++ b/src/main/java/it/ernytech/tdlib/utils/Init.java @@ -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) { diff --git a/src/main/java/it/ernytech/tdlib/utils/LoadLibrary.java b/src/main/java/it/ernytech/tdlib/utils/LoadLibrary.java index a9d42a1..a345da7 100644 --- a/src/main/java/it/ernytech/tdlib/utils/LoadLibrary.java +++ b/src/main/java/it/ernytech/tdlib/utils/LoadLibrary.java @@ -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(); diff --git a/src/main/java/it/ernytech/tdlib/utils/MissingTdlibLibrary.java b/src/main/java/it/ernytech/tdlib/utils/MissingTdlibLibrary.java deleted file mode 100644 index 3ba85cf..0000000 --- a/src/main/java/it/ernytech/tdlib/utils/MissingTdlibLibrary.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -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"); - } -}