Skip to content

Commit fc6891c

Browse files
committed
Simplified exceptions throwing using a Validator API
1 parent 8e5a9e1 commit fc6891c

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

snaploader/src/main/java/electrostatic4j/snaploader/filesystem/FileExtractor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
import electrostatic4j.snaploader.throwable.FilesystemResourceInitializationException;
3636
import electrostatic4j.snaploader.util.SnapLoaderLogger;
37+
import electrostatic4j.snaploader.util.StreamObjectValidator;
38+
3739
import java.io.*;
3840
import java.util.logging.Level;
3941

@@ -133,6 +135,7 @@ public void extract() throws IOException, FileNotFoundException {
133135
* pipe, and allocate memory according to the active bytes manipulated
134136
* by the pipeline. */
135137
InputStream fileStream = fileLocator.getFileInputStream();
138+
StreamObjectValidator.validateAndThrow(fileStream, StreamObjectValidator.BROKEN_FILE_LOCATOR_PROVIDER);
136139

137140
/* Extracts the shipped native files */
138141
/* Allocate a byte buffer for the buffered streams */

snaploader/src/main/java/electrostatic4j/snaploader/filesystem/FileLocator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
import electrostatic4j.snaploader.throwable.FilesystemResourceInitializationException;
3636
import electrostatic4j.snaploader.util.SnapLoaderLogger;
37+
import electrostatic4j.snaploader.util.StreamObjectValidator;
38+
3739
import java.io.*;
3840
import java.util.logging.Level;
3941
import java.util.zip.ZipEntry;
@@ -129,20 +131,19 @@ public void initialize(int size) throws IOException {
129131
externalCompressionRoutine(size);
130132
}
131133

134+
StreamObjectValidator.validateAndThrow(fileInputStream, StreamObjectValidator.BROKEN_FILE_LOCATOR_PROVIDER);
135+
132136
// fire the success listener if the file localization has passed!
133137
if (fileLocalizingListener != null) {
134138
fileLocalizingListener.onFileLocalizationSuccess(this);
135139
}
136140
} catch (Exception e) {
137141
close();
138-
final FilesystemResourceInitializationException exception = new FilesystemResourceInitializationException(
139-
"Failed to initialize the file locator handler #" + getHashKey(), e);
140142
// fire the failure listener when file localization fails and pass
141143
// the causative exception
142144
if (fileLocalizingListener != null) {
143-
fileLocalizingListener.onFileLocalizationFailure(this, exception);
145+
fileLocalizingListener.onFileLocalizationFailure(this, e);
144146
}
145-
throw exception;
146147
}
147148
}
148149

@@ -164,9 +165,6 @@ protected void classPathRoutine() throws FilesystemResourceInitializationExcepti
164165
// getClassLoader() is invoked on them, it will return "null" pointer
165166
// indicating the invalidity of active loaders
166167
this.fileInputStream = getClass().getClassLoader().getResourceAsStream(filePath);
167-
if (this.fileInputStream == null) {
168-
throw new FilesystemResourceInitializationException("Classpath Routine failed: the file is not in the classpath!");
169-
}
170168
}
171169

172170
/**
@@ -178,11 +176,13 @@ protected void classPathRoutine() throws FilesystemResourceInitializationExcepti
178176
*/
179177
protected void externalCompressionRoutine(int size) throws IOException {
180178
final ZipEntry zipEntry = compression.getEntry(filePath);
179+
StreamObjectValidator.validateAndThrow(zipEntry, StreamObjectValidator.COMPRESSION_FILE_LOCALIZING_FAIL);
181180
if (size > 0) {
182181
this.fileInputStream = new BufferedInputStream(compression.getInputStream(zipEntry), size);
183182
} else {
184183
this.fileInputStream = compression.getInputStream(zipEntry);
185184
}
185+
186186
SnapLoaderLogger.log(Level.INFO, getClass().getName(), "initialize(int)",
187187
"File locator initialized using external compression routine with hash key #" + getHashKey());
188188
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2023-2024, 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.util;
34+
35+
import electrostatic4j.snaploader.filesystem.StreamProvider;
36+
import electrostatic4j.snaploader.throwable.FilesystemResourceInitializationException;
37+
38+
/**
39+
* Validates the stream providers tokens.
40+
*
41+
* @author pavl_g
42+
*/
43+
public final class StreamObjectValidator {
44+
45+
public static final String BROKEN_FILE_LOCATOR_PROVIDER = "Broken file locator stream provider!";
46+
public static final String COMPRESSION_FILE_LOCALIZING_FAIL = "Cannot locate the file entry in the compression!";
47+
48+
private StreamObjectValidator() {
49+
}
50+
51+
public static void validateAndThrow(final StreamProvider object, final String message, final Throwable cause) {
52+
if (object != null) {
53+
return;
54+
}
55+
throw new FilesystemResourceInitializationException(message, cause);
56+
}
57+
58+
public static void validateAndThrow(final Object object, final String message) {
59+
if (object != null) {
60+
return;
61+
}
62+
throw new FilesystemResourceInitializationException(message);
63+
}
64+
}

0 commit comments

Comments
 (0)