27package org.jau.sys.dl;
29import java.util.ArrayList;
30import java.util.Arrays;
31import java.util.HashSet;
34import org.jau.sys.JNILibrary;
35import org.jau.util.parallel.RunnableExecutor;
64 private final List<List<String>> toolLibNames;
65 private final List<String> glueLibNames;
66 private final boolean[] toolLibLoaded;
68 private int toolLibLoadedNumber;
70 private final boolean[] glueLibLoaded;
71 private int glueLibLoadedNumber;
73 private long toolGetProcAddressHandle;
74 private boolean toolGetProcAddressComplete;
75 private HashSet<String> toolGetProcAddressFuncNameSet;
76 private final List<String> toolGetProcAddressFuncNameList;
92 throw new RuntimeException(
"Null DynamicLibraryBundleInfo");
96 System.err.println(Thread.currentThread().getName()+
" - DynamicLibraryBundle.init start with: "+info.getClass().getName());
101 toolLibLoaded =
new boolean[toolLibNames.size()];
103 if( toolLibNames.size() == 0 ) {
104 System.err.println(
"No Tool native library names given");
107 if( glueLibNames.size() == 0 ) {
108 System.err.println(
"No Glue native library names given");
112 for(
int i=toolLibNames.size()-1; i>=0; i--) {
113 toolLibLoaded[i] =
false;
115 glueLibLoaded =
new boolean[glueLibNames.size()];
116 for(
int i=glueLibNames.size()-1; i>=0; i--) {
117 glueLibLoaded[i] =
false;
125 _dynLinkGlobal[0] = loadLibraries();
127 dynLinkGlobal = _dynLinkGlobal[0];
131 if(
null != toolGetProcAddressFuncNameList ) {
132 toolGetProcAddressFuncNameSet =
new HashSet<String>(toolGetProcAddressFuncNameList);
134 toolGetProcAddressComplete = 0 != toolGetProcAddressHandle;
136 toolGetProcAddressFuncNameSet =
new HashSet<String>();
137 toolGetProcAddressHandle = 0;
138 toolGetProcAddressComplete =
true;
141 System.err.println(
"DynamicLibraryBundle.init Summary: "+info.getClass().getName());
142 System.err.println(
" toolGetProcAddressFuncNameList: "+toolGetProcAddressFuncNameList+
", complete: "+toolGetProcAddressComplete+
", 0x"+Long.toHexString(toolGetProcAddressHandle));
143 System.err.println(
" Tool Lib Names : "+toolLibNames);
145 System.err.println(
" Glue Lib Names : "+glueLibNames);
155 System.err.println(Thread.currentThread().getName()+
" - DynamicLibraryBundle.destroy() START: "+info.getClass().getName());
157 toolGetProcAddressFuncNameSet =
null;
158 toolGetProcAddressHandle = 0;
159 toolGetProcAddressComplete =
false;
164 toolLibNames.clear();
165 glueLibNames.clear();
167 System.err.println(Thread.currentThread().getName()+
" - DynamicLibraryBundle.destroy() END: "+info.getClass().getName());
176 return toolLibNames.size();
180 return toolLibLoadedNumber;
191 return toolGetProcAddressComplete &&
192 ( 0 == toolLibNumber ||
null != dynLinkGlobal ) &&
197 return 0 < toolLibLoadedNumber;
201 if(0 <= i && i < toolLibLoaded.length) {
202 return toolLibLoaded[i];
208 return glueLibNames.size();
212 return glueLibLoadedNumber;
227 if(0 <= i && i < glueLibLoaded.length) {
228 return glueLibLoaded[i];
240 for (
int i=0; i < toolGetProcAddressFuncNameList.size(); i++) {
241 final String name = toolGetProcAddressFuncNameList.get(i);
242 aptr = dynamicLookupFunctionOnLibs(name);
244 System.err.println(
"getToolGetProcAddressHandle: "+name+
" -> 0x"+Long.toHexString(aptr));
251 final boolean searchSystemPath,
252 final boolean searchSystemPathFirst,
253 final ClassLoader loader,
final boolean global)
throws SecurityException {
254 for (
int i=0; i < libNames.size(); i++) {
263 final DynamicLinker loadLibraries() throws SecurityException {
265 toolLibLoadedNumber = 0;
266 final ClassLoader cl = info.getClass().getClassLoader();
270 for (i=0; i < toolLibNames.size(); i++) {
271 final List<String> libNames = toolLibNames.get(i);
272 if(
null != libNames && libNames.size() > 0 ) {
279 System.err.println(
"Unable to load any Tool library of: "+libNames);
282 if(
null == dynLinkGlobal ) {
283 dynLinkGlobal = lib.dynamicLinker();
286 toolLibLoaded[i]=
true;
287 toolLibLoadedNumber++;
289 System.err.println(
"Loaded Tool library: "+lib);
296 System.err.println(
"No Tool libraries loaded");
298 return dynLinkGlobal;
301 glueLibLoadedNumber = 0;
302 for (i=0; i < glueLibNames.size(); i++) {
303 final String libName = glueLibNames.get(i);
304 final boolean ignoreError =
true;
307 res = JNILibrary.loadLibrary(libName, ignoreError, cl);
309 System.err.println(
"Info: Could not load JNI/Glue library: "+libName);
311 }
catch (
final UnsatisfiedLinkError e) {
314 System.err.println(
"Unable to load JNI/Glue library: "+libName);
318 glueLibLoaded[i] = res;
320 glueLibLoadedNumber++;
324 return dynLinkGlobal;
332 private final long dynamicLookupFunctionOnLibs(
final String funcName)
throws SecurityException {
335 System.err.println(
"Lookup-Native: <" + funcName +
"> ** FAILED ** Tool native library not loaded");
340 NativeLibrary lib =
null;
350 addr = lib.dynamicLookupFunction(funcName);
353 final String libName = (
null == lib ) ?
"GLOBAL" : lib.toString();
355 System.err.println(
"Lookup-Native: <" + funcName +
"> 0x" + Long.toHexString(addr) +
" in lib " + libName );
357 System.err.println(
"Lookup-Native: <" + funcName +
"> ** FAILED ** in libs " +
nativeLibraries);
363 private final long toolDynamicLookupFunction(
final String funcName) {
364 if(0 != toolGetProcAddressHandle) {
368 System.err.println(
"Lookup-Tool: <"+funcName+
"> 0x"+Long.toHexString(addr)+
", via tool 0x"+Long.toHexString(toolGetProcAddressHandle));
393 System.err.println(
"Lookup: <" + funcName +
"> ** FAILED ** Tool native library not loaded");
398 if(toolGetProcAddressFuncNameSet.contains(funcName)) {
399 return toolGetProcAddressHandle;
405 if(useToolGetProcAdressFirst) {
406 addr = toolDynamicLookupFunction(funcName);
409 addr = dynamicLookupFunctionOnLibs(funcName);
411 if(0==addr && !useToolGetProcAdressFirst) {
412 addr = toolDynamicLookupFunction(funcName);
final DynamicLibraryBundleInfo getBundleInfo()
final void claimAllLinkPermission()
final int getGlueLibNumber()
final int getToolLibNumber()
final boolean isToolLibLoaded(final int i)
final boolean isGlueLibComplete()
static final NativeLibrary loadFirstAvailable(final List< String > libNames, final boolean searchSystemPath, final boolean searchSystemPathFirst, final ClassLoader loader, final boolean global)
DynamicLibraryBundle(final DynamicLibraryBundleInfo info)
Instantiates and loads all NativeLibrarys incl.
final long dynamicLookupFunction(final String funcName)
Returns the function handle for function 'funcName'.
final void releaseAllLinkPermission()
final boolean isGlueLibLoaded(final int i)
final List< NativeLibrary > nativeLibraries
final void destroy()
Unload all NativeLibrarys, and remove all references.
static RunnableExecutor getDefaultRunnableExecutor()
Returns the default RunnableExecutor#currentThreadExecutor.
final long getToolGetProcAddressHandle()
final boolean isFunctionAvailable(final String funcName)
Queries whether function 'funcName' is available.
final int getToolLibLoadedNumber()
final int getGlueLibLoadedNumber()
final boolean isToolLibLoaded()
final boolean isToolLibComplete()
final boolean isLibComplete()
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.
List< String > getToolGetProcAddressFuncNameList()
May return the native libraries.
boolean shallLinkGlobal()
boolean searchToolLibSystemPathFirst()
Returns true if system path shall be searched first (default), rather than searching it last.
boolean useToolGetProcAdressFirst(String funcName)
RunnableExecutor getLibLoaderExecutor()
Returns a suitable RunnableExecutor implementation, which is being used to load the tool and glue nat...
List< String > getGlueLibNames()
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.
List< List< String > > getToolLibNames()
If a SecurityManager is installed, user needs link permissions for the named libraries.
boolean shallLookupGlobal()
If method returns true and if a SecurityManager is installed, user needs link permissions for all lib...
long toolGetProcAddress(long toolGetProcAddressHandle, String funcName)
May implement the lookup function using the Tools facility.
Low level secure dynamic linker access.
long lookupSymbolGlobal(String symbolName)
If a SecurityManager is installed, user needs link permissions for all libraries, i....
static final boolean DEBUG
static final boolean DEBUG_LOOKUP
void invoke(boolean wait, Runnable r)
static final RunnableExecutor currentThreadExecutor
This RunnableExecutor implementation simply invokes Runnable#run() on the current thread.