diff --git a/snaploader-examples/src/main/java/electrostatic/snaploader/examples/TestFilesystemMemoryLeak.java b/snaploader-examples/src/main/java/electrostatic/snaploader/examples/TestFilesystemMemoryLeak.java index f2f4295..848f044 100644 --- a/snaploader-examples/src/main/java/electrostatic/snaploader/examples/TestFilesystemMemoryLeak.java +++ b/snaploader-examples/src/main/java/electrostatic/snaploader/examples/TestFilesystemMemoryLeak.java @@ -37,6 +37,8 @@ import electrostatic.snaploader.filesystem.FileLocator; import electrostatic.snaploader.filesystem.ZipCompressionType; import electrostatic.snaploader.platform.util.PropertiesProvider; +import electrostatic.snaploader.util.SnapLoaderLogger; + import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; @@ -49,9 +51,12 @@ public class TestFilesystemMemoryLeak { public static void main(String[] args) throws IOException { /* Locates the image inside the Zip Compression */ + SnapLoaderLogger.setLoggingEnabled(true); final FileLocator fileLocator = new FileLocator(getZipAbsolutePath(), getFilePath(), ZipCompressionType.ZIP); /* Extracts the image filesystem from the Zip Compression */ final FileExtractor fileExtractor = new FileExtractor(fileLocator, getExtractionPath()); + fileLocator.initialize(0); + fileExtractor.initialize(0); /* CLOSE/CLEAR I/O Resources */ fileExtractor.setExtractionListener(new FileExtractionListener() { @Override diff --git a/snaploader-examples/src/main/java/electrostatic/snaploader/examples/TestMultiThreading.java b/snaploader-examples/src/main/java/electrostatic/snaploader/examples/TestMultiThreading.java index e25913c..3ae9571 100644 --- a/snaploader-examples/src/main/java/electrostatic/snaploader/examples/TestMultiThreading.java +++ b/snaploader-examples/src/main/java/electrostatic/snaploader/examples/TestMultiThreading.java @@ -37,7 +37,7 @@ import electrostatic.snaploader.LoadingCriterion; import electrostatic.snaploader.ConcurrentNativeBinaryLoader; -import electrostatic.snaploader.UnSupportedSystemError; +import electrostatic.snaploader.throwable.UnSupportedSystemError; import electrostatic.snaploader.platform.util.DefaultDynamicLibraries; import electrostatic.snaploader.platform.NativeDynamicLibrary; diff --git a/snaploader-examples/src/main/java/electrostatic/snaploader/examples/TestZipExtractor.java b/snaploader-examples/src/main/java/electrostatic/snaploader/examples/TestZipExtractor.java index 27d7b5d..69231bf 100644 --- a/snaploader-examples/src/main/java/electrostatic/snaploader/examples/TestZipExtractor.java +++ b/snaploader-examples/src/main/java/electrostatic/snaploader/examples/TestZipExtractor.java @@ -41,6 +41,7 @@ import electrostatic.snaploader.filesystem.FileLocator; import electrostatic.snaploader.filesystem.ZipCompressionType; import electrostatic.snaploader.platform.util.PropertiesProvider; +import electrostatic.snaploader.throwable.FilesystemResourceScavengingException; /** * Tests extracting an image compression from a Zip compression type filesystem using {@link FileExtractor} API. @@ -54,6 +55,8 @@ public static void main(String[] args) throws IOException { final FileLocator fileLocator = new FileLocator(getZipAbsolutePath(), getFilePath(), ZipCompressionType.ZIP); /* Extracts the image filesystem from the Zip Compression */ final FileExtractor fileExtractor = new FileExtractor(fileLocator, getExtractionPath()); + fileLocator.initialize(0); + fileExtractor.initialize(0); /* CLOSE/CLEAR I/O Resources */ fileExtractor.setExtractionListener(new FileExtractionListener() { @Override @@ -70,11 +73,8 @@ public void onExtractionFailure(FileExtractor fileExtractor, Throwable throwable public void onExtractionFinalization(FileExtractor fileExtractor, FileLocator fileLocator) { try { fileExtractor.close(); - fileLocator.close(); - Logger.getLogger(TestZipExtractor.class.getName()) - .log(Level.INFO, "Filesystem Resources Closed!"); - } catch (IOException e) { - throw new RuntimeException(e); + } catch (Exception e) { + throw new FilesystemResourceScavengingException(e); } } }); diff --git a/snaploader/src/main/java/electrostatic/snaploader/NativeBinaryLoader.java b/snaploader/src/main/java/electrostatic/snaploader/NativeBinaryLoader.java index c34f63c..0837183 100644 --- a/snaploader/src/main/java/electrostatic/snaploader/NativeBinaryLoader.java +++ b/snaploader/src/main/java/electrostatic/snaploader/NativeBinaryLoader.java @@ -36,7 +36,6 @@ import java.util.Arrays; import java.util.List; import java.util.logging.Level; -import java.util.logging.Logger; import java.lang.UnsatisfiedLinkError; import electrostatic.snaploader.filesystem.FileExtractionListener; import electrostatic.snaploader.filesystem.FileExtractor; @@ -46,6 +45,8 @@ import electrostatic.snaploader.library.LibraryLocator; import electrostatic.snaploader.platform.NativeDynamicLibrary; import electrostatic.snaploader.platform.util.NativeVariant; +import electrostatic.snaploader.throwable.UnSupportedSystemError; +import electrostatic.snaploader.util.SnapLoaderLogger; /** * A cross-platform utility for extracting and loading native binaries based on @@ -55,11 +56,6 @@ */ public class NativeBinaryLoader { - /** - * NativeBinaryLoader logger object. - */ - protected static final Logger logger = Logger.getLogger(NativeBinaryLoader.class.getName()); - protected final LibraryInfo libraryInfo; protected List registeredLibraries; @@ -82,11 +78,6 @@ public class NativeBinaryLoader { */ protected NativeDynamicLibrary nativeDynamicLibrary; - /** - * Flag for enable/disable logging. - */ - protected boolean loggingEnabled; - /** * Flag for retry loading with clean extract if UnSatisfiedLinkError is thrown. */ @@ -173,7 +164,7 @@ public NativeBinaryLoader loadLibrary(LoadingCriterion criterion) throws IOExcep /** * Retrieves the native dynamic library object representing the library to extract and load. * - * @return an object representing the platform dependent native dynamic library + * @return an object representing the platform-dependent native dynamic library */ public NativeDynamicLibrary getNativeDynamicLibrary() { return nativeDynamicLibrary; @@ -181,20 +172,11 @@ public NativeDynamicLibrary getNativeDynamicLibrary() { /** * Enables the logging for this object, default value is false. - * + * * @param loggingEnabled true to enable logging, false otherwise */ public void setLoggingEnabled(boolean loggingEnabled) { - this.loggingEnabled = loggingEnabled; - } - - /** - * Tests the logging flag, default value is false. - * - * @return true if the logging flag is enabled, false otherwise - */ - public boolean isLoggingEnabled() { - return loggingEnabled; + SnapLoaderLogger.setLoggingEnabled(loggingEnabled); } /** @@ -252,7 +234,7 @@ public FileLocalizingListener getLibraryLocalizingListener() { } /** - * Loads a native binary using the platform dependent object, for android, + * Loads a native binary using the platform-dependent object, for Android; * the library is loaded by its basename (variant is managed internally by the android sdk). * * @param library the platform-specific library to load @@ -260,18 +242,22 @@ public FileLocalizingListener getLibraryLocalizingListener() { */ protected void loadBinary(NativeDynamicLibrary library) throws IOException { try { - /* sanity check for android java vm (the dalvik) */ + /* sanity-check for android java vm (the dalvik) */ if (NativeVariant.Os.isAndroid()) { System.loadLibrary(libraryInfo.getBaseName()); + SnapLoaderLogger.log(Level.INFO, getClass().getName(),"loadBinary", "Successfully loaded library for Android: " + + library.getExtractedLibrary()); return; } System.load(library.getExtractedLibrary()); - log(Level.INFO, "loadBinary", "Successfully loaded library: " + library.getExtractedLibrary(), null); + SnapLoaderLogger.log(Level.INFO, getClass().getName(),"loadBinary", "Successfully loaded library: " + + library.getExtractedLibrary()); if (nativeBinaryLoadingListener != null) { nativeBinaryLoadingListener.onLoadingSuccess(this); } } catch (final UnsatisfiedLinkError error) { - log(Level.SEVERE, "loadBinary", "Cannot load the dynamic library: " + library.getExtractedLibrary(), error); + SnapLoaderLogger.log(Level.SEVERE, getClass().getName(), "loadBinary", "Cannot load the dynamic library: " + + library.getExtractedLibrary(), error); if (nativeBinaryLoadingListener != null) { nativeBinaryLoadingListener.onLoadingFailure(this); } @@ -289,12 +275,13 @@ protected void loadBinary(NativeDynamicLibrary library) throws IOException { * Cleanly extracts and loads the native binary to the current [user.dir]. * * @param library the platform-specific library to extract and load - * @throws IOException in case the binary to be extracted is not found on the specified jar or an - * interrupted I/O operation has occured + * @throws IOException in case the binary to be extracted is not found on the specified jar, or an + * interrupted I/O operation has occurred */ protected void cleanExtractBinary(NativeDynamicLibrary library) throws IOException { libraryExtractor = initializeLibraryExtractor(library); - log(Level.INFO, "cleanExtractBinary", "File extractor handler initialized!", null); + SnapLoaderLogger.log(Level.INFO, getClass().getName(), "cleanExtractBinary", + "File extractor handler initialized!"); /* CLEAR RESOURCES AND RESET OBJECTS ON-EXTRACTION */ libraryExtractor.setExtractionListener(new FileExtractionListener() { @Override @@ -302,15 +289,15 @@ public void onExtractionCompleted(FileExtractor fileExtractor) { try { // free resources // removes file locks on some OS - libraryExtractor.getFileLocator().close(); libraryExtractor.close(); libraryExtractor = null; - log(Level.INFO, "cleanExtractBinary", "Extracted successfully to " + library.getExtractedLibrary(), null); - log(Level.INFO, "cleanExtractBinary", "Filesystem Resources closed!", null); + SnapLoaderLogger.log(Level.INFO, getClass().getName(), "cleanExtractBinary", + "Extracted successfully to " + library.getExtractedLibrary()); // load the native binary loadBinary(library); } catch (Exception e) { - log(Level.SEVERE, "cleanExtractBinary", "Error while loading the binary!", e); + SnapLoaderLogger.log(Level.SEVERE, getClass().getName(), "cleanExtractBinary", + "Error while loading the binary!", e); } // bind the extraction lifecycle to the user application @@ -321,7 +308,8 @@ public void onExtractionCompleted(FileExtractor fileExtractor) { @Override public void onExtractionFailure(FileExtractor fileExtractor, Throwable throwable) { - log(Level.SEVERE, "cleanExtractBinary", "Extraction has failed!", throwable); + SnapLoaderLogger.log(Level.SEVERE, getClass().getName(), + "cleanExtractBinary", "Extraction has failed!", throwable); // bind the extraction lifecycle to the user application if (libraryExtractionListener != null) { @@ -332,18 +320,13 @@ public void onExtractionFailure(FileExtractor fileExtractor, Throwable throwable @Override public void onExtractionFinalization(FileExtractor fileExtractor, FileLocator fileLocator) { try { - if (fileLocator != null && - fileLocator.getFileInputStream() != null) { - fileLocator.close(); - log(Level.INFO, "cleanExtractBinary", "File locator Resources closed!", null); - } if (fileExtractor != null && fileExtractor.getFileOutputStream() != null) { fileExtractor.close(); - log(Level.INFO, "cleanExtractBinary", "File extractor Resources closed!", null); } - } catch (IOException e) { - log(Level.SEVERE, "cleanExtractBinary", "Error while closing the resources!", e); + } catch (Exception e) { + SnapLoaderLogger.log(Level.SEVERE, getClass().getName(), + "cleanExtractBinary", "Error while closing the resources!", e); } // bind the extraction lifecycle to the user application @@ -370,8 +353,9 @@ protected FileExtractor initializeLibraryExtractor(NativeDynamicLibrary library) } else { extractor = new LibraryExtractor(library.getCompressedLibrary(), library.getExtractedLibrary()); } + extractor.initialize(0); final LibraryLocator fileLocator = preInitLibraryLocator(extractor); - fileLocator.initializeLocator(); + fileLocator.initialize(0); return extractor; } @@ -379,7 +363,8 @@ protected LibraryLocator preInitLibraryLocator(FileExtractor extractor) { extractor.getFileLocator().setFileLocalizingListener(new FileLocalizingListener() { @Override public void onFileLocalizationSuccess(FileLocator locator) { - log(Level.INFO, "initializeLibraryExtractor", "Locating native libraries has succeeded!", null); + SnapLoaderLogger.log(Level.INFO, getClass().getName(), "initializeLibraryExtractor", + "Locating native libraries has succeeded!"); // bind the library locator lifecycle to the user application if (libraryLocalizingListener != null) { @@ -389,13 +374,13 @@ public void onFileLocalizationSuccess(FileLocator locator) { @Override public void onFileLocalizationFailure(FileLocator locator, Throwable throwable) { - log(Level.SEVERE, "initializeLibraryExtractor", "Locating native libraries has failed!", throwable); + SnapLoaderLogger.log(Level.SEVERE, getClass().getName(), "initializeLibraryExtractor", + "Locating native libraries has failed!", throwable); try { extractor.close(); - locator.close(); - log(Level.INFO, "initializeLibraryExtractor", "Filesystem resources closed!", null); - } catch (IOException e) { - log(Level.SEVERE, "initializeLibraryExtractor", "File locator closure failed!", e); + } catch (Exception e) { + SnapLoaderLogger.log(Level.SEVERE, getClass().getName(), + "initializeLibraryExtractor", "File locator closure failed!", e); } // bind the library locator lifecycle to the user application @@ -406,23 +391,4 @@ public void onFileLocalizationFailure(FileLocator locator, Throwable throwable) }); return (LibraryLocator) extractor.getFileLocator(); } - - /** - * Log data with a level and a throwable (optional). - * - * @param level the logger level - * @param sourceMethod the source of this call - * @param msg a string formatted message to display - * @param throwable optional param for error messages - */ - protected void log(Level level, String sourceMethod, String msg, Throwable throwable) { - if (!isLoggingEnabled()) { - return; - } - if (throwable == null) { - logger.logp(level, this.getClass().getName(), sourceMethod, msg); - } else { - logger.logp(level, this.getClass().getName(), sourceMethod, msg, throwable); - } - } } diff --git a/snaploader/src/main/java/electrostatic/snaploader/SystemDetectionListener.java b/snaploader/src/main/java/electrostatic/snaploader/SystemDetectionListener.java index 95d64bf..cd2f420 100644 --- a/snaploader/src/main/java/electrostatic/snaploader/SystemDetectionListener.java +++ b/snaploader/src/main/java/electrostatic/snaploader/SystemDetectionListener.java @@ -33,6 +33,7 @@ package electrostatic.snaploader; import electrostatic.snaploader.platform.NativeDynamicLibrary; +import electrostatic.snaploader.throwable.UnSupportedSystemError; /** * Provides executable functions binding the user applications to diff --git a/snaploader/src/main/java/electrostatic/snaploader/filesystem/FileExtractor.java b/snaploader/src/main/java/electrostatic/snaploader/filesystem/FileExtractor.java index 8b42227..90ea441 100644 --- a/snaploader/src/main/java/electrostatic/snaploader/filesystem/FileExtractor.java +++ b/snaploader/src/main/java/electrostatic/snaploader/filesystem/FileExtractor.java @@ -32,11 +32,11 @@ package electrostatic.snaploader.filesystem; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import electrostatic.snaploader.throwable.FilesystemResourceInitializationException; +import electrostatic.snaploader.util.SnapLoaderLogger; + +import java.io.*; +import java.util.logging.Level; /** * Extracts a filesystem from a zip compression to a destination filesystem. @@ -76,7 +76,7 @@ public class FileExtractor implements OutputStreamProvider { */ public FileExtractor(FileLocator fileLocator, String destination) throws FileNotFoundException { this.fileLocator = fileLocator; - this.fileOutputStream = new FileOutputStream(destination); + this.destination = destination; } /** @@ -85,6 +85,25 @@ public FileExtractor(FileLocator fileLocator, String destination) throws FileNot protected FileExtractor() { } + @Override + public void initialize(int size) { + try { + if (size > 0) { + this.fileOutputStream = new BufferedOutputStream( + new FileOutputStream(destination), size); + SnapLoaderLogger.log(Level.INFO, getClass().getName(), "initialize(int)", + "File extractor initialized with hash key #" + getHashKey()); + return; + } + this.fileOutputStream = new FileOutputStream(destination); + SnapLoaderLogger.log(Level.INFO, getClass().getName(), "initialize(int)", + "File extractor initialized with hash key #" + getHashKey()); + } catch (FileNotFoundException e) { + throw new FilesystemResourceInitializationException( + "Failed to initialize the file extractor handler #" + getHashKey(), e); + } + } + /** * Commands and Extract the specified filesystem to the specified destination filesystem. *

@@ -133,11 +152,21 @@ public void extract() throws IOException, FileNotFoundException { } @Override - public void close() throws IOException { + public void close() throws Exception { if (fileOutputStream != null) { fileOutputStream.close(); fileOutputStream = null; } + + // close the associated file locator resources + if (getFileLocator() != null && getFileLocator().getFileInputStream() != null) { + getFileLocator().close(); + SnapLoaderLogger.log(Level.INFO, getClass().getName(), "close", + "Delegation for file locator resources closure has succeeded!"); + } + + SnapLoaderLogger.log(Level.INFO, getClass().getName(), "close", + "File extractor #" + getHashKey() + " resources closed!"); } @Override diff --git a/snaploader/src/main/java/electrostatic/snaploader/filesystem/FileLocator.java b/snaploader/src/main/java/electrostatic/snaploader/filesystem/FileLocator.java index 3a658ff..65981fb 100644 --- a/snaploader/src/main/java/electrostatic/snaploader/filesystem/FileLocator.java +++ b/snaploader/src/main/java/electrostatic/snaploader/filesystem/FileLocator.java @@ -32,9 +32,11 @@ package electrostatic.snaploader.filesystem; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; +import electrostatic.snaploader.throwable.FilesystemResourceInitializationException; +import electrostatic.snaploader.util.SnapLoaderLogger; + +import java.io.*; +import java.util.logging.Level; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -88,11 +90,26 @@ public FileLocator(String directory, String filePath, ZipCompressionType compres protected FileLocator() { } - public void initializeLocator() throws IOException { - ZipFile compression = compressionType.createNewCompressionObject(directory); - ZipEntry zipEntry = compression.getEntry(filePath); - validateFileLocalization(zipEntry); - this.fileInputStream = compression.getInputStream(zipEntry); + + @Override + public void initialize(int size) { + try { + final ZipFile compression = compressionType.createNewCompressionObject(directory); + final ZipEntry zipEntry = compression.getEntry(filePath); + validateFileLocalization(zipEntry); + if (size > 0) { + this.fileInputStream = new BufferedInputStream(compression.getInputStream(zipEntry), size); + SnapLoaderLogger.log(Level.INFO, getClass().getName(), "initialize(int)", + "File locator initialized with hash key #" + getHashKey()); + return; + } + this.fileInputStream = compression.getInputStream(zipEntry); + SnapLoaderLogger.log(Level.INFO, getClass().getName(), "initialize(int)", + "File locator initialized with hash key #" + getHashKey()); + } catch (IOException e) { + throw new FilesystemResourceInitializationException( + "Failed to initialize the file locator handler #" + getHashKey(), e); + } } /** @@ -128,6 +145,8 @@ public void close() throws IOException { fileInputStream.close(); fileInputStream = null; } + SnapLoaderLogger.log(Level.INFO, getClass().getName(), + "close", "File locator #" + getHashKey() + " resources closed!"); } @Override diff --git a/snaploader/src/main/java/electrostatic/snaploader/filesystem/InputStreamProvider.java b/snaploader/src/main/java/electrostatic/snaploader/filesystem/InputStreamProvider.java index e5dcd78..707cdc3 100644 --- a/snaploader/src/main/java/electrostatic/snaploader/filesystem/InputStreamProvider.java +++ b/snaploader/src/main/java/electrostatic/snaploader/filesystem/InputStreamProvider.java @@ -40,7 +40,7 @@ * * @author pavl_g */ -public interface InputStreamProvider extends AutoCloseable { +public interface InputStreamProvider extends StreamProvider { /** * Retrieves the input stream object associated with this filesystem entry. diff --git a/snaploader/src/main/java/electrostatic/snaploader/filesystem/OutputStreamProvider.java b/snaploader/src/main/java/electrostatic/snaploader/filesystem/OutputStreamProvider.java index 40441d2..6d631c1 100644 --- a/snaploader/src/main/java/electrostatic/snaploader/filesystem/OutputStreamProvider.java +++ b/snaploader/src/main/java/electrostatic/snaploader/filesystem/OutputStreamProvider.java @@ -40,7 +40,7 @@ * * @author pavl_g */ -public interface OutputStreamProvider extends AutoCloseable { +public interface OutputStreamProvider extends StreamProvider { /** * Retrieves the input stream provider object (the filesystem locator object). diff --git a/snaploader/src/main/java/electrostatic/snaploader/filesystem/StreamProvider.java b/snaploader/src/main/java/electrostatic/snaploader/filesystem/StreamProvider.java new file mode 100644 index 0000000..aed08fa --- /dev/null +++ b/snaploader/src/main/java/electrostatic/snaploader/filesystem/StreamProvider.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023-2024, The Electrostatic-Sandbox Distributed Simulation Framework, jSnapLoader + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Electrostatic-Sandbox' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package electrostatic.snaploader.filesystem; + +/** + * A generic interface providing the main entry for + * all stream providers for buffered IO operations. + * + * @author pavl_g + */ +public interface StreamProvider extends AutoCloseable { + + /** + * Initializes the buffered stream handler object. + * + * @param size the size of the buffered IO in bytes or zero + * for auto filesystem size + */ + void initialize(int size); + + /** + * Generates a unique hash key for this object + * using an MSB to LSB spreading algorithm. + * + * @return an XoR operation of the MSBs (Most Significant Bits) with + * the LSBs (the Least Significant Bits) of the object hash code + */ + default int getHashKey() { + return (hashCode() >>> 16) ^ hashCode(); + } +} diff --git a/snaploader/src/main/java/electrostatic/snaploader/throwable/FilesystemResourceInitializationException.java b/snaploader/src/main/java/electrostatic/snaploader/throwable/FilesystemResourceInitializationException.java new file mode 100644 index 0000000..0532b2b --- /dev/null +++ b/snaploader/src/main/java/electrostatic/snaploader/throwable/FilesystemResourceInitializationException.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023-2024, The Electrostatic-Sandbox Distributed Simulation Framework, jSnapLoader + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Electrostatic-Sandbox' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package electrostatic.snaploader.throwable; + +/** + * Should be thrown when the initialization of the native streams + * has failed. + * + * @author pavl_g + */ +public class FilesystemResourceInitializationException extends RuntimeException { + public FilesystemResourceInitializationException() { + } + + public FilesystemResourceInitializationException(String message) { + super(message); + } + + public FilesystemResourceInitializationException(String message, Throwable cause) { + super(message, cause); + } + + public FilesystemResourceInitializationException(Throwable cause) { + super(cause); + } + + public FilesystemResourceInitializationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/snaploader/src/main/java/electrostatic/snaploader/throwable/FilesystemResourceScavengingException.java b/snaploader/src/main/java/electrostatic/snaploader/throwable/FilesystemResourceScavengingException.java new file mode 100644 index 0000000..de7cdf1 --- /dev/null +++ b/snaploader/src/main/java/electrostatic/snaploader/throwable/FilesystemResourceScavengingException.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023-2024, The Electrostatic-Sandbox Distributed Simulation Framework, jSnapLoader + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Electrostatic-Sandbox' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package electrostatic.snaploader.throwable; + +/** + * Should be thrown when the closure of resources has failed. + * + * @author pavl_g + */ +public class FilesystemResourceScavengingException extends RuntimeException { + public FilesystemResourceScavengingException() { + } + + public FilesystemResourceScavengingException(String message) { + super(message); + } + + public FilesystemResourceScavengingException(String message, Throwable cause) { + super(message, cause); + } + + public FilesystemResourceScavengingException(Throwable cause) { + super(cause); + } + + public FilesystemResourceScavengingException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/snaploader/src/main/java/electrostatic/snaploader/UnSupportedSystemError.java b/snaploader/src/main/java/electrostatic/snaploader/throwable/UnSupportedSystemError.java similarity index 98% rename from snaploader/src/main/java/electrostatic/snaploader/UnSupportedSystemError.java rename to snaploader/src/main/java/electrostatic/snaploader/throwable/UnSupportedSystemError.java index 4bccc5b..09888d0 100644 --- a/snaploader/src/main/java/electrostatic/snaploader/UnSupportedSystemError.java +++ b/snaploader/src/main/java/electrostatic/snaploader/throwable/UnSupportedSystemError.java @@ -30,7 +30,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package electrostatic.snaploader; +package electrostatic.snaploader.throwable; /** * A business error of type {@link UnsatisfiedLinkError} to indicate an unsupported system. diff --git a/snaploader/src/main/java/electrostatic/snaploader/throwable/package-info.java b/snaploader/src/main/java/electrostatic/snaploader/throwable/package-info.java new file mode 100644 index 0000000..215e09a --- /dev/null +++ b/snaploader/src/main/java/electrostatic/snaploader/throwable/package-info.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023-2024, The Electrostatic-Sandbox Distributed Simulation Framework, jSnapLoader + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Electrostatic-Sandbox' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * Pack for throwable objects that are recoverable targets. + */ +package electrostatic.snaploader.throwable; \ No newline at end of file diff --git a/snaploader/src/main/java/electrostatic/snaploader/util/SnapLoaderLogger.java b/snaploader/src/main/java/electrostatic/snaploader/util/SnapLoaderLogger.java new file mode 100644 index 0000000..c68ab17 --- /dev/null +++ b/snaploader/src/main/java/electrostatic/snaploader/util/SnapLoaderLogger.java @@ -0,0 +1,64 @@ +package electrostatic.snaploader.util; + +import java.util.logging.Level; +import java.util.logging.Logger; + +public final class SnapLoaderLogger { + + /** + * NativeBinaryLoader logger object. + */ + private static final Logger logger = Logger.getLogger("jSnapLoader"); + + /** + * Flag for enable/disable logging. + */ + private static boolean loggingEnabled; + + /** + * Log data with a level and a throwable (optional). + * + * @param level the logger level + * @param sourceMethod the source of this call + * @param msg a string formatted message to display + */ + public static void log(Level level, String sourceClass, String sourceMethod, String msg) { + if (!SnapLoaderLogger.isLoggingEnabled()) { + return; + } + logger.logp(level, sourceClass, sourceMethod, msg); + } + + /** + * Log data with a level and a throwable (optional). + * + * @param level the logger level + * @param sourceMethod the source of this call + * @param msg a string formatted message to display + * @param throwable optional param for error messages + */ + public static void log(Level level, String sourceClass, String sourceMethod, String msg, Throwable throwable) { + if (!SnapLoaderLogger.isLoggingEnabled()) { + return; + } + logger.logp(level, sourceClass, sourceMethod, msg, throwable); + } + + /** + * Enables the logging for this object, default value is false. + * + * @param loggingEnabled true to enable logging, false otherwise + */ + public static void setLoggingEnabled(boolean loggingEnabled) { + SnapLoaderLogger.loggingEnabled = loggingEnabled; + } + + /** + * Tests the logging flag, default value is false. + * + * @return true if the logging flag is enabled, false otherwise + */ + public static boolean isLoggingEnabled() { + return SnapLoaderLogger.loggingEnabled; + } +}