-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate and package jni lib in jar + load lib from jar without assum…
…ing it in java.library.path
- Loading branch information
Showing
4 changed files
with
228 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<assembly> | ||
<id>all</id> | ||
<formats> | ||
<format>jar</format> | ||
</formats> | ||
|
||
<includeBaseDirectory>false</includeBaseDirectory> | ||
<dependencySets> | ||
<dependencySet> | ||
<excludes> | ||
<exclude>com.scurrilous</exclude> | ||
<exclude>*:nar:*</exclude> | ||
</excludes> | ||
</dependencySet> | ||
</dependencySets> | ||
<fileSets> | ||
<fileSet> | ||
<directory>${project.build.directory}/nar/${project.artifactId}-${project.version}-${os.arch}-MacOSX-gpp-jni/lib/${os.arch}-MacOSX-gpp/jni | ||
</directory> | ||
<outputDirectory>lib</outputDirectory> | ||
<includes> | ||
<include>lib*</include> | ||
</includes> | ||
</fileSet> | ||
<fileSet> | ||
<directory>${project.build.directory}/nar/${project.artifactId}-${project.version}-${os.arch}-Linux-gpp-jni/lib/${os.arch}-Linux-gpp/jni | ||
</directory> | ||
<outputDirectory>lib</outputDirectory> | ||
<includes> | ||
<include>lib*</include> | ||
</includes> | ||
</fileSet> | ||
<fileSet> | ||
<directory>${project.build.directory}/nar/${project.artifactId}-${project.version}-${os.arch}-${os.name}-gpp-jni/lib/${os.arch}-${os.name}-gpp/jni | ||
</directory> | ||
<outputDirectory>lib</outputDirectory> | ||
<includes> | ||
<include>lib*</include> | ||
</includes> | ||
</fileSet> | ||
<fileSet> | ||
<directory>${project.build.directory}/classes</directory> | ||
<outputDirectory></outputDirectory> | ||
<includes> | ||
<include>**/*</include> | ||
</includes> | ||
</fileSet> | ||
</fileSets> | ||
</assembly> |
90 changes: 90 additions & 0 deletions
90
circe-crc32c-sse42/src/main/java/com/scurrilous/circe/crc/LibraryUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.scurrilous.circe.crc; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.Locale; | ||
|
||
/** | ||
* provides utility to load libraries at runtime. | ||
* | ||
*/ | ||
public class LibraryUtils { | ||
|
||
public static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.US); | ||
|
||
/** | ||
* loads given library from the this jar. ie: this jar contains: | ||
* /lib/libcirce-crc32c-sse42.jnilib | ||
* | ||
* @param path | ||
* : absolute path of the library in the jar <br/> | ||
* if this jar contains: /lib/libcirce-crc32c-sse42.jnilib then | ||
* provide the same absolute path as input | ||
* @throws Exception | ||
*/ | ||
public static void loadLibraryFromJar(String path) throws Exception { | ||
|
||
if (!path.startsWith("/")) { | ||
throw new IllegalArgumentException("absolute path must start with /"); | ||
} | ||
|
||
String[] parts = path.split("/"); | ||
String filename = (parts.length > 0) ? parts[parts.length - 1] : null; | ||
|
||
File dir = File.createTempFile("native", ""); | ||
dir.delete(); | ||
if (!(dir.mkdir())) { | ||
throw new IOException("Failed to create temp directory " + dir.getAbsolutePath()); | ||
} | ||
dir.deleteOnExit(); | ||
File temp = new File(dir, filename); | ||
temp.deleteOnExit(); | ||
|
||
byte[] buffer = new byte[1024]; | ||
int read; | ||
|
||
InputStream input = LibraryUtils.class.getResourceAsStream(path); | ||
if (input == null) { | ||
throw new FileNotFoundException("Couldn't find file into jar " + path); | ||
} | ||
|
||
OutputStream out = new FileOutputStream(temp); | ||
try { | ||
while ((read = input.read(buffer)) != -1) { | ||
out.write(buffer, 0, read); | ||
} | ||
} finally { | ||
out.close(); | ||
input.close(); | ||
} | ||
|
||
if (!temp.exists()) { | ||
throw new FileNotFoundException("Failed to copy file from jar at " + temp.getAbsolutePath()); | ||
} | ||
|
||
System.load(temp.getAbsolutePath()); | ||
} | ||
|
||
/** | ||
* Returns jni library extension based on OS specification. Maven-nar | ||
* generates jni library based on different OS : | ||
* http://mark.donszelmann.org/maven-nar-plugin/aol.html (jni.extension) | ||
* | ||
* @return library type based on operating system | ||
*/ | ||
public static String libType() { | ||
|
||
if (OS_NAME.indexOf("mac") >= 0) { | ||
return "jnilib"; | ||
} else if (OS_NAME.indexOf("nix") >= 0 || OS_NAME.indexOf("nux") >= 0 || OS_NAME.indexOf("aix") > 0) { | ||
return "so"; | ||
} else if (OS_NAME.indexOf("win") >= 0) { | ||
return "dll"; | ||
} | ||
throw new TypeNotPresentException(OS_NAME + " not supported", null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters