From a1691837833cc58ad940cd69b69a1f7a3cd11b1c Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Sat, 16 Nov 2013 09:16:35 -0500 Subject: [PATCH] Updated error reporting if data does not exist Former-commit-id: 299c9815cc5c65d7d16c267a185388367529ee90 --- .../org/owasp/dependencycheck/Engine.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) 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 0c254a0ec..d64a7a14f 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 @@ -20,6 +20,7 @@ package org.owasp.dependencycheck; import java.util.EnumMap; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; @@ -31,9 +32,12 @@ import org.owasp.dependencycheck.analyzer.AnalysisException; import org.owasp.dependencycheck.analyzer.AnalysisPhase; import org.owasp.dependencycheck.analyzer.Analyzer; import org.owasp.dependencycheck.analyzer.AnalyzerService; +import org.owasp.dependencycheck.analyzer.CPEAnalyzer; import org.owasp.dependencycheck.data.CachedWebDataSource; +import org.owasp.dependencycheck.data.NoDataException; import org.owasp.dependencycheck.data.UpdateException; import org.owasp.dependencycheck.data.UpdateService; +import org.owasp.dependencycheck.data.cpe.CpeIndexReader; import org.owasp.dependencycheck.dependency.Dependency; import org.owasp.dependencycheck.utils.FileUtils; import org.owasp.dependencycheck.utils.InvalidSettingException; @@ -277,6 +281,17 @@ public class Engine { * Runs the analyzers against all of the dependencies. */ public void analyzeDependencies() { + + //need to ensure that data exists + try { + ensureDataExists(); + } catch (NoDataException ex) { + String msg = String.format("\n\n%s\n\nUnable to continue dependency-check analysis.", ex.getMessage()); + Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, msg); + Logger.getLogger(Engine.class.getName()).log(Level.FINE, null, ex); + return; + } + //phase one initialize for (AnalysisPhase phase : AnalysisPhase.values()) { final List analyzerList = analyzers.get(phase); @@ -396,4 +411,37 @@ public class Engine { } return false; } + + /** + * Checks the CPE Index to ensure documents exists. If none exist a + * NoDataException is thrown. + * + * @throws NoDataException thrown if no data exists in the CPE Index + */ + private void ensureDataExists() throws NoDataException { + CpeIndexReader cpe = null; + boolean noDataExists = false; + try { + cpe = new CpeIndexReader(); + cpe.open(); + if (cpe.numDocs() <= 0) { + noDataExists = true; + } + } catch (IOException ex) { + noDataExists = true; + } catch (NullPointerException ex) { + noDataExists = true; + } finally { + if (cpe != null) { + cpe.close(); + } + } + if (noDataExists) { + throw new NoDataException("No data exists in the data store. Please check that you are able to connect " + + "to the Internet and re-run dependency-check. If the problem persists determine whether you need " + + "to set a proxy url and port.\\n\\nIf you are unable to solve this problem please contact the mailing " + + "list for help: dependency-check@googlegroups.com"); + + } + } }