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