- shutdown() ExecutorService after task execution
- javadoc
- improve unit test coverage
This commit is contained in:
Stefan Neuhaus
2016-10-09 13:42:10 +02:00
parent b2149ff4b9
commit 9b43bf004a
8 changed files with 252 additions and 33 deletions

View File

@@ -11,6 +11,9 @@ import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.concurrent.Callable;
/**
* The task of analyzing a single {@link Dependency} by a specific {@link Analyzer}.
*/
class AnalysisTask implements Callable<Void> {
private static final Logger LOGGER = LoggerFactory.getLogger(AnalysisTask.class);
@@ -28,7 +31,7 @@ class AnalysisTask implements Callable<Void> {
}
@Override
public Void call() throws Exception {
public Void call() {
Settings.initialize();
if (shouldAnalyze()) {
@@ -50,10 +53,10 @@ class AnalysisTask implements Callable<Void> {
return null;
}
private boolean shouldAnalyze() {
boolean shouldAnalyze() {
if (analyzer instanceof FileTypeAnalyzer) {
final FileTypeAnalyzer fAnalyzer = (FileTypeAnalyzer) analyzer;
return fAnalyzer.accept(dependency.getActualFile());
final FileTypeAnalyzer fileTypeAnalyzer = (FileTypeAnalyzer) analyzer;
return fileTypeAnalyzer.accept(dependency.getActualFile());
}
return true;

View File

@@ -47,6 +47,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -396,20 +397,20 @@ public class Engine implements FileFilter {
for (AnalysisPhase phase : AnalysisPhase.values()) {
final List<Analyzer> analyzerList = analyzers.get(phase);
for (final Analyzer a : analyzerList) {
for (final Analyzer analyzer : analyzerList) {
final long analyzerStart = System.currentTimeMillis();
try {
initializeAnalyzer(a);
initializeAnalyzer(analyzer);
} catch (InitializationException ex) {
exceptions.add(ex);
continue;
}
executeAnalysisTasks(exceptions, a);
executeAnalysisTasks(analyzer, exceptions);
final long analyzerDurationMillis = System.currentTimeMillis() - analyzerStart;
final long analyzerDurationSeconds = TimeUnit.MILLISECONDS.toSeconds(analyzerDurationMillis);
LOGGER.info("Finished {}. Took {} secs.", a.getName(), analyzerDurationSeconds);
LOGGER.info("Finished {}. Took {} secs.", analyzer.getName(), analyzerDurationSeconds);
}
}
for (AnalysisPhase phase : AnalysisPhase.values()) {
@@ -427,7 +428,7 @@ public class Engine implements FileFilter {
}
}
private void executeAnalysisTasks(List<Throwable> exceptions, Analyzer analyzer) throws ExceptionCollection {
void executeAnalysisTasks(Analyzer analyzer, List<Throwable> exceptions) throws ExceptionCollection {
LOGGER.debug("Starting {}", analyzer.getName());
final List<AnalysisTask> analysisTasks = getAnalysisTasks(analyzer, exceptions);
final ExecutorService executorService = getExecutorService(analyzer);
@@ -441,14 +442,18 @@ public class Engine implements FileFilter {
result.get();
} catch (ExecutionException e) {
throwFatalExceptionCollection("Analysis task failed with a fatal exception.", e, exceptions);
} catch (CancellationException e) {
throwFatalExceptionCollection("Analysis task timed out.", e, exceptions);
}
}
} catch (InterruptedException e) {
throwFatalExceptionCollection("Analysis has been interrupted.", e, exceptions);
} finally {
executorService.shutdown();
}
}
private List<AnalysisTask> getAnalysisTasks(Analyzer analyzer, List<Throwable> exceptions) {
List<AnalysisTask> getAnalysisTasks(Analyzer analyzer, List<Throwable> exceptions) {
final List<AnalysisTask> result = new ArrayList<AnalysisTask>();
synchronized (dependencies) {
for (final Dependency dependency : dependencies) {
@@ -459,7 +464,7 @@ public class Engine implements FileFilter {
return result;
}
private ExecutorService getExecutorService(Analyzer analyzer) {
ExecutorService getExecutorService(Analyzer analyzer) {
if (analyzer.supportsParallelProcessing()) {
// just a fair trade-off that should be reasonable for all analyzer types
int maximumNumberOfThreads = 4 * Runtime.getRuntime().availableProcessors();

View File

@@ -58,10 +58,11 @@ public class DependencyBundlingAnalyzer extends AbstractAnalyzer {
* A pattern for obtaining the first part of a filename.
*/
private static final Pattern STARTING_TEXT_PATTERN = Pattern.compile("^[a-zA-Z0-9]*");
/**
* a flag indicating if this analyzer has run. This analyzer only runs once.
*/
private boolean analyzed = false;
boolean analyzed = false;
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="All standard implementation details of Analyzer">
/**
@@ -95,7 +96,7 @@ public class DependencyBundlingAnalyzer extends AbstractAnalyzer {
//</editor-fold>
/**
* Does not support parallel processing as it both modifies and iterates over the engine's list of dependencies.
* Does not support parallel processing as it only runs once and then operates on <em>all</em> dependencies.
*
* @see #analyze(Dependency, Engine)
*/