Skip to content

Commit d7b42ef

Browse files
andyphillips404glibas
authored andcommitted
Update FileUtil.java (#24)
* Update FileUtil.java Update to the FileUtil to make the java script resource loading work in an OSGI environment. Kept the original loader to ensure backwards compatibility. Essentially checks to make sure the InputStream is found. If not, it will try loading the resource using the proper method for an OSGI environment. If it still is unable to load the file, it will throw an exception to avoid a non descriptive null pointer exception. * Update FileUtil.java Forgot the import java.io.InputStream
1 parent 184d689 commit d7b42ef

File tree

1 file changed

+9
-1
lines changed
  • src/main/java/com/assertthat/selenium_shutterbug/utils/file

1 file changed

+9
-1
lines changed

src/main/java/com/assertthat/selenium_shutterbug/utils/file/FileUtil.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.awt.image.BufferedImage;
1414
import java.io.File;
1515
import java.io.IOException;
16+
import java.io.InputStream;
1617

1718
/**
1819
* Created by Glib_Briia on 17/06/2016.
@@ -21,7 +22,14 @@ public class FileUtil {
2122

2223
public static String getJsScript(String filePath) {
2324
try {
24-
return IOUtils.toString(Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath));
25+
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
26+
if (is == null) {
27+
// This is needed to load the files in an OSGI enviroment when enclosed in a bundle
28+
is = FileUtil.class.getClassLoader().getResourceAsStream(filePath);
29+
}
30+
// if the input stream is still null, this will avoid a non descriptive null pointer exception
31+
if (is == null) new UnableTakeSnapshotException("Unable to load JS script, unable to locate resource stream.");
32+
return IOUtils.toString(is);
2533
} catch (IOException e) {
2634
throw new UnableTakeSnapshotException("Unable to load JS script", e);
2735
}

0 commit comments

Comments
 (0)