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