| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.maven; |
| 19 | |
|
| 20 | |
import java.util.List; |
| 21 | |
import java.util.logging.Logger; |
| 22 | |
import org.apache.maven.project.MavenProject; |
| 23 | |
import org.owasp.dependencycheck.analyzer.Analyzer; |
| 24 | |
import org.owasp.dependencycheck.analyzer.CPEAnalyzer; |
| 25 | |
import org.owasp.dependencycheck.analyzer.FileTypeAnalyzer; |
| 26 | |
import org.owasp.dependencycheck.data.nvdcve.DatabaseException; |
| 27 | |
import org.owasp.dependencycheck.utils.Settings; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
public class Engine extends org.owasp.dependencycheck.Engine { |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 1 | private static final transient Logger LOGGER = Logger.getLogger(Engine.class.getName()); |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
private static final String CPE_ANALYZER_KEY = "dependency-check-CPEAnalyzer"; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
private MavenProject currentProject; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
private List<MavenProject> reactorProjects; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public static final String UPDATE_EXECUTED_FLAG = "dependency-check-update-executed"; |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | 1 | public Engine(MavenProject project, List<MavenProject> reactorProjects) throws DatabaseException { |
| 69 | 1 | this.currentProject = project; |
| 70 | 1 | this.reactorProjects = reactorProjects; |
| 71 | 1 | initializeEngine(); |
| 72 | 1 | } |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
@Override |
| 78 | |
public void analyzeDependencies() { |
| 79 | 0 | final MavenProject root = getExecutionRoot(); |
| 80 | 0 | if (root != null) { |
| 81 | 0 | LOGGER.fine(String.format("Checking root project, %s, if updates have already been completed", root.getArtifactId())); |
| 82 | |
} else { |
| 83 | 0 | LOGGER.fine("Checking root project, null, if updates have already been completed"); |
| 84 | |
} |
| 85 | 0 | if (root != null && root.getContextValue(UPDATE_EXECUTED_FLAG) != null) { |
| 86 | 0 | System.setProperty(Settings.KEYS.AUTO_UPDATE, Boolean.FALSE.toString()); |
| 87 | |
} |
| 88 | 0 | super.analyzeDependencies(); |
| 89 | 0 | if (root != null) { |
| 90 | 0 | root.setContextValue(UPDATE_EXECUTED_FLAG, Boolean.TRUE); |
| 91 | |
} |
| 92 | 0 | } |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | 0 | private Engine() throws DatabaseException { |
| 101 | 0 | } |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
@Override |
| 111 | |
protected Analyzer initializeAnalyzer(Analyzer analyzer) { |
| 112 | 0 | if ((analyzer instanceof CPEAnalyzer)) { |
| 113 | 0 | CPEAnalyzer cpe = getPreviouslyLoadedCPEAnalyzer(); |
| 114 | 0 | if (cpe != null) { |
| 115 | 0 | return cpe; |
| 116 | |
} |
| 117 | 0 | cpe = (CPEAnalyzer) super.initializeAnalyzer(analyzer); |
| 118 | 0 | storeCPEAnalyzer(cpe); |
| 119 | |
} |
| 120 | 0 | return super.initializeAnalyzer(analyzer); |
| 121 | |
} |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
@Override |
| 128 | |
public void cleanup() { |
| 129 | 1 | super.cleanup(); |
| 130 | 1 | if (currentProject == null || reactorProjects == null) { |
| 131 | 1 | return; |
| 132 | |
} |
| 133 | 0 | if (this.currentProject == reactorProjects.get(reactorProjects.size() - 1)) { |
| 134 | 0 | final CPEAnalyzer cpe = getPreviouslyLoadedCPEAnalyzer(); |
| 135 | 0 | if (cpe != null) { |
| 136 | 0 | cpe.close(); |
| 137 | |
} |
| 138 | |
} |
| 139 | 0 | } |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
@Override |
| 147 | |
protected void closeAnalyzer(Analyzer analyzer) { |
| 148 | 0 | if ((analyzer instanceof CPEAnalyzer)) { |
| 149 | 0 | if (getPreviouslyLoadedCPEAnalyzer() == null) { |
| 150 | 0 | super.closeAnalyzer(analyzer); |
| 151 | |
} |
| 152 | |
} else { |
| 153 | 0 | super.closeAnalyzer(analyzer); |
| 154 | |
} |
| 155 | 0 | } |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
private CPEAnalyzer getPreviouslyLoadedCPEAnalyzer() { |
| 163 | 0 | CPEAnalyzer cpe = null; |
| 164 | 0 | final MavenProject project = getExecutionRoot(); |
| 165 | 0 | if (project != null) { |
| 166 | 0 | final Object obj = project.getContextValue(CPE_ANALYZER_KEY); |
| 167 | 0 | if (obj != null && obj instanceof CPEAnalyzer) { |
| 168 | 0 | cpe = (CPEAnalyzer) project.getContextValue(CPE_ANALYZER_KEY); |
| 169 | |
} |
| 170 | |
} |
| 171 | 0 | return cpe; |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
private void storeCPEAnalyzer(CPEAnalyzer cpe) { |
| 180 | 0 | final MavenProject p = getExecutionRoot(); |
| 181 | 0 | if (p != null) { |
| 182 | 0 | p.setContextValue(CPE_ANALYZER_KEY, cpe); |
| 183 | |
} |
| 184 | 0 | } |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
private MavenProject getExecutionRoot() { |
| 192 | 0 | if (reactorProjects == null) { |
| 193 | 0 | return null; |
| 194 | |
} |
| 195 | 0 | for (MavenProject p : reactorProjects) { |
| 196 | 0 | if (p.isExecutionRoot()) { |
| 197 | 0 | return p; |
| 198 | |
} |
| 199 | 0 | } |
| 200 | |
|
| 201 | 0 | if (this.currentProject == null) { |
| 202 | 0 | return null; |
| 203 | |
} |
| 204 | 0 | MavenProject p = this.currentProject; |
| 205 | 0 | while (p.getParent() != null) { |
| 206 | 0 | p = p.getParent(); |
| 207 | |
} |
| 208 | 0 | return p; |
| 209 | |
} |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
public void resetFileTypeAnalyzers() { |
| 218 | 0 | for (FileTypeAnalyzer a : getFileTypeAnalyzers()) { |
| 219 | 0 | a.reset(); |
| 220 | 0 | } |
| 221 | 0 | } |
| 222 | |
} |