47
47
import electrostatic4j .snaploader .platform .util .NativeVariant ;
48
48
import electrostatic4j .snaploader .throwable .LoadingRetryExhaustionException ;
49
49
import electrostatic4j .snaploader .throwable .UnSupportedSystemError ;
50
+ import electrostatic4j .snaploader .util .CallingStackMetaData ;
50
51
import electrostatic4j .snaploader .util .SnapLoaderLogger ;
51
52
52
53
/**
@@ -160,7 +161,7 @@ public NativeBinaryLoader initPlatformLibrary() throws UnSupportedSystemError {
160
161
* </p>
161
162
*
162
163
* <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 )}
164
165
* and are left for the user applications.
165
166
* </p>
166
167
*
@@ -179,7 +180,7 @@ public NativeBinaryLoader loadLibrary(LoadingCriterion criterion) throws Excepti
179
180
return this ;
180
181
}
181
182
if (criterion == LoadingCriterion .INCREMENTAL_LOADING && nativeDynamicLibrary .isExtracted ()) {
182
- loadBinary (nativeDynamicLibrary );
183
+ loadBinary (nativeDynamicLibrary , criterion );
183
184
return this ;
184
185
}
185
186
cleanExtractBinary (nativeDynamicLibrary );
@@ -200,8 +201,9 @@ public NativeDynamicLibrary getNativeDynamicLibrary() {
200
201
*
201
202
* @param loggingEnabled true to enable logging, false otherwise
202
203
*/
203
- public void setLoggingEnabled (boolean loggingEnabled ) {
204
+ public NativeBinaryLoader setLoggingEnabled (boolean loggingEnabled ) {
204
205
SnapLoaderLogger .setLoggingEnabled (loggingEnabled );
206
+ return this ;
205
207
}
206
208
207
209
/**
@@ -218,8 +220,9 @@ public boolean isRetryWithCleanExtraction() {
218
220
*
219
221
* @param retryWithCleanExtraction true to enable the flag, false otherwise
220
222
*/
221
- public void setRetryWithCleanExtraction (boolean retryWithCleanExtraction ) {
223
+ public NativeBinaryLoader setRetryWithCleanExtraction (boolean retryWithCleanExtraction ) {
222
224
this .retryWithCleanExtraction = retryWithCleanExtraction ;
225
+ return this ;
223
226
}
224
227
225
228
public List <NativeDynamicLibrary > getRegisteredLibraries () {
@@ -230,36 +233,41 @@ public NativeBinaryLoadingListener getNativeBinaryLoadingListener() {
230
233
return nativeBinaryLoadingListener ;
231
234
}
232
235
233
- public void setNativeBinaryLoadingListener (NativeBinaryLoadingListener nativeBinaryLoadingListener ) {
236
+ public NativeBinaryLoader setNativeBinaryLoadingListener (NativeBinaryLoadingListener nativeBinaryLoadingListener ) {
234
237
this .nativeBinaryLoadingListener = nativeBinaryLoadingListener ;
238
+ return this ;
235
239
}
236
240
237
241
public SystemDetectionListener getSystemDetectionListener () {
238
242
return systemDetectionListener ;
239
243
}
240
244
241
- public void setSystemDetectionListener (SystemDetectionListener systemDetectionListener ) {
245
+ public NativeBinaryLoader setSystemDetectionListener (SystemDetectionListener systemDetectionListener ) {
242
246
this .systemDetectionListener = systemDetectionListener ;
247
+ return this ;
243
248
}
244
249
245
250
public FileExtractionListener getLibraryExtractionListener () {
246
251
return libraryExtractionListener ;
247
252
}
248
253
249
- public void setLibraryExtractionListener (FileExtractionListener libraryExtractionListener ) {
254
+ public NativeBinaryLoader setLibraryExtractionListener (FileExtractionListener libraryExtractionListener ) {
250
255
this .libraryExtractionListener = libraryExtractionListener ;
256
+ return this ;
251
257
}
252
258
253
259
public FileLocalizingListener getLibraryLocalizingListener () {
254
260
return libraryLocalizingListener ;
255
261
}
256
262
257
- public void setLibraryLocalizingListener (FileLocalizingListener libraryLocalizingListener ) {
263
+ public NativeBinaryLoader setLibraryLocalizingListener (FileLocalizingListener libraryLocalizingListener ) {
258
264
this .libraryLocalizingListener = libraryLocalizingListener ;
265
+ return this ;
259
266
}
260
267
261
- public void setMaxNumberOfLoadingFailure (int maxNumberOfLoadingFailure ) {
268
+ public NativeBinaryLoader setMaxNumberOfLoadingFailure (int maxNumberOfLoadingFailure ) {
262
269
this .maxNumberOfLoadingFailure = Math .abs (maxNumberOfLoadingFailure );
270
+ return this ;
263
271
}
264
272
265
273
/**
@@ -272,14 +280,16 @@ protected void loadSystemBinary() {
272
280
SnapLoaderLogger .log (Level .INFO , getClass ().getName (), "loadSystemBinary" , "Successfully loaded library from the system: "
273
281
+ libraryInfo .getBaseName ());
274
282
if (nativeBinaryLoadingListener != null ) {
275
- nativeBinaryLoadingListener .onLoadingSuccess (this );
283
+ nativeBinaryLoadingListener .onLoadingSuccess (this ,
284
+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], LoadingCriterion .SYSTEM_LOAD ));
276
285
}
277
286
} catch (UnsatisfiedLinkError e ) {
278
287
SnapLoaderLogger .log (Level .SEVERE , getClass ().getName (), "loadSystemBinary" , "Cannot load the dynamic library from the system: "
279
288
+ libraryInfo .getBaseName (), e );
280
289
// fire failure routine for fallback criteria
281
290
if (nativeBinaryLoadingListener != null ) {
282
- nativeBinaryLoadingListener .onLoadingFailure (this );
291
+ nativeBinaryLoadingListener .onLoadingFailure (this ,
292
+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], LoadingCriterion .SYSTEM_LOAD , e ));
283
293
}
284
294
}
285
295
}
@@ -289,28 +299,32 @@ protected void loadSystemBinary() {
289
299
* native library data structure defining the directory path.
290
300
*
291
301
* @param library the platform-specific library to load
302
+ * @param loadingCriterion pass the loading criterion condition to the calling stack metadata structure
292
303
* @throws IOException in case the binary to be extracted is not found on the specified jar
293
304
* @throws LoadingRetryExhaustionException if the number of loading failure exceeds the specified
294
305
* number.
295
306
*/
296
- protected void loadBinary (NativeDynamicLibrary library ) throws Exception {
307
+ protected void loadBinary (NativeDynamicLibrary library , LoadingCriterion loadingCriterion ) throws Exception {
297
308
try {
298
309
System .load (library .getExtractedLibrary ());
299
310
SnapLoaderLogger .log (Level .INFO , getClass ().getName (), "loadBinary" , "Successfully loaded library: "
300
311
+ library .getExtractedLibrary ());
301
312
if (nativeBinaryLoadingListener != null ) {
302
- nativeBinaryLoadingListener .onLoadingSuccess (this );
313
+ nativeBinaryLoadingListener .onLoadingSuccess (this ,
314
+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], loadingCriterion ));
303
315
}
304
316
} catch (final UnsatisfiedLinkError error ) {
305
317
SnapLoaderLogger .log (Level .SEVERE , getClass ().getName (), "loadBinary" , "Cannot load the dynamic library: "
306
318
+ library .getExtractedLibrary (), error );
307
319
if (nativeBinaryLoadingListener != null ) {
308
- nativeBinaryLoadingListener .onLoadingFailure (this );
320
+ nativeBinaryLoadingListener .onLoadingFailure (this ,
321
+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], loadingCriterion , error ));
309
322
}
310
323
/* Retry with clean extract */
311
324
if (isRetryWithCleanExtraction ()) {
312
325
if (nativeBinaryLoadingListener != null ) {
313
- nativeBinaryLoadingListener .onRetryCriterionExecution (this );
326
+ nativeBinaryLoadingListener .onRetryCriterionExecution (this ,
327
+ new CallingStackMetaData (Thread .currentThread ().getStackTrace ()[1 ], loadingCriterion ));
314
328
}
315
329
// limit the number of retries to maxNumberOfLoadingFailure
316
330
if (numberOfLoadingFailure >= maxNumberOfLoadingFailure ) {
@@ -347,7 +361,7 @@ public void onExtractionCompleted(FileExtractor fileExtractor) {
347
361
SnapLoaderLogger .log (Level .INFO , getClass ().getName (), "cleanExtractBinary" ,
348
362
"Extracted successfully to " + library .getExtractedLibrary ());
349
363
// load the native binary
350
- loadBinary (library );
364
+ loadBinary (library , LoadingCriterion . CLEAN_EXTRACTION );
351
365
} catch (Exception e ) {
352
366
SnapLoaderLogger .log (Level .SEVERE , getClass ().getName (), "cleanExtractBinary" ,
353
367
"Error while loading the binary!" , e );
@@ -442,6 +456,14 @@ public void onFileLocalizationFailure(FileLocator locator, Throwable throwable)
442
456
if (libraryLocalizingListener != null ) {
443
457
libraryLocalizingListener .onFileLocalizationFailure (locator , throwable );
444
458
}
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
+ }
445
467
}
446
468
});
447
469
return (LibraryLocator ) extractor .getFileLocator ();
0 commit comments