1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.taskdefs;
19
20 import io.github.jeremylong.jcs3.slf4j.Slf4jAdapter;
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.Task;
28 import org.owasp.dependencycheck.Engine;
29 import org.owasp.dependencycheck.utils.Downloader;
30 import org.owasp.dependencycheck.utils.InvalidSettingException;
31 import org.owasp.dependencycheck.utils.Settings;
32 import org.slf4j.impl.StaticLoggerBinder;
33
34
35
36
37
38
39 public class Purge extends Task {
40
41
42
43
44 private static final String PROPERTIES_FILE = "task.properties";
45
46
47
48 private Settings settings;
49
50
51
52
53 private String dataDirectory = null;
54
55
56
57
58 private boolean failOnError = true;
59
60
61
62
63 private String hostedSuppressionsUrl = null;
64
65
66
67
68 public Purge() {
69 super();
70
71
72
73 StaticLoggerBinder.getSingleton().setTask(this);
74 }
75
76 public Settings getSettings() {
77 return settings;
78 }
79
80
81
82
83
84
85 public String getDataDirectory() {
86 return dataDirectory;
87 }
88
89
90
91
92
93
94 public void setDataDirectory(String dataDirectory) {
95 this.dataDirectory = dataDirectory;
96 }
97
98
99
100
101
102
103 public boolean isFailOnError() {
104 return failOnError;
105 }
106
107
108
109
110
111
112 public void setFailOnError(boolean failOnError) {
113 this.failOnError = failOnError;
114 }
115
116
117
118
119
120
121 public String getHostedSuppressionsUrl() {
122 return hostedSuppressionsUrl;
123 }
124
125
126
127
128
129
130 public void setHostedSuppressionsUrl(final String hostedSuppressionsUrl) {
131 this.hostedSuppressionsUrl = hostedSuppressionsUrl;
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146 @Override
147 public final void execute() throws BuildException {
148 muteNoisyLoggers();
149 final ClassLoader current = Thread.currentThread().getContextClassLoader();
150 try {
151 Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
152
153 executeWithContextClassloader();
154 } finally {
155 Thread.currentThread().setContextClassLoader(current);
156 }
157 }
158
159
160
161
162 private void muteNoisyLoggers() {
163 System.setProperty("jcs.logSystem", "slf4j");
164 Slf4jAdapter.muteLogging(true);
165
166 final String[] noisyLoggers = {
167 "org.apache.hc"
168 };
169 for (String loggerName : noisyLoggers) {
170 System.setProperty("org.slf4j.simpleLogger.log." + loggerName, "error");
171 }
172 }
173
174
175
176
177
178
179
180
181 @SuppressWarnings("squid:RedundantThrowsDeclarationCheck")
182 protected void executeWithContextClassloader() throws BuildException {
183 populateSettings();
184 try {
185 Downloader.getInstance().configure(settings);
186 } catch (InvalidSettingException e) {
187 throw new BuildException(e);
188 }
189 try (Engine engine = new Engine(Engine.Mode.EVIDENCE_PROCESSING, getSettings())) {
190 engine.purge();
191 } finally {
192 settings.cleanup(true);
193 }
194 }
195
196
197
198
199
200
201
202
203
204 @SuppressWarnings("squid:RedundantThrowsDeclarationCheck")
205 protected void populateSettings() throws BuildException {
206 settings = new Settings();
207 try (InputStream taskProperties = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE)) {
208 settings.mergeProperties(taskProperties);
209 } catch (IOException ex) {
210 final String msg = "Unable to load the dependency-check ant task.properties file.";
211 if (this.failOnError) {
212 throw new BuildException(msg, ex);
213 }
214 log(msg, ex, Project.MSG_WARN);
215 }
216 settings.setStringIfNotEmpty(Settings.KEYS.HOSTED_SUPPRESSIONS_URL, hostedSuppressionsUrl);
217 if (dataDirectory != null) {
218 settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDirectory);
219 } else {
220 final File jarPath = new File(Purge.class.getProtectionDomain().getCodeSource().getLocation().getPath());
221 final File base = jarPath.getParentFile();
222 final String sub = settings.getString(Settings.KEYS.DATA_DIRECTORY);
223 final File dataDir = new File(base, sub);
224 settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath());
225 }
226 }
227 }