Class Settings


  • public final class Settings
    extends java.lang.Object
    A simple settings container that wraps the dependencycheck.properties file.
    Version:
    $Id: $Id
    Author:
    Jeremy Long
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Settings.KEYS
      The collection of keys used within the properties file.
    • Constructor Summary

      Constructors 
      Constructor Description
      Settings()
      Initialize the settings object.
      Settings​(@NotNull java.lang.String propertiesFilePath)
      Initialize the settings object using the given properties file.
      Settings​(java.util.Properties properties)
      Initialize the settings object using the given properties.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cleanup()
      Cleans up resources to prevent memory leaks.
      void cleanup​(boolean deleteTemporary)
      Cleans up resources to prevent memory leaks.
      java.lang.String[] getArray​(@NotNull java.lang.String key)
      Returns a list with the given key.
      boolean getBoolean​(@NotNull java.lang.String key)
      Returns a boolean value from the properties file.
      boolean getBoolean​(@NotNull java.lang.String key, boolean defaultValue)
      Returns a boolean value from the properties file.
      java.lang.String getConnectionString​(java.lang.String connectionStringKey, java.lang.String dbFileNameKey)
      Returns a connection string from the configured properties.
      java.io.File getDataDirectory()
      Retrieves the primary data directory that is used for caching web content.
      @Nullable java.io.File getFile​(@NotNull java.lang.String key)
      Returns a value from the properties file as a File object.
      float getFloat​(@NotNull java.lang.String key, float defaultValue)
      Returns a float value from the properties file.
      java.io.File getH2DataDirectory()
      Retrieves the H2 data directory - if the database has been moved to the temp directory this method will return the temp directory.
      int getInt​(@NotNull java.lang.String key)
      Returns an int value from the properties file.
      int getInt​(@NotNull java.lang.String key, int defaultValue)
      Returns an int value from the properties file.
      long getLong​(@NotNull java.lang.String key)
      Returns a long value from the properties file.
      java.lang.String getString​(@NotNull java.lang.String key)
      Returns a value from the properties file.
      java.lang.String getString​(@NotNull java.lang.String key, @Nullable java.lang.String defaultValue)
      Returns a value from the properties file.
      java.io.File getTempDirectory()
      Returns the temporary directory.
      java.io.File getTempFile​(@NotNull java.lang.String prefix, @NotNull java.lang.String extension)
      Generates a new temporary file name that is guaranteed to be unique.
      void mergeProperties​(@NotNull java.io.File filePath)
      Merges a new properties file into the current properties.
      void mergeProperties​(@NotNull java.io.InputStream stream)
      Merges a new properties file into the current properties.
      void mergeProperties​(@NotNull java.lang.String filePath)
      Merges a new properties file into the current properties.
      void removeProperty​(@NotNull java.lang.String key)
      Removes a property from the local properties collection.
      void setArrayIfNotEmpty​(@NotNull java.lang.String key, @Nullable java.lang.String[] value)
      Sets a property value only if the array value is not null and not empty.
      void setArrayIfNotEmpty​(@NotNull java.lang.String key, @Nullable java.util.List<java.lang.String> value)
      Sets a property value only if the array value is not null and not empty.
      void setBoolean​(@NotNull java.lang.String key, boolean value)
      Sets a property value.
      void setBooleanIfNotNull​(@NotNull java.lang.String key, @Nullable java.lang.Boolean value)
      Sets a property value only if the value is not null.
      void setFloat​(@NotNull java.lang.String key, float value)
      Sets a float property value.
      void setInt​(@NotNull java.lang.String key, int value)
      Sets a property value.
      void setIntIfNotNull​(@NotNull java.lang.String key, @Nullable java.lang.Integer value)
      Sets a property value only if the value is not null.
      void setString​(@NotNull java.lang.String key, @NotNull java.lang.String value)
      Sets a property value.
      void setStringIfNotEmpty​(@NotNull java.lang.String key, @Nullable java.lang.String value)
      Sets a property value only if the value is not null and not empty.
      void setStringIfNotNull​(@NotNull java.lang.String key, @Nullable java.lang.String value)
      Sets a property value only if the value is not null.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Settings

        public Settings()
        Initialize the settings object.
      • Settings

        public Settings​(java.util.Properties properties)
        Initialize the settings object using the given properties.
        Parameters:
        properties - the properties to be used with this Settings instance
        Since:
        4.0.3
      • Settings

        public Settings​(@NotNull
                        @NotNull java.lang.String propertiesFilePath)
        Initialize the settings object using the given properties file.
        Parameters:
        propertiesFilePath - the path to the base properties file to load
    • Method Detail

      • cleanup

        public void cleanup()
        Cleans up resources to prevent memory leaks.
      • cleanup

        public void cleanup​(boolean deleteTemporary)
        Cleans up resources to prevent memory leaks.
        Parameters:
        deleteTemporary - flag indicating whether any temporary directories generated should be removed
      • setString

        public void setString​(@NotNull
                              @NotNull java.lang.String key,
                              @NotNull
                              @NotNull java.lang.String value)
        Sets a property value.
        Parameters:
        key - the key for the property
        value - the value for the property
      • setStringIfNotNull

        public void setStringIfNotNull​(@NotNull
                                       @NotNull java.lang.String key,
                                       @Nullable
                                       @Nullable java.lang.String value)
        Sets a property value only if the value is not null.
        Parameters:
        key - the key for the property
        value - the value for the property
      • setStringIfNotEmpty

        public void setStringIfNotEmpty​(@NotNull
                                        @NotNull java.lang.String key,
                                        @Nullable
                                        @Nullable java.lang.String value)
        Sets a property value only if the value is not null and not empty.
        Parameters:
        key - the key for the property
        value - the value for the property
      • setArrayIfNotEmpty

        public void setArrayIfNotEmpty​(@NotNull
                                       @NotNull java.lang.String key,
                                       @Nullable
                                       @Nullable java.lang.String[] value)
        Sets a property value only if the array value is not null and not empty.
        Parameters:
        key - the key for the property
        value - the value for the property
      • setArrayIfNotEmpty

        public void setArrayIfNotEmpty​(@NotNull
                                       @NotNull java.lang.String key,
                                       @Nullable
                                       @Nullable java.util.List<java.lang.String> value)
        Sets a property value only if the array value is not null and not empty.
        Parameters:
        key - the key for the property
        value - the value for the property
      • setBoolean

        public void setBoolean​(@NotNull
                               @NotNull java.lang.String key,
                               boolean value)
        Sets a property value.
        Parameters:
        key - the key for the property
        value - the value for the property
      • setBooleanIfNotNull

        public void setBooleanIfNotNull​(@NotNull
                                        @NotNull java.lang.String key,
                                        @Nullable
                                        @Nullable java.lang.Boolean value)
        Sets a property value only if the value is not null.
        Parameters:
        key - the key for the property
        value - the value for the property
      • setFloat

        public void setFloat​(@NotNull
                             @NotNull java.lang.String key,
                             float value)
        Sets a float property value.
        Parameters:
        key - the key for the property
        value - the value for the property
      • setInt

        public void setInt​(@NotNull
                           @NotNull java.lang.String key,
                           int value)
        Sets a property value.
        Parameters:
        key - the key for the property
        value - the value for the property
      • setIntIfNotNull

        public void setIntIfNotNull​(@NotNull
                                    @NotNull java.lang.String key,
                                    @Nullable
                                    @Nullable java.lang.Integer value)
        Sets a property value only if the value is not null.
        Parameters:
        key - the key for the property
        value - the value for the property
      • mergeProperties

        public void mergeProperties​(@NotNull
                                    @NotNull java.io.File filePath)
                             throws java.io.FileNotFoundException,
                                    java.io.IOException
        Merges a new properties file into the current properties. This method allows for the loading of a user provided properties file.

        Note: even if using this method - system properties will be loaded before properties loaded from files.
        Parameters:
        filePath - the path to the properties file to merge.
        Throws:
        java.io.FileNotFoundException - is thrown when the filePath points to a non-existent file
        java.io.IOException - is thrown when there is an exception loading/merging the properties
      • mergeProperties

        public void mergeProperties​(@NotNull
                                    @NotNull java.lang.String filePath)
                             throws java.io.FileNotFoundException,
                                    java.io.IOException
        Merges a new properties file into the current properties. This method allows for the loading of a user provided properties file.

        Note: even if using this method - system properties will be loaded before properties loaded from files.
        Parameters:
        filePath - the path to the properties file to merge.
        Throws:
        java.io.FileNotFoundException - is thrown when the filePath points to a non-existent file
        java.io.IOException - is thrown when there is an exception loading/merging the properties
      • mergeProperties

        public void mergeProperties​(@NotNull
                                    @NotNull java.io.InputStream stream)
                             throws java.io.IOException
        Merges a new properties file into the current properties. This method allows for the loading of a user provided properties file.

        Note: even if using this method - system properties will be loaded before properties loaded from files.
        Parameters:
        stream - an Input Stream pointing at a properties file to merge
        Throws:
        java.io.IOException - is thrown when there is an exception loading/merging the properties
      • getFile

        @Nullable
        public @Nullable java.io.File getFile​(@NotNull
                                              @NotNull java.lang.String key)
        Returns a value from the properties file as a File object. If the value was specified as a system property or passed in via the -Dprop=value argument - this method will return the value from the system properties before the values in the contained configuration file.
        Parameters:
        key - the key to lookup within the properties file
        Returns:
        the property from the properties file converted to a File object
      • getString

        public java.lang.String getString​(@NotNull
                                          @NotNull java.lang.String key,
                                          @Nullable
                                          @Nullable java.lang.String defaultValue)
        Returns a value from the properties file. If the value was specified as a system property or passed in via the -Dprop=value argument - this method will return the value from the system properties before the values in the contained configuration file.
        Parameters:
        key - the key to lookup within the properties file
        defaultValue - the default value for the requested property
        Returns:
        the property from the properties file
      • getTempDirectory

        public java.io.File getTempDirectory()
                                      throws java.io.IOException
        Returns the temporary directory.
        Returns:
        the temporary directory
        Throws:
        java.io.IOException - if any.
      • getString

        public java.lang.String getString​(@NotNull
                                          @NotNull java.lang.String key)
        Returns a value from the properties file. If the value was specified as a system property or passed in via the -Dprop=value argument - this method will return the value from the system properties before the values in the contained configuration file.
        Parameters:
        key - the key to lookup within the properties file
        Returns:
        the property from the properties file
      • getArray

        public java.lang.String[] getArray​(@NotNull
                                           @NotNull java.lang.String key)
        Returns a list with the given key.

        If the property is not set then null will be returned.

        Parameters:
        key - the key to get from this Settings.
        Returns:
        the list or null if the key wasn't present.
      • removeProperty

        public void removeProperty​(@NotNull
                                   @NotNull java.lang.String key)
        Removes a property from the local properties collection. This is mainly used in test cases.
        Parameters:
        key - the property key to remove
      • getInt

        public int getInt​(@NotNull
                          @NotNull java.lang.String key)
                   throws InvalidSettingException
        Returns an int value from the properties file. If the value was specified as a system property or passed in via the -Dprop=value argument - this method will return the value from the system properties before the values in the contained configuration file.
        Parameters:
        key - the key to lookup within the properties file
        Returns:
        the property from the properties file
        Throws:
        InvalidSettingException - is thrown if there is an error retrieving the setting
      • getInt

        public int getInt​(@NotNull
                          @NotNull java.lang.String key,
                          int defaultValue)
        Returns an int value from the properties file. If the value was specified as a system property or passed in via the -Dprop=value argument - this method will return the value from the system properties before the values in the contained configuration file.
        Parameters:
        key - the key to lookup within the properties file
        defaultValue - the default value to return
        Returns:
        the property from the properties file or the defaultValue if the property does not exist or cannot be converted to an integer
      • getLong

        public long getLong​(@NotNull
                            @NotNull java.lang.String key)
                     throws InvalidSettingException
        Returns a long value from the properties file. If the value was specified as a system property or passed in via the -Dprop=value argument - this method will return the value from the system properties before the values in the contained configuration file.
        Parameters:
        key - the key to lookup within the properties file
        Returns:
        the property from the properties file
        Throws:
        InvalidSettingException - is thrown if there is an error retrieving the setting
      • getBoolean

        public boolean getBoolean​(@NotNull
                                  @NotNull java.lang.String key)
                           throws InvalidSettingException
        Returns a boolean value from the properties file. If the value was specified as a system property or passed in via the -Dprop=value argument this method will return the value from the system properties before the values in the contained configuration file.
        Parameters:
        key - the key to lookup within the properties file
        Returns:
        the property from the properties file
        Throws:
        InvalidSettingException - is thrown if there is an error retrieving the setting
      • getBoolean

        public boolean getBoolean​(@NotNull
                                  @NotNull java.lang.String key,
                                  boolean defaultValue)
        Returns a boolean value from the properties file. If the value was specified as a system property or passed in via the -Dprop=value argument this method will return the value from the system properties before the values in the contained configuration file.
        Parameters:
        key - the key to lookup within the properties file
        defaultValue - the default value to return if the setting does not exist
        Returns:
        the property from the properties file
      • getFloat

        public float getFloat​(@NotNull
                              @NotNull java.lang.String key,
                              float defaultValue)
        Returns a float value from the properties file. If the value was specified as a system property or passed in via the -Dprop=value argument this method will return the value from the system properties before the values in the contained configuration file.
        Parameters:
        key - the key to lookup within the properties file
        defaultValue - the default value to return if the setting does not exist
        Returns:
        the property from the properties file
      • getConnectionString

        public java.lang.String getConnectionString​(java.lang.String connectionStringKey,
                                                    java.lang.String dbFileNameKey)
                                             throws java.io.IOException,
                                                    InvalidSettingException
        Returns a connection string from the configured properties. If the connection string contains a %s, this method will determine the 'data' directory and replace the %s with the path to the data directory. If the data directory does not exist it will be created.
        Parameters:
        connectionStringKey - the property file key for the connection string
        dbFileNameKey - the settings key for the db filename
        Returns:
        the connection string
        Throws:
        java.io.IOException - thrown the data directory cannot be created
        InvalidSettingException - thrown if there is an invalid setting
      • getDataDirectory

        public java.io.File getDataDirectory()
                                      throws java.io.IOException
        Retrieves the primary data directory that is used for caching web content.
        Returns:
        the data directory to store data files
        Throws:
        java.io.IOException - is thrown if an java.io.IOException occurs of course...
      • getH2DataDirectory

        public java.io.File getH2DataDirectory()
                                        throws java.io.IOException
        Retrieves the H2 data directory - if the database has been moved to the temp directory this method will return the temp directory.
        Returns:
        the data directory to store data files
        Throws:
        java.io.IOException - is thrown if an java.io.IOException occurs of course...
      • getTempFile

        public java.io.File getTempFile​(@NotNull
                                        @NotNull java.lang.String prefix,
                                        @NotNull
                                        @NotNull java.lang.String extension)
                                 throws java.io.IOException
        Generates a new temporary file name that is guaranteed to be unique.
        Parameters:
        prefix - the prefix for the file name to generate
        extension - the extension of the generated file name
        Returns:
        a temporary File
        Throws:
        java.io.IOException - if any.