Class PropertiesLoader

java.lang.Object
org.codice.ddf.platform.util.properties.PropertiesLoader

public final class PropertiesLoader extends Object
Utility class that attempts several different methods for loading in properties files from the classpath or file system. The strategies are attempted in the following order:
  1. Spring (default class loader) uses utilities found in Spring-Core to load properties.
  2. Spring (given class loader) does the same as above, but with respect to the given class loader.
  3. Direct file system loading; useful when the properties have a fully-qualified absolute path. Relative paths are also valid, and loading attempts path resolution using (in order):
    1. The relative path prepended with the karaf.home property
    2. The relative path prepended with the ddf.home property
    3. The relative path itself, without any modification
  4. Resource loading using Java's built in resource system
Note that the first successful strategy is the one whose results are non-empty, and no further strategies will be attempted thereafter. Property placeholders are always substituted after the loading has finished.

If all strategies fail, then the returned Properties object will be empty.

  • Method Details

    • getInstance

      public static PropertiesLoader getInstance()
    • toMap

      public <K, V> Map<K,V> toMap(Properties properties)
      Converts an instance of Properties to an instance of a Map, which is the preferred API for working with key-value collections.
      Type Parameters:
      K - the object type of the key in the returned map
      V - the object type of the value in the returned map
      Parameters:
      properties - the properties object whose elements should be in the resultant map
      Returns:
      a new map with all elements of the given properties, or empty if the properties were null
    • loadProperties

      public Properties loadProperties(String propertiesFile)
      Load properties from a file with no classloader specified.
      Parameters:
      propertiesFile - the resource name or the file path of the properties file
      Returns:
      Properties deserialized from the specified file, or empty if the load failed
    • loadProperties

      public Properties loadProperties(String propertiesFile, ClassLoader classLoader)
      Will attempt to load properties from a file using the given classloader. If that fails, several other methods will be tried until the properties file is located.
      Parameters:
      propertiesFile - the resource name or the file path of the properties file
      classLoader - the class loader with access to the properties file
      Returns:
      Properties deserialized from the specified file, or empty if the load failed
    • loadPropertiesWithoutSystemPropertySubstitution

      public Properties loadPropertiesWithoutSystemPropertySubstitution(String propertiesFile, ClassLoader classLoader)
      Will attempt to load properties from a file using the given classloader without replacing the system properties with their value. If that fails, several other methods will be tried until the properties file is located.
      Parameters:
      propertiesFile - the resource name or the file path of the properties file
      classLoader - the class loader with access to the properties file
      Returns:
      Properties deserialized from the specified file, or empty if the load failed