Coverage Report - org.owasp.dependencycheck.taskdefs.Purge
 
Classes in this File Line Coverage Branch Coverage Complexity
Purge
34%
19/55
12%
2/16
3.143
 
 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  
     /**
 75  
      * Indicates if dependency-check should fail the build if an exception
 76  
      * occurs.
 77  
      */
 78  4
     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  0
         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  0
         this.failOnError = failOnError;
 96  0
     }
 97  
 
 98  
     @Override
 99  
     public void execute() throws BuildException {
 100  0
         populateSettings();
 101  
         File db;
 102  
         try {
 103  0
             db = new File(Settings.getDataDirectory(), "dc.h2.db");
 104  0
             if (db.exists()) {
 105  0
                 if (db.delete()) {
 106  0
                     log("Database file purged; local copy of the NVD has been removed", Project.MSG_INFO);
 107  
                 } else {
 108  0
                     final String msg = String.format("Unable to delete '%s'; please delete the file manually", db.getAbsolutePath());
 109  0
                     if (this.failOnError) {
 110  0
                         throw new BuildException(msg);
 111  
                     }
 112  0
                     log(msg, Project.MSG_ERR);
 113  0
                 }
 114  
             } else {
 115  0
                 final String msg = String.format("Unable to purge database; the database file does not exists: %s", db.getAbsolutePath());
 116  0
                 if (this.failOnError) {
 117  0
                     throw new BuildException(msg);
 118  
                 }
 119  0
                 log(msg, Project.MSG_ERR);
 120  
             }
 121  0
         } catch (IOException ex) {
 122  0
             final String msg = "Unable to delete the database";
 123  0
             if (this.failOnError) {
 124  0
                 throw new BuildException(msg);
 125  
             }
 126  0
             log(msg, Project.MSG_ERR);
 127  
         } finally {
 128  0
             Settings.cleanup(true);
 129  0
         }
 130  0
     }
 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  3
         Settings.initialize();
 141  3
         InputStream taskProperties = null;
 142  
         try {
 143  3
             taskProperties = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
 144  3
             Settings.mergeProperties(taskProperties);
 145  0
         } catch (IOException ex) {
 146  0
             final String msg = "Unable to load the dependency-check ant task.properties file.";
 147  0
             if (this.failOnError) {
 148  0
                 throw new BuildException(msg, ex);
 149  
             }
 150  0
             log(msg, ex, Project.MSG_WARN);
 151  
         } finally {
 152  3
             if (taskProperties != null) {
 153  
                 try {
 154  3
                     taskProperties.close();
 155  0
                 } catch (IOException ex) {
 156  0
                     log("", ex, Project.MSG_DEBUG);
 157  3
                 }
 158  
             }
 159  
         }
 160  3
         if (dataDirectory != null) {
 161  0
             Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDirectory);
 162  
         } else {
 163  3
             final File jarPath = new File(Purge.class.getProtectionDomain().getCodeSource().getLocation().getPath());
 164  3
             final File base = jarPath.getParentFile();
 165  3
             final String sub = Settings.getString(Settings.KEYS.DATA_DIRECTORY);
 166  3
             final File dataDir = new File(base, sub);
 167  3
             Settings.setString(Settings.KEYS.DATA_DIRECTORY, dataDir.getAbsolutePath());
 168  
         }
 169  3
     }
 170  
 }