Skip to content

Commit 897c1f5

Browse files
committed
Throw exception when library for RuntimeUtils is missing
1 parent 937f484 commit 897c1f5

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

src/main/java/ca/weblite/nativeutils/NativeUtils.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,44 @@ private NativeUtils() {
2525
}
2626

2727
/**
28-
* <p>loadLibraryFromJar.</p>
28+
* Loads a library from current JAR archive, using the class loader of the {@code NativeUtils}
29+
* class to find the resource in the JAR.
2930
*
3031
* @param path a {@link java.lang.String} object.
3132
* @throws java.io.IOException if any.
33+
* @throws UnsatisfiedLinkError if loading the native library fails.
3234
*/
3335
public static void loadLibraryFromJar(String path) throws IOException {
3436
loadLibraryFromJar(path, NativeUtils.class);
3537
}
3638

3739
/**
38-
* Loads library from current JAR archive
40+
* Loads a library from current JAR archive.
3941
*
4042
* The file from JAR is copied into system temporary directory and then loaded. The temporary file is deleted after exiting.
4143
* Method uses String as filename because the pathname is "abstract", not system-dependent.
4244
*
4345
* @throws java.lang.IllegalArgumentException If the path is not absolute or if the filename is shorter than three characters (restriction of @see File#createTempFile(java.lang.String, java.lang.String)).
4446
* @param path a {@link java.lang.String} object.
45-
* @param source a {@link java.lang.Class} object.
47+
* @param source {@code Class} whose class loader should be used to look up the resource in the JAR file
4648
* @throws java.io.IOException if any.
49+
* @throws UnsatisfiedLinkError if loading the native library fails.
4750
*/
48-
public static void loadLibraryFromJar(String path, Class<?> source) throws IOException {
51+
public static void loadLibraryFromJar(String path, Class<?> source) throws IOException, UnsatisfiedLinkError {
4952
// Finally, load the library
50-
System.load(loadFileFromJar(path, source).toAbsolutePath().toString());
53+
System.load(extractFromJar(path, source).toAbsolutePath().toString());
5154
}
5255

5356
/**
54-
* <p>loadFileFromJar.</p>
57+
* Extracts a resource from the JAR and stores it as temporary file
58+
* in the file system.
5559
*
56-
* @param path a {@link java.lang.String} object.
57-
* @param source a {@link java.lang.Class} object.
60+
* @param path path of the resource, must begin with {@code '/'}, see {@link Class#getResourceAsStream(String)}
61+
* @param source {@code Class} whose class loader should be used to look up the resource in the JAR file
5862
* @return file path of the temporary file extracted from this JAR
5963
* @throws java.io.IOException if any.
6064
*/
61-
public static Path loadFileFromJar(String path, Class<?> source) throws IOException {
65+
public static Path extractFromJar(String path, Class<?> source) throws IOException {
6266
if (!path.startsWith("/")) {
6367
throw new IllegalArgumentException("The path has to be absolute (start with '/').");
6468
}

src/main/java/ca/weblite/objc/RuntimeUtils.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package ca.weblite.objc;
22

33

4-
import ca.weblite.nativeutils.NativeUtils;
4+
import java.io.IOException;
5+
import java.io.UncheckedIOException;
6+
57
import com.sun.jna.Pointer;
68
import com.sun.jna.ptr.ByReference;
79
import com.sun.jna.ptr.ByteByReference;
@@ -11,7 +13,8 @@
1113
import com.sun.jna.ptr.LongByReference;
1214
import com.sun.jna.ptr.PointerByReference;
1315
import com.sun.jna.ptr.ShortByReference;
14-
import java.io.IOException;
16+
17+
import ca.weblite.nativeutils.NativeUtils;
1518

1619
/**
1720
* A Java class with static methods that interact with the Objective-C runtime.
@@ -45,28 +48,16 @@ private RuntimeUtils() {
4548

4649
}
4750

48-
/**
49-
* Flag to indicate whether the jcocoa native library was loaded successfully.
50-
* If it fails to load, then this flag will be false. Therefore, if this
51-
* flag is false, you shouldn't try to use the the api at all.
52-
*/
53-
private static boolean loaded = false;
5451
static {
52+
String libraryPath = "/libjcocoa.dylib";
5553
try {
56-
//System.loadLibrary("jcocoa");
57-
NativeUtils.loadLibraryFromJar("/libjcocoa.dylib");
58-
loaded = true;
59-
} catch (UnsatisfiedLinkError err){
60-
err.printStackTrace(System.err);
61-
} catch (IOException ex) {
62-
ex.printStackTrace(System.err);
54+
NativeUtils.loadLibraryFromJar(libraryPath);
55+
init();
56+
} catch (IOException ioException) {
57+
throw new UncheckedIOException("Failed loading library " + libraryPath, ioException);
6358
}
64-
init();
6559
}
6660

67-
68-
69-
7061
/**
7162
* Returns a pointer to the class for specific class name.
7263
* <pre>
@@ -886,7 +877,7 @@ public static ByReference getAsReferenceWrapper(Object val, String signature){
886877
* Initializes the libjcocoa library. This is called when the class is first
887878
* loaded. It sets up the JNI environment that will be used there forward.
888879
*/
889-
public static native void init();
880+
private static native void init();
890881

891882
/**
892883
* Registers a Java object with the Objective-C runtime so that it can begin

0 commit comments

Comments
 (0)