Coverage Report - org.owasp.dependencycheck.utils.Settings
 
Classes in this File Line Coverage Branch Coverage Complexity
Settings
60%
49/81
42%
6/14
2.294
Settings$KEYS
0%
0/2
N/A
2.294
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Dependency-check-core is free software: you can redistribute it and/or modify it
 5  
  * under the terms of the GNU General Public License as published by the Free
 6  
  * Software Foundation, either version 3 of the License, or (at your option) any
 7  
  * later version.
 8  
  *
 9  
  * Dependency-check-core is distributed in the hope that it will be useful, but
 10  
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 12  
  * details.
 13  
  *
 14  
  * You should have received a copy of the GNU General Public License along with
 15  
  * dependency-check-core. If not, see http://www.gnu.org/licenses/.
 16  
  *
 17  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 18  
  */
 19  
 package org.owasp.dependencycheck.utils;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.FileInputStream;
 23  
 import java.io.FileNotFoundException;
 24  
 import java.io.IOException;
 25  
 import java.io.InputStream;
 26  
 import java.io.UnsupportedEncodingException;
 27  
 import java.net.URLDecoder;
 28  
 import java.util.Properties;
 29  
 import java.util.logging.Level;
 30  
 import java.util.logging.Logger;
 31  
 
 32  
 /**
 33  
  * A simple settings container that wraps the dependencycheck.properties file.
 34  
  *
 35  
  * @author Jeremy Long (jeremy.long@owasp.org)
 36  
  */
 37  
 public final class Settings {
 38  
 
 39  
     /**
 40  
      * The collection of keys used within the properties file.
 41  
      */
 42  
     public static final class KEYS {
 43  
 
 44  
         /**
 45  
          * private constructor because this is a "utility" class containing
 46  
          * constants
 47  
          */
 48  0
         private KEYS() {
 49  
             //do nothing
 50  0
         }
 51  
         /**
 52  
          * The properties key indicating whether or not the cached data sources
 53  
          * should be updated.
 54  
          */
 55  
         public static final String AUTO_UPDATE = "autoupdate";
 56  
         /**
 57  
          * The database driver class name. If this is not in the properties file
 58  
          * the embedded database is used.
 59  
          */
 60  
         public static final String DB_DRIVER = "database.drive";
 61  
         /**
 62  
          * The database connection string. If this is not in the properties file
 63  
          * the embedded database is used.
 64  
          */
 65  
         public static final String DB_CONNECTION_STRING = "database.connectionstring";
 66  
         /**
 67  
          * The base path to use for the data directory.
 68  
          */
 69  
         public static final String DATA_DIRECTORY = "data.directory";
 70  
         /**
 71  
          * The location of the batch update URL. This is a zip file that
 72  
          * contains the contents of the data directory.
 73  
          */
 74  
         public static final String BATCH_UPDATE_URL = "batch.update.url";
 75  
         /**
 76  
          * The properties key for the path where the CPE Lucene Index will be
 77  
          * stored.
 78  
          */
 79  
         public static final String CPE_DATA_DIRECTORY = "data.cpe";
 80  
         /**
 81  
          * The properties key for the path where the CVE H2 database will be
 82  
          * stored.
 83  
          */
 84  
         public static final String CVE_DATA_DIRECTORY = "data.cve";
 85  
         /**
 86  
          * The properties key for the URL to retrieve the "meta" data from about
 87  
          * the CVE entries.
 88  
          */
 89  
         public static final String CVE_META_URL = "cve.url.meta";
 90  
         /**
 91  
          * The properties key for the URL to retrieve the recently modified and
 92  
          * added CVE entries (last 8 days) using the 2.0 schema.
 93  
          */
 94  
         public static final String CVE_MODIFIED_20_URL = "cve.url-2.0.modified";
 95  
         /**
 96  
          * The properties key for the URL to retrieve the recently modified and
 97  
          * added CVE entries (last 8 days) using the 1.2 schema.
 98  
          */
 99  
         public static final String CVE_MODIFIED_12_URL = "cve.url-1.2.modified";
 100  
         /**
 101  
          * The properties key for the URL to retrieve the recently modified and
 102  
          * added CVE entries (last 8 days).
 103  
          */
 104  
         public static final String CVE_MODIFIED_VALID_FOR_DAYS = "cve.url.modified.validfordays";
 105  
         /**
 106  
          * The properties key for the telling us how many cvr.url.* URLs exists.
 107  
          * This is used in combination with CVE_BASE_URL to be able to retrieve
 108  
          * the URLs for all of the files that make up the NVD CVE listing.
 109  
          */
 110  
         public static final String CVE_START_YEAR = "cve.startyear";
 111  
         /**
 112  
          * The properties key for the CVE schema version 1.2.
 113  
          */
 114  
         public static final String CVE_SCHEMA_1_2 = "cve.url-1.2.base";
 115  
         /**
 116  
          * The properties key for the CVE schema version 2.0.
 117  
          */
 118  
         public static final String CVE_SCHEMA_2_0 = "cve.url-2.0.base";
 119  
         /**
 120  
          * The properties key for the proxy url.
 121  
          */
 122  
         public static final String PROXY_URL = "proxy.url";
 123  
         /**
 124  
          * The properties key for the proxy port - this must be an integer
 125  
          * value.
 126  
          */
 127  
         public static final String PROXY_PORT = "proxy.port";
 128  
         /**
 129  
          * The properties key for the connection timeout.
 130  
          */
 131  
         public static final String CONNECTION_TIMEOUT = "connection.timeout";
 132  
         /**
 133  
          * The location of the temporary directory.
 134  
          */
 135  
         public static final String TEMP_DIRECTORY = "temp.directory";
 136  
     }
 137  
     /**
 138  
      * The properties file location.
 139  
      */
 140  
     private static final String PROPERTIES_FILE = "dependencycheck.properties";
 141  
     /**
 142  
      * The singleton instance variable.
 143  
      */
 144  1
     private static final Settings INSTANCE = new Settings();
 145  
     /**
 146  
      * The properties.
 147  
      */
 148  1
     private Properties props = null;
 149  
 
 150  
     /**
 151  
      * Private constructor for the Settings class. This class loads the
 152  
      * properties files.
 153  
      */
 154  1
     private Settings() {
 155  1
         InputStream in = null;
 156  1
         props = new Properties();
 157  
         try {
 158  1
             in = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
 159  1
             props.load(in);
 160  0
         } catch (IOException ex) {
 161  0
             Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, "Unable to load default settings.");
 162  0
             Logger.getLogger(Settings.class.getName()).log(Level.FINE, null, ex);
 163  
         } finally {
 164  1
             if (in != null) {
 165  
                 try {
 166  1
                     in.close();
 167  0
                 } catch (IOException ex) {
 168  0
                     Logger.getLogger(Settings.class.getName()).log(Level.FINEST, null, ex);
 169  1
                 }
 170  
             }
 171  
         }
 172  1
     }
 173  
 
 174  
     /**
 175  
      * Sets a property value.
 176  
      *
 177  
      * @param key the key for the property
 178  
      * @param value the value for the property
 179  
      */
 180  
     public static void setString(String key, String value) {
 181  17
         INSTANCE.props.setProperty(key, value);
 182  17
     }
 183  
 
 184  
     /**
 185  
      * Sets a property value.
 186  
      *
 187  
      * @param key the key for the property
 188  
      * @param value the value for the property
 189  
      */
 190  
     public static void setBoolean(String key, boolean value) {
 191  2
         if (value) {
 192  0
             INSTANCE.props.setProperty(key, Boolean.TRUE.toString());
 193  
         } else {
 194  2
             INSTANCE.props.setProperty(key, Boolean.FALSE.toString());
 195  
         }
 196  2
     }
 197  
 
 198  
     /**
 199  
      * Merges a new properties file into the current properties. This method
 200  
      * allows for the loading of a user provided properties file.<br/><br/>
 201  
      * Note: even if using this method - system properties will be loaded before
 202  
      * properties loaded from files.
 203  
      *
 204  
      * @param filePath the path to the properties file to merge.
 205  
      * @throws FileNotFoundException is thrown when the filePath points to a
 206  
      * non-existent file
 207  
      * @throws IOException is thrown when there is an exception loading/merging
 208  
      * the properties
 209  
      */
 210  
     public static void mergeProperties(File filePath) throws FileNotFoundException, IOException {
 211  0
         final FileInputStream fis = new FileInputStream(filePath);
 212  0
         mergeProperties(fis);
 213  0
     }
 214  
 
 215  
     /**
 216  
      * Merges a new properties file into the current properties. This method
 217  
      * allows for the loading of a user provided properties file.<br/><br/>
 218  
      * Note: even if using this method - system properties will be loaded before
 219  
      * properties loaded from files.
 220  
      *
 221  
      * @param filePath the path to the properties file to merge.
 222  
      * @throws FileNotFoundException is thrown when the filePath points to a
 223  
      * non-existent file
 224  
      * @throws IOException is thrown when there is an exception loading/merging
 225  
      * the properties
 226  
      */
 227  
     public static void mergeProperties(String filePath) throws FileNotFoundException, IOException {
 228  1
         final FileInputStream fis = new FileInputStream(filePath);
 229  1
         mergeProperties(fis);
 230  1
     }
 231  
 
 232  
     /**
 233  
      * Merges a new properties file into the current properties. This method
 234  
      * allows for the loading of a user provided properties file.<br/><br/>
 235  
      * Note: even if using this method - system properties will be loaded before
 236  
      * properties loaded from files.
 237  
      *
 238  
      * @param stream an Input Stream pointing at a properties file to merge
 239  
      * @throws IOException is thrown when there is an exception loading/merging
 240  
      * the properties
 241  
      */
 242  
     public static void mergeProperties(InputStream stream) throws IOException {
 243  1
         INSTANCE.props.load(stream);
 244  1
     }
 245  
 
 246  
     /**
 247  
      * Returns a value from the properties file as a File object. If the value
 248  
      * was specified as a system property or passed in via the -Dprop=value
 249  
      * argument - this method will return the value from the system properties
 250  
      * before the values in the contained configuration file.
 251  
      *
 252  
      * This method will also replace a leading "[JAR]\" sequence with the path
 253  
      * to the folder containing the JAR file containing this class.
 254  
      *
 255  
      * @param key the key to lookup within the properties file
 256  
      * @return the property from the properties file converted to a File object
 257  
      */
 258  
     public static File getFile(String key) {
 259  60
         final String file = getString(key);
 260  60
         final String baseDir = getString(Settings.KEYS.DATA_DIRECTORY);
 261  60
         if (baseDir != null) {
 262  60
             if (baseDir.startsWith("[JAR]/")) {
 263  0
                 final File jarPath = getJarPath();
 264  0
                 final File newBase = new File(jarPath, baseDir.substring(6));
 265  0
                 if (Settings.KEYS.DATA_DIRECTORY.equals(key)) {
 266  0
                     return newBase;
 267  
                 }
 268  0
                 return new File(newBase, file);
 269  
             }
 270  60
             if (Settings.KEYS.DATA_DIRECTORY.equals(key)) {
 271  25
                 return new File(baseDir);
 272  
             }
 273  35
             return new File(baseDir, file);
 274  
         }
 275  0
         return new File(file);
 276  
     }
 277  
 
 278  
     /**
 279  
      * Attempts to retrieve the folder containing the Jar file containing the
 280  
      * Settings class.
 281  
      *
 282  
      * @return a File object
 283  
      */
 284  
     private static File getJarPath() {
 285  0
         final String jarPath = Settings.class.getProtectionDomain().getCodeSource().getLocation().getPath();
 286  0
         String decodedPath = ".";
 287  
         try {
 288  0
             decodedPath = URLDecoder.decode(jarPath, "UTF-8");
 289  0
         } catch (UnsupportedEncodingException ex) {
 290  0
             Logger.getLogger(Settings.class.getName()).log(Level.FINEST, null, ex);
 291  0
         }
 292  
 
 293  0
         final File path = new File(decodedPath);
 294  0
         if (path.getName().toLowerCase().endsWith(".jar")) {
 295  0
             return path.getParentFile();
 296  
         } else {
 297  0
             return new File(".");
 298  
         }
 299  
     }
 300  
 
 301  
     /**
 302  
      * Returns a value from the properties file. If the value was specified as a
 303  
      * system property or passed in via the -Dprop=value argument - this method
 304  
      * will return the value from the system properties before the values in the
 305  
      * contained configuration file.
 306  
      *
 307  
      * @param key the key to lookup within the properties file
 308  
      * @param defaultValue the default value for the requested property
 309  
      * @return the property from the properties file
 310  
      */
 311  
     public static String getString(String key, String defaultValue) {
 312  21
         final String str = System.getProperty(key, INSTANCE.props.getProperty(key, defaultValue));
 313  21
         return str;
 314  
     }
 315  
 
 316  
     /**
 317  
      * Returns the temporary directory.
 318  
      *
 319  
      * @return the temporary directory
 320  
      */
 321  
     public static File getTempDirectory() {
 322  6
         return new File(Settings.getString(Settings.KEYS.TEMP_DIRECTORY, System.getProperty("java.io.tmpdir")));
 323  
     }
 324  
 
 325  
     /**
 326  
      * Returns a value from the properties file. If the value was specified as a
 327  
      * system property or passed in via the -Dprop=value argument - this method
 328  
      * will return the value from the system properties before the values in the
 329  
      * contained configuration file.
 330  
      *
 331  
      * @param key the key to lookup within the properties file
 332  
      * @return the property from the properties file
 333  
      */
 334  
     public static String getString(String key) {
 335  143
         return System.getProperty(key, INSTANCE.props.getProperty(key));
 336  
     }
 337  
 
 338  
     /**
 339  
      * Removes a property from the local properties collection. This is mainly
 340  
      * used in test cases.
 341  
      *
 342  
      * @param key the property key to remove
 343  
      */
 344  
     public static void removeProperty(String key) {
 345  1
         INSTANCE.props.remove(key);
 346  1
     }
 347  
 
 348  
     /**
 349  
      * Returns an int value from the properties file. If the value was specified
 350  
      * as a system property or passed in via the -Dprop=value argument - this
 351  
      * method will return the value from the system properties before the values
 352  
      * in the contained configuration file.
 353  
      *
 354  
      * @param key the key to lookup within the properties file
 355  
      * @return the property from the properties file
 356  
      * @throws InvalidSettingException is thrown if there is an error retrieving
 357  
      * the setting
 358  
      */
 359  
     public static int getInt(String key) throws InvalidSettingException {
 360  
         int value;
 361  
         try {
 362  1
             value = Integer.parseInt(Settings.getString(key));
 363  0
         } catch (NumberFormatException ex) {
 364  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 365  1
         }
 366  1
         return value;
 367  
     }
 368  
 
 369  
     /**
 370  
      * Returns an int value from the properties file. If the value was specified
 371  
      * as a system property or passed in via the -Dprop=value argument - this
 372  
      * method will return the value from the system properties before the values
 373  
      * in the contained configuration file.
 374  
      *
 375  
      * @param key the key to lookup within the properties file
 376  
      * @param defaultValue the default value to return
 377  
      * @return the property from the properties file or the defaultValue if the
 378  
      * property does not exist or cannot be converted to an integer
 379  
      */
 380  
     public static int getInt(String key, int defaultValue) {
 381  
         int value;
 382  
         try {
 383  1
             value = Integer.parseInt(Settings.getString(key));
 384  1
         } catch (NumberFormatException ex) {
 385  1
             final String msg = String.format("Could not convert property '%s' to an int.", key);
 386  1
             Logger.getLogger(Settings.class.getName()).log(Level.FINEST, msg, ex);
 387  1
             value = defaultValue;
 388  0
         }
 389  1
         return value;
 390  
     }
 391  
 
 392  
     /**
 393  
      * Returns a long value from the properties file. If the value was specified
 394  
      * as a system property or passed in via the -Dprop=value argument - this
 395  
      * method will return the value from the system properties before the values
 396  
      * in the contained configuration file.
 397  
      *
 398  
      * @param key the key to lookup within the properties file
 399  
      * @return the property from the properties file
 400  
      * @throws InvalidSettingException is thrown if there is an error retrieving
 401  
      * the setting
 402  
      */
 403  
     public static long getLong(String key) throws InvalidSettingException {
 404  
         long value;
 405  
         try {
 406  1
             value = Long.parseLong(Settings.getString(key));
 407  0
         } catch (NumberFormatException ex) {
 408  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 409  1
         }
 410  1
         return value;
 411  
     }
 412  
 
 413  
     /**
 414  
      * Returns a boolean value from the properties file. If the value was
 415  
      * specified as a system property or passed in via the
 416  
      * <code>-Dprop=value</code> argument this method will return the value from
 417  
      * the system properties before the values in the contained configuration
 418  
      * file.
 419  
      *
 420  
      * @param key the key to lookup within the properties file
 421  
      * @return the property from the properties file
 422  
      * @throws InvalidSettingException is thrown if there is an error retrieving
 423  
      * the setting
 424  
      */
 425  
     public static boolean getBoolean(String key) throws InvalidSettingException {
 426  
         boolean value;
 427  
         try {
 428  4
             value = Boolean.parseBoolean(Settings.getString(key));
 429  0
         } catch (NumberFormatException ex) {
 430  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 431  4
         }
 432  4
         return value;
 433  
     }
 434  
 }