Skip to content

Commit

Permalink
Merge pull request #29 from Electrostat-Lab/epsilon-patch
Browse files Browse the repository at this point in the history
Epsilon-patch: introduced a sanity-check of the stream provider handler for other File locator routines
  • Loading branch information
pavly-gerges authored Aug 4, 2024
2 parents 91fbefc + 8ac5a23 commit dd1adcb
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public final class TestBasicFeatures {
DefaultDynamicLibraries.MAC_X86_64,
};

public static void main(String[] args) throws IOException {
public static void main(String[] args) throws Exception {
if (loader == null) {
loader = new NativeBinaryLoader(libraryInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
*/
public final class TestBasicFeatures2 {

public static void main(String[] args) throws IOException {
public static void main(String[] args) throws Exception {

final Path compressionPath = Paths.get(PropertiesProvider.USER_DIR.getSystemProperty(), "libs", TestBasicFeatures.getJarFile());
final Path extractionPath = Files.createDirectories(Paths.get(PropertiesProvider.USER_DIR.getSystemProperty(), "libs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.nio.file.Paths;

public final class TestFilesystemException {
public static void main(String[] args) throws IOException {
public static void main(String[] args) throws Exception {
final Path compressionPath = Paths.get(PropertiesProvider.USER_DIR.getSystemProperty(), "libs", TestBasicFeatures.getJarFile());
final Path extractionPath = Files.createDirectories(Paths.get(PropertiesProvider.USER_DIR.getSystemProperty(), "libs",
NativeVariant.OS_NAME.getProperty(), NativeVariant.OS_ARCH.getProperty()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* @author pavl_g
*/
public class TestFilesystemMemoryLeak {
public static void main(String[] args) throws IOException {
public static void main(String[] args) throws Exception {
/* Locates the image inside the Zip Compression */
SnapLoaderLogger.setLoggingEnabled(true);
final FileLocator fileLocator = new FileLocator(getZipAbsolutePath(), getFilePath(), ZipCompressionType.ZIP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void run() {
try {
Thread.sleep(200);
TestBasicFeatures.loader.loadLibrary(LoadingCriterion.CLEAN_EXTRACTION);
} catch (UnSupportedSystemError | IOException | InterruptedException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
Expand All @@ -69,7 +69,7 @@ public void run() {
try {
Thread.sleep(200);
TestBasicFeatures.loader.loadLibrary(LoadingCriterion.CLEAN_EXTRACTION);
} catch (UnSupportedSystemError | IOException | InterruptedException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
Expand All @@ -83,7 +83,7 @@ public void run() {
try {
Thread.sleep(200);
TestBasicFeatures.loader.loadLibrary(LoadingCriterion.CLEAN_EXTRACTION);
} catch (UnSupportedSystemError | IOException | InterruptedException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*/
public final class TestMultipleLoads {

public static void main(String[] args) throws InterruptedException, IOException {
public static void main(String[] args) throws Exception {
TestBasicFeatures.main(args);
new File(TestBasicFeatures.getNativeDynamicLibraryPath()).delete();
Thread.sleep(5000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
*/
public class TestZipExtractor {

public static void main(String[] args) throws IOException {
public static void main(String[] args) throws Exception {
/* Locates the image inside the Zip Compression */
final FileLocator fileLocator = new FileLocator(getZipAbsolutePath(), getFilePath(), ZipCompressionType.ZIP);
/* Extracts the image filesystem from the Zip Compression */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

package electrostatic4j.snaploader;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import electrostatic4j.snaploader.platform.NativeDynamicLibrary;
Expand All @@ -59,7 +58,7 @@ public ConcurrentNativeBinaryLoader(final List<NativeDynamicLibrary> registeredL
}

@Override
protected void cleanExtractBinary(NativeDynamicLibrary library) throws IOException {
protected void cleanExtractBinary(NativeDynamicLibrary library) throws Exception {
try {
/* CRITICAL SECTION STARTS */
lock.lock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public NativeBinaryLoader initPlatformLibrary() throws UnSupportedSystemError {
* @return this instance for chained invocations
* @throws IOException if the library to extract is not present in the jar filesystem
*/
public NativeBinaryLoader loadLibrary(LoadingCriterion criterion) throws IOException {
public NativeBinaryLoader loadLibrary(LoadingCriterion criterion) throws Exception {
if (criterion == LoadingCriterion.INCREMENTAL_LOADING && nativeDynamicLibrary.isExtracted()) {
loadBinary(nativeDynamicLibrary);
return this;
Expand Down Expand Up @@ -240,7 +240,7 @@ public FileLocalizingListener getLibraryLocalizingListener() {
* @param library the platform-specific library to load
* @throws IOException in case the binary to be extracted is not found on the specified jar
*/
protected void loadBinary(NativeDynamicLibrary library) throws IOException {
protected void loadBinary(NativeDynamicLibrary library) throws Exception {
try {
/* sanity-check for android java vm (the dalvik) */
if (NativeVariant.Os.isAndroid()) {
Expand Down Expand Up @@ -278,7 +278,7 @@ protected void loadBinary(NativeDynamicLibrary library) throws IOException {
* @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 {
protected void cleanExtractBinary(NativeDynamicLibrary library) throws Exception {
libraryExtractor = initializeLibraryExtractor(library);
SnapLoaderLogger.log(Level.INFO, getClass().getName(), "cleanExtractBinary",
"File extractor handler initialized!");
Expand Down Expand Up @@ -346,7 +346,7 @@ public void onExtractionFinalization(FileExtractor fileExtractor, FileLocator fi
* @return a new FileExtractor object that represents an output stream provider
* @throws IOException if the jar filesystem to be located is not found, or if the extraction destination is not found
*/
protected FileExtractor initializeLibraryExtractor(NativeDynamicLibrary library) throws IOException {
protected FileExtractor initializeLibraryExtractor(NativeDynamicLibrary library) throws Exception {
FileExtractor extractor;
if (library.getJarPath() != null) {
extractor = new LibraryExtractor(library.getJarPath(), library.getCompressedLibrary(), library.getExtractedLibrary());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ protected FileExtractor() {
}

@Override
public void initialize(int size) {
public void initialize(int size) throws Exception {
// 1) sanity-check for double initializing
// 2) sanity-check for pre-initialization using other routines
if (this.fileOutputStream != null) {
return;
}
try {
if (size > 0) {
this.fileOutputStream = new BufferedOutputStream(
Expand All @@ -98,7 +103,8 @@ public void initialize(int size) {
this.fileOutputStream = new FileOutputStream(destination);
SnapLoaderLogger.log(Level.INFO, getClass().getName(), "initialize(int)",
"File extractor initialized with hash key #" + getHashKey());
} catch (FileNotFoundException e) {
} catch (Exception e) {
close();
throw new FilesystemResourceInitializationException(
"Failed to initialize the file extractor handler #" + getHashKey(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ protected FileLocator() {


@Override
public void initialize(int size) {
public void initialize(int size) throws IOException {
// 1) sanity-check for double initializing
// 2) sanity-check for pre-initialization using other routines
// (e.g., classpath resources stream).
if (this.fileInputStream != null) {
return;
}
try {
final ZipFile compression = compressionType.createNewCompressionObject(directory);
final ZipEntry zipEntry = compression.getEntry(filePath);
Expand All @@ -106,7 +112,8 @@ public void initialize(int size) {
this.fileInputStream = compression.getInputStream(zipEntry);
SnapLoaderLogger.log(Level.INFO, getClass().getName(), "initialize(int)",
"File locator initialized with hash key #" + getHashKey());
} catch (IOException e) {
} catch (Exception e) {
close();
throw new FilesystemResourceInitializationException(
"Failed to initialize the file locator handler #" + getHashKey(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public interface StreamProvider extends AutoCloseable {
* @param size the size of the buffered IO in bytes or zero
* for auto filesystem size
*/
void initialize(int size);
void initialize(int size) throws Exception;

/**
* Generates a unique hash key for this object
Expand Down

0 comments on commit dd1adcb

Please sign in to comment.