Coverage Report - org.owasp.dependencycheck.utils.Settings
 
Classes in this File Line Coverage Branch Coverage Complexity
Settings
63%
48/76
41%
5/12
2.333
Settings$KEYS
0%
0/2
N/A
2.333
 
 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 properties key for the path where the CPE Lucene Index will be
 72  
          * stored.
 73  
          */
 74  
         public static final String CPE_DATA_DIRECTORY = "data.cpe";
 75  
         /**
 76  
          * The properties key for the path where the CVE H2 database will be
 77  
          * stored.
 78  
          */
 79  
         public static final String CVE_DATA_DIRECTORY = "data.cve";
 80  
         /**
 81  
          * The properties key for the URL to the CPE.
 82  
          */
 83  
         public static final String CPE_URL = "cpe.url";
 84  
         /**
 85  
          * The properties key for the URL to the CPE.
 86  
          */
 87  
         public static final String CPE_META_URL = "cpe.meta.url";
 88  
         /**
 89  
          * The properties key for the URL to retrieve the "meta" data from about
 90  
          * the CVE entries.
 91  
          */
 92  
         public static final String CVE_META_URL = "cve.url.meta";
 93  
         /**
 94  
          * The properties key for the URL to retrieve the recently modified and
 95  
          * added CVE entries (last 8 days) using the 2.0 schema.
 96  
          */
 97  
         public static final String CVE_MODIFIED_20_URL = "cve.url-2.0.modified";
 98  
         /**
 99  
          * The properties key for the URL to retrieve the recently modified and
 100  
          * added CVE entries (last 8 days) using the 1.2 schema.
 101  
          */
 102  
         public static final String CVE_MODIFIED_12_URL = "cve.url-1.2.modified";
 103  
         /**
 104  
          * The properties key for the URL to retrieve the recently modified and
 105  
          * added CVE entries (last 8 days).
 106  
          */
 107  
         public static final String CVE_MODIFIED_VALID_FOR_DAYS = "cve.url.modified.validfordays";
 108  
         /**
 109  
          * The properties key for the telling us how many cvr.url.* URLs exists.
 110  
          * This is used in combination with CVE_BASE_URL to be able to retrieve
 111  
          * the URLs for all of the files that make up the NVD CVE listing.
 112  
          */
 113  
         public static final String CVE_START_YEAR = "cve.startyear";
 114  
         /**
 115  
          * The properties key for the CVE schema version 1.2.
 116  
          */
 117  
         public static final String CVE_SCHEMA_1_2 = "cve.url-1.2.base";
 118  
         /**
 119  
          * The properties key for the CVE schema version 2.0.
 120  
          */
 121  
         public static final String CVE_SCHEMA_2_0 = "cve.url-2.0.base";
 122  
         /**
 123  
          * The properties key for the proxy url.
 124  
          */
 125  
         public static final String PROXY_URL = "proxy.url";
 126  
         /**
 127  
          * The properties key for the proxy port - this must be an integer
 128  
          * value.
 129  
          */
 130  
         public static final String PROXY_PORT = "proxy.port";
 131  
         /**
 132  
          * The properties key for the connection timeout.
 133  
          */
 134  
         public static final String CONNECTION_TIMEOUT = "connection.timeout";
 135  
         /**
 136  
          * The location of the temporary directory.
 137  
          */
 138  
         public static final String TEMP_DIRECTORY = "temp.directory";
 139  
     }
 140  
     /**
 141  
      * The properties file location.
 142  
      */
 143  
     private static final String PROPERTIES_FILE = "dependencycheck.properties";
 144  
     /**
 145  
      * The singleton instance variable.
 146  
      */
 147  3
     private static final Settings INSTANCE = new Settings();
 148  
     /**
 149  
      * The properties.
 150  
      */
 151  3
     private Properties props = null;
 152  
 
 153  
     /**
 154  
      * Private constructor for the Settings class. This class loads the
 155  
      * properties files.
 156  
      */
 157  3
     private Settings() {
 158  3
         InputStream in = null;
 159  3
         props = new Properties();
 160  
         try {
 161  3
             in = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
 162  3
             props.load(in);
 163  0
         } catch (IOException ex) {
 164  0
             Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, "Unable to load default settings.");
 165  0
             Logger.getLogger(Settings.class.getName()).log(Level.FINE, null, ex);
 166  
         } finally {
 167  3
             if (in != null) {
 168  
                 try {
 169  3
                     in.close();
 170  0
                 } catch (IOException ex) {
 171  0
                     Logger.getLogger(Settings.class.getName()).log(Level.FINEST, null, ex);
 172  3
                 }
 173  
             }
 174  
         }
 175  3
     }
 176  
 
 177  
     /**
 178  
      * Sets a property value.
 179  
      *
 180  
      * @param key the key for the property
 181  
      * @param value the value for the property
 182  
      */
 183  
     public static void setString(String key, String value) {
 184  12
         INSTANCE.props.setProperty(key, value);
 185  12
     }
 186  
 
 187  
     /**
 188  
      * Sets a property value.
 189  
      *
 190  
      * @param key the key for the property
 191  
      * @param value the value for the property
 192  
      */
 193  
     public static void setBoolean(String key, boolean value) {
 194  3
         if (value) {
 195  0
             INSTANCE.props.setProperty(key, Boolean.TRUE.toString());
 196  
         } else {
 197  3
             INSTANCE.props.setProperty(key, Boolean.FALSE.toString());
 198  
         }
 199  3
     }
 200  
 
 201  
     /**
 202  
      * Merges a new properties file into the current properties. This method
 203  
      * allows for the loading of a user provided properties file.<br/><br/>
 204  
      * Note: even if using this method - system properties will be loaded before
 205  
      * properties loaded from files.
 206  
      *
 207  
      * @param filePath the path to the properties file to merge.
 208  
      * @throws FileNotFoundException is thrown when the filePath points to a
 209  
      * non-existent file
 210  
      * @throws IOException is thrown when there is an exception loading/merging
 211  
      * the properties
 212  
      */
 213  
     public static void mergeProperties(String filePath) throws FileNotFoundException, IOException {
 214  3
         final FileInputStream fis = new FileInputStream(filePath);
 215  3
         mergeProperties(fis);
 216  3
     }
 217  
 
 218  
     /**
 219  
      * Merges a new properties file into the current properties. This method
 220  
      * allows for the loading of a user provided properties file.<br/><br/>
 221  
      * Note: even if using this method - system properties will be loaded before
 222  
      * properties loaded from files.
 223  
      *
 224  
      * @param stream an Input Stream pointing at a properties file to merge
 225  
      * @throws IOException is thrown when there is an exception loading/merging
 226  
      * the properties
 227  
      */
 228  
     public static void mergeProperties(InputStream stream) throws IOException {
 229  3
         INSTANCE.props.load(stream);
 230  3
     }
 231  
 
 232  
     /**
 233  
      * Returns a value from the properties file as a File object. If the value
 234  
      * was specified as a system property or passed in via the -Dprop=value
 235  
      * argument - this method will return the value from the system properties
 236  
      * before the values in the contained configuration file.
 237  
      *
 238  
      * @param key the key to lookup within the properties file
 239  
      * @param defaultValue the default value for the requested property
 240  
      * @return the property from the properties file as a File object
 241  
      */
 242  
     public static File getFile(String key, String defaultValue) {
 243  3
         final String baseDir = getString(Settings.KEYS.DATA_DIRECTORY);
 244  3
         final String str = getString(key, defaultValue);
 245  3
         if (baseDir != null) {
 246  3
             return new File(baseDir, str);
 247  
         }
 248  0
         return new File(str);
 249  
     }
 250  
 
 251  
     /**
 252  
      * Returns a value from the properties file as a File object. If the value
 253  
      * was specified as a system property or passed in via the -Dprop=value
 254  
      * argument - this method will return the value from the system properties
 255  
      * before the values in the contained configuration file.
 256  
      *
 257  
      * This method will also replace a leading "[JAR]\" sequence with the path
 258  
      * to the folder containing the JAR file containing this class.
 259  
      *
 260  
      * @param key the key to lookup within the properties file
 261  
      * @return the property from the properties file converted to a File object
 262  
      * @throws IOException thrown if the file path to the JAR cannot be found
 263  
      */
 264  
     public static File getFile(String key) throws IOException {
 265  66
         final String file = getString(key);
 266  66
         final String baseDir = getString(Settings.KEYS.DATA_DIRECTORY);
 267  66
         if (baseDir != null) {
 268  66
             if (baseDir.startsWith("[JAR]/")) {
 269  0
                 final File jarPath = getJarPath();
 270  0
                 final File newBase = new File(jarPath.getCanonicalPath(), baseDir.substring(6));
 271  0
                 return new File(newBase, file);
 272  
             }
 273  66
             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  12
         final String str = System.getProperty(key, INSTANCE.props.getProperty(key, defaultValue));
 313  12
         return str;
 314  
     }
 315  
 
 316  
     /**
 317  
      * Returns a value from the properties file. If the value was specified as a
 318  
      * system property or passed in via the -Dprop=value argument - this method
 319  
      * will return the value from the system properties before the values in the
 320  
      * contained configuration file.
 321  
      *
 322  
      * @param key the key to lookup within the properties file
 323  
      * @return the property from the properties file
 324  
      */
 325  
     public static String getString(String key) {
 326  246
         return System.getProperty(key, INSTANCE.props.getProperty(key));
 327  
     }
 328  
 
 329  
     /**
 330  
      * Returns an int value from the properties file. If the value was specified
 331  
      * as a system property or passed in via the -Dprop=value argument - this
 332  
      * method will return the value from the system properties before the values
 333  
      * in the contained configuration file.
 334  
      *
 335  
      * @param key the key to lookup within the properties file
 336  
      * @return the property from the properties file
 337  
      * @throws InvalidSettingException is thrown if there is an error retrieving
 338  
      * the setting
 339  
      */
 340  
     public static int getInt(String key) throws InvalidSettingException {
 341  
         int value;
 342  
         try {
 343  3
             value = Integer.parseInt(Settings.getString(key));
 344  0
         } catch (NumberFormatException ex) {
 345  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 346  3
         }
 347  3
         return value;
 348  
     }
 349  
 
 350  
     /**
 351  
      * Returns an int value from the properties file. If the value was specified
 352  
      * as a system property or passed in via the -Dprop=value argument - this
 353  
      * method will return the value from the system properties before the values
 354  
      * in the contained configuration file.
 355  
      *
 356  
      * @param key the key to lookup within the properties file
 357  
      * @param defaultValue the default value to return
 358  
      * @return the property from the properties file or the defaultValue if the
 359  
      * property does not exist or cannot be converted to an integer
 360  
      */
 361  
     public static int getInt(String key, int defaultValue) {
 362  
         int value;
 363  
         try {
 364  3
             value = Integer.parseInt(Settings.getString(key));
 365  3
         } catch (NumberFormatException ex) {
 366  3
             final String msg = String.format("Could not convert property '%s' to an int.", key);
 367  3
             Logger.getLogger(Settings.class.getName()).log(Level.FINEST, msg, ex);
 368  3
             value = defaultValue;
 369  0
         }
 370  3
         return value;
 371  
     }
 372  
 
 373  
     /**
 374  
      * Returns a long value from the properties file. If the value was specified
 375  
      * as a system property or passed in via the -Dprop=value argument - this
 376  
      * method will return the value from the system properties before the values
 377  
      * in the contained configuration file.
 378  
      *
 379  
      * @param key the key to lookup within the properties file
 380  
      * @return the property from the properties file
 381  
      * @throws InvalidSettingException is thrown if there is an error retrieving
 382  
      * the setting
 383  
      */
 384  
     public static long getLong(String key) throws InvalidSettingException {
 385  
         long value;
 386  
         try {
 387  3
             value = Long.parseLong(Settings.getString(key));
 388  0
         } catch (NumberFormatException ex) {
 389  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 390  3
         }
 391  3
         return value;
 392  
     }
 393  
 
 394  
     /**
 395  
      * Returns a boolean value from the properties file. If the value was
 396  
      * specified as a system property or passed in via the
 397  
      * <code>-Dprop=value</code> argument this method will return the value from
 398  
      * the system properties before the values in the contained configuration
 399  
      * file.
 400  
      *
 401  
      * @param key the key to lookup within the properties file
 402  
      * @return the property from the properties file
 403  
      * @throws InvalidSettingException is thrown if there is an error retrieving
 404  
      * the setting
 405  
      */
 406  
     public static boolean getBoolean(String key) throws InvalidSettingException {
 407  
         boolean value;
 408  
         try {
 409  6
             value = Boolean.parseBoolean(Settings.getString(key));
 410  0
         } catch (NumberFormatException ex) {
 411  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 412  6
         }
 413  6
         return value;
 414  
     }
 415  
 }