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 );
@@ -272,14 +273,16 @@ protected void loadSystemBinary() {
272273 SnapLoaderLogger .log (Level .INFO , getClass ().getName (), "loadSystemBinary" , "Successfully loaded library from the system: "
273274 + libraryInfo .getBaseName ());
274275 if (nativeBinaryLoadingListener != null ) {
275- nativeBinaryLoadingListener .onLoadingSuccess (this );
276+ nativeBinaryLoadingListener .onLoadingSuccess (this ,
277+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], LoadingCriterion .SYSTEM_LOAD ));
276278 }
277279 } catch (UnsatisfiedLinkError e ) {
278280 SnapLoaderLogger .log (Level .SEVERE , getClass ().getName (), "loadSystemBinary" , "Cannot load the dynamic library from the system: "
279281 + libraryInfo .getBaseName (), e );
280282 // fire failure routine for fallback criteria
281283 if (nativeBinaryLoadingListener != null ) {
282- nativeBinaryLoadingListener .onLoadingFailure (this );
284+ nativeBinaryLoadingListener .onLoadingFailure (this ,
285+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], LoadingCriterion .SYSTEM_LOAD , e ));
283286 }
284287 }
285288 }
@@ -289,28 +292,32 @@ protected void loadSystemBinary() {
289292 * native library data structure defining the directory path.
290293 *
291294 * @param library the platform-specific library to load
295+ * @param loadingCriterion pass the loading criterion condition to the calling stack metadata structure
292296 * @throws IOException in case the binary to be extracted is not found on the specified jar
293297 * @throws LoadingRetryExhaustionException if the number of loading failure exceeds the specified
294298 * number.
295299 */
296- protected void loadBinary (NativeDynamicLibrary library ) throws Exception {
300+ protected void loadBinary (NativeDynamicLibrary library , LoadingCriterion loadingCriterion ) throws Exception {
297301 try {
298302 System .load (library .getExtractedLibrary ());
299303 SnapLoaderLogger .log (Level .INFO , getClass ().getName (), "loadBinary" , "Successfully loaded library: "
300304 + library .getExtractedLibrary ());
301305 if (nativeBinaryLoadingListener != null ) {
302- nativeBinaryLoadingListener .onLoadingSuccess (this );
306+ nativeBinaryLoadingListener .onLoadingSuccess (this ,
307+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], loadingCriterion ));
303308 }
304309 } catch (final UnsatisfiedLinkError error ) {
305310 SnapLoaderLogger .log (Level .SEVERE , getClass ().getName (), "loadBinary" , "Cannot load the dynamic library: "
306311 + library .getExtractedLibrary (), error );
307312 if (nativeBinaryLoadingListener != null ) {
308- nativeBinaryLoadingListener .onLoadingFailure (this );
313+ nativeBinaryLoadingListener .onLoadingFailure (this ,
314+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], loadingCriterion , error ));
309315 }
310316 /* Retry with clean extract */
311317 if (isRetryWithCleanExtraction ()) {
312318 if (nativeBinaryLoadingListener != null ) {
313- nativeBinaryLoadingListener .onRetryCriterionExecution (this );
319+ nativeBinaryLoadingListener .onRetryCriterionExecution (this ,
320+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], loadingCriterion ));
314321 }
315322 // limit the number of retries to maxNumberOfLoadingFailure
316323 if (numberOfLoadingFailure >= maxNumberOfLoadingFailure ) {
@@ -347,7 +354,7 @@ public void onExtractionCompleted(FileExtractor fileExtractor) {
347354 SnapLoaderLogger .log (Level .INFO , getClass ().getName (), "cleanExtractBinary" ,
348355 "Extracted successfully to " + library .getExtractedLibrary ());
349356 // load the native binary
350- loadBinary (library );
357+ loadBinary (library , LoadingCriterion . CLEAN_EXTRACTION );
351358 } catch (Exception e ) {
352359 SnapLoaderLogger .log (Level .SEVERE , getClass ().getName (), "cleanExtractBinary" ,
353360 "Error while loading the binary!" , e );
@@ -442,6 +449,14 @@ public void onFileLocalizationFailure(FileLocator locator, Throwable throwable)
442449 if (libraryLocalizingListener != null ) {
443450 libraryLocalizingListener .onFileLocalizationFailure (locator , throwable );
444451 }
452+
453+ // make use of the loader listeners
454+ if (nativeBinaryLoadingListener != null ) {
455+ // a file locator and extractor loader is always a CLEAN_EXTRACTION regarding
456+ // the loading criterion
457+ nativeBinaryLoadingListener .onLoadingFailure (NativeBinaryLoader .this ,
458+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], LoadingCriterion .CLEAN_EXTRACTION , throwable ));
459+ }
445460 }
446461 });
447462 return (LibraryLocator ) extractor .getFileLocator ();
0 commit comments