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