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 @Override
75 public void execute() throws BuildException {
76 populateSettings();
77 File db;
78 try {
79 db = new File(Settings.getDataDirectory(), "dc.h2.db");
80 if (db.exists()) {
81 if (db.delete()) {
82 log("Database file purged; local copy of the NVD has been removed", Project.MSG_INFO);
83 } else {
84 log(String.format("Unable to delete '%s'; please delete the file manually", db.getAbsolutePath()), Project.MSG_ERR);
85 }
86 } else {
87 log(String.format("Unable to purge database; the database file does not exists: %s", db.getAbsolutePath()), Project.MSG_ERR);
88 }
89 } catch (IOException ex) {
90 log("Unable to delete the database", Project.MSG_ERR);
91 } finally {
92 Settings.cleanup(true);
93 }
94 }
95
96
97
98
99
100 protected void populateSettings() {
101 Settings.initialize();
102 InputStream taskProperties = null;
103 try {
104 taskProperties = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
105 Settings.mergeProperties(taskProperties);
106 } catch (IOException ex) {
107 log("Unable to load the dependency-check ant task.properties file.", ex, Project.MSG_WARN);
108 } finally {
109 if (taskProperties != null) {
110 try {
111 taskProperties.close();
112 } catch (IOException ex) {
113 log("", ex, Project.MSG_DEBUG);
114 }
115 }
116 }
117 if (dataDirectory != null) {
118 Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDirectory);
119 } else {
120 final File jarPath = new File(Purge.class.getProtectionDomain().getCodeSource().getLocation().getPath());
121 final File base = jarPath.getParentFile();
122 final String sub = Settings.getString(Settings.KEYS.DATA_DIRECTORY);
123 final File dataDir = new File(base, sub);
124 Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath());
125 }
126 }
127 }