Class Settings

java.lang.Object
org.owasp.dependencycheck.utils.Settings

public final class Settings extends 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 final class 
    The collection of keys used within the properties file.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initialize the settings object.
    Settings(@NonNull String propertiesFilePath)
    Initialize the settings object using the given properties file.
    Settings(Properties properties)
    Initialize the settings object using the given properties.
  • Method Summary

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

    Methods inherited from class Object

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

    • Settings

      public Settings()
      Initialize the settings object.
    • Settings

      public Settings(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(@NonNull String propertiesFilePath)
      Initialize the settings object using the given properties file.
      Parameters:
      propertiesFilePath - the path to the base properties file to load
  • Method Details

    • 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(@NonNull String key, @NonNull String value)
      Sets a property value.
      Parameters:
      key - the key for the property
      value - the value for the property
    • setStringIfNotNull

      public void setStringIfNotNull(@NonNull String key, @Nullable 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(@NonNull String key, @Nullable 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(@NonNull String key, @Nullable 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(@NonNull String key, @Nullable List<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(@NonNull 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(@NonNull String key, @Nullable 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(@NonNull 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(@NonNull 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(@NonNull String key, @Nullable 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(@NonNull File filePath) throws FileNotFoundException, 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:
      FileNotFoundException - is thrown when the filePath points to a non-existent file
      IOException - is thrown when there is an exception loading/merging the properties
    • mergeProperties

      public void mergeProperties(@NonNull String filePath) throws FileNotFoundException, 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:
      FileNotFoundException - is thrown when the filePath points to a non-existent file
      IOException - is thrown when there is an exception loading/merging the properties
    • mergeProperties

      public void mergeProperties(@NonNull InputStream stream) throws 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:
      IOException - is thrown when there is an exception loading/merging the properties
    • getFile

      public @Nullable File getFile(@NonNull 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 String getString(@NonNull String key, @Nullable 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 File getTempDirectory() throws IOException
      Returns the temporary directory.
      Returns:
      the temporary directory
      Throws:
      IOException - if any.
    • getString

      public String getString(@NonNull 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 String[] getArray(@NonNull 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(@NonNull 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(@NonNull 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(@NonNull 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(@NonNull 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
    • getLong

      public long getLong(@NonNull String key, long defaultValue)
      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
      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
    • getBoolean

      public boolean getBoolean(@NonNull 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(@NonNull 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(@NonNull 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 String getConnectionString(String connectionStringKey, String dbFileNameKey) throws 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:
      IOException - thrown the data directory cannot be created
      InvalidSettingException - thrown if there is an invalid setting
    • getDataDirectory

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

      public File getH2DataDirectory() throws 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:
      IOException - is thrown if an java.io.IOException occurs of course...
    • getTempFile

      public File getTempFile(@NonNull String prefix, @NonNull String extension) throws 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:
      IOException - if any.