|
| 1 | +/* |
| 2 | + * Copyright (c) 2023-2025, The Electrostatic-Sandbox Distributed Simulation Framework, jSnapLoader |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'Electrostatic-Sandbox' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | + |
| 33 | +package electrostatic4j.snaploader.examples; |
| 34 | + |
| 35 | +import java.nio.file.Files; |
| 36 | +import java.nio.file.Path; |
| 37 | +import java.nio.file.Paths; |
| 38 | +import electrostatic4j.snaploader.LibraryInfo; |
| 39 | +import electrostatic4j.snaploader.LoadingCriterion; |
| 40 | +import electrostatic4j.snaploader.NativeBinaryLoader; |
| 41 | +import electrostatic4j.snaploader.filesystem.DirectoryPath; |
| 42 | +import electrostatic4j.snaploader.platform.NativeDynamicLibrary; |
| 43 | +import electrostatic4j.snaploader.platform.util.DefaultDynamicLibraries; |
| 44 | +import electrostatic4j.snaploader.platform.util.NativeVariant; |
| 45 | +import electrostatic4j.snaploader.platform.util.PlatformPredicate; |
| 46 | +import electrostatic4j.snaploader.platform.util.PropertiesProvider; |
| 47 | + |
| 48 | +/** |
| 49 | + * Tests retry exhaustion on a broken library. |
| 50 | + * |
| 51 | + * @author pavl_g. |
| 52 | + */ |
| 53 | +public class TestRetryExhaustionException { |
| 54 | + public static void main(String[] args) throws Exception { |
| 55 | + |
| 56 | + final Path compressionPath = Paths.get(PropertiesProvider.USER_DIR.getSystemProperty(), "libs", "corrupted-lib.jar"); |
| 57 | + final Path extractionPath = Files.createDirectories(Paths.get(PropertiesProvider.USER_DIR.getSystemProperty(), "libs", |
| 58 | + NativeVariant.OS_NAME.getProperty(), NativeVariant.OS_ARCH.getProperty())); |
| 59 | + |
| 60 | + final LibraryInfo libraryInfo = new LibraryInfo(new DirectoryPath(compressionPath.toString()), new DirectoryPath("lib/placeholder"), |
| 61 | + "jmealloc", new DirectoryPath(extractionPath.toString())); |
| 62 | + |
| 63 | + final NativeDynamicLibrary[] libraries = new NativeDynamicLibrary[] { |
| 64 | + DefaultDynamicLibraries.LINUX_X86_64, |
| 65 | + DefaultDynamicLibraries.LINUX_X86, |
| 66 | + new NativeDynamicLibrary("lib/windows/x86", "libjmealloc.dll", PlatformPredicate.WIN_X86), |
| 67 | + new NativeDynamicLibrary("lib/windows/x86-64", "libjmealloc.dll", PlatformPredicate.WIN_X86_64), |
| 68 | + DefaultDynamicLibraries.MAC_X86, |
| 69 | + DefaultDynamicLibraries.MAC_X86_64, |
| 70 | + }; |
| 71 | + |
| 72 | + final NativeBinaryLoader loader = new NativeBinaryLoader(libraryInfo); |
| 73 | + |
| 74 | + loader.registerNativeLibraries(libraries).initPlatformLibrary(); |
| 75 | + loader.setLoggingEnabled(true); |
| 76 | + loader.setRetryWithCleanExtraction(true); |
| 77 | + /* Native dynamic library properties */ |
| 78 | + printDetails(loader); |
| 79 | + loader.loadLibrary(LoadingCriterion.INCREMENTAL_LOADING); |
| 80 | + } |
| 81 | + |
| 82 | + public static void printDetails(NativeBinaryLoader loader) { |
| 83 | + System.out.println("--------------------------------------------------------------"); |
| 84 | + System.out.println("OS: " + NativeVariant.OS_NAME.getProperty()); |
| 85 | + System.out.println("ARCH: " + NativeVariant.OS_ARCH.getProperty()); |
| 86 | + System.out.println("VM: " + NativeVariant.JVM.getProperty()); |
| 87 | + System.out.println("--------------------------------------------------------------"); |
| 88 | + System.out.println("Jar Path: " + loader.getNativeDynamicLibrary().getJarPath()); |
| 89 | + System.out.println("Library Directory: " + loader.getNativeDynamicLibrary().getPlatformDirectory()); |
| 90 | + System.out.println("Compressed library path: " + loader.getNativeDynamicLibrary().getCompressedLibrary()); |
| 91 | + System.out.println("Extracted library absolute path: " + loader.getNativeDynamicLibrary().getExtractedLibrary()); |
| 92 | + System.out.println("Is Extracted: " + loader.getNativeDynamicLibrary().isExtracted()); |
| 93 | + System.out.println("--------------------------------------------------------------"); |
| 94 | + } |
| 95 | +} |
0 commit comments