Extracted the strategy for finding native library out of Native.
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
package net.rubygrapefruit.platform;
|
||||
|
||||
import net.rubygrapefruit.platform.internal.*;
|
||||
import net.rubygrapefruit.platform.internal.NativeLibraryLocator;
|
||||
import net.rubygrapefruit.platform.internal.Platform;
|
||||
import net.rubygrapefruit.platform.internal.jni.NativeLibraryFunctions;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Provides access to the native integrations. Use {@link #get(Class)} to load a particular integration.
|
||||
@@ -27,30 +27,14 @@ public class Native {
|
||||
if (!loaded) {
|
||||
Platform platform = Platform.current();
|
||||
try {
|
||||
File libFile;
|
||||
URL resource = Native.class.getClassLoader().getResource(platform.getLibraryName());
|
||||
if (resource != null) {
|
||||
File libDir = extractDir;
|
||||
if (libDir == null) {
|
||||
libDir = File.createTempFile("native-platform", "dir");
|
||||
libDir.delete();
|
||||
libDir.mkdirs();
|
||||
}
|
||||
libFile = new File(libDir, platform.getLibraryName());
|
||||
libFile.deleteOnExit();
|
||||
copy(resource, libFile);
|
||||
} else {
|
||||
libFile = new File("build/binaries/" + platform.getLibraryName());
|
||||
}
|
||||
if (!libFile.isFile()) {
|
||||
File libFile = new NativeLibraryLocator(extractDir).find(platform.getLibraryName());
|
||||
if (libFile == null) {
|
||||
throw new NativeIntegrationUnavailableException(String.format("Native library is not available for this operating system and architecture."));
|
||||
}
|
||||
System.load(libFile.getCanonicalPath());
|
||||
int nativeVersion = NativeLibraryFunctions.getVersion();
|
||||
if (nativeVersion != NativeLibraryFunctions.VERSION) {
|
||||
throw new NativeException(String.format(
|
||||
"Unexpected native library version loaded. Expected %s, was %s.", nativeVersion,
|
||||
NativeLibraryFunctions.VERSION));
|
||||
throw new NativeException(String.format("Unexpected native library version loaded. Expected %s, was %s.", nativeVersion, NativeLibraryFunctions.VERSION));
|
||||
}
|
||||
loaded = true;
|
||||
} catch (NativeException e) {
|
||||
@@ -82,29 +66,4 @@ public class Native {
|
||||
throw new NativeException(String.format("Failed to load native integration %s.", type.getSimpleName()), t);
|
||||
}
|
||||
}
|
||||
|
||||
private static void copy(URL source, File dest) {
|
||||
try {
|
||||
InputStream inputStream = source.openStream();
|
||||
try {
|
||||
OutputStream outputStream = new FileOutputStream(dest);
|
||||
try {
|
||||
byte[] buffer = new byte[4096];
|
||||
while (true) {
|
||||
int nread = inputStream.read(buffer);
|
||||
if (nread < 0) {
|
||||
break;
|
||||
}
|
||||
outputStream.write(buffer, 0, nread);
|
||||
}
|
||||
} finally {
|
||||
outputStream.close();
|
||||
}
|
||||
} finally {
|
||||
inputStream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new NativeException(String.format("Could not extract native JNI library."), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package net.rubygrapefruit.platform.internal;
|
||||
|
||||
import net.rubygrapefruit.platform.NativeException;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
|
||||
public class NativeLibraryLocator {
|
||||
private final File extractDir;
|
||||
|
||||
public NativeLibraryLocator(File extractDir) {
|
||||
this.extractDir = extractDir;
|
||||
}
|
||||
|
||||
public File find(String libraryName) throws IOException {
|
||||
File libFile;
|
||||
URL resource = getClass().getClassLoader().getResource(libraryName);
|
||||
if (resource != null) {
|
||||
File libDir = extractDir;
|
||||
if (libDir == null) {
|
||||
libDir = File.createTempFile("native-platform", "dir");
|
||||
libDir.delete();
|
||||
libDir.mkdirs();
|
||||
}
|
||||
libFile = new File(libDir, libraryName);
|
||||
libFile.deleteOnExit();
|
||||
copy(resource, libFile);
|
||||
return libFile;
|
||||
}
|
||||
|
||||
libFile = new File("build/binaries/" + libraryName);
|
||||
if (libFile.isFile()) {
|
||||
return libFile;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void copy(URL source, File dest) {
|
||||
try {
|
||||
InputStream inputStream = source.openStream();
|
||||
try {
|
||||
OutputStream outputStream = new FileOutputStream(dest);
|
||||
try {
|
||||
byte[] buffer = new byte[4096];
|
||||
while (true) {
|
||||
int nread = inputStream.read(buffer);
|
||||
if (nread < 0) {
|
||||
break;
|
||||
}
|
||||
outputStream.write(buffer, 0, nread);
|
||||
}
|
||||
} finally {
|
||||
outputStream.close();
|
||||
}
|
||||
} finally {
|
||||
inputStream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new NativeException(String.format("Could not extract native JNI library."), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user