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