29package com.jogamp.common.os;
31import java.util.ArrayList;
32import java.util.Arrays;
33import java.util.HashSet;
36import com.jogamp.common.jvm.JNILibLoaderBase;
37import com.jogamp.common.util.RunnableExecutor;
64 private final List<List<String>> toolLibNames;
66 private final List<String> toolLibSymbolNames;
67 private final List<String> glueLibNames;
68 private final boolean[] toolLibLoaded;
71 private int toolLibLoadedNumber;
73 private final boolean[] glueLibLoaded;
74 private int glueLibLoadedNumber;
76 private long toolGetProcAddressHandle;
77 private boolean toolGetProcAddressComplete;
78 private HashSet<String> toolGetProcAddressFuncNameSet;
79 private final List<String> toolGetProcAddressFuncNameList;
95 throw new RuntimeException(
"Null DynamicLibraryBundleInfo");
99 System.err.println(Thread.currentThread().getName()+
" - DynamicLibraryBundle.init start with: "+info.getClass().getName());
102 toolLibraries =
new ArrayList<NativeLibrary>(toolLibNames.size());
105 toolLibLoaded =
new boolean[toolLibNames.size()];
107 if( toolLibNames.size() == 0 ) {
108 System.err.println(
"No Tool native library names given");
111 if( glueLibNames.size() == 0 ) {
112 System.err.println(
"No Glue native library names given");
116 for(
int i=toolLibNames.size()-1; i>=0; i--) {
117 toolLibLoaded[i] =
false;
119 glueLibLoaded =
new boolean[glueLibNames.size()];
120 for(
int i=glueLibNames.size()-1; i>=0; i--) {
121 glueLibLoaded[i] =
false;
129 _dynLinkGlobal[0] = loadLibraries();
131 dynLinkGlobal = _dynLinkGlobal[0];
135 if(
null != toolGetProcAddressFuncNameList ) {
136 toolGetProcAddressFuncNameSet =
new HashSet<String>(toolGetProcAddressFuncNameList);
138 toolGetProcAddressComplete = 0 != toolGetProcAddressHandle;
140 toolGetProcAddressFuncNameSet =
new HashSet<String>();
141 toolGetProcAddressHandle = 0;
142 toolGetProcAddressComplete =
true;
145 System.err.println(
"DynamicLibraryBundle.init Summary: "+info.getClass().getName());
146 System.err.println(
" toolGetProcAddressFuncNameList: "+toolGetProcAddressFuncNameList+
", complete: "+toolGetProcAddressComplete+
", 0x"+Long.toHexString(toolGetProcAddressHandle));
147 System.err.println(
" Tool Lib Names : "+toolLibNames);
148 System.err.println(
" Tool Lib Symbol: "+toolLibSymbolNames);
151 System.err.println(
" Glue Lib Names : "+glueLibNames);
161 System.err.println(Thread.currentThread().getName()+
" - DynamicLibraryBundle.destroy() START: "+info.getClass().getName());
163 toolGetProcAddressFuncNameSet =
null;
164 toolGetProcAddressHandle = 0;
165 toolGetProcAddressComplete =
false;
173 toolLibNames.clear();
174 glueLibNames.clear();
176 System.err.println(Thread.currentThread().getName()+
" - DynamicLibraryBundle.destroy() END: "+info.getClass().getName());
185 return toolLibNames.size();
189 return toolLibLoadedNumber;
200 return toolGetProcAddressComplete &&
201 ( 0 == toolLibNumber ||
null != dynLinkGlobal ) &&
206 return 0 < toolLibLoadedNumber;
210 if(0 <= i && i < toolLibLoaded.length) {
211 return toolLibLoaded[i];
237 return glueLibNames.size();
241 return glueLibLoadedNumber;
256 if(0 <= i && i < glueLibLoaded.length) {
257 return glueLibLoaded[i];
271 for (
int i=0; i < toolGetProcAddressFuncNameList.size(); i++) {
272 final String name = toolGetProcAddressFuncNameList.get(i);
273 aptr = dynamicLookupFunctionOnLibs(name);
275 System.err.println(
"getToolGetProcAddressHandle: "+name+
" -> 0x"+Long.toHexString(aptr));
282 final boolean searchOSSystemPath,
283 final boolean searchSystemPathFirst,
284 final ClassLoader loader,
final boolean global,
final String symbolName)
throws SecurityException {
285 for (
int i=0; i < libNames.size(); i++) {
294 final DynamicLinker loadLibraries() throws SecurityException {
296 toolLibLoadedNumber = 0;
297 final ClassLoader cl = info.getClass().getClassLoader();
301 for (i=0; i < toolLibNames.size(); i++) {
302 final List<String> libNames = toolLibNames.get(i);
303 final String symbolName = i < toolLibSymbolNames.size() ? toolLibSymbolNames.get(i) :
null;
304 if(
null != libNames && libNames.size() > 0 ) {
312 System.err.println(
"Unable to load any Tool library of: "+libNames);
315 if(
null == dynLinkGlobal ) {
316 dynLinkGlobal = lib.dynamicLinker();
318 toolLibLoaded[i] =
true;
319 toolLibLoadedNumber++;
321 System.err.println(
"Loaded Tool library: "+lib);
330 System.err.println(
"No Tool libraries loaded");
332 return dynLinkGlobal;
335 glueLibLoadedNumber = 0;
336 for (i=0; i < glueLibNames.size(); i++) {
337 final String libName = glueLibNames.get(i);
338 final boolean ignoreError =
true;
341 res = GlueJNILibLoader.loadLibrary(libName, ignoreError, cl);
343 System.err.println(
"Info: Could not load JNI/Glue library: "+libName);
345 }
catch (
final UnsatisfiedLinkError e) {
348 System.err.println(
"Unable to load JNI/Glue library: "+libName);
352 glueLibLoaded[i] = res;
354 glueLibLoadedNumber++;
358 return dynLinkGlobal;
366 private final long dynamicLookupFunctionOnLibs(
final String funcName)
throws SecurityException {
369 System.err.println(
"Lookup-Native: <" + funcName +
"> ** FAILED ** Tool native library not loaded");
374 NativeLibrary lib =
null;
384 if(
null != lib0 && lib0.isOpen() ) {
386 addr = lib0.dynamicLookupFunction(funcName);
390 final String libName = (
null == lib ) ?
"GLOBAL" : lib.toString();
392 System.err.println(
"Lookup-Native: <" + funcName +
"> 0x" + Long.toHexString(addr) +
" in lib " + libName );
394 System.err.println(
"Lookup-Native: <" + funcName +
"> ** FAILED ** in libs " +
toolLibraries);
400 private final long toolDynamicLookupFunction(
final String funcName) {
401 if(0 != toolGetProcAddressHandle) {
405 System.err.println(
"Lookup-Tool: <"+funcName+
"> 0x"+Long.toHexString(addr)+
", via tool 0x"+Long.toHexString(toolGetProcAddressHandle));
436 System.err.println(
"Lookup: <" + funcName +
"> ** FAILED ** Tool native library not loaded");
441 if(toolGetProcAddressFuncNameSet.contains(funcName)) {
442 return toolGetProcAddressHandle;
448 if(useToolGetProcAdressFirst) {
449 addr = toolDynamicLookupFunction(funcName);
452 addr = dynamicLookupFunctionOnLibs(funcName);
454 if(0==addr && !useToolGetProcAdressFirst) {
455 addr = toolDynamicLookupFunction(funcName);
467 protected static synchronized boolean loadLibrary(
final String libname,
final boolean ignoreError,
final ClassLoader cl) {
static synchronized boolean loadLibrary(final String libname, final boolean ignoreError, final ClassLoader cl)
Loads the library specified by libname, using the LoaderAction set by setLoadingAction(LoaderAction).
DynamicLibraryBundle(final DynamicLibraryBundleInfo info)
Instantiates and loads all NativeLibrarys incl.
final boolean isGlueLibComplete()
final List< NativeLibrary > getToolLibraries()
Returns list of NativeLibrarys for each DynamicLibraryBundleInfo#getToolLibNames() in the same size a...
final NativeLibrary getToolLibrary(final int i)
Returns NativeLibrary at given index of all DynamicLibraryBundleInfo#getToolLibNames() in the same si...
final void destroy()
Unload all NativeLibrarys, and remove all references.
final void releaseAllLinkPermission()
final List< NativeLibrary > toolLibraries
final int getGlueLibLoadedNumber()
final boolean isToolLibLoaded()
final long retrieveToolGetProcAddressHandle()
final int getToolLibLoadedNumber()
final boolean isToolLibLoaded(final int i)
static final NativeLibrary loadFirstAvailable(final List< String > libNames, final boolean searchOSSystemPath, final boolean searchSystemPathFirst, final ClassLoader loader, final boolean global, final String symbolName)
final boolean isOpen()
Returns true if library is loaded and open, otherwise false.
final DynamicLibraryBundleInfo getBundleInfo()
final boolean isToolLibComplete()
final int getGlueLibNumber()
final void claimAllLinkPermission()
final long getToolGetProcAddressHandle()
final boolean isFunctionAvailable(final String funcName)
Queries whether function 'funcName' is available.
final boolean isLibComplete()
static RunnableExecutor getDefaultRunnableExecutor()
Returns an AWT-EDT RunnableExecutor implementation if AWT is available, otherwise RunnableExecutor#cu...
final int getToolLibNumber()
final boolean isGlueLibLoaded(final int i)
final long dynamicLookupFunction(final String funcName)
Returns the function handle for function 'funcName'.
Provides low-level, relatively platform-independent access to shared ("native") libraries.
final void claimAllLinkPermission()
final void close()
Closes this native library.
static final NativeLibrary open(final String libName, final boolean searchOSSystemPath, 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 void releaseAllLinkPermission()
boolean useToolGetProcAdressFirst(String funcName)
long toolGetProcAddress(long toolGetProcAddressHandle, String funcName)
May implement the lookup function using the Tools facility.
List< List< String > > getToolLibNames()
If a SecurityManager is installed, user needs link permissions for the named libraries.
boolean searchToolLibInSystemPath()
Returns true if tool libraries shall be searched in the system path (default), otherwise false.
boolean shallLookupGlobal()
If method returns true and if a SecurityManager is installed, user needs link permissions for all lib...
List< String > getSymbolForToolLibPath()
Returns optional list of optional symbol names per getToolLibNames() in same order for an OS which re...
List< String > getGlueLibNames()
If a SecurityManager is installed, user needs link permissions for the named libraries.
RunnableExecutor getLibLoaderExecutor()
Returns a suitable RunnableExecutor implementation, which is being used to load the tool and glue nat...
boolean shallLinkGlobal()
List< String > getToolGetProcAddressFuncNameList()
May return the native libraries.
boolean searchToolLibSystemPathFirst()
Returns true if system path shall be searched first (default), rather than searching it last.
Low level secure dynamic linker access.
long lookupSymbolGlobal(String symbolName)
If a SecurityManager is installed, user needs link permissions for all libraries, i....
Interface callers may use ProcAddressHelper's reset helper method to install function pointers into a...
static final boolean DEBUG_LOOKUP
static final boolean DEBUG
void invoke(boolean wait, Runnable r)
static final RunnableExecutor currentThreadExecutor
This RunnableExecutor implementation simply invokes Runnable#run() on the current thread.