View Javadoc
1   /*
2    * This file is part of dependency-check-ant.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2015 Jeremy Long. All Rights Reserved.
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   * An Ant task definition to execute dependency-check during an Ant build.
31   *
32   * @author Jeremy Long
33   */
34  public class Purge extends Task {
35  
36      /**
37       * The properties file location.
38       */
39      private static final String PROPERTIES_FILE = "task.properties";
40  
41      /**
42       * Construct a new DependencyCheckTask.
43       */
44      public Purge() {
45          super();
46          // Call this before Dependency Check Core starts logging anything - this way, all SLF4J messages from
47          // core end up coming through this tasks logger
48          StaticLoggerBinder.getSingleton().setTask(this);
49      }
50  
51      /**
52       * The location of the data directory that contains
53       */
54      private String dataDirectory = null;
55  
56      /**
57       * Get the value of dataDirectory.
58       *
59       * @return the value of dataDirectory
60       */
61      public String getDataDirectory() {
62          return dataDirectory;
63      }
64  
65      /**
66       * Set the value of dataDirectory.
67       *
68       * @param dataDirectory new value of dataDirectory
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       * Takes the properties supplied and updates the dependency-check settings. Additionally, this sets the system properties
98       * required to change the proxy server, port, and connection timeout.
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 }