Skip to content

Commit

Permalink
(Hotfix) Fix the reading of default pattern file when run from JAR
Browse files Browse the repository at this point in the history
  • Loading branch information
emaiannone committed May 15, 2023
1 parent ef7ba06 commit aa186fd
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/main/java/org/surface/surface/cli/CLIArgumentsParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.surface.surface.cli;

import org.apache.commons.cli.*;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.surface.surface.core.Utils;
Expand All @@ -14,7 +15,9 @@
import org.surface.surface.core.engine.metrics.api.MetricsManager;
import org.surface.surface.core.exporters.RunResultsExporter;

import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
Expand Down Expand Up @@ -72,16 +75,17 @@ public static RunningMode parse(String[] args) throws ParseException {
classifiedPatterns = new ClassifiedPatternsInterpreter().interpret(fileUsed);
} catch (IllegalArgumentException e) {
LOGGER.info("* {} Using the built-in set of patterns.", e.getMessage());
URL resource = CLIArgumentsParser.class.getClassLoader().getResource("defaultPatterns.txt");
if (resource == null) {
try (InputStream patternStream = CLIArgumentsParser.class.getClassLoader().getResourceAsStream("defaultPatterns.txt")) {
if (patternStream == null) {
throw new RuntimeException("The default pattern file cannot be found. Please, use a custom file to circumvent this issue.");
}
Path tempFilePath = Files.createTempFile(null, null);
FileUtils.copyInputStreamToFile(patternStream, tempFilePath.toFile());
classifiedPatterns = new ClassifiedPatternsInterpreter().interpret(tempFilePath.toString());
tempFilePath.toFile().delete();
} catch (IOException ex) {
throw new RuntimeException("The default pattern file cannot be found. Please, use a custom file to circumvent this issue.");
}
fileUsed = resource.getPath();
try {
classifiedPatterns = new ClassifiedPatternsInterpreter().interpret(fileUsed);
} catch (IllegalArgumentException e2) {
throw new RuntimeException("The default pattern file cannot be read. Please, use a custom file to circumvent this issue.");
}
}
LOGGER.info("* Going to detect classified patterns using file: {}", fileUsed);

Expand Down

0 comments on commit aa186fd

Please sign in to comment.