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 java.io.File;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.Task;
26 import org.owasp.dependencycheck.utils.Settings;
27 import org.slf4j.impl.StaticLoggerBinder;
28
29
30
31
32
33
34 public class Purge extends Task {
35
36
37
38
39 private static final String PROPERTIES_FILE = "task.properties";
40
41
42
43
44 public Purge() {
45 super();
46
47
48 StaticLoggerBinder.getSingleton().setTask(this);
49 }
50
51
52
53
54 private String dataDirectory = null;
55
56
57
58
59
60
61 public String getDataDirectory() {
62 return dataDirectory;
63 }
64
65
66
67
68
69
70 public void setDataDirectory(String dataDirectory) {
71 this.dataDirectory = dataDirectory;
72 }
73
74
75
76
77
78 private boolean failOnError = true;
79
80
81
82
83
84
85 public boolean isFailOnError() {
86 return failOnError;
87 }
88
89
90
91
92
93
94 public void setFailOnError(boolean failOnError) {
95 this.failOnError = failOnError;
96 }
97
98 @Override
99 public void execute() throws BuildException {
100 populateSettings();
101 File db;
102 try {
103 db = new File(Settings.getDataDirectory(), "dc.h2.db");
104 if (db.exists()) {
105 if (db.delete()) {
106 log("Database file purged; local copy of the NVD has been removed", Project.MSG_INFO);
107 } else {
108 final String msg = String.format("Unable to delete '%s'; please delete the file manually", db.getAbsolutePath());
109 if (this.failOnError) {
110 throw new BuildException(msg);
111 }
112 log(msg, Project.MSG_ERR);
113 }
114 } else {
115 final String msg = String.format("Unable to purge database; the database file does not exists: %s", db.getAbsolutePath());
116 if (this.failOnError) {
117 throw new BuildException(msg);
118 }
119 log(msg, Project.MSG_ERR);
120 }
121 } catch (IOException ex) {
122 final String msg = "Unable to delete the database";
123 if (this.failOnError) {
124 throw new BuildException(msg);
125 }
126 log(msg, Project.MSG_ERR);
127 } finally {
128 Settings.cleanup(true);
129 }
130 }
131
132
133
134
135
136
137
138
139 protected void populateSettings() throws BuildException {
140 Settings.initialize();
141 InputStream taskProperties = null;
142 try {
143 taskProperties = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
144 Settings.mergeProperties(taskProperties);
145 } catch (IOException ex) {
146 final String msg = "Unable to load the dependency-check ant task.properties file.";
147 if (this.failOnError) {
148 throw new BuildException(msg, ex);
149 }
150 log(msg, ex, Project.MSG_WARN);
151 } finally {
152 if (taskProperties != null) {
153 try {
154 taskProperties.close();
155 } catch (IOException ex) {
156 log("", ex, Project.MSG_DEBUG);
157 }
158 }
159 }
160 if (dataDirectory != null) {
161 Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDirectory);
162 } else {
163 final File jarPath = new File(Purge.class.getProtectionDomain().getCodeSource().getLocation().getPath());
164 final File base = jarPath.getParentFile();
165 final String sub = Settings.getString(Settings.KEYS.DATA_DIRECTORY);
166 final File dataDir = new File(base, sub);
167 Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath());
168 }
169 }
170 }