From fdd7f30e9af3c150cae08ad2aa86e6b41c198df0 Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Sun, 16 Mar 2014 07:50:41 -0400 Subject: [PATCH] updated initialization of the analyzers to use less looping Former-commit-id: eab3ca87bdf55a46a530e20a84b0948a3c93b9c6 --- .../org/owasp/dependencycheck/Engine.java | 75 ++++++++++--------- 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/Engine.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/Engine.java index f84a7ed84..9add540b9 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/Engine.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/Engine.java @@ -291,32 +291,13 @@ public class Engine { Logger.getLogger(Engine.class.getName()).log(Level.FINE, logHeader); Logger.getLogger(Engine.class.getName()).log(Level.INFO, "Analysis Starting"); - //phase one initialize - for (AnalysisPhase phase : AnalysisPhase.values()) { - final List analyzerList = analyzers.get(phase); - for (Analyzer a : analyzerList) { - try { - final String msg = String.format("Initializing %s", a.getName()); - Logger.getLogger(Engine.class.getName()).log(Level.FINE, msg); - a.initialize(); - } catch (Throwable ex) { - final String msg = String.format("Exception occurred initializing %s.", a.getName()); - Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, msg); - Logger.getLogger(Engine.class.getName()).log(Level.FINE, null, ex); - try { - a.close(); - } catch (Throwable ex1) { - Logger.getLogger(Engine.class.getName()).log(Level.FINEST, null, ex1); - } - } - } - } - // analysis phases for (AnalysisPhase phase : AnalysisPhase.values()) { final List analyzerList = analyzers.get(phase); for (Analyzer a : analyzerList) { + initializeAnalyzer(a); + /* need to create a copy of the collection because some of the * analyzers may modify it. This prevents ConcurrentModificationExceptions. * This is okay for adds/deletes because it happens per analyzer. @@ -343,20 +324,7 @@ public class Engine { } } } - } - } - - //close/cleanup - for (AnalysisPhase phase : AnalysisPhase.values()) { - final List analyzerList = analyzers.get(phase); - for (Analyzer a : analyzerList) { - final String msg = String.format("Closing Analyzer '%s'", a.getName()); - Logger.getLogger(Engine.class.getName()).log(Level.FINE, msg); - try { - a.close(); - } catch (Throwable ex) { - Logger.getLogger(Engine.class.getName()).log(Level.FINEST, null, ex); - } + initializeAnalyzer(a); } } @@ -368,6 +336,43 @@ public class Engine { Logger.getLogger(Engine.class.getName()).log(Level.INFO, "Analysis Complete"); } + /** + * Initializes the given analyzer. + * + * @param analyzer the analyzer to initialize + */ + private void initializeAnalyzer(Analyzer analyzer) { + try { + final String msg = String.format("Initializing %s", analyzer.getName()); + Logger.getLogger(Engine.class.getName()).log(Level.FINE, msg); + analyzer.initialize(); + } catch (Throwable ex) { + final String msg = String.format("Exception occurred initializing %s.", analyzer.getName()); + Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, msg); + Logger.getLogger(Engine.class.getName()).log(Level.FINE, null, ex); + try { + analyzer.close(); + } catch (Throwable ex1) { + Logger.getLogger(Engine.class.getName()).log(Level.FINEST, null, ex1); + } + } + } + + /** + * Closes the given analyzer. + * + * @param analyzer the analyzer to close + */ + private void closeAnalyzer(Analyzer analyzer) { + final String msg = String.format("Closing Analyzer '%s'", analyzer.getName()); + Logger.getLogger(Engine.class.getName()).log(Level.FINE, msg); + try { + analyzer.close(); + } catch (Throwable ex) { + Logger.getLogger(Engine.class.getName()).log(Level.FINEST, null, ex); + } + } + /** * Cycles through the cached web data sources and calls update on all of them. */