Some tweaks to error messages for unsupported platforms.
This commit is contained in:
@@ -51,7 +51,7 @@ public class Native {
|
|||||||
if (loader == null) {
|
if (loader == null) {
|
||||||
Platform platform = Platform.current();
|
Platform platform = Platform.current();
|
||||||
try {
|
try {
|
||||||
loader = new NativeLibraryLoader(new NativeLibraryLocator(extractDir));
|
loader = new NativeLibraryLoader(platform, new NativeLibraryLocator(extractDir));
|
||||||
loader.load(platform.getLibraryName());
|
loader.load(platform.getLibraryName());
|
||||||
int nativeVersion = NativeLibraryFunctions.getVersion();
|
int nativeVersion = NativeLibraryFunctions.getVersion();
|
||||||
if (nativeVersion != NativeLibraryFunctions.VERSION) {
|
if (nativeVersion != NativeLibraryFunctions.VERSION) {
|
||||||
|
|||||||
21
src/main/java/net/rubygrapefruit/platform/internal/NativeLibraryLoader.java
Normal file → Executable file
21
src/main/java/net/rubygrapefruit/platform/internal/NativeLibraryLoader.java
Normal file → Executable file
@@ -25,28 +25,29 @@ import java.util.Set;
|
|||||||
|
|
||||||
public class NativeLibraryLoader {
|
public class NativeLibraryLoader {
|
||||||
private final Set<String> loaded = new HashSet<String>();
|
private final Set<String> loaded = new HashSet<String>();
|
||||||
private final NativeLibraryLocator locator;
|
private final Platform platform;
|
||||||
|
private final NativeLibraryLocator nativeLibraryLocator;
|
||||||
|
|
||||||
public NativeLibraryLoader(NativeLibraryLocator locator) {
|
public NativeLibraryLoader(Platform platform, NativeLibraryLocator nativeLibraryLocator) {
|
||||||
this.locator = locator;
|
this.platform = platform;
|
||||||
|
this.nativeLibraryLocator = nativeLibraryLocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void load(String name) {
|
public void load(String libraryFileName) {
|
||||||
if (loaded.contains(name)) {
|
if (loaded.contains(libraryFileName)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
File libFile = locator.find(name);
|
File libFile = nativeLibraryLocator.find(libraryFileName);
|
||||||
if (libFile == null) {
|
if (libFile == null) {
|
||||||
throw new NativeIntegrationUnavailableException(String.format(
|
throw new NativeIntegrationUnavailableException(String.format("Native library is not available for %s.", platform));
|
||||||
"Native library is not available for this operating system and architecture."));
|
|
||||||
}
|
}
|
||||||
System.load(libFile.getCanonicalPath());
|
System.load(libFile.getCanonicalPath());
|
||||||
} catch (NativeException e) {
|
} catch (NativeException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
throw new NativeException(String.format("Failed to load native library '%s'.", name), t);
|
throw new NativeException(String.format("Failed to load native library '%s' for %s.", libraryFileName, platform), t);
|
||||||
}
|
}
|
||||||
loaded.add(name);
|
loaded.add(libraryFileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/main/java/net/rubygrapefruit/platform/internal/NativeLibraryLocator.java
Normal file → Executable file
12
src/main/java/net/rubygrapefruit/platform/internal/NativeLibraryLocator.java
Normal file → Executable file
@@ -30,9 +30,9 @@ public class NativeLibraryLocator {
|
|||||||
this.extractDir = extractDir;
|
this.extractDir = extractDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
public File find(String libraryName) throws IOException {
|
public File find(String libraryFileName) throws IOException {
|
||||||
if (extractDir != null) {
|
if (extractDir != null) {
|
||||||
File libFile = new File(extractDir, String.format("%s/%s", NativeLibraryFunctions.VERSION, libraryName));
|
File libFile = new File(extractDir, String.format("%s/%s", NativeLibraryFunctions.VERSION, libraryFileName));
|
||||||
File lockFile = new File(libFile.getParentFile(), libFile.getName() + ".lock");
|
File lockFile = new File(libFile.getParentFile(), libFile.getName() + ".lock");
|
||||||
lockFile.getParentFile().mkdirs();
|
lockFile.getParentFile().mkdirs();
|
||||||
lockFile.createNewFile();
|
lockFile.createNewFile();
|
||||||
@@ -44,7 +44,7 @@ public class NativeLibraryLocator {
|
|||||||
// Library has been extracted
|
// Library has been extracted
|
||||||
return libFile;
|
return libFile;
|
||||||
}
|
}
|
||||||
URL resource = getClass().getClassLoader().getResource(libraryName);
|
URL resource = getClass().getClassLoader().getResource(libraryFileName);
|
||||||
if (resource != null) {
|
if (resource != null) {
|
||||||
// Extract library and write marker to lock file
|
// Extract library and write marker to lock file
|
||||||
libFile.getParentFile().mkdirs();
|
libFile.getParentFile().mkdirs();
|
||||||
@@ -58,20 +58,20 @@ public class NativeLibraryLocator {
|
|||||||
lockFileAccess.close();
|
lockFileAccess.close();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
URL resource = getClass().getClassLoader().getResource(libraryName);
|
URL resource = getClass().getClassLoader().getResource(libraryFileName);
|
||||||
if (resource != null) {
|
if (resource != null) {
|
||||||
File libFile;
|
File libFile;
|
||||||
File libDir = File.createTempFile("native-platform", "dir");
|
File libDir = File.createTempFile("native-platform", "dir");
|
||||||
libDir.delete();
|
libDir.delete();
|
||||||
libDir.mkdirs();
|
libDir.mkdirs();
|
||||||
libFile = new File(libDir, libraryName);
|
libFile = new File(libDir, libraryFileName);
|
||||||
libFile.deleteOnExit();
|
libFile.deleteOnExit();
|
||||||
copy(resource, libFile);
|
copy(resource, libFile);
|
||||||
return libFile;
|
return libFile;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
File libFile = new File("build/binaries/" + libraryName);
|
File libFile = new File("build/binaries/" + libraryFileName);
|
||||||
if (libFile.isFile()) {
|
if (libFile.isFile()) {
|
||||||
return libFile;
|
return libFile;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,13 +67,11 @@ public abstract class Platform {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <T extends NativeIntegration> T get(Class<T> type, NativeLibraryLoader nativeLibraryLoader) {
|
public <T extends NativeIntegration> T get(Class<T> type, NativeLibraryLoader nativeLibraryLoader) {
|
||||||
throw new NativeIntegrationUnavailableException(String.format("Native integration %s is not supported for the current operating system (%s)",
|
throw new NativeIntegrationUnavailableException(String.format("Native integration %s is not supported for %s.", type.getSimpleName(), toString()));
|
||||||
type.getSimpleName(), toString()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLibraryName() {
|
public String getLibraryName() {
|
||||||
throw new NativeIntegrationUnavailableException(String.format(
|
throw new NativeIntegrationUnavailableException(String.format("Native integration is not available for %s.", toString()));
|
||||||
"Native integration is not available for the current operating system (%s)", toString()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getOperatingSystem() {
|
private static String getOperatingSystem() {
|
||||||
|
|||||||
Reference in New Issue
Block a user