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