From 67f057f6af35addb7463e4033849089b22f8a799 Mon Sep 17 00:00:00 2001 From: Adam Murdoch Date: Mon, 3 Sep 2012 14:05:01 +1000 Subject: [PATCH] Extracted the strategy for finding native library out of Native. --- .../net/rubygrapefruit/platform/Native.java | 53 ++-------------- .../internal/NativeLibraryLocator.java | 63 +++++++++++++++++++ 2 files changed, 69 insertions(+), 47 deletions(-) create mode 100644 src/main/java/net/rubygrapefruit/platform/internal/NativeLibraryLocator.java diff --git a/src/main/java/net/rubygrapefruit/platform/Native.java b/src/main/java/net/rubygrapefruit/platform/Native.java index ee299e0..fb08944 100755 --- a/src/main/java/net/rubygrapefruit/platform/Native.java +++ b/src/main/java/net/rubygrapefruit/platform/Native.java @@ -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); - } - } } diff --git a/src/main/java/net/rubygrapefruit/platform/internal/NativeLibraryLocator.java b/src/main/java/net/rubygrapefruit/platform/internal/NativeLibraryLocator.java new file mode 100644 index 0000000..63f415a --- /dev/null +++ b/src/main/java/net/rubygrapefruit/platform/internal/NativeLibraryLocator.java @@ -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); + } + } +}