OSDN Git Service

git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/sqlite-jdbc@1662 ae02f08e...
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Sun, 14 Oct 2007 23:21:28 +0000 (23:21 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Sun, 14 Oct 2007 23:21:28 +0000 (23:21 +0000)
Makefile [new file with mode: 0644]
pom.xml
src/main/java/org/xerial/db/sql/sqlite/SQLiteJDBCLoader.java [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..8d8fad2
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,16 @@
+\r
+all: win mac linux\r
+\r
+win:\r
+       mvn -P win package\r
+       \r
+mac:\r
+       mvn -P mac package\r
+       \r
+linux:\r
+       mvn -P linux package\r
+       \r
+\r
+\r
+clean:\r
+       mvn clean\r
diff --git a/pom.xml b/pom.xml
index b14fc84..8c3c658 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -11,7 +11,7 @@
   <modelVersion>4.0.0</modelVersion>\r
   <groupId>org.xerial</groupId>\r
   <artifactId>sqlite-jdbc</artifactId>\r
-  <version>v037</version>\r
+  <version>${sqlite.os}-v037</version>\r
   <name>SQLite JDBC</name>\r
   <description>SQLite JDBC library</description>\r
 \r
   <!--  OS dependent settings for GWT compiler -->\r
   <profiles>\r
     <profile>\r
-      <id>windows</id>\r
+      <id>win</id>\r
       <properties>\r
-        <sqlite.lib>win</sqlite.lib>\r
+        <sqlite.os>win</sqlite.os>\r
       </properties>\r
     </profile>\r
     <profile>\r
       <id>linux</id>\r
       <properties>\r
-        <sqlite.lib>linux</sqlite.lib>\r
+        <sqlite.os>linux</sqlite.os>\r
       </properties>\r
     </profile>\r
     <profile>\r
       <id>mac</id>\r
       <properties>\r
-        <sqlite.lib>mac</sqlite.lib>\r
+        <sqlite.os>mac</sqlite.os>\r
       </properties>\r
     </profile>\r
   </profiles>\r
 \r
+  <build>\r
+    <resources>\r
+      <resource>\r
+        <directory>src/main/resources</directory>\r
+        <includes>\r
+          <include>native/${sqlite.os}/**</include>\r
+          <include>org/**</include>\r
+        </includes> \r
+      </resource>\r
+    </resources>\r
+  </build>  \r
+  \r
+\r
 \r
 </project>\r
 \r
diff --git a/src/main/java/org/xerial/db/sql/sqlite/SQLiteJDBCLoader.java b/src/main/java/org/xerial/db/sql/sqlite/SQLiteJDBCLoader.java
new file mode 100644 (file)
index 0000000..9222ce4
--- /dev/null
@@ -0,0 +1,127 @@
+//--------------------------------------\r
+// SQLite JDBC Project\r
+//\r
+// SQLite.java\r
+// Since: 2007/05/10\r
+//\r
+// $URL$ \r
+// $Author$\r
+//--------------------------------------\r
+package org.xerial.db.sql.sqlite;\r
+\r
+import java.io.File;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+\r
+/**\r
+ * Set the system properties, org.sqlite.lib.path, org.sqlite.lib.name, appropriately \r
+ * so that the SQLite JDBC driver can find *.dll, *.jnilib and *.so files, according   \r
+ * to the current OS (win, linux, mac).\r
+ * \r
+ * The library files are automatically extracted from this project's package (JAR).\r
+ *\r
+ * usage: call {@link #setSQLiteNativeLibraryPath()} before using SQLite JDBC driver.\r
+ * \r
+ * @author leo\r
+ *\r
+ */\r
+public class SQLiteJDBCLoader {\r
+\r
+    private static boolean extracted = false;\r
+    \r
+    private static boolean extractLibraryFile(String libraryName, String outputFileName)\r
+    {\r
+        File libFile = new File(outputFileName);\r
+\r
+        try\r
+        {\r
+            // extract file into the current directory\r
+            InputStream reader = SQLiteJDBCLoader.class.getResourceAsStream(libraryName);\r
+            FileOutputStream writer = new FileOutputStream(libFile);\r
+            byte[] buffer = new byte[1024];\r
+            int bytesRead = 0;\r
+            while ((bytesRead = reader.read(buffer)) != -1)\r
+            {\r
+                writer.write(buffer, 0, bytesRead);\r
+            }\r
+            \r
+            writer.close();\r
+            reader.close();\r
+            \r
+            if(!System.getProperty("os.name").contains("Windows"))\r
+            {\r
+                try {\r
+                    Runtime.getRuntime ().exec (new String []{"chmod", "755", outputFileName}).waitFor(); \r
+                } catch (Throwable e) {}\r
+            }\r
+            \r
+            return setNativeLibraryPath(null, outputFileName);\r
+        }\r
+        catch (IOException e)\r
+        {\r
+            return false;\r
+        }\r
+\r
+    }\r
+\r
+    private static boolean setNativeLibraryPath(String path, String name)\r
+    {\r
+        File libPath = new File(path, name);\r
+        if(libPath.exists())\r
+        {\r
+            System.setProperty("org.sqlite.lib.path", path == null ? "./" : path);\r
+            System.setProperty("org.sqlite.lib.name", name);\r
+            return true;\r
+        }\r
+        else\r
+            return false;\r
+    }\r
+\r
+    public static void setSQLiteNativeLibraryPath()\r
+    {\r
+        if (extracted)\r
+            return;\r
+\r
+        // Try loading library from org.sqlite.lib.path library path */\r
+        String sqliteNativeLibraryPath = System.getProperty("org.sqlite.lib.path");\r
+        String sqliteNativeLibraryName = System.getProperty("org.sqlite.lib.name");\r
+        if (sqliteNativeLibraryName == null)\r
+            sqliteNativeLibraryName = System.mapLibraryName("sqlitejdbc");\r
+\r
+        if (setNativeLibraryPath(sqliteNativeLibraryPath, sqliteNativeLibraryName))\r
+        {\r
+            extracted = true;\r
+            return;\r
+        }\r
+\r
+        // Load the os-dependent library from a jar file\r
+        String osName = System.getProperty("os.name");\r
+        if (osName.contains("Windows"))\r
+        {\r
+            sqliteNativeLibraryPath = "/sqlite/win32";\r
+        }\r
+        else if (osName.contains("Mac"))\r
+        {\r
+            sqliteNativeLibraryPath = "/sqlite/mac";\r
+        }\r
+        /*\r
+         * else if (osName.contains("Linux")) { sqliteNativeLibraryPath =\r
+         * "/sqlite/linux"; }\r
+         */\r
+        else\r
+            throw new UnsupportedOperationException("unsupported OS for SQLite-JDBC driver: " + osName);\r
+\r
+        /* Try extracting and loading library from jar */\r
+        if (extractLibraryFile(sqliteNativeLibraryPath + "/" + sqliteNativeLibraryName,\r
+                sqliteNativeLibraryName))\r
+        {\r
+            extracted = true;\r
+            return;\r
+        }\r
+\r
+        extracted = false;\r
+        return;\r
+    }\r
+\r
+}\r