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 public Purge() {
64 super();
65
66
67
68 StaticLoggerBinder.getSingleton().setTask(this);
69 }
70
71 public Settings getSettings() {
72 return settings;
73 }
74
75
76
77
78
79
80 public void setDataDirectory(String dataDirectory) {
81 this.dataDirectory = dataDirectory;
82 }
83
84
85
86
87
88
89 public boolean isFailOnError() {
90 return failOnError;
91 }
92
93
94
95
96
97
98 public void setFailOnError(boolean failOnError) {
99 this.failOnError = failOnError;
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114 @Override
115 public final void execute() throws BuildException {
116 muteNoisyLoggers();
117 final ClassLoader current = Thread.currentThread().getContextClassLoader();
118 try {
119 Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
120
121 executeWithContextClassloader();
122 } finally {
123 Thread.currentThread().setContextClassLoader(current);
124 }
125 }
126
127
128
129
130 private void muteNoisyLoggers() {
131 System.setProperty("jcs.logSystem", "slf4j");
132 Slf4jAdapter.muteLogging(true);
133
134 final String[] noisyLoggers = {
135 "org.apache.hc"
136 };
137 for (String loggerName : noisyLoggers) {
138 System.setProperty("org.slf4j.simpleLogger.log." + loggerName, "error");
139 }
140 }
141
142
143
144
145
146
147
148
149 @SuppressWarnings("squid:RedundantThrowsDeclarationCheck")
150 protected void executeWithContextClassloader() throws BuildException {
151 populateSettings();
152 try {
153 Downloader.getInstance().configure(settings);
154 } catch (InvalidSettingException e) {
155 throw new BuildException(e);
156 }
157 try (Engine engine = new Engine(Engine.Mode.EVIDENCE_PROCESSING, getSettings())) {
158 engine.purge();
159 } finally {
160 settings.cleanup(true);
161 }
162 }
163
164
165
166
167
168
169
170
171
172 @SuppressWarnings("squid:RedundantThrowsDeclarationCheck")
173 protected void populateSettings() throws BuildException {
174 settings = new Settings();
175 try (InputStream taskProperties = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE)) {
176 settings.mergeProperties(taskProperties);
177 } catch (IOException ex) {
178 final String msg = "Unable to load the dependency-check ant task.properties file.";
179 if (this.failOnError) {
180 throw new BuildException(msg, ex);
181 }
182 log(msg, ex, Project.MSG_WARN);
183 }
184 if (dataDirectory != null) {
185 settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDirectory);
186 } else {
187 final File jarPath = new File(Purge.class.getProtectionDomain().getCodeSource().getLocation().getPath());
188 final File base = jarPath.getParentFile();
189 final String sub = settings.getString(Settings.KEYS.DATA_DIRECTORY);
190 final File dataDir = new File(base, sub);
191 settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath());
192 }
193 }
194 }