4747import electrostatic4j .snaploader .platform .util .NativeVariant ;
4848import electrostatic4j .snaploader .throwable .LoadingRetryExhaustionException ;
4949import electrostatic4j .snaploader .throwable .UnSupportedSystemError ;
50+ import electrostatic4j .snaploader .util .CallingStackMetaData ;
5051import electrostatic4j .snaploader .util .SnapLoaderLogger ;
5152
5253/**
@@ -160,7 +161,7 @@ public NativeBinaryLoader initPlatformLibrary() throws UnSupportedSystemError {
160161 * </p>
161162 *
162163 * <p>
163- * Fallback loading routines can be implemented as needed via {@link NativeBinaryLoadingListener#onLoadingFailure(NativeBinaryLoader)}
164+ * Fallback loading routines can be implemented as needed via {@link NativeBinaryLoadingListener#onLoadingFailure(NativeBinaryLoader, CallingStackMetaData )}
164165 * and are left for the user applications.
165166 * </p>
166167 *
@@ -179,7 +180,7 @@ public NativeBinaryLoader loadLibrary(LoadingCriterion criterion) throws Excepti
179180 return this ;
180181 }
181182 if (criterion == LoadingCriterion .INCREMENTAL_LOADING && nativeDynamicLibrary .isExtracted ()) {
182- loadBinary (nativeDynamicLibrary );
183+ loadBinary (nativeDynamicLibrary , criterion );
183184 return this ;
184185 }
185186 cleanExtractBinary (nativeDynamicLibrary );
@@ -200,8 +201,9 @@ public NativeDynamicLibrary getNativeDynamicLibrary() {
200201 *
201202 * @param loggingEnabled true to enable logging, false otherwise
202203 */
203- public void setLoggingEnabled (boolean loggingEnabled ) {
204+ public NativeBinaryLoader setLoggingEnabled (boolean loggingEnabled ) {
204205 SnapLoaderLogger .setLoggingEnabled (loggingEnabled );
206+ return this ;
205207 }
206208
207209 /**
@@ -218,8 +220,9 @@ public boolean isRetryWithCleanExtraction() {
218220 *
219221 * @param retryWithCleanExtraction true to enable the flag, false otherwise
220222 */
221- public void setRetryWithCleanExtraction (boolean retryWithCleanExtraction ) {
223+ public NativeBinaryLoader setRetryWithCleanExtraction (boolean retryWithCleanExtraction ) {
222224 this .retryWithCleanExtraction = retryWithCleanExtraction ;
225+ return this ;
223226 }
224227
225228 public List <NativeDynamicLibrary > getRegisteredLibraries () {
@@ -230,36 +233,41 @@ public NativeBinaryLoadingListener getNativeBinaryLoadingListener() {
230233 return nativeBinaryLoadingListener ;
231234 }
232235
233- public void setNativeBinaryLoadingListener (NativeBinaryLoadingListener nativeBinaryLoadingListener ) {
236+ public NativeBinaryLoader setNativeBinaryLoadingListener (NativeBinaryLoadingListener nativeBinaryLoadingListener ) {
234237 this .nativeBinaryLoadingListener = nativeBinaryLoadingListener ;
238+ return this ;
235239 }
236240
237241 public SystemDetectionListener getSystemDetectionListener () {
238242 return systemDetectionListener ;
239243 }
240244
241- public void setSystemDetectionListener (SystemDetectionListener systemDetectionListener ) {
245+ public NativeBinaryLoader setSystemDetectionListener (SystemDetectionListener systemDetectionListener ) {
242246 this .systemDetectionListener = systemDetectionListener ;
247+ return this ;
243248 }
244249
245250 public FileExtractionListener getLibraryExtractionListener () {
246251 return libraryExtractionListener ;
247252 }
248253
249- public void setLibraryExtractionListener (FileExtractionListener libraryExtractionListener ) {
254+ public NativeBinaryLoader setLibraryExtractionListener (FileExtractionListener libraryExtractionListener ) {
250255 this .libraryExtractionListener = libraryExtractionListener ;
256+ return this ;
251257 }
252258
253259 public FileLocalizingListener getLibraryLocalizingListener () {
254260 return libraryLocalizingListener ;
255261 }
256262
257- public void setLibraryLocalizingListener (FileLocalizingListener libraryLocalizingListener ) {
263+ public NativeBinaryLoader setLibraryLocalizingListener (FileLocalizingListener libraryLocalizingListener ) {
258264 this .libraryLocalizingListener = libraryLocalizingListener ;
265+ return this ;
259266 }
260267
261- public void setMaxNumberOfLoadingFailure (int maxNumberOfLoadingFailure ) {
268+ public NativeBinaryLoader setMaxNumberOfLoadingFailure (int maxNumberOfLoadingFailure ) {
262269 this .maxNumberOfLoadingFailure = Math .abs (maxNumberOfLoadingFailure );
270+ return this ;
263271 }
264272
265273 /**
@@ -272,14 +280,16 @@ protected void loadSystemBinary() {
272280 SnapLoaderLogger .log (Level .INFO , getClass ().getName (), "loadSystemBinary" , "Successfully loaded library from the system: "
273281 + libraryInfo .getBaseName ());
274282 if (nativeBinaryLoadingListener != null ) {
275- nativeBinaryLoadingListener .onLoadingSuccess (this );
283+ nativeBinaryLoadingListener .onLoadingSuccess (this ,
284+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], LoadingCriterion .SYSTEM_LOAD ));
276285 }
277286 } catch (UnsatisfiedLinkError e ) {
278287 SnapLoaderLogger .log (Level .SEVERE , getClass ().getName (), "loadSystemBinary" , "Cannot load the dynamic library from the system: "
279288 + libraryInfo .getBaseName (), e );
280289 // fire failure routine for fallback criteria
281290 if (nativeBinaryLoadingListener != null ) {
282- nativeBinaryLoadingListener .onLoadingFailure (this );
291+ nativeBinaryLoadingListener .onLoadingFailure (this ,
292+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], LoadingCriterion .SYSTEM_LOAD , e ));
283293 }
284294 }
285295 }
@@ -289,28 +299,32 @@ protected void loadSystemBinary() {
289299 * native library data structure defining the directory path.
290300 *
291301 * @param library the platform-specific library to load
302+ * @param loadingCriterion pass the loading criterion condition to the calling stack metadata structure
292303 * @throws IOException in case the binary to be extracted is not found on the specified jar
293304 * @throws LoadingRetryExhaustionException if the number of loading failure exceeds the specified
294305 * number.
295306 */
296- protected void loadBinary (NativeDynamicLibrary library ) throws Exception {
307+ protected void loadBinary (NativeDynamicLibrary library , LoadingCriterion loadingCriterion ) throws Exception {
297308 try {
298309 System .load (library .getExtractedLibrary ());
299310 SnapLoaderLogger .log (Level .INFO , getClass ().getName (), "loadBinary" , "Successfully loaded library: "
300311 + library .getExtractedLibrary ());
301312 if (nativeBinaryLoadingListener != null ) {
302- nativeBinaryLoadingListener .onLoadingSuccess (this );
313+ nativeBinaryLoadingListener .onLoadingSuccess (this ,
314+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], loadingCriterion ));
303315 }
304316 } catch (final UnsatisfiedLinkError error ) {
305317 SnapLoaderLogger .log (Level .SEVERE , getClass ().getName (), "loadBinary" , "Cannot load the dynamic library: "
306318 + library .getExtractedLibrary (), error );
307319 if (nativeBinaryLoadingListener != null ) {
308- nativeBinaryLoadingListener .onLoadingFailure (this );
320+ nativeBinaryLoadingListener .onLoadingFailure (this ,
321+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], loadingCriterion , error ));
309322 }
310323 /* Retry with clean extract */
311324 if (isRetryWithCleanExtraction ()) {
312325 if (nativeBinaryLoadingListener != null ) {
313- nativeBinaryLoadingListener .onRetryCriterionExecution (this );
326+ nativeBinaryLoadingListener .onRetryCriterionExecution (this ,
327+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], loadingCriterion ));
314328 }
315329 // limit the number of retries to maxNumberOfLoadingFailure
316330 if (numberOfLoadingFailure >= maxNumberOfLoadingFailure ) {
@@ -347,7 +361,7 @@ public void onExtractionCompleted(FileExtractor fileExtractor) {
347361 SnapLoaderLogger .log (Level .INFO , getClass ().getName (), "cleanExtractBinary" ,
348362 "Extracted successfully to " + library .getExtractedLibrary ());
349363 // load the native binary
350- loadBinary (library );
364+ loadBinary (library , LoadingCriterion . CLEAN_EXTRACTION );
351365 } catch (Exception e ) {
352366 SnapLoaderLogger .log (Level .SEVERE , getClass ().getName (), "cleanExtractBinary" ,
353367 "Error while loading the binary!" , e );
@@ -442,6 +456,14 @@ public void onFileLocalizationFailure(FileLocator locator, Throwable throwable)
442456 if (libraryLocalizingListener != null ) {
443457 libraryLocalizingListener .onFileLocalizationFailure (locator , throwable );
444458 }
459+
460+ // make use of the loader listeners
461+ if (nativeBinaryLoadingListener != null ) {
462+ // a file locator and extractor loader is always a CLEAN_EXTRACTION regarding
463+ // the loading criterion
464+ nativeBinaryLoadingListener .onLoadingFailure (NativeBinaryLoader .this ,
465+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], LoadingCriterion .CLEAN_EXTRACTION , throwable ));
466+ }
445467 }
446468 });
447469 return (LibraryLocator ) extractor .getFileLocator ();
0 commit comments