Coverage Report - org.owasp.dependencycheck.utils.Settings
 
Classes in this File Line Coverage Branch Coverage Complexity
Settings
47%
82/173
35%
19/54
2.893
Settings$KEYS
0%
0/2
N/A
2.893
 
 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 org.slf4j.Logger;
 21  
 import org.slf4j.LoggerFactory;
 22  
 
 23  
 import java.io.File;
 24  
 import java.io.FileInputStream;
 25  
 import java.io.FileNotFoundException;
 26  
 import java.io.IOException;
 27  
 import java.io.InputStream;
 28  
 import java.io.PrintWriter;
 29  
 import java.io.StringWriter;
 30  
 import java.io.UnsupportedEncodingException;
 31  
 import java.net.URLDecoder;
 32  
 import java.util.Enumeration;
 33  
 import java.util.Properties;
 34  
 
 35  
 /**
 36  
  * A simple settings container that wraps the dependencycheck.properties file.
 37  
  *
 38  
  * @author Jeremy Long
 39  
  */
 40  
 public final class Settings {
 41  
 
 42  
     //<editor-fold defaultstate="collapsed" desc="KEYS used to access settings">
 43  
     /**
 44  
      * The collection of keys used within the properties file.
 45  
      */
 46  
     public static final class KEYS {
 47  
 
 48  
         /**
 49  
          * private constructor because this is a "utility" class containing constants
 50  
          */
 51  0
         private KEYS() {
 52  
             //do nothing
 53  0
         }
 54  
         /**
 55  
          * The key to obtain the application name.
 56  
          */
 57  
         public static final String APPLICATION_VAME = "application.name";
 58  
         /**
 59  
          * The key to obtain the application version.
 60  
          */
 61  
         public static final String APPLICATION_VERSION = "application.version";
 62  
         /**
 63  
          * The key to obtain the URL to retrieve the current release version from.
 64  
          */
 65  
         public static final String ENGINE_VERSION_CHECK_URL = "engine.version.url";
 66  
         /**
 67  
          * The properties key indicating whether or not the cached data sources should be updated.
 68  
          */
 69  
         public static final String AUTO_UPDATE = "autoupdate";
 70  
         /**
 71  
          * The database driver class name. If this is not in the properties file the embedded database is used.
 72  
          */
 73  
         public static final String DB_DRIVER_NAME = "data.driver_name";
 74  
         /**
 75  
          * The database driver class name. If this is not in the properties file the embedded database is used.
 76  
          */
 77  
         public static final String DB_DRIVER_PATH = "data.driver_path";
 78  
         /**
 79  
          * The database connection string. If this is not in the properties file the embedded database is used.
 80  
          */
 81  
         public static final String DB_CONNECTION_STRING = "data.connection_string";
 82  
         /**
 83  
          * The username to use when connecting to the database.
 84  
          */
 85  
         public static final String DB_USER = "data.user";
 86  
         /**
 87  
          * The password to authenticate to the database.
 88  
          */
 89  
         public static final String DB_PASSWORD = "data.password";
 90  
         /**
 91  
          * The base path to use for the data directory (for embedded db).
 92  
          */
 93  
         public static final String DATA_DIRECTORY = "data.directory";
 94  
         /**
 95  
          * The database file name.
 96  
          */
 97  
         public static final String DB_FILE_NAME = "data.file_name";
 98  
         /**
 99  
          * The database schema version.
 100  
          */
 101  
         public static final String DB_VERSION = "data.version";
 102  
         /**
 103  
          * The properties key for the URL to retrieve the "meta" data from about the CVE entries.
 104  
          */
 105  
         public static final String CVE_META_URL = "cve.url.meta";
 106  
         /**
 107  
          * The properties key for the URL to retrieve the recently modified and added CVE entries (last 8 days) using the 2.0
 108  
          * schema.
 109  
          */
 110  
         public static final String CVE_MODIFIED_20_URL = "cve.url-2.0.modified";
 111  
         /**
 112  
          * The properties key for the URL to retrieve the recently modified and added CVE entries (last 8 days) using the 1.2
 113  
          * schema.
 114  
          */
 115  
         public static final String CVE_MODIFIED_12_URL = "cve.url-1.2.modified";
 116  
         /**
 117  
          * The properties key for the URL to retrieve the recently modified and added CVE entries (last 8 days).
 118  
          */
 119  
         public static final String CVE_MODIFIED_VALID_FOR_DAYS = "cve.url.modified.validfordays";
 120  
         /**
 121  
          * The properties key for the telling us how many cve.url.* URLs exists. This is used in combination with CVE_BASE_URL to
 122  
          * be able to retrieve the URLs for all of the files that make up the NVD CVE listing.
 123  
          */
 124  
         public static final String CVE_START_YEAR = "cve.startyear";
 125  
         /**
 126  
          * The properties key for the CVE schema version 1.2.
 127  
          */
 128  
         public static final String CVE_SCHEMA_1_2 = "cve.url-1.2.base";
 129  
         /**
 130  
          * The properties key for the CVE schema version 2.0.
 131  
          */
 132  
         public static final String CVE_SCHEMA_2_0 = "cve.url-2.0.base";
 133  
         /**
 134  
          * The properties key that indicates how often the CPE data needs to be updated.
 135  
          */
 136  
         public static final String CPE_MODIFIED_VALID_FOR_DAYS = "cpe.validfordays";
 137  
         /**
 138  
          * The properties key for the URL to retrieve the CPE.
 139  
          */
 140  
         public static final String CPE_URL = "cpe.url";
 141  
         /**
 142  
          * The properties key for the proxy server.
 143  
          *
 144  
          * @deprecated use {@link org.owasp.dependencycheck.utils.Settings.KEYS#PROXY_SERVER} instead.
 145  
          */
 146  
         @Deprecated
 147  
         public static final String PROXY_URL = "proxy.server";
 148  
         /**
 149  
          * The properties key for the proxy server.
 150  
          */
 151  
         public static final String PROXY_SERVER = "proxy.server";
 152  
         /**
 153  
          * The properties key for the proxy port - this must be an integer value.
 154  
          */
 155  
         public static final String PROXY_PORT = "proxy.port";
 156  
         /**
 157  
          * The properties key for the proxy username.
 158  
          */
 159  
         public static final String PROXY_USERNAME = "proxy.username";
 160  
         /**
 161  
          * The properties key for the proxy password.
 162  
          */
 163  
         public static final String PROXY_PASSWORD = "proxy.password";
 164  
         /**
 165  
          * The properties key for the connection timeout.
 166  
          */
 167  
         public static final String CONNECTION_TIMEOUT = "connection.timeout";
 168  
         /**
 169  
          * The location of the temporary directory.
 170  
          */
 171  
         public static final String TEMP_DIRECTORY = "temp.directory";
 172  
         /**
 173  
          * The maximum number of threads to allocate when downloading files.
 174  
          */
 175  
         public static final String MAX_DOWNLOAD_THREAD_POOL_SIZE = "max.download.threads";
 176  
         /**
 177  
          * The key for a list of suppression files.
 178  
          */
 179  
         public static final String SUPPRESSION_FILE = "suppression.file";
 180  
         /**
 181  
          * The properties key for whether the Jar Analyzer is enabled.
 182  
          */
 183  
         public static final String ANALYZER_JAR_ENABLED = "analyzer.jar.enabled";
 184  
         /**
 185  
          * The properties key for whether the Archive analyzer is enabled.
 186  
          */
 187  
         public static final String ANALYZER_ARCHIVE_ENABLED = "analyzer.archive.enabled";
 188  
         /**
 189  
          * The properties key for whether the Python Distribution analyzer is enabled.
 190  
          */
 191  
         public static final String ANALYZER_PYTHON_DISTRIBUTION_ENABLED = "analyzer.python.distribution.enabled";
 192  
         /**
 193  
          * The properties key for whether the Python Package analyzer is enabled.
 194  
          */
 195  
         public static final String ANALYZER_PYTHON_PACKAGE_ENABLED = "analyzer.python.package.enabled";
 196  
         /**
 197  
          * The properties key for whether the Autoconf analyzer is enabled.
 198  
          */
 199  
         public static final String ANALYZER_AUTOCONF_ENABLED = "analyzer.autoconf.enabled";
 200  
         /**
 201  
          * The properties key for whether the CMake analyzer is enabled.
 202  
          */
 203  
         public static final String ANALYZER_CMAKE_ENABLED = "analyzer.cmake.enabled";
 204  
         /**
 205  
          * The properties key for whether the .NET Assembly analyzer is enabled.
 206  
          */
 207  
         public static final String ANALYZER_ASSEMBLY_ENABLED = "analyzer.assembly.enabled";
 208  
         /**
 209  
          * The properties key for whether the .NET Nuspec analyzer is enabled.
 210  
          */
 211  
         public static final String ANALYZER_NUSPEC_ENABLED = "analyzer.nuspec.enabled";
 212  
         /**
 213  
          * The properties key for whether the JavaScript analyzer is enabled.
 214  
          */
 215  
         public static final String ANALYZER_JAVASCRIPT_ENABLED = "analyzer.javascript.enabled";
 216  
         /**
 217  
          * The properties key for whether the Nexus analyzer is enabled.
 218  
          */
 219  
         public static final String ANALYZER_NEXUS_ENABLED = "analyzer.nexus.enabled";
 220  
         /**
 221  
          * The properties key for the Nexus search URL.
 222  
          */
 223  
         public static final String ANALYZER_NEXUS_URL = "analyzer.nexus.url";
 224  
         /**
 225  
          * The properties key for using the proxy to reach Nexus.
 226  
          */
 227  
         public static final String ANALYZER_NEXUS_PROXY = "analyzer.nexus.proxy";
 228  
         /**
 229  
          * The properties key for whether the Central analyzer is enabled.
 230  
          */
 231  
         public static final String ANALYZER_CENTRAL_ENABLED = "analyzer.central.enabled";
 232  
         /**
 233  
          * The properties key for whether the OpenSSL analyzer is enabled.
 234  
          */
 235  
         public static final String ANALYZER_OPENSSL_ENABLED = "analyzer.openssl.enabled";
 236  
         /**
 237  
          * The properties key for the Central search URL.
 238  
          */
 239  
         public static final String ANALYZER_CENTRAL_URL = "analyzer.central.url";
 240  
         /**
 241  
          * The path to mono, if available.
 242  
          */
 243  
         public static final String ANALYZER_ASSEMBLY_MONO_PATH = "analyzer.assembly.mono.path";
 244  
         /**
 245  
          * The additional configured zip file extensions, if available.
 246  
          */
 247  
         public static final String ADDITIONAL_ZIP_EXTENSIONS = "extensions.zip";
 248  
         /**
 249  
          * The properties key for whether Test Scope dependencies should be skipped.
 250  
          */
 251  
         public static final String SKIP_TEST_SCOPE = "skip.test.scope";
 252  
         /**
 253  
          * The properties key for whether Runtime Scope dependencies should be skipped.
 254  
          */
 255  
         public static final String SKIP_RUNTIME_SCOPE = "skip.runtime.scope";
 256  
         /**
 257  
          * The properties key for whether Provided Scope dependencies should be skipped.
 258  
          */
 259  
         public static final String SKIP_PROVIDED_SCOPE = "skip.provided.scope";
 260  
 
 261  
         /**
 262  
          * The key to obtain the path to the VFEED data file.
 263  
          */
 264  
         public static final String VFEED_DATA_FILE = "vfeed.data_file";
 265  
         /**
 266  
          * The key to obtain the VFEED connection string.
 267  
          */
 268  
         public static final String VFEED_CONNECTION_STRING = "vfeed.connection_string";
 269  
 
 270  
         /**
 271  
          * The key to obtain the base download URL for the VFeed data file.
 272  
          */
 273  
         public static final String VFEED_DOWNLOAD_URL = "vfeed.download_url";
 274  
         /**
 275  
          * The key to obtain the download file name for the VFeed data.
 276  
          */
 277  
         public static final String VFEED_DOWNLOAD_FILE = "vfeed.download_file";
 278  
         /**
 279  
          * The key to obtain the VFeed update status.
 280  
          */
 281  
         public static final String VFEED_UPDATE_STATUS = "vfeed.update_status";
 282  
 
 283  
         /**
 284  
          * The HTTP request method for query last modified date.
 285  
          */
 286  
         public static final String DOWNLOADER_QUICK_QUERY_TIMESTAMP = "downloader.quick.query.timestamp";
 287  
     }
 288  
     //</editor-fold>
 289  
 
 290  
     /**
 291  
      * The logger.
 292  
      */
 293  8
     private static final Logger LOGGER = LoggerFactory.getLogger(Settings.class);
 294  
     /**
 295  
      * The properties file location.
 296  
      */
 297  
     private static final String PROPERTIES_FILE = "dependencycheck.properties";
 298  
     /**
 299  
      * Thread local settings.
 300  
      */
 301  8
     private static ThreadLocal<Settings> localSettings = new ThreadLocal<Settings>();
 302  
     /**
 303  
      * The properties.
 304  
      */
 305  16
     private Properties props = null;
 306  
 
 307  
     /**
 308  
      * Private constructor for the Settings class. This class loads the properties files.
 309  
      *
 310  
      * @param propertiesFilePath the path to the base properties file to load
 311  
      */
 312  16
     private Settings(String propertiesFilePath) {
 313  16
         InputStream in = null;
 314  16
         props = new Properties();
 315  
         try {
 316  16
             in = this.getClass().getClassLoader().getResourceAsStream(propertiesFilePath);
 317  16
             props.load(in);
 318  0
         } catch (IOException ex) {
 319  0
             LOGGER.error("Unable to load default settings.");
 320  0
             LOGGER.debug("", ex);
 321  
         } finally {
 322  16
             if (in != null) {
 323  
                 try {
 324  16
                     in.close();
 325  0
                 } catch (IOException ex) {
 326  0
                     LOGGER.trace("", ex);
 327  16
                 }
 328  
             }
 329  
         }
 330  16
         logProperties("Properties loaded", props);
 331  16
     }
 332  
 
 333  
     /**
 334  
      * Initializes the thread local settings object. Note, to use the settings object you must call this method. However, you must
 335  
      * also call Settings.cleanup() to properly release resources.
 336  
      */
 337  
     public static void initialize() {
 338  16
         localSettings.set(new Settings(PROPERTIES_FILE));
 339  16
     }
 340  
 
 341  
     /**
 342  
      * Initializes the thread local settings object. Note, to use the settings object you must call this method. However, you must
 343  
      * also call Settings.cleanup() to properly release resources.
 344  
      *
 345  
      * @param propertiesFilePath the path to the base properties file to load
 346  
      */
 347  
     public static void initialize(String propertiesFilePath) {
 348  0
         localSettings.set(new Settings(propertiesFilePath));
 349  0
     }
 350  
 
 351  
     /**
 352  
      * Cleans up resources to prevent memory leaks.
 353  
      *
 354  
      */
 355  
     public static void cleanup() {
 356  0
         cleanup(true);
 357  0
     }
 358  
 
 359  
     /**
 360  
      * Cleans up resources to prevent memory leaks.
 361  
      *
 362  
      * @param deleteTemporary flag indicating whether any temporary directories generated should be removed
 363  
      */
 364  
     public static void cleanup(boolean deleteTemporary) {
 365  16
         if (deleteTemporary && tempDirectory != null && tempDirectory.exists()) {
 366  16
             FileUtils.delete(tempDirectory);
 367  
         }
 368  
         try {
 369  16
             localSettings.remove();
 370  0
         } catch (Throwable ex) {
 371  0
             LOGGER.debug("Error cleaning up Settings", ex);
 372  16
         }
 373  16
     }
 374  
 
 375  
     /**
 376  
      * Gets the underlying instance of the Settings object.
 377  
      *
 378  
      * @return the Settings object
 379  
      */
 380  
     public static Settings getInstance() {
 381  0
         return localSettings.get();
 382  
     }
 383  
 
 384  
     /**
 385  
      * Sets the instance of the Settings object to use in this thread.
 386  
      *
 387  
      * @param instance the instance of the settings object to use in this thread
 388  
      */
 389  
     public static void setInstance(Settings instance) {
 390  0
         localSettings.set(instance);
 391  0
     }
 392  
 
 393  
     /**
 394  
      * Logs the properties. This will not log any properties that contain 'password' in the key.
 395  
      *
 396  
      * @param header the header to print with the log message
 397  
      * @param properties the properties to log
 398  
      */
 399  
     private static void logProperties(String header, Properties properties) {
 400  24
         if (LOGGER.isDebugEnabled()) {
 401  0
             final StringWriter sw = new StringWriter();
 402  0
             PrintWriter pw = null;
 403  
             try {
 404  0
                 pw = new PrintWriter(sw);
 405  0
                 pw.format("%s:%n%n", header);
 406  0
                 final Enumeration<?> e = properties.propertyNames();
 407  0
                 while (e.hasMoreElements()) {
 408  0
                     final String key = (String) e.nextElement();
 409  0
                     if (key.contains("password")) {
 410  0
                         pw.format("%s='*****'%n", key);
 411  
                     } else {
 412  0
                         final String value = properties.getProperty(key);
 413  0
                         if (value != null) {
 414  0
                             pw.format("%s='%s'%n", key, value);
 415  
                         }
 416  
                     }
 417  0
                 }
 418  0
                 pw.flush();
 419  0
                 LOGGER.debug(sw.toString());
 420  
             } finally {
 421  0
                 if (pw != null) {
 422  0
                     pw.close();
 423  
                 }
 424  
             }
 425  
 
 426  
         }
 427  24
     }
 428  
 
 429  
     /**
 430  
      * Sets a property value.
 431  
      *
 432  
      * @param key the key for the property
 433  
      * @param value the value for the property
 434  
      */
 435  
     public static void setString(String key, String value) {
 436  40
         localSettings.get().props.setProperty(key, value);
 437  40
         LOGGER.debug("Setting: {}='{}'", key, value);
 438  40
     }
 439  
 
 440  
     /**
 441  
      * Sets a property value.
 442  
      *
 443  
      * @param key the key for the property
 444  
      * @param value the value for the property
 445  
      */
 446  
     public static void setBoolean(String key, boolean value) {
 447  0
         if (value) {
 448  0
             localSettings.get().props.setProperty(key, Boolean.TRUE.toString());
 449  
         } else {
 450  0
             localSettings.get().props.setProperty(key, Boolean.FALSE.toString());
 451  
         }
 452  0
         LOGGER.debug("Setting: {}='{}'", key, value);
 453  0
     }
 454  
 
 455  
     /**
 456  
      * Merges a new properties file into the current properties. This method allows for the loading of a user provided properties
 457  
      * file.<br/><br/>
 458  
      * Note: even if using this method - system properties will be loaded before properties loaded from files.
 459  
      *
 460  
      * @param filePath the path to the properties file to merge.
 461  
      * @throws FileNotFoundException is thrown when the filePath points to a non-existent file
 462  
      * @throws IOException is thrown when there is an exception loading/merging the properties
 463  
      */
 464  
     public static void mergeProperties(File filePath) throws FileNotFoundException, IOException {
 465  0
         FileInputStream fis = null;
 466  
         try {
 467  0
             fis = new FileInputStream(filePath);
 468  0
             mergeProperties(fis);
 469  
         } finally {
 470  0
             if (fis != null) {
 471  
                 try {
 472  0
                     fis.close();
 473  0
                 } catch (IOException ex) {
 474  0
                     LOGGER.trace("close error", ex);
 475  0
                 }
 476  
             }
 477  
         }
 478  0
     }
 479  
 
 480  
     /**
 481  
      * Merges a new properties file into the current properties. This method allows for the loading of a user provided properties
 482  
      * file.<br/><br/>
 483  
      * Note: even if using this method - system properties will be loaded before properties loaded from files.
 484  
      *
 485  
      * @param filePath the path to the properties file to merge.
 486  
      * @throws FileNotFoundException is thrown when the filePath points to a non-existent file
 487  
      * @throws IOException is thrown when there is an exception loading/merging the properties
 488  
      */
 489  
     public static void mergeProperties(String filePath) throws FileNotFoundException, IOException {
 490  8
         FileInputStream fis = null;
 491  
         try {
 492  8
             fis = new FileInputStream(filePath);
 493  8
             mergeProperties(fis);
 494  
         } finally {
 495  8
             if (fis != null) {
 496  
                 try {
 497  8
                     fis.close();
 498  0
                 } catch (IOException ex) {
 499  0
                     LOGGER.trace("close error", ex);
 500  8
                 }
 501  
             }
 502  
         }
 503  8
     }
 504  
 
 505  
     /**
 506  
      * Merges a new properties file into the current properties. This method allows for the loading of a user provided properties
 507  
      * file.<br/><br/>
 508  
      * Note: even if using this method - system properties will be loaded before properties loaded from files.
 509  
      *
 510  
      * @param stream an Input Stream pointing at a properties file to merge
 511  
      * @throws IOException is thrown when there is an exception loading/merging the properties
 512  
      */
 513  
     public static void mergeProperties(InputStream stream) throws IOException {
 514  8
         localSettings.get().props.load(stream);
 515  8
         logProperties("Properties updated via merge", localSettings.get().props);
 516  8
     }
 517  
 
 518  
     /**
 519  
      * Returns a value from the properties file as a File object. If the value was specified as a system property or passed in via
 520  
      * the -Dprop=value argument - this method will return the value from the system properties before the values in the contained
 521  
      * configuration file.
 522  
      *
 523  
      * @param key the key to lookup within the properties file
 524  
      * @return the property from the properties file converted to a File object
 525  
      */
 526  
     public static File getFile(String key) {
 527  0
         final String file = getString(key);
 528  0
         if (file == null) {
 529  0
             return null;
 530  
         }
 531  0
         return new File(file);
 532  
     }
 533  
 
 534  
     /**
 535  
      * Returns a value from the properties file as a File object. If the value was specified as a system property or passed in via
 536  
      * the -Dprop=value argument - this method will return the value from the system properties before the values in the contained
 537  
      * configuration file.
 538  
      *
 539  
      * This method will check the configured base directory and will use this as the base of the file path. Additionally, if the
 540  
      * base directory begins with a leading "[JAR]\" sequence with the path to the folder containing the JAR file containing this
 541  
      * class.
 542  
      *
 543  
      * @param key the key to lookup within the properties file
 544  
      * @return the property from the properties file converted to a File object
 545  
      */
 546  
     protected static File getDataFile(String key) {
 547  16
         final String file = getString(key);
 548  16
         LOGGER.debug("Settings.getDataFile() - file: '{}'", file);
 549  16
         if (file == null) {
 550  0
             return null;
 551  
         }
 552  16
         if (file.startsWith("[JAR]")) {
 553  0
             LOGGER.debug("Settings.getDataFile() - transforming filename");
 554  0
             final File jarPath = getJarPath();
 555  0
             LOGGER.debug("Settings.getDataFile() - jar file: '{}'", jarPath.toString());
 556  0
             final File retVal = new File(jarPath, file.substring(6));
 557  0
             LOGGER.debug("Settings.getDataFile() - returning: '{}'", retVal.toString());
 558  0
             return retVal;
 559  
         }
 560  16
         return new File(file);
 561  
     }
 562  
 
 563  
     /**
 564  
      * Attempts to retrieve the folder containing the Jar file containing the Settings class.
 565  
      *
 566  
      * @return a File object
 567  
      */
 568  
     private static File getJarPath() {
 569  0
         final String jarPath = Settings.class.getProtectionDomain().getCodeSource().getLocation().getPath();
 570  0
         String decodedPath = ".";
 571  
         try {
 572  0
             decodedPath = URLDecoder.decode(jarPath, "UTF-8");
 573  0
         } catch (UnsupportedEncodingException ex) {
 574  0
             LOGGER.trace("", ex);
 575  0
         }
 576  
 
 577  0
         final File path = new File(decodedPath);
 578  0
         if (path.getName().toLowerCase().endsWith(".jar")) {
 579  0
             return path.getParentFile();
 580  
         } else {
 581  0
             return new File(".");
 582  
         }
 583  
     }
 584  
 
 585  
     /**
 586  
      * Returns a value from the properties file. If the value was specified as a system property or passed in via the -Dprop=value
 587  
      * argument - this method will return the value from the system properties before the values in the contained configuration
 588  
      * file.
 589  
      *
 590  
      * @param key the key to lookup within the properties file
 591  
      * @param defaultValue the default value for the requested property
 592  
      * @return the property from the properties file
 593  
      */
 594  
     public static String getString(String key, String defaultValue) {
 595  32
         final String str = System.getProperty(key, localSettings.get().props.getProperty(key, defaultValue));
 596  32
         return str;
 597  
     }
 598  
 
 599  
     /**
 600  
      * A reference to the temporary directory; used incase it needs to be deleted during cleanup.
 601  
      */
 602  8
     private static File tempDirectory = null;
 603  
 
 604  
     /**
 605  
      * Returns the temporary directory.
 606  
      *
 607  
      * @return the temporary directory
 608  
      * @throws java.io.IOException thrown if the temporary directory does not exist and cannot be created
 609  
      */
 610  
     public static File getTempDirectory() throws IOException {
 611  16
         final File tmpDir = new File(Settings.getString(Settings.KEYS.TEMP_DIRECTORY, System.getProperty("java.io.tmpdir")), "dctemp");
 612  16
         if (!tmpDir.exists()) {
 613  16
             if (!tmpDir.mkdirs()) {
 614  0
                 final String msg = String.format("Unable to make a temporary folder '%s'", tmpDir.getPath());
 615  0
                 throw new IOException(msg);
 616  
             }
 617  
         }
 618  16
         tempDirectory = tmpDir;
 619  16
         return tmpDir;
 620  
     }
 621  
 
 622  
     /**
 623  
      * Returns a value from the properties file. If the value was specified as a system property or passed in via the -Dprop=value
 624  
      * argument - this method will return the value from the system properties before the values in the contained configuration
 625  
      * file.
 626  
      *
 627  
      * @param key the key to lookup within the properties file
 628  
      * @return the property from the properties file
 629  
      */
 630  
     public static String getString(String key) {
 631  128
         return System.getProperty(key, localSettings.get().props.getProperty(key));
 632  
     }
 633  
 
 634  
     /**
 635  
      * Removes a property from the local properties collection. This is mainly used in test cases.
 636  
      *
 637  
      * @param key the property key to remove
 638  
      */
 639  
     public static void removeProperty(String key) {
 640  8
         localSettings.get().props.remove(key);
 641  8
     }
 642  
 
 643  
     /**
 644  
      * Returns an int value from the properties file. If the value was specified as a system property or passed in via the
 645  
      * -Dprop=value argument - this method will return the value from the system properties before the values in the contained
 646  
      * configuration file.
 647  
      *
 648  
      * @param key the key to lookup within the properties file
 649  
      * @return the property from the properties file
 650  
      * @throws InvalidSettingException is thrown if there is an error retrieving the setting
 651  
      */
 652  
     public static int getInt(String key) throws InvalidSettingException {
 653  
         int value;
 654  
         try {
 655  8
             value = Integer.parseInt(Settings.getString(key));
 656  0
         } catch (NumberFormatException ex) {
 657  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 658  8
         }
 659  8
         return value;
 660  
     }
 661  
 
 662  
     /**
 663  
      * Returns an int value from the properties file. If the value was specified as a system property or passed in via the
 664  
      * -Dprop=value argument - this method will return the value from the system properties before the values in the contained
 665  
      * configuration file.
 666  
      *
 667  
      * @param key the key to lookup within the properties file
 668  
      * @param defaultValue the default value to return
 669  
      * @return the property from the properties file or the defaultValue if the property does not exist or cannot be converted to
 670  
      * an integer
 671  
      */
 672  
     public static int getInt(String key, int defaultValue) {
 673  
         int value;
 674  
         try {
 675  0
             value = Integer.parseInt(Settings.getString(key));
 676  0
         } catch (NumberFormatException ex) {
 677  0
             LOGGER.trace("Could not convert property '{}' to an int.", key, ex);
 678  0
             value = defaultValue;
 679  0
         }
 680  0
         return value;
 681  
     }
 682  
 
 683  
     /**
 684  
      * Returns a long value from the properties file. If the value was specified as a system property or passed in via the
 685  
      * -Dprop=value argument - this method will return the value from the system properties before the values in the contained
 686  
      * configuration file.
 687  
      *
 688  
      * @param key the key to lookup within the properties file
 689  
      * @return the property from the properties file
 690  
      * @throws InvalidSettingException is thrown if there is an error retrieving the setting
 691  
      */
 692  
     public static long getLong(String key) throws InvalidSettingException {
 693  
         long value;
 694  
         try {
 695  8
             value = Long.parseLong(Settings.getString(key));
 696  0
         } catch (NumberFormatException ex) {
 697  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 698  8
         }
 699  8
         return value;
 700  
     }
 701  
 
 702  
     /**
 703  
      * Returns a boolean value from the properties file. If the value was specified as a system property or passed in via the
 704  
      * <code>-Dprop=value</code> argument this method will return the value from the system properties before the values in the
 705  
      * contained configuration file.
 706  
      *
 707  
      * @param key the key to lookup within the properties file
 708  
      * @return the property from the properties file
 709  
      * @throws InvalidSettingException is thrown if there is an error retrieving the setting
 710  
      */
 711  
     public static boolean getBoolean(String key) throws InvalidSettingException {
 712  
         boolean value;
 713  
         try {
 714  8
             value = Boolean.parseBoolean(Settings.getString(key));
 715  0
         } catch (NumberFormatException ex) {
 716  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 717  8
         }
 718  8
         return value;
 719  
     }
 720  
 
 721  
     /**
 722  
      * Returns a boolean value from the properties file. If the value was specified as a system property or passed in via the
 723  
      * <code>-Dprop=value</code> argument this method will return the value from the system properties before the values in the
 724  
      * contained configuration file.
 725  
      *
 726  
      * @param key the key to lookup within the properties file
 727  
      * @param defaultValue the default value to return if the setting does not exist
 728  
      * @return the property from the properties file
 729  
      * @throws InvalidSettingException is thrown if there is an error retrieving the setting
 730  
      */
 731  
     public static boolean getBoolean(String key, boolean defaultValue) throws InvalidSettingException {
 732  
         boolean value;
 733  
         try {
 734  8
             final String strValue = Settings.getString(key);
 735  8
             if (strValue == null) {
 736  8
                 return defaultValue;
 737  
             }
 738  0
             value = Boolean.parseBoolean(strValue);
 739  0
         } catch (NumberFormatException ex) {
 740  0
             throw new InvalidSettingException("Could not convert property '" + key + "' to an int.", ex);
 741  0
         }
 742  0
         return value;
 743  
     }
 744  
 
 745  
     /**
 746  
      * Returns a connection string from the configured properties. If the connection string contains a %s, this method will
 747  
      * determine the 'data' directory and replace the %s with the path to the data directory. If the data directory does not
 748  
      * exists it will be created.
 749  
      *
 750  
      * @param connectionStringKey the property file key for the connection string
 751  
      * @param dbFileNameKey the settings key for the db filename
 752  
      * @return the connection string
 753  
      * @throws IOException thrown the data directory cannot be created
 754  
      * @throws InvalidSettingException thrown if there is an invalid setting
 755  
      */
 756  
     public static String getConnectionString(String connectionStringKey, String dbFileNameKey)
 757  
             throws IOException, InvalidSettingException {
 758  16
         final String connStr = Settings.getString(connectionStringKey);
 759  16
         if (connStr == null) {
 760  8
             final String msg = String.format("Invalid properties file; %s is missing.", connectionStringKey);
 761  8
             throw new InvalidSettingException(msg);
 762  
         }
 763  8
         if (connStr.contains("%s")) {
 764  8
             final File directory = getDataDirectory();
 765  8
             String fileName = null;
 766  8
             if (dbFileNameKey != null) {
 767  8
                 fileName = Settings.getString(dbFileNameKey);
 768  
             }
 769  8
             if (fileName == null) {
 770  0
                 final String msg = String.format("Invalid properties file to get a file based connection string; '%s' must be defined.",
 771  
                         dbFileNameKey);
 772  0
                 throw new InvalidSettingException(msg);
 773  
             }
 774  8
             if (connStr.startsWith("jdbc:h2:file:") && fileName.endsWith(".h2.db")) {
 775  8
                 fileName = fileName.substring(0, fileName.length() - 6);
 776  
             }
 777  
             // yes, for H2 this path won't actually exists - but this is sufficient to get the value needed
 778  8
             final File dbFile = new File(directory, fileName);
 779  8
             final String cString = String.format(connStr, dbFile.getCanonicalPath());
 780  8
             LOGGER.debug("Connection String: '{}'", cString);
 781  8
             return cString;
 782  
         }
 783  0
         return connStr;
 784  
     }
 785  
 
 786  
     /**
 787  
      * Retrieves the directory that the JAR file exists in so that we can ensure we always use a common data directory for the
 788  
      * embedded H2 database. This is public solely for some unit tests; otherwise this should be private.
 789  
      *
 790  
      * @return the data directory to store data files
 791  
      * @throws IOException is thrown if an IOException occurs of course...
 792  
      */
 793  
     public static File getDataDirectory() throws IOException {
 794  8
         final File path = Settings.getDataFile(Settings.KEYS.DATA_DIRECTORY);
 795  8
         if (path.exists() || path.mkdirs()) {
 796  8
             return path;
 797  
         }
 798  0
         throw new IOException(String.format("Unable to create the data directory '%s'", path.getAbsolutePath()));
 799  
     }
 800  
 }