Coverage Report - org.owasp.dependencycheck.utils.Settings
 
Classes in this File Line Coverage Branch Coverage Complexity
Settings
61%
49/80
41%
5/12
2.111
Settings$KEYS
0%
0/2
N/A
2.111
 
 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_NAME = "data.driver_name";
 61  
         /**
 62  
          * The database driver class name. If this is not in the properties file
 63  
          * the embedded database is used.
 64  
          */
 65  
         public static final String DB_DRIVER_PATH = "data.driver_path";
 66  
         /**
 67  
          * The database connection string. If this is not in the properties file
 68  
          * the embedded database is used.
 69  
          */
 70  
         public static final String DB_CONNECTION_STRING = "data.connection_string";
 71  
         /**
 72  
          * The username to use when connecting to the database.
 73  
          */
 74  
         public static final String DB_USER = "data.user";
 75  
         /**
 76  
          * The password to authenticate to the database.
 77  
          */
 78  
         public static final String DB_PASSWORD = "data.password";
 79  
         /**
 80  
          * The base path to use for the data directory (for embedded db).
 81  
          */
 82  
         public static final String DATA_DIRECTORY = "data.directory";
 83  
         /**
 84  
          * The properties key for the URL to retrieve the "meta" data from about
 85  
          * the CVE entries.
 86  
          */
 87  
         public static final String CVE_META_URL = "cve.url.meta";
 88  
         /**
 89  
          * The properties key for the URL to retrieve the recently modified and
 90  
          * added CVE entries (last 8 days) using the 2.0 schema.
 91  
          */
 92  
         public static final String CVE_MODIFIED_20_URL = "cve.url-2.0.modified";
 93  
         /**
 94  
          * The properties key for the URL to retrieve the recently modified and
 95  
          * added CVE entries (last 8 days) using the 1.2 schema.
 96  
          */
 97  
         public static final String CVE_MODIFIED_12_URL = "cve.url-1.2.modified";
 98  
         /**
 99  
          * The properties key for the URL to retrieve the recently modified and
 100  
          * added CVE entries (last 8 days).
 101  
          */
 102  
         public static final String CVE_MODIFIED_VALID_FOR_DAYS = "cve.url.modified.validfordays";
 103  
         /**
 104  
          * The properties key for the telling us how many cvr.url.* URLs exists.
 105  
          * This is used in combination with CVE_BASE_URL to be able to retrieve
 106  
          * the URLs for all of the files that make up the NVD CVE listing.
 107  
          */
 108  
         public static final String CVE_START_YEAR = "cve.startyear";
 109  
         /**
 110  
          * The properties key for the CVE schema version 1.2.
 111  
          */
 112  
         public static final String CVE_SCHEMA_1_2 = "cve.url-1.2.base";
 113  
         /**
 114  
          * The properties key for the CVE schema version 2.0.
 115  
          */
 116  
         public static final String CVE_SCHEMA_2_0 = "cve.url-2.0.base";
 117  
         /**
 118  
          * The properties key for the proxy url.
 119  
          */
 120  
         public static final String PROXY_URL = "proxy.url";
 121  
         /**
 122  
          * The properties key for the proxy port - this must be an integer
 123  
          * value.
 124  
          */
 125  
         public static final String PROXY_PORT = "proxy.port";
 126  
         /**
 127  
          * The properties key for the proxy username.
 128  
          */
 129  
         public static final String PROXY_USERNAME = "proxy.username";
 130  
         /**
 131  
          * The properties key for the proxy password.
 132  
          */
 133  
         public static final String PROXY_PASSWORD = "proxy.password";
 134  
         /**
 135  
          * The properties key for the connection timeout.
 136  
          */
 137  
         public static final String CONNECTION_TIMEOUT = "connection.timeout";
 138  
         /**
 139  
          * The location of the temporary directory.
 140  
          */
 141  
         public static final String TEMP_DIRECTORY = "temp.directory";
 142  
         /**
 143  
          * The maximum number of threads to allocate when downloading files.
 144  
          */
 145  
         public static final String MAX_DOWNLOAD_THREAD_POOL_SIZE = "max.download.threads";
 146  
         /**
 147  
          * The key for a list of suppression files.
 148  
          */
 149  
         public static final String SUPPRESSION_FILE = "suppression.file";
 150  
         /**
 151  
          * The properties key for whether the Nexus analyzer is enabled.
 152  
          */
 153  
         public static final String ANALYZER_NEXUS_ENABLED = "analyzer.nexus.enabled";
 154  
         /**
 155  
          * The properties key for the Nexus search URL.
 156  
          */
 157  
         public static final String ANALYZER_NEXUS_URL = "analyzer.nexus.url";
 158  
     }
 159  
     /**
 160  
      * The properties file location.
 161  
      */
 162  
     private static final String PROPERTIES_FILE = "dependencycheck.properties";
 163  
     /**
 164  
      * The singleton instance variable.
 165  
      */
 166  1
     private static final Settings INSTANCE = new Settings();
 167  
     /**
 168  
      * The properties.
 169  
      */
 170  1
     private Properties props = null;
 171  
 
 172  
     /**
 173  
      * Private constructor for the Settings class. This class loads the
 174  
      * properties files.
 175  
      */
 176  1
     private Settings() {
 177  1
         InputStream in = null;
 178  1
         props = new Properties();
 179  
         try {
 180  1
             in = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
 181  1
             props.load(in);
 182  0
         } catch (IOException ex) {
 183  0
             Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, "Unable to load default settings.");
 184  0
             Logger.getLogger(Settings.class.getName()).log(Level.FINE, null, ex);
 185  
         } finally {
 186  1
             if (in != null) {
 187  
                 try {
 188  1
                     in.close();
 189  0
                 } catch (IOException ex) {
 190  0
                     Logger.getLogger(Settings.class.getName()).log(Level.FINEST, null, ex);
 191  1
                 }
 192  
             }
 193  
         }
 194  1
     }
 195  
 
 196  
     /**
 197  
      * Sets a property value.
 198  
      *
 199  
      * @param key the key for the property
 200  
      * @param value the value for the property
 201  
      */
 202  
     public static void setString(String key, String value) {
 203  5
         INSTANCE.props.setProperty(key, value);
 204  5
     }
 205  
 
 206  
     /**
 207  
      * Sets a property value.
 208  
      *
 209  
      * @param key the key for the property
 210  
      * @param value the value for the property
 211  
      */
 212  
     public static void setBoolean(String key, boolean value) {
 213  5
         if (value) {
 214  0
             INSTANCE.props.setProperty(key, Boolean.TRUE.toString());
 215  
         } else {
 216  5
             INSTANCE.props.setProperty(key, Boolean.FALSE.toString());
 217  
         }
 218  5
     }
 219  
 
 220  
     /**
 221  
      * Merges a new properties file into the current properties. This method
 222  
      * allows for the loading of a user provided properties file.<br/><br/>
 223  
      * Note: even if using this method - system properties will be loaded before
 224  
      * properties loaded from files.
 225  
      *
 226  
      * @param filePath the path to the properties file to merge.
 227  
      * @throws FileNotFoundException is thrown when the filePath points to a
 228  
      * non-existent file
 229  
      * @throws IOException is thrown when there is an exception loading/merging
 230  
      * the properties
 231  
      */
 232  
     public static void mergeProperties(File filePath) throws FileNotFoundException, IOException {
 233  0
         final FileInputStream fis = new FileInputStream(filePath);
 234  0
         mergeProperties(fis);
 235  0
     }
 236  
 
 237  
     /**
 238  
      * Merges a new properties file into the current properties. This method
 239  
      * allows for the loading of a user provided properties file.<br/><br/>
 240  
      * Note: even if using this method - system properties will be loaded before
 241  
      * properties loaded from files.
 242  
      *
 243  
      * @param filePath the path to the properties file to merge.
 244  
      * @throws FileNotFoundException is thrown when the filePath points to a
 245  
      * non-existent file
 246  
      * @throws IOException is thrown when there is an exception loading/merging
 247  
      * the properties
 248  
      */
 249  
     public static void mergeProperties(String filePath) throws FileNotFoundException, IOException {
 250  1
         final FileInputStream fis = new FileInputStream(filePath);
 251  1
         mergeProperties(fis);
 252  1
     }
 253  
 
 254  
     /**
 255  
      * Merges a new properties file into the current properties. This method
 256  
      * allows for the loading of a user provided properties file.<br/><br/>
 257  
      * Note: even if using this method - system properties will be loaded before
 258  
      * properties loaded from files.
 259  
      *
 260  
      * @param stream an Input Stream pointing at a properties file to merge
 261  
      * @throws IOException is thrown when there is an exception loading/merging
 262  
      * the properties
 263  
      */
 264  
     public static void mergeProperties(InputStream stream) throws IOException {
 265  1
         INSTANCE.props.load(stream);
 266  1
     }
 267  
 
 268  
     /**
 269  
      * Returns a value from the properties file as a File object. If the value
 270  
      * was specified as a system property or passed in via the -Dprop=value
 271  
      * argument - this method will return the value from the system properties
 272  
      * before the values in the contained configuration file.
 273  
      *
 274  
      * @param key the key to lookup within the properties file
 275  
      * @return the property from the properties file converted to a File object
 276  
      */
 277  
     public static File getFile(String key) {
 278  6
         final String file = getString(key);
 279  6
         if (file == null) {
 280  6
             return null;
 281  
         }
 282  0
         return new File(file);
 283  
     }
 284  
 
 285  
     /**
 286  
      * Returns a value from the properties file as a File object. If the value
 287  
      * was specified as a system property or passed in via the -Dprop=value
 288  
      * argument - this method will return the value from the system properties
 289  
      * before the values in the contained configuration file.
 290  
      *
 291  
      * This method will check the configured base directory and will use this as
 292  
      * the base of the file path. Additionally, if the base directory begins
 293  
      * with a leading "[JAR]\" sequence with the path to the folder containing
 294  
      * the JAR file containing this class.
 295  
      *
 296  
      * @param key the key to lookup within the properties file
 297  
      * @return the property from the properties file converted to a File object
 298  
      */
 299  
     public static File getDataFile(String key) {
 300  114
         final String file = getString(key);
 301  114
         if (file == null) {
 302  0
             return null;
 303  
         }
 304  114
         if (file.startsWith("[JAR]/")) {
 305  0
             final File jarPath = getJarPath();
 306  0
             final File newBase = new File(jarPath, file.substring(6));
 307  0
             return new File(newBase, file);
 308  
         }
 309  114
         return new File(file);
 310  
     }
 311  
 
 312  
     /**
 313  
      * Attempts to retrieve the folder containing the Jar file containing the
 314  
      * Settings class.
 315  
      *
 316  
      * @return a File object
 317  
      */
 318  
     private static File getJarPath() {
 319  0
         final String jarPath = Settings.class.getProtectionDomain().getCodeSource().getLocation().getPath();
 320  0
         String decodedPath = ".";
 321  
         try {
 322  0
             decodedPath = URLDecoder.decode(jarPath, "UTF-8");
 323  0
         } catch (UnsupportedEncodingException ex) {
 324  0
             Logger.getLogger(Settings.class.getName()).log(Level.FINEST, null, ex);
 325  0
         }
 326  
 
 327  0
         final File path = new File(decodedPath);
 328  0
         if (path.getName().toLowerCase().endsWith(".jar")) {
 329  0
             return path.getParentFile();
 330  
         } else {
 331  0
             return new File(".");
 332  
         }
 333  
     }
 334  
 
 335  
     /**
 336  
      * Returns a value from the properties file. If the value was specified as a
 337  
      * system property or passed in via the -Dprop=value argument - this method
 338  
      * will return the value from the system properties before the values in the
 339  
      * contained configuration file.
 340  
      *
 341  
      * @param key the key to lookup within the properties file
 342  
      * @param defaultValue the default value for the requested property
 343  
      * @return the property from the properties file
 344  
      */
 345  
     public static String getString(String key, String defaultValue) {
 346  203
         final String str = System.getProperty(key, INSTANCE.props.getProperty(key, defaultValue));
 347  203
         return str;
 348  
     }
 349  
 
 350  
     /**
 351  
      * Returns the temporary directory.
 352  
      *
 353  
      * @return the temporary directory
 354  
      */
 355  
     public static File getTempDirectory() {
 356  9
         return new File(Settings.getString(Settings.KEYS.TEMP_DIRECTORY, System.getProperty("java.io.tmpdir")));
 357  
     }
 358  
 
 359  
     /**
 360  
      * Returns a value from the properties file. If the value was specified as a
 361  
      * system property or passed in via the -Dprop=value argument - this method
 362  
      * will return the value from the system properties before the values in the
 363  
      * contained configuration file.
 364  
      *
 365  
      * @param key the key to lookup within the properties file
 366  
      * @return the property from the properties file
 367  
      */
 368  
     public static String getString(String key) {
 369  145
         return System.getProperty(key, INSTANCE.props.getProperty(key));
 370  
     }
 371  
 
 372  
     /**
 373  
      * Removes a property from the local properties collection. This is mainly
 374  
      * used in test cases.
 375  
      *
 376  
      * @param key the property key to remove
 377  
      */
 378  
     public static void removeProperty(String key) {
 379  1
         INSTANCE.props.remove(key);
 380  1
     }
 381  
 
 382  
     /**
 383  
      * Returns an int value from the properties file. If the value was specified
 384  
      * as a system property or passed in via the -Dprop=value argument - this
 385  
      * method will return the value from the system properties before the values
 386  
      * in the contained configuration file.
 387  
      *
 388  
      * @param key the key to lookup within the properties file
 389  
      * @return the property from the properties file
 390  
      * @throws InvalidSettingException is thrown if there is an error retrieving
 391  
      * the setting
 392  
      */
 393  
     public static int getInt(String key) throws InvalidSettingException {
 394  
         int value;
 395  
         try {
 396  1
             value = Integer.parseInt(Settings.getString(key));
 397  0
         } catch (NumberFormatException ex) {
 398  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 399  1
         }
 400  1
         return value;
 401  
     }
 402  
 
 403  
     /**
 404  
      * Returns an int value from the properties file. If the value was specified
 405  
      * as a system property or passed in via the -Dprop=value argument - this
 406  
      * method will return the value from the system properties before the values
 407  
      * in the contained configuration file.
 408  
      *
 409  
      * @param key the key to lookup within the properties file
 410  
      * @param defaultValue the default value to return
 411  
      * @return the property from the properties file or the defaultValue if the
 412  
      * property does not exist or cannot be converted to an integer
 413  
      */
 414  
     public static int getInt(String key, int defaultValue) {
 415  
         int value;
 416  
         try {
 417  1
             value = Integer.parseInt(Settings.getString(key));
 418  1
         } catch (NumberFormatException ex) {
 419  1
             final String msg = String.format("Could not convert property '%s' to an int.", key);
 420  1
             Logger.getLogger(Settings.class.getName()).log(Level.FINEST, msg, ex);
 421  1
             value = defaultValue;
 422  0
         }
 423  1
         return value;
 424  
     }
 425  
 
 426  
     /**
 427  
      * Returns a long value from the properties file. If the value was specified
 428  
      * as a system property or passed in via the -Dprop=value argument - this
 429  
      * method will return the value from the system properties before the values
 430  
      * in the contained configuration file.
 431  
      *
 432  
      * @param key the key to lookup within the properties file
 433  
      * @return the property from the properties file
 434  
      * @throws InvalidSettingException is thrown if there is an error retrieving
 435  
      * the setting
 436  
      */
 437  
     public static long getLong(String key) throws InvalidSettingException {
 438  
         long value;
 439  
         try {
 440  1
             value = Long.parseLong(Settings.getString(key));
 441  0
         } catch (NumberFormatException ex) {
 442  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 443  1
         }
 444  1
         return value;
 445  
     }
 446  
 
 447  
     /**
 448  
      * Returns a boolean value from the properties file. If the value was
 449  
      * specified as a system property or passed in via the
 450  
      * <code>-Dprop=value</code> argument this method will return the value from
 451  
      * the system properties before the values in the contained configuration
 452  
      * file.
 453  
      *
 454  
      * @param key the key to lookup within the properties file
 455  
      * @return the property from the properties file
 456  
      * @throws InvalidSettingException is thrown if there is an error retrieving
 457  
      * the setting
 458  
      */
 459  
     public static boolean getBoolean(String key) throws InvalidSettingException {
 460  
         boolean value;
 461  
         try {
 462  10
             value = Boolean.parseBoolean(Settings.getString(key));
 463  0
         } catch (NumberFormatException ex) {
 464  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 465  10
         }
 466  10
         return value;
 467  
     }
 468  
 }