jna/jna-3.5.0-loadlibrary.patch

162 lines
6.9 KiB
Diff
Raw Normal View History

2010-08-07 16:56:37 +00:00
diff -up ./src/com/sun/jna/Native.java.loadlib ./src/com/sun/jna/Native.java
2012-10-26 11:31:17 +00:00
--- ./src/com/sun/jna/Native.java.loadlib 2012-10-19 03:53:11.000000000 +0200
+++ ./src/com/sun/jna/Native.java 2012-10-26 11:36:51.028999957 +0200
@@ -641,152 +641,18 @@ public final class Native {
2010-08-07 16:56:37 +00:00
/**
2012-03-07 11:20:26 +00:00
* Loads the JNA stub library.
- * First tries jna.boot.library.path, then the system path, then from the
- * jar file.
+ * MODIFIED FROM UPSTREAM - we rip out all sorts of gunk here that is
+ * unnecessary when JNA is properly installed with the OS.
2010-08-07 16:56:37 +00:00
*/
private static void loadNativeLibrary() {
2012-03-07 11:20:26 +00:00
removeTemporaryFiles();
- String libName = System.getProperty("jna.boot.library.name", "jnidispatch");
2010-08-07 16:56:37 +00:00
- String bootPath = System.getProperty("jna.boot.library.path");
- if (bootPath != null) {
2012-03-07 11:20:26 +00:00
- // String.split not available in 1.4
- StringTokenizer dirs = new StringTokenizer(bootPath, File.pathSeparator);
- while (dirs.hasMoreTokens()) {
- String dir = dirs.nextToken();
- File file = new File(new File(dir), System.mapLibraryName(libName));
- String path = file.getAbsolutePath();
- if (file.exists()) {
- try {
- System.load(path);
- nativeLibraryPath = path;
- return;
- } catch (UnsatisfiedLinkError ex) {
- // Not a problem if already loaded in anoteher class loader
- // Unfortunately we can't distinguish the difference...
- //System.out.println("File found at " + file + " but not loadable: " + ex.getMessage());
- }
2010-08-07 16:56:37 +00:00
- }
- if (Platform.isMac()) {
- String orig, ext;
- if (path.endsWith("dylib")) {
- orig = "dylib";
- ext = "jnilib";
- } else {
- orig = "jnilib";
- ext = "dylib";
- }
2012-03-07 11:20:26 +00:00
- path = path.substring(0, path.lastIndexOf(orig)) + ext;
- if (new File(path).exists()) {
- try {
- System.load(path);
- nativeLibraryPath = path;
- return;
- } catch (UnsatisfiedLinkError ex) {
- System.err.println("File found at " + path + " but not loadable: " + ex.getMessage());
- }
2010-08-07 16:56:37 +00:00
- }
- }
- }
2012-10-26 11:31:17 +00:00
- }
- if (Platform.isAndroid()) {
- // Native libraries on android must be bundled with the APK
- System.setProperty("jna.nounpack", "true");
2010-08-07 16:56:37 +00:00
- }
try {
2012-03-07 11:20:26 +00:00
- if (!Boolean.getBoolean("jna.nosys")) {
- System.loadLibrary(libName);
- return;
- }
2010-08-07 16:56:37 +00:00
+ System.load("@JNIPATH@/" + System.mapLibraryName("jnidispatch"));
+ nativeLibraryPath = "@JNIPATH@/" + System.mapLibraryName("jnidispatch");
}
catch(UnsatisfiedLinkError e) {
2012-03-07 11:20:26 +00:00
- if (Boolean.getBoolean("jna.nounpack")) {
- throw e;
- }
- }
- if (!Boolean.getBoolean("jna.nounpack")) {
2010-08-07 16:56:37 +00:00
- loadNativeLibraryFromJar();
2012-03-07 11:20:26 +00:00
- return;
2010-08-07 16:56:37 +00:00
- }
2012-03-07 11:20:26 +00:00
- throw new UnsatisfiedLinkError("Native jnidispatch library not found");
2010-08-07 16:56:37 +00:00
- }
-
- /**
- * Attempts to load the native library resource from the filesystem,
- * extracting the JNA stub library from jna.jar if not already available.
- */
- private static void loadNativeLibraryFromJar() {
- String libname = System.mapLibraryName("jnidispatch");
- String arch = System.getProperty("os.arch");
- String name = System.getProperty("os.name");
- String resourceName = getNativeLibraryResourcePath(Platform.getOSType(), arch, name) + "/" + libname;
- URL url = Native.class.getResource(resourceName);
2012-03-07 11:20:26 +00:00
- boolean unpacked = false;
2010-08-07 16:56:37 +00:00
-
- // Add an ugly hack for OpenJDK (soylatte) - JNI libs use the usual
- // .dylib extension
- if (url == null && Platform.isMac()
- && resourceName.endsWith(".dylib")) {
- resourceName = resourceName.substring(0, resourceName.lastIndexOf(".dylib")) + ".jnilib";
- url = Native.class.getResource(resourceName);
- }
- if (url == null) {
2012-10-26 11:31:17 +00:00
- throw new UnsatisfiedLinkError("JNA native support (" + resourceName
2010-08-07 16:56:37 +00:00
- + ") not found in resource path");
- }
-
- File lib = null;
- if (url.getProtocol().toLowerCase().equals("file")) {
- try {
- lib = new File(new URI(url.toString()));
- }
- catch(URISyntaxException e) {
- lib = new File(url.getPath());
- }
- if (!lib.exists()) {
- throw new Error("File URL " + url + " could not be properly decoded");
- }
- }
- else {
- InputStream is = Native.class.getResourceAsStream(resourceName);
- if (is == null) {
- throw new Error("Can't obtain jnidispatch InputStream");
- }
-
- FileOutputStream fos = null;
- try {
- // Suffix is required on windows, or library fails to load
- // Let Java pick the suffix, except on windows, to avoid
- // problems with Web Start.
2012-03-07 11:20:26 +00:00
- File dir = getTempDir();
- lib = File.createTempFile("jna", Platform.isWindows()?".dll":null, dir);
2010-08-07 16:56:37 +00:00
- lib.deleteOnExit();
- fos = new FileOutputStream(lib);
- int count;
- byte[] buf = new byte[1024];
- while ((count = is.read(buf, 0, buf.length)) > 0) {
- fos.write(buf, 0, count);
- }
2012-03-07 11:20:26 +00:00
- unpacked = true;
2010-08-07 16:56:37 +00:00
- }
- catch(IOException e) {
2012-10-26 11:31:17 +00:00
- throw new Error("Failed to create temporary file for jnidispatch library", e);
2010-08-07 16:56:37 +00:00
- }
- finally {
- try { is.close(); } catch(IOException e) { }
- if (fos != null) {
- try { fos.close(); } catch(IOException e) { }
- }
- }
2012-03-07 11:20:26 +00:00
- }
2010-08-07 16:56:37 +00:00
- System.load(lib.getAbsolutePath());
- nativeLibraryPath = lib.getAbsolutePath();
2012-03-07 11:20:26 +00:00
- // Attempt to delete immediately once jnidispatch is successfully
- // loaded. This avoids the complexity of trying to do so on "exit",
- // which point can vary under different circumstances (native
- // compilation, dynamically loaded modules, normal application, etc).
- if (unpacked) {
- deleteNativeLibrary(lib.getAbsolutePath());
+ throw new RuntimeException(e);
}
2010-08-07 16:56:37 +00:00
}