29package org.jau.sys.dl;
31import java.util.Iterator;
34import org.jau.lang.ExceptionUtils;
35import org.jau.sys.JNILibrary;
36import org.jau.sys.PlatformProps;
37import org.jau.sys.RuntimeProps;
39import jau.sys.dl.BionicDynamicLinker32bitImpl;
40import jau.sys.dl.BionicDynamicLinker64BitImpl;
41import jau.sys.dl.MacOSXDynamicLinkerImpl;
42import jau.sys.dl.PosixDynamicLinkerImpl;
43import jau.sys.dl.WindowsDynamicLinkerImpl;
64 private long libraryHandle;
67 private final String libraryPath;
69 private final boolean global;
73 this.dynLink = dynLink;
74 this.libraryHandle = libraryHandle;
75 this.libraryPath = libraryPath;
78 System.err.println(
"NativeLibrary.open(): Successfully loaded: " +
this);
84 return "NativeLibrary[" + dynLink.getClass().getSimpleName() +
", " + libraryPath +
", 0x" + Long.toHexString(libraryHandle) +
", global " + global +
"]";
109 final boolean searchSystemPath,
110 final boolean searchSystemPathFirst,
111 final ClassLoader loader,
final boolean global)
throws SecurityException {
112 return open(libName, libName, libName, searchSystemPath, searchSystemPathFirst, loader, global);
148 final String unixLibName,
149 final String macOSXLibName,
150 final boolean searchSystemPath,
151 final boolean searchSystemPathFirst,
152 final ClassLoader loader,
final boolean global)
throws SecurityException {
156 searchSystemPath, searchSystemPathFirst,
163 for (
final Iterator<String> iter = possiblePaths.iterator(); iter.hasNext(); ) {
164 final String path = iter.next();
166 System.err.println(
"NativeLibrary.open(global "+global+
"): Trying to load " + path);
176 }
catch (
final Throwable t1) {
184 System.err.println(
"NativeLibrary.open: Caught "+t.getClass().getSimpleName()+
": "+t.getMessage());
189 }
catch (
final Throwable t2) { errstr=
null; }
190 System.err.println(
"NativeLibrary.open: Last error "+errstr);
198 System.err.println(
"NativeLibrary.open(global "+global+
"): Did not succeed in loading (" + libName +
", " + unixLibName +
", " + macOSXLibName +
")");
218 if ( 0 == libraryHandle ) {
219 throw new RuntimeException(
"Library is not open");
226 if ( 0 == libraryHandle ) {
227 throw new RuntimeException(
"Library is not open");
229 return 0 != dynLink.
lookupSymbol(libraryHandle, funcName);
241 static DynamicLinker getDynamicLinker() {
242 final DynamicLinker dynLink;
245 dynLink =
new WindowsDynamicLinkerImpl();
250 dynLink =
new MacOSXDynamicLinkerImpl();
255 dynLink =
new BionicDynamicLinker32bitImpl();
257 dynLink =
new BionicDynamicLinker64BitImpl();
262 dynLink =
new PosixDynamicLinkerImpl();
272 return libraryHandle;
284 public final void close() throws SecurityException {
286 System.err.println(
"NativeLibrary.close(): closing " +
this);
288 if ( 0 == libraryHandle ) {
289 throw new RuntimeException(
"Library already closed");
291 final long handle = libraryHandle;
293 dynLink.closeLibrary(handle,
DEBUG);
295 System.err.println(
"NativeLibrary.close(): Successfully closed " +
this);
static void dumpStack(final PrintStream out)
Static JNI Native Libraries handler.
static final List< String > enumerateLibraryPaths(final String libName, final boolean searchSystemPath, final boolean searchSystemPathFirst, final ClassLoader loader)
Given the base library names (no prefixes/suffixes) for the various platforms, enumerate the possible...
Runtime platform properties derived from PlatformProps and runtime query.
static void initSingleton()
Provides low-level, relatively platform-independent access to shared ("native") libraries.
static final NativeLibrary open(final String libName, final boolean searchSystemPath, final boolean searchSystemPathFirst, final ClassLoader loader, final boolean global)
Opens the given native library, assuming it has the same base name on all platforms.
final String getLibraryPath()
Retrieves the path under which this library was opened.
static final NativeLibrary open(final String libName, final String unixLibName, final String macOSXLibName, final boolean searchSystemPath, final boolean searchSystemPathFirst, final ClassLoader loader, final boolean global)
Opens the given native library, assuming it has the given base names (no "lib" prefix or "....
final long dynamicLookupFunction(final String funcName)
Returns the function handle for function 'funcName'.
final long dynamicLookupFunctionGlobal(final String funcName)
Looks up the given function name in all loaded libraries.
final void claimAllLinkPermission()
final boolean isFunctionAvailable(final String funcName)
Queries whether function 'funcName' is available.
final long getLibraryHandle()
Retrieves the low-level library handle from this NativeLibrary object.
final void releaseAllLinkPermission()
final void close()
Closes this native library.
Low level secure dynamic linker access.
long lookupSymbol(long libraryHandle, String symbolName)
Security checks are implicit by previous call of openLibraryLocal(String, boolean) or openLibraryGlob...
void releaseAllLinkPermission()
long openLibraryLocal(String pathname, boolean debug)
If a SecurityManager is installed, user needs link permissions for the named library.
long openLibraryGlobal(String pathname, boolean debug)
If a SecurityManager is installed, user needs link permissions for the named library.
String getLastError()
Returns a string containing the last error.
long lookupSymbolGlobal(String symbolName)
If a SecurityManager is installed, user needs link permissions for all libraries, i....
void claimAllLinkPermission()
static final boolean DEBUG