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