Coverage Report - org.owasp.dependencycheck.utils.Settings
 
Classes in this File Line Coverage Branch Coverage Complexity
Settings
62%
49/78
42%
6/14
2.375
Settings$KEYS
0%
0/2
N/A
2.375
 
 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  14
         INSTANCE.props.setProperty(key, value);
 182  14
     }
 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(String filePath) throws FileNotFoundException, IOException {
 211  1
         final FileInputStream fis = new FileInputStream(filePath);
 212  1
         mergeProperties(fis);
 213  1
     }
 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 stream an Input Stream pointing at a properties file to merge
 222  
      * @throws IOException is thrown when there is an exception loading/merging
 223  
      * the properties
 224  
      */
 225  
     public static void mergeProperties(InputStream stream) throws IOException {
 226  1
         INSTANCE.props.load(stream);
 227  1
     }
 228  
 
 229  
     /**
 230  
      * Returns a value from the properties file as a File object. If the value
 231  
      * was specified as a system property or passed in via the -Dprop=value
 232  
      * argument - this method will return the value from the system properties
 233  
      * before the values in the contained configuration file.
 234  
      *
 235  
      * This method will also replace a leading "[JAR]\" sequence with the path
 236  
      * to the folder containing the JAR file containing this class.
 237  
      *
 238  
      * @param key the key to lookup within the properties file
 239  
      * @return the property from the properties file converted to a File object
 240  
      */
 241  
     public static File getFile(String key) {
 242  53
         final String file = getString(key);
 243  53
         final String baseDir = getString(Settings.KEYS.DATA_DIRECTORY);
 244  53
         if (baseDir != null) {
 245  53
             if (baseDir.startsWith("[JAR]/")) {
 246  0
                 final File jarPath = getJarPath();
 247  0
                 final File newBase = new File(jarPath, baseDir.substring(6));
 248  0
                 if (Settings.KEYS.DATA_DIRECTORY.equals(key)) {
 249  0
                     return newBase;
 250  
                 }
 251  0
                 return new File(newBase, file);
 252  
             }
 253  53
             if (Settings.KEYS.DATA_DIRECTORY.equals(key)) {
 254  16
                 return new File(baseDir);
 255  
             }
 256  37
             return new File(baseDir, file);
 257  
         }
 258  0
         return new File(file);
 259  
     }
 260  
 
 261  
     /**
 262  
      * Attempts to retrieve the folder containing the Jar file containing the
 263  
      * Settings class.
 264  
      *
 265  
      * @return a File object
 266  
      */
 267  
     private static File getJarPath() {
 268  0
         final String jarPath = Settings.class.getProtectionDomain().getCodeSource().getLocation().getPath();
 269  0
         String decodedPath = ".";
 270  
         try {
 271  0
             decodedPath = URLDecoder.decode(jarPath, "UTF-8");
 272  0
         } catch (UnsupportedEncodingException ex) {
 273  0
             Logger.getLogger(Settings.class.getName()).log(Level.FINEST, null, ex);
 274  0
         }
 275  
 
 276  0
         final File path = new File(decodedPath);
 277  0
         if (path.getName().toLowerCase().endsWith(".jar")) {
 278  0
             return path.getParentFile();
 279  
         } else {
 280  0
             return new File(".");
 281  
         }
 282  
     }
 283  
 
 284  
     /**
 285  
      * Returns a value from the properties file. If the value was specified as a
 286  
      * system property or passed in via the -Dprop=value argument - this method
 287  
      * will return the value from the system properties before the values in the
 288  
      * contained configuration file.
 289  
      *
 290  
      * @param key the key to lookup within the properties file
 291  
      * @param defaultValue the default value for the requested property
 292  
      * @return the property from the properties file
 293  
      */
 294  
     public static String getString(String key, String defaultValue) {
 295  9
         final String str = System.getProperty(key, INSTANCE.props.getProperty(key, defaultValue));
 296  9
         return str;
 297  
     }
 298  
 
 299  
     /**
 300  
      * Returns the temporary directory.
 301  
      *
 302  
      * @return the temporary directory
 303  
      */
 304  
     public static File getTempDirectory() {
 305  5
         return new File(Settings.getString(Settings.KEYS.TEMP_DIRECTORY, System.getProperty("java.io.tmpdir")));
 306  
     }
 307  
 
 308  
     /**
 309  
      * Returns a value from the properties file. If the value was specified as a
 310  
      * system property or passed in via the -Dprop=value argument - this method
 311  
      * will return the value from the system properties before the values in the
 312  
      * contained configuration file.
 313  
      *
 314  
      * @param key the key to lookup within the properties file
 315  
      * @return the property from the properties file
 316  
      */
 317  
     public static String getString(String key) {
 318  129
         return System.getProperty(key, INSTANCE.props.getProperty(key));
 319  
     }
 320  
 
 321  
     /**
 322  
      * Removes a property from the local properties collection. This is mainly
 323  
      * used in test cases.
 324  
      *
 325  
      * @param key the property key to remove
 326  
      */
 327  
     public static void removeProperty(String key) {
 328  4
         INSTANCE.props.remove(key);
 329  4
     }
 330  
 
 331  
     /**
 332  
      * Returns an int value from the properties file. If the value was specified
 333  
      * as a system property or passed in via the -Dprop=value argument - this
 334  
      * method will return the value from the system properties before the values
 335  
      * in the contained configuration file.
 336  
      *
 337  
      * @param key the key to lookup within the properties file
 338  
      * @return the property from the properties file
 339  
      * @throws InvalidSettingException is thrown if there is an error retrieving
 340  
      * the setting
 341  
      */
 342  
     public static int getInt(String key) throws InvalidSettingException {
 343  
         int value;
 344  
         try {
 345  1
             value = Integer.parseInt(Settings.getString(key));
 346  0
         } catch (NumberFormatException ex) {
 347  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 348  1
         }
 349  1
         return value;
 350  
     }
 351  
 
 352  
     /**
 353  
      * Returns an int value from the properties file. If the value was specified
 354  
      * as a system property or passed in via the -Dprop=value argument - this
 355  
      * method will return the value from the system properties before the values
 356  
      * in the contained configuration file.
 357  
      *
 358  
      * @param key the key to lookup within the properties file
 359  
      * @param defaultValue the default value to return
 360  
      * @return the property from the properties file or the defaultValue if the
 361  
      * property does not exist or cannot be converted to an integer
 362  
      */
 363  
     public static int getInt(String key, int defaultValue) {
 364  
         int value;
 365  
         try {
 366  1
             value = Integer.parseInt(Settings.getString(key));
 367  1
         } catch (NumberFormatException ex) {
 368  1
             final String msg = String.format("Could not convert property '%s' to an int.", key);
 369  1
             Logger.getLogger(Settings.class.getName()).log(Level.FINEST, msg, ex);
 370  1
             value = defaultValue;
 371  0
         }
 372  1
         return value;
 373  
     }
 374  
 
 375  
     /**
 376  
      * Returns a long value from the properties file. If the value was specified
 377  
      * as a system property or passed in via the -Dprop=value argument - this
 378  
      * method will return the value from the system properties before the values
 379  
      * in the contained configuration file.
 380  
      *
 381  
      * @param key the key to lookup within the properties file
 382  
      * @return the property from the properties file
 383  
      * @throws InvalidSettingException is thrown if there is an error retrieving
 384  
      * the setting
 385  
      */
 386  
     public static long getLong(String key) throws InvalidSettingException {
 387  
         long value;
 388  
         try {
 389  1
             value = Long.parseLong(Settings.getString(key));
 390  0
         } catch (NumberFormatException ex) {
 391  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 392  1
         }
 393  1
         return value;
 394  
     }
 395  
 
 396  
     /**
 397  
      * Returns a boolean value from the properties file. If the value was
 398  
      * specified as a system property or passed in via the
 399  
      * <code>-Dprop=value</code> argument this method will return the value from
 400  
      * the system properties before the values in the contained configuration
 401  
      * file.
 402  
      *
 403  
      * @param key the key to lookup within the properties file
 404  
      * @return the property from the properties file
 405  
      * @throws InvalidSettingException is thrown if there is an error retrieving
 406  
      * the setting
 407  
      */
 408  
     public static boolean getBoolean(String key) throws InvalidSettingException {
 409  
         boolean value;
 410  
         try {
 411  3
             value = Boolean.parseBoolean(Settings.getString(key));
 412  0
         } catch (NumberFormatException ex) {
 413  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 414  3
         }
 415  3
         return value;
 416  
     }
 417  
 }