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