3232
3333package electrostatic4j .snaploader ;
3434
35+ import java .io .File ;
3536import java .io .IOException ;
37+ import java .nio .file .FileSystems ;
3638import java .util .Arrays ;
3739import java .util .List ;
3840import java .util .jar .JarFile ;
4547import electrostatic4j .snaploader .library .LibraryLocator ;
4648import electrostatic4j .snaploader .platform .NativeDynamicLibrary ;
4749import electrostatic4j .snaploader .platform .util .NativeVariant ;
50+ import electrostatic4j .snaploader .platform .util .PropertiesControllerNamespace ;
51+ import electrostatic4j .snaploader .platform .util .PropertiesController ;
4852import electrostatic4j .snaploader .throwable .LoadingRetryExhaustionException ;
4953import electrostatic4j .snaploader .throwable .UnSupportedSystemError ;
5054import electrostatic4j .snaploader .util .CallingStackMetaData ;
@@ -89,11 +93,15 @@ public class NativeBinaryLoader {
8993
9094 protected int numberOfLoadingFailure = 0 ;
9195
96+ private LoadingCriterion loadingCriterion ; // cache the loading criterion for system controller use
97+
9298 /**
9399 * Instantiates a native dynamic library loader to extract and load a system-specific native dynamic library.
94100 */
95101 public NativeBinaryLoader (final LibraryInfo libraryInfo ) {
96102 this .libraryInfo = libraryInfo ;
103+ // initialize the system controller object
104+ PropertiesControllerNamespace .systemDirectoryController .initialize ();
97105 }
98106
99107 /**
@@ -176,14 +184,17 @@ public NativeBinaryLoader loadLibrary(LoadingCriterion criterion) throws Excepti
176184 }
177185 // commands and loads the library from the system directories
178186 if (NativeVariant .Os .isAndroid () || criterion == LoadingCriterion .SYSTEM_LOAD ) {
179- loadSystemBinary ();
187+ loadSystemBinary (nativeDynamicLibrary );
188+ loadingCriterion = LoadingCriterion .SYSTEM_LOAD ;
180189 return this ;
181190 }
182191 if (criterion == LoadingCriterion .INCREMENTAL_LOADING && nativeDynamicLibrary .isExtracted ()) {
183192 loadBinary (nativeDynamicLibrary , criterion );
193+ loadingCriterion = LoadingCriterion .INCREMENTAL_LOADING ;
184194 return this ;
185195 }
186196 cleanExtractBinary (nativeDynamicLibrary );
197+ loadingCriterion = LoadingCriterion .CLEAN_EXTRACTION ;
187198 return this ;
188199 }
189200
@@ -196,6 +207,10 @@ public NativeDynamicLibrary getNativeDynamicLibrary() {
196207 return nativeDynamicLibrary ;
197208 }
198209
210+ public PropertiesController getSystemDirectoryController () {
211+ return PropertiesControllerNamespace .systemDirectoryController ;
212+ }
213+
199214 /**
200215 * Enables the logging for this object, default value is false.
201216 *
@@ -271,12 +286,27 @@ public NativeBinaryLoader setMaxNumberOfLoadingFailure(int maxNumberOfLoadingFai
271286 }
272287
273288 /**
274- * Loads a native binary from the system directories into the process virtual
289+ * Loads a native binary from the customized system directories into the process virtual
275290 * address space using the library basename in a platform-dependent way.
276291 */
277- protected void loadSystemBinary () {
292+ protected void loadSystemBinary (NativeDynamicLibrary dynamicLibrary ) {
293+ SnapLoaderLogger .log (Level .INFO , getClass ().getName (), "loadSystemBinary" , "Loading library from the system: "
294+ + Arrays .toString (PropertiesControllerNamespace .systemDirectoryController .toList ()));
278295 try {
279- System .loadLibrary (libraryInfo .getBaseName ());
296+ // use custom system directories for desktop ONLY!
297+ if (NativeVariant .Os .isDesktop ()) {
298+ PropertiesControllerNamespace .systemDirectoryController .iterate (path -> {
299+ final File lib = new File (FileSystems .getDefault ()
300+ .getPath (path .toString (),
301+ dynamicLibrary .getLibraryFile ()).toString ());
302+ if (lib .exists ()) {
303+ System .load (lib .getAbsolutePath ());
304+ }
305+ return "" ;
306+ });
307+ } else {
308+ System .loadLibrary (libraryInfo .getBaseName ());
309+ }
280310 SnapLoaderLogger .log (Level .INFO , getClass ().getName (), "loadSystemBinary" , "Successfully loaded library from the system: "
281311 + libraryInfo .getBaseName ());
282312 if (nativeBinaryLoadingListener != null ) {
0 commit comments