Coverage Report - org.owasp.dependencycheck.taskdefs.Purge
 
Classes in this File Line Coverage Branch Coverage Complexity
Purge
47%
18/38
25%
2/8
2.4
 
 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  4
         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  4
         StaticLoggerBinder.getSingleton().setTask(this);
 49  4
     }
 50  
 
 51  
     /**
 52  
      * The location of the data directory that contains
 53  
      */
 54  4
     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  0
         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  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  
      * 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  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  
 }