Skip to content

Commit

Permalink
Default to extract in the users' home folder in case the temp directo…
Browse files Browse the repository at this point in the history
…ry is not writable
  • Loading branch information
gnodet committed Apr 27, 2017
1 parent ed95784 commit 906cedb
Showing 1 changed file with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;

/**
Expand Down Expand Up @@ -190,10 +191,11 @@ private void doLoad() {

/* Try extracting the library from the jar */
if( classLoader!=null ) {
String targetLibName = version != null ? versionlibFilename : libFilename;
for ( String dir: specificDirs ) {
if( version!=null && extractAndLoad(errors, customPath, dir, versionlibFilename) )
if( version!=null && extractAndLoad(errors, customPath, dir, versionlibFilename, targetLibName) )
return;
if( extractAndLoad(errors, customPath, dir, libFilename) )
if( extractAndLoad(errors, customPath, dir, libFilename, targetLibName) )
return;
}
}
Expand Down Expand Up @@ -269,33 +271,32 @@ final public String[] getSpecificSearchDirs() {
};
}

private boolean extractAndLoad(ArrayList<Throwable> errors, String customPath, String dir, String libName) {
private boolean extractAndLoad(ArrayList<Throwable> errors, String customPath, String dir, String libName, String targetLibName) {
String resourcePath = "META-INF/native/" + ( dir == null ? "" : (dir + '/')) + libName;
URL resource = classLoader.getResource(resourcePath);
if( resource !=null ) {

int idx = libName.lastIndexOf('.');
String prefix = libName.substring(0, idx)+"-";
String suffix = libName.substring(idx);

if( customPath!=null ) {
// Try to extract it to the custom path...
File target = extract(errors, resource, prefix, suffix, file(customPath));
if( target!=null ) {
if( load(errors, target) ) {
return true;
int idx = targetLibName.lastIndexOf('.');
String prefix = targetLibName.substring(0, idx)+"-";
String suffix = targetLibName.substring(idx);

// Use the user provided path,
// then fallback to the java temp directory,
// and last, use the user home folder
for (File path : Arrays.asList(
customPath != null ? file(customPath) : null,
file(System.getProperty("java.io.tmpdir")),
file(System.getProperty("user.home"), ".hawtjni", name))) {
if( path!=null ) {
// Try to extract it to the custom path...
File target = extract(errors, resource, prefix, suffix, path);
if( target!=null ) {
if( load(errors, target) ) {
return true;
}
}
}
}

// Fall back to extracting to the tmp dir
customPath = System.getProperty("java.io.tmpdir");
File target = extract(errors, resource, prefix, suffix, file(customPath));
if( target!=null ) {
if( load(errors, target) ) {
return true;
}
}
}
return false;
}
Expand Down Expand Up @@ -327,8 +328,12 @@ private String map(String libName) {

private File extract(ArrayList<Throwable> errors, URL source, String prefix, String suffix, File directory) {
File target = null;
if (directory != null) {
directory = directory.getAbsoluteFile();
directory = directory.getAbsoluteFile();
if (!directory.exists()) {
if (!directory.mkdirs()) {
errors.add(new IOException("Unable to create directory: " + directory));
return null;
}
}
try {
FileOutputStream os = null;
Expand Down

0 comments on commit 906cedb

Please sign in to comment.