Permalink
Browse files

add library open options

  • Loading branch information...
1 parent 6b7f2ee commit 559c7d7f13bff80621eb8a47c8ca361021e6a250 @twall twall committed Oct 5, 2012
Showing with 33 additions and 10 deletions.
  1. +3 −2 .gitignore
  2. +2 −0 src/com/sun/jna/Library.java
  3. +20 −8 src/com/sun/jna/NativeLibrary.java
  4. +8 −0 test/com/sun/jna/NativeLibraryTest.java
View
@@ -10,7 +10,8 @@ contrib/platform/dist
doc
*~
**/*~
-dist/jna-*-javadoc.jar
+dist/*.asc
+dist/*-sources.jar
+dist/*-javadoc.jar
dist/src-mvn.zip
dist/out-of-date.jar
-
@@ -81,6 +81,8 @@
String OPTION_ALLOW_OBJECTS = "allow-objects";
/** Calling convention for the entire library. */
String OPTION_CALLING_CONVENTION = "calling-convention";
+ /** Flags to use when opening the native library (see {@link Native#open(String,int)}) */
+ String OPTION_OPEN_FLAGS = "open-flags";
static class Handler implements InvocationHandler {
@@ -96,8 +96,19 @@ Object invoke(Object[] args, Class returnType, boolean b) {
}
}
+ private static final int DEFAULT_OPEN_OPTIONS = -1;
+ private static int openFlags(Map options) {
+ try {
+ return ((Integer)options.get(Library.OPTION_OPEN_FLAGS)).intValue();
+ }
+ catch(Throwable t) {
+ return DEFAULT_OPEN_OPTIONS;
+ }
+ }
+
private static NativeLibrary loadLibrary(String libraryName, Map options) {
List searchPath = new LinkedList();
+ int openFlags = openFlags(options);
// Append web start path, if available. Note that this does not
// attempt any library name variations
@@ -125,7 +136,7 @@ private static NativeLibrary loadLibrary(String libraryName, Map options) {
// name if it cannot find the library.
//
try {
- handle = Native.open(libraryPath);
+ handle = Native.open(libraryPath, openFlags);
}
catch(UnsatisfiedLinkError e) {
// Add the system paths back for all fallback searching
@@ -134,7 +145,7 @@ private static NativeLibrary loadLibrary(String libraryName, Map options) {
try {
if (handle == 0) {
libraryPath = findLibraryPath(libraryName, searchPath);
- handle = Native.open(libraryPath);
+ handle = Native.open(libraryPath, openFlags);
if (handle == 0) {
throw new UnsatisfiedLinkError("Failed to load library '" + libraryName + "'");
}
@@ -147,7 +158,7 @@ private static NativeLibrary loadLibrary(String libraryName, Map options) {
if (Platform.isAndroid()) {
try {
System.loadLibrary(libraryName);
- handle = Native.open(libraryPath);
+ handle = Native.open(libraryPath, openFlags);
}
catch(UnsatisfiedLinkError e2) { e = e2; }
}
@@ -158,7 +169,7 @@ else if (Platform.isLinux()) {
libraryPath = matchLibrary(libraryName, searchPath);
if (libraryPath != null) {
try {
- handle = Native.open(libraryPath);
+ handle = Native.open(libraryPath, openFlags);
}
catch(UnsatisfiedLinkError e2) { e = e2; }
}
@@ -168,15 +179,15 @@ else if (Platform.isMac() && !libraryName.endsWith(".dylib")) {
libraryPath = matchFramework(libraryName);
if (libraryPath != null) {
try {
- handle = Native.open(libraryPath);
+ handle = Native.open(libraryPath, openFlags);
}
catch(UnsatisfiedLinkError e2) { e = e2; }
}
}
// Try the same library with a "lib" prefix
else if (Platform.isWindows()) {
libraryPath = findLibraryPath("lib" + libraryName, searchPath);
- try { handle = Native.open(libraryPath); }
+ try { handle = Native.open(libraryPath, openFlags); }
catch(UnsatisfiedLinkError e2) { e = e2; }
}
if (handle == 0) {
@@ -256,7 +267,8 @@ public static final NativeLibrary getInstance(String libraryName, Map options) {
// Use current process to load libraries we know are already
// loaded by the VM to ensure we get the correct version
- if ((Platform.isLinux() || Platform.isAix()) && "c".equals(libraryName)) {
+ if ((Platform.isLinux() || Platform.isAix())
+ && Platform.C_LIBRARY_NAME.equals(libraryName)) {
libraryName = null;
}
synchronized (libraries) {
@@ -265,7 +277,7 @@ public static final NativeLibrary getInstance(String libraryName, Map options) {
if (library == null) {
if (libraryName == null) {
- library = new NativeLibrary("<process>", null, Native.open(null), options);
+ library = new NativeLibrary("<process>", null, Native.open(null, openFlags(options)), options);
}
else {
library = loadLibrary(libraryName, options);
@@ -15,7 +15,9 @@
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import junit.framework.TestCase;
@@ -217,6 +219,12 @@ public void testMatchFramework() {
}
}
+ public void testLoadLibraryWithOptions() {
+ Map options = new HashMap();
+ options.put(Library.OPTION_OPEN_FLAGS, new Integer(-1));
+ Native.loadLibrary("testlib", TestLibrary.class, options);
+ }
+
public static void main(String[] args) {
junit.textui.TestRunner.run(NativeLibraryTest.class);
}

0 comments on commit 559c7d7

Please sign in to comment.