From 1fa8d1afec2fb181706a29ac5b4d5d0750a03dd2 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 10 Mar 2008 07:24:46 +0000 Subject: [PATCH] git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/sqlite-jdbc@2025 ae02f08e-27ec-0310-ae8c-8ba02fe2eafd --- Makefile | 12 +-- .../org/xerial/db/sql/sqlite/SQLiteJDBCLoader.java | 100 ++++++++++++++++----- src/main/resources/native/linux/md5sum | 1 - src/main/resources/native/mac/md5sum | 1 - src/main/resources/native/win/md5sum | 1 - 5 files changed, 83 insertions(+), 32 deletions(-) delete mode 100644 src/main/resources/native/linux/md5sum delete mode 100644 src/main/resources/native/mac/md5sum delete mode 100644 src/main/resources/native/win/md5sum diff --git a/Makefile b/Makefile index a524037..ac458fa 100644 --- a/Makefile +++ b/Makefile @@ -12,14 +12,14 @@ package: LIBDIR=src/main/resources/native -win: $(LIBDIR)/win/sqlitejdbc.dll - $(shell md5sum -b $< | cut -f 1 -d ' ' > $(LIBDIR)/win/md5sum) +#win: $(LIBDIR)/win/sqlitejdbc.dll +# $(shell md5sum -b $< | cut -f 1 -d ' ' > $(LIBDIR)/win/md5sum) -mac: $(LIBDIR)/win/sqlitejdbc.dll - $(shell md5sum -b $< | cut -f 1 -d ' ' > $(LIBDIR)/mac/md5sum) +#mac: $(LIBDIR)/win/sqlitejdbc.dll +# $(shell md5sum -b $< | cut -f 1 -d ' ' > $(LIBDIR)/mac/md5sum) -linux: $(LIBDIR)/linux/libsqlitejdbc.so - $(shell md5sum -b $< | cut -f 1 -d ' ' > $(LIBDIR)/linux/md5sum) +#linux: $(LIBDIR)/linux/libsqlitejdbc.so +# $(shell md5sum -b $< | cut -f 1 -d ' ' > $(LIBDIR)/linux/md5sum) native: diff --git a/src/main/java/org/xerial/db/sql/sqlite/SQLiteJDBCLoader.java b/src/main/java/org/xerial/db/sql/sqlite/SQLiteJDBCLoader.java index 32da908..25096e6 100644 --- a/src/main/java/org/xerial/db/sql/sqlite/SQLiteJDBCLoader.java +++ b/src/main/java/org/xerial/db/sql/sqlite/SQLiteJDBCLoader.java @@ -24,10 +24,16 @@ //-------------------------------------- package org.xerial.db.sql.sqlite; +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.security.DigestInputStream; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; /** * Set the system properties, org.sqlite.lib.path, org.sqlite.lib.name, @@ -61,45 +67,94 @@ public class SQLiteJDBCLoader return extracted; } - private static boolean extractLibraryFile(String libraryResourcePath, String libraryFolder, String libraryFileName) + private static String md5sum(InputStream input) throws IOException, NoSuchAlgorithmException { - File libFile = new File(libraryFolder, libraryFileName); + BufferedInputStream in = new BufferedInputStream(input); try { - if (!libFile.exists()) + MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); + DigestInputStream digestInputStream = new DigestInputStream(in, digest); + for (; digestInputStream.read() >= 0;) { - // extract file into the current directory - InputStream reader = SQLiteJDBCLoader.class.getResourceAsStream(libraryResourcePath); - FileOutputStream writer = new FileOutputStream(libFile); - byte[] buffer = new byte[1024]; - int bytesRead = 0; - while ((bytesRead = reader.read(buffer)) != -1) - { - writer.write(buffer, 0, bytesRead); - } - writer.close(); - reader.close(); + } + ByteArrayOutputStream md5out = new ByteArrayOutputStream(); + md5out.write(digest.digest()); + return md5out.toString(); + } + finally + { + in.close(); + } + } - if (!System.getProperty("os.name").contains("Windows")) + private static boolean extractLibraryFile(String libFolderForCurrentOS, String libraryFileName, String targetFolder) + { + String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName; + + File extractedLibFile = new File(targetFolder, libraryFileName); + + try + { + if (extractedLibFile.exists()) + { + // test md5sum value + String md5sum1 = md5sum(SQLiteJDBCLoader.class.getResourceAsStream(nativeLibraryFilePath)); + String md5sum2 = md5sum(new FileInputStream(extractedLibFile)); + + if (md5sum1.equals(md5sum2)) { - try + return loadNativeLibrary(targetFolder, libraryFileName); + } + else + { + // remove old native library file + boolean deletionSucceeded = extractedLibFile.delete(); + if (!deletionSucceeded) { - Runtime.getRuntime().exec(new String[] { "chmod", "755", libFile.getAbsolutePath() }).waitFor(); + throw new IOException("failed to remove existing native library file: " + + extractedLibFile.getAbsolutePath()); } - catch (Throwable e) - {} } } - return loadNativeLibrary(libraryFolder, libraryFileName); + // extract file into the current directory + InputStream reader = SQLiteJDBCLoader.class.getResourceAsStream(nativeLibraryFilePath); + FileOutputStream writer = new FileOutputStream(extractedLibFile); + byte[] buffer = new byte[1024]; + int bytesRead = 0; + while ((bytesRead = reader.read(buffer)) != -1) + { + writer.write(buffer, 0, bytesRead); + } + + writer.close(); + reader.close(); + + if (!System.getProperty("os.name").contains("Windows")) + { + try + { + Runtime.getRuntime().exec(new String[] { "chmod", "755", extractedLibFile.getAbsolutePath() }) + .waitFor(); + } + catch (Throwable e) + {} + } + + return loadNativeLibrary(targetFolder, libraryFileName); } catch (IOException e) { System.err.println(e.getMessage()); return false; } + catch (NoSuchAlgorithmException e) + { + System.err.println(e.getMessage()); + return false; + } } @@ -166,10 +221,9 @@ public class SQLiteJDBCLoader throw new UnsupportedOperationException("unsupported OS for SQLite-JDBC driver: " + osName); // temporary library folder - String libraryFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath(); + String tempFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath(); /* Try extracting thelibrary from jar */ - if (extractLibraryFile(sqliteNativeLibraryPath + "/" + sqliteNativeLibraryName, libraryFolder, - sqliteNativeLibraryName)) + if (extractLibraryFile(sqliteNativeLibraryPath, sqliteNativeLibraryName, tempFolder)) { extracted = true; return; diff --git a/src/main/resources/native/linux/md5sum b/src/main/resources/native/linux/md5sum deleted file mode 100644 index aafb143..0000000 --- a/src/main/resources/native/linux/md5sum +++ /dev/null @@ -1 +0,0 @@ -81960645eb1e717b47827e5c2abed55d diff --git a/src/main/resources/native/mac/md5sum b/src/main/resources/native/mac/md5sum deleted file mode 100644 index 510d536..0000000 --- a/src/main/resources/native/mac/md5sum +++ /dev/null @@ -1 +0,0 @@ -ff28263497922882efa8307efea473ef diff --git a/src/main/resources/native/win/md5sum b/src/main/resources/native/win/md5sum deleted file mode 100644 index 510d536..0000000 --- a/src/main/resources/native/win/md5sum +++ /dev/null @@ -1 +0,0 @@ -ff28263497922882efa8307efea473ef -- 2.11.0