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      /**
75       * Indicates if dependency-check should fail the build if an exception
76       * occurs.
77       */
78      private boolean failOnError = true;
79  
80      /**
81       * Get the value of failOnError.
82       *
83       * @return the value of failOnError
84       */
85      public boolean isFailOnError() {
86          return failOnError;
87      }
88  
89      /**
90       * Set the value of failOnError.
91       *
92       * @param failOnError new value of failOnError
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      * Takes the properties supplied and updates the dependency-check settings.
134      * Additionally, this sets the system properties required to change the
135      * proxy server, port, and connection timeout.
136      *
137      * @throws BuildException thrown if the properties file cannot be read.
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 }