Some tweaks to error messages for unsupported platforms.

This commit is contained in:
Adam Murdoch
2013-02-07 16:58:33 +11:00
parent 9e0493f94e
commit ff3abfd4fd
4 changed files with 257 additions and 258 deletions

View File

@@ -1,96 +1,96 @@
/* /*
* Copyright 2012 Adam Murdoch * Copyright 2012 Adam Murdoch
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package net.rubygrapefruit.platform; package net.rubygrapefruit.platform;
import net.rubygrapefruit.platform.internal.NativeLibraryLoader; import net.rubygrapefruit.platform.internal.NativeLibraryLoader;
import net.rubygrapefruit.platform.internal.NativeLibraryLocator; import net.rubygrapefruit.platform.internal.NativeLibraryLocator;
import net.rubygrapefruit.platform.internal.Platform; import net.rubygrapefruit.platform.internal.Platform;
import net.rubygrapefruit.platform.internal.jni.NativeLibraryFunctions; import net.rubygrapefruit.platform.internal.jni.NativeLibraryFunctions;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
* Provides access to the native integrations. Use {@link #get(Class)} to load a particular integration. * Provides access to the native integrations. Use {@link #get(Class)} to load a particular integration.
*/ */
@ThreadSafe @ThreadSafe
public class Native { public class Native {
private static NativeLibraryLoader loader; private static NativeLibraryLoader loader;
private static final Map<Class<?>, Object> integrations = new HashMap<Class<?>, Object>(); private static final Map<Class<?>, Object> integrations = new HashMap<Class<?>, Object>();
private Native() { private Native() {
} }
/** /**
* Initialises the native integration, if not already initialized. * Initialises the native integration, if not already initialized.
* *
* @param extractDir The directory to extract native resources into. May be null, in which case a default is * @param extractDir The directory to extract native resources into. May be null, in which case a default is
* selected. * selected.
* *
* @throws NativeIntegrationUnavailableException When native integration is not available on the current machine. * @throws NativeIntegrationUnavailableException When native integration is not available on the current machine.
* @throws NativeException On failure to load the native integration. * @throws NativeException On failure to load the native integration.
*/ */
@ThreadSafe @ThreadSafe
static public void init(File extractDir) throws NativeIntegrationUnavailableException, NativeException { static public void init(File extractDir) throws NativeIntegrationUnavailableException, NativeException {
synchronized (Native.class) { synchronized (Native.class) {
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) {
throw new NativeException(String.format("Unexpected native library version loaded. Expected %s, was %s.", NativeLibraryFunctions.VERSION, nativeVersion)); throw new NativeException(String.format("Unexpected native library version loaded. Expected %s, was %s.", NativeLibraryFunctions.VERSION, nativeVersion));
} }
} catch (NativeException e) { } catch (NativeException e) {
throw e; throw e;
} catch (Throwable t) { } catch (Throwable t) {
throw new NativeException("Failed to initialise native integration.", t); throw new NativeException("Failed to initialise native integration.", t);
} }
} }
} }
} }
/** /**
* Locates a native integration of the given type. * Locates a native integration of the given type.
* *
* @return The native integration. Never returns null. * @return The native integration. Never returns null.
* @throws NativeIntegrationUnavailableException When the given native integration is not available on the current * @throws NativeIntegrationUnavailableException When the given native integration is not available on the current
* machine. * machine.
* @throws NativeException On failure to load the native integration. * @throws NativeException On failure to load the native integration.
*/ */
@ThreadSafe @ThreadSafe
public static <T extends NativeIntegration> T get(Class<T> type) public static <T extends NativeIntegration> T get(Class<T> type)
throws NativeIntegrationUnavailableException, NativeException { throws NativeIntegrationUnavailableException, NativeException {
init(null); init(null);
synchronized (Native.class) { synchronized (Native.class) {
Object instance = integrations.get(type); Object instance = integrations.get(type);
if (instance == null) { if (instance == null) {
try { try {
instance = Platform.current().get(type, loader); instance = Platform.current().get(type, loader);
} 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 integration %s.", type.getSimpleName()), t); throw new NativeException(String.format("Failed to load native integration %s.", type.getSimpleName()), t);
} }
integrations.put(type, instance); integrations.put(type, instance);
} }
return type.cast(instance); return type.cast(instance);
} }
} }
} }

View File

@@ -1,52 +1,53 @@
/* /*
* Copyright 2012 Adam Murdoch * Copyright 2012 Adam Murdoch
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package net.rubygrapefruit.platform.internal; package net.rubygrapefruit.platform.internal;
import net.rubygrapefruit.platform.NativeException; import net.rubygrapefruit.platform.NativeException;
import net.rubygrapefruit.platform.NativeIntegrationUnavailableException; import net.rubygrapefruit.platform.NativeIntegrationUnavailableException;
import java.io.File; import java.io.File;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; 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) {
this.locator = locator; public NativeLibraryLoader(Platform platform, NativeLibraryLocator nativeLibraryLocator) {
} this.platform = platform;
this.nativeLibraryLocator = nativeLibraryLocator;
public void load(String name) { }
if (loaded.contains(name)) {
return; public void load(String libraryFileName) {
} if (loaded.contains(libraryFileName)) {
try { return;
File libFile = locator.find(name); }
if (libFile == null) { try {
throw new NativeIntegrationUnavailableException(String.format( File libFile = nativeLibraryLocator.find(libraryFileName);
"Native library is not available for this operating system and architecture.")); if (libFile == null) {
} throw new NativeIntegrationUnavailableException(String.format("Native library is not available for %s.", platform));
System.load(libFile.getCanonicalPath()); }
} catch (NativeException e) { System.load(libFile.getCanonicalPath());
throw e; } catch (NativeException e) {
} catch (Throwable t) { throw e;
throw new NativeException(String.format("Failed to load native library '%s'.", name), t); } catch (Throwable t) {
} throw new NativeException(String.format("Failed to load native library '%s' for %s.", libraryFileName, platform), t);
loaded.add(name); }
} loaded.add(libraryFileName);
} }
}

View File

@@ -1,106 +1,106 @@
/* /*
* Copyright 2012 Adam Murdoch * Copyright 2012 Adam Murdoch
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package net.rubygrapefruit.platform.internal; package net.rubygrapefruit.platform.internal;
import net.rubygrapefruit.platform.NativeException; import net.rubygrapefruit.platform.NativeException;
import net.rubygrapefruit.platform.internal.jni.NativeLibraryFunctions; import net.rubygrapefruit.platform.internal.jni.NativeLibraryFunctions;
import java.io.*; import java.io.*;
import java.net.URL; import java.net.URL;
import java.nio.channels.FileLock; import java.nio.channels.FileLock;
public class NativeLibraryLocator { public class NativeLibraryLocator {
private final File extractDir; private final File extractDir;
public NativeLibraryLocator(File extractDir) { public NativeLibraryLocator(File extractDir) {
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();
RandomAccessFile lockFileAccess = new RandomAccessFile(lockFile, "rw"); RandomAccessFile lockFileAccess = new RandomAccessFile(lockFile, "rw");
try { try {
// Take exclusive lock on lock file // Take exclusive lock on lock file
FileLock lock = lockFileAccess.getChannel().lock(); FileLock lock = lockFileAccess.getChannel().lock();
if (lockFile.length() > 0 && lockFileAccess.readBoolean()) { if (lockFile.length() > 0 && lockFileAccess.readBoolean()) {
// 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();
copy(resource, libFile); copy(resource, libFile);
lockFileAccess.seek(0); lockFileAccess.seek(0);
lockFileAccess.writeBoolean(true); lockFileAccess.writeBoolean(true);
return libFile; return libFile;
} }
} finally { } finally {
// Also releases lock // Also releases lock
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;
} }
return null; return null;
} }
private static void copy(URL source, File dest) { private static void copy(URL source, File dest) {
try { try {
InputStream inputStream = source.openStream(); InputStream inputStream = source.openStream();
try { try {
OutputStream outputStream = new FileOutputStream(dest); OutputStream outputStream = new FileOutputStream(dest);
try { try {
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
while (true) { while (true) {
int nread = inputStream.read(buffer); int nread = inputStream.read(buffer);
if (nread < 0) { if (nread < 0) {
break; break;
} }
outputStream.write(buffer, 0, nread); outputStream.write(buffer, 0, nread);
} }
} finally { } finally {
outputStream.close(); outputStream.close();
} }
} finally { } finally {
inputStream.close(); inputStream.close();
} }
} catch (IOException e) { } catch (IOException e) {
throw new NativeException(String.format("Could not extract native JNI library."), e); throw new NativeException(String.format("Could not extract native JNI library."), e);
} }
} }
} }

View File

@@ -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() {