From 97c08822e27c8694f9a600a1fe8437187ba42c82 Mon Sep 17 00:00:00 2001 From: PseudoKnight Date: Tue, 7 Nov 2023 13:45:12 -0800 Subject: [PATCH] Exclude some sqlite native libraries from the jar These operating systems or architectures are not officially supported, are probably unused, are only relevent to cmdline, and most were only recently added to the sqlite package. But they still contribute significantly to the overall jar size. These exclusions reduces file size by around 7.8 MB. A fallback was added so that a user can still use sqlite on these platforms. --- pom.xml | 16 +++++++++++-- .../laytonsmith/database/SQLiteProfile.java | 19 +++++++++++++++ .../persistence/SQLiteDataSource.java | 23 ++++++++++++++++++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 71d1230004..bcb9443dd0 100644 --- a/pom.xml +++ b/pom.xml @@ -700,10 +700,22 @@ org.xerial:sqlite-jdbc:jar:* - native/** - org/ibex/** org/sqlite/** + + + + org/sqlite/native/FreeBSD/** + org/sqlite/native/Linux-Android/** + org/sqlite/native/Linux/ppc64/* + org/sqlite/native/Linux/arm/* + org/sqlite/native/Linux/armv6/* + org/sqlite/native/Linux/armv7/* + org/sqlite/native/Linux/x86/* + org/sqlite/native/Linux-Musl/x86/* + org/sqlite/native/Windows/x86/* + org/sqlite/native/Windows/armv7/* + org.apache.commons:commons-io:jar:* diff --git a/src/main/java/com/laytonsmith/database/SQLiteProfile.java b/src/main/java/com/laytonsmith/database/SQLiteProfile.java index 6fedba9bc9..ae0f456aed 100644 --- a/src/main/java/com/laytonsmith/database/SQLiteProfile.java +++ b/src/main/java/com/laytonsmith/database/SQLiteProfile.java @@ -2,6 +2,9 @@ import com.laytonsmith.core.Profiles; import com.laytonsmith.core.MethodScriptFileLocations; +import org.sqlite.SQLiteJDBCLoader; +import org.sqlite.util.OSInfo; + import java.io.File; import java.sql.SQLException; import java.util.Map; @@ -37,6 +40,22 @@ public String getConnectionString() throws SQLException { } catch (ClassNotFoundException ex) { throw new SQLException("Cannot load SQLite. Check your installation and try again"); } + // Set native library override path if not already set. (e.g. -Dorg.sqlite.lib.path="/path/to/lib") + if(System.getProperty("org.sqlite.lib.path") == null) { + System.setProperty("org.sqlite.lib.path", new File(MethodScriptFileLocations.getDefault().getConfigDirectory(), + "sqlite/native/" + OSInfo.getNativeLibFolderPathForCurrentOS()).getAbsolutePath()); + } + try { + // Load native library before connection to detect when the library is missing. + // This is done in SQLiteDataSource as well. + SQLiteJDBCLoader.initialize(); + } catch (Exception ex) { + throw new SQLException("Failed to load a native sqlite library for your platform." + + " You can download the library file from" + + " https://github.com/xerial/sqlite-jdbc/tree/master/src/main/resources/org/sqlite/native/" + + OSInfo.getNativeLibFolderPathForCurrentOS() + " and place it into " + + System.getProperty("org.sqlite.lib.path")); + } return "jdbc:sqlite:" + getFile(); } diff --git a/src/main/java/com/laytonsmith/persistence/SQLiteDataSource.java b/src/main/java/com/laytonsmith/persistence/SQLiteDataSource.java index d27699e3cd..6ed04b014c 100644 --- a/src/main/java/com/laytonsmith/persistence/SQLiteDataSource.java +++ b/src/main/java/com/laytonsmith/persistence/SQLiteDataSource.java @@ -5,9 +5,14 @@ import com.laytonsmith.annotations.datasource; import com.laytonsmith.core.MSLog; import com.laytonsmith.core.MSVersion; +import com.laytonsmith.core.MethodScriptFileLocations; import com.laytonsmith.core.constructs.Target; import com.laytonsmith.persistence.io.ConnectionMixin; import com.laytonsmith.persistence.io.ConnectionMixinFactory; +import org.sqlite.SQLiteJDBCLoader; +import org.sqlite.util.OSInfo; + +import java.io.File; import java.io.IOException; import java.net.URI; import java.sql.DriverManager; @@ -61,6 +66,22 @@ public SQLiteDataSource(URI uri, ConnectionMixinFactory.ConnectionMixinOptions o mixin = getConnectionMixin(); try { Class.forName(org.sqlite.JDBC.class.getName()); + // Set native library override path if not already set. (e.g. -Dorg.sqlite.lib.path="/path/to/lib") + if(System.getProperty("org.sqlite.lib.path") == null) { + System.setProperty("org.sqlite.lib.path", new File(MethodScriptFileLocations.getDefault().getConfigDirectory(), + "sqlite/native/" + OSInfo.getNativeLibFolderPathForCurrentOS()).getAbsolutePath()); + } + try { + // Load native library before connection to detect when the library is missing. + // This is done in SQLiteProfile as well. + SQLiteJDBCLoader.initialize(); + } catch (Exception ex) { + throw new DataSourceException("Failed to load a native sqlite library for your platform." + + " You can download the library file from" + + " https://github.com/xerial/sqlite-jdbc/tree/master/src/main/resources/org/sqlite/native/" + + OSInfo.getNativeLibFolderPathForCurrentOS() + " and place it into " + + System.getProperty("org.sqlite.lib.path")); + } path = mixin.getPath(); connect(); long startTime = System.currentTimeMillis(); @@ -88,7 +109,7 @@ public SQLiteDataSource(URI uri, ConnectionMixinFactory.ConnectionMixinOptions o } } } catch (ClassNotFoundException | UnsupportedOperationException | IOException | SQLException ex) { - throw new DataSourceException("An error occured while setting up a connection to the SQLite database", ex); + throw new DataSourceException("An error occurred while setting up a connection to the SQLite database", ex); } finally { if(DO_DISCONNECTS) { disconnect();