Class Config

  • All Implemented Interfaces:
    Serializable

    public final class Config
    extends Object
    implements Serializable

    A complete configuration class that allows for inheritance, multiline inputs, variable interpolation, and scripting.

    • ${var} will substitute the value of "var"

    Author:
    David B. Bracewell
    See Also:
    Serialized Form
    • Constructor Detail

      • Config

        public Config()
    • Method Detail

      • clear

        public static void clear()
        Clears all set properties
      • findKey

        public static String findKey​(Class<?> root,
                                     Object... path)
        Finds the best key for the given class and child path. If a language is specified, it should be the first argument of the path.
        Parameters:
        root - the root
        path - the path
        Returns:
        the best matching key or null
      • findKey

        public static String findKey​(String root,
                                     Object... path)
        Finds the best key for the given class and child path. If a language is specified, it should be the first argument of the path.
        Parameters:
        root - the root
        path - the path
        Returns:
        the best matching key or null
      • fromJson

        public static Config fromJson​(JsonEntry entry,
                                      Type... params)
        Static method allowing the Config to be deserialized from Json
        Parameters:
        entry - the Json entry
        params - The type parameters (not used)
        Returns:
        the Config instance
      • get

        public static Val get​(Class<?> clazz,
                              Object... propertyComponents)
        Gets the value of a property for a given class
        Parameters:
        clazz - The class
        propertyComponents - The components
        Returns:
        The value associated with clazz.propertyName
      • get

        public static Val get​(String propertyPrefix,
                              Object... propertyComponents)
        Gets the value of a property for a given class and language (the language is optional)
        Parameters:
        propertyPrefix - The prefix
        propertyComponents - The components.
        Returns:
        The value associated with clazz.propertyName.language exists or clazz.propertyName
      • getDefaultClassLoader

        public static ClassLoader getDefaultClassLoader()
        Gets default class loader.
        Returns:
        the default class loader
      • setDefaultClassLoader

        public static void setDefaultClassLoader​(ClassLoader newDefaultClassLoader)
        Sets the default ClassLoader to use with Classpath resources
        Parameters:
        newDefaultClassLoader - The new ClassLoader
      • getInstance

        public static Config getInstance()
        Gets the singleton instance of the config. This should not normally be used by api consumers
        Returns:
        the singleton instance of the config.
      • setInstance

        public static Config setInstance​(Config config)
        Sets the singleton instance of the config. This is mainly useful in distributed environments where we are passing configurations around.
        Parameters:
        config - the config that will become the single instance
        Returns:
        the singleton instance of the config
      • getPropertiesMatching

        public static List<String> getPropertiesMatching​(Predicate<? super String> matcher)

        Gets a list of properties whose name is matched using the supplied StringMatcher.

        Parameters:
        matcher - The StringMatcher to use for matching property names.
        Returns:
        A List of properties matched using the StringMatcher.
      • hasProperty

        public static boolean hasProperty​(String root,
                                          Object... path)
        Checks if a property is in the config or or set on the system. The property name is constructed as propertyPrefix + . + propertyComponent[0] + . + propertyComponent[1] + ...
        Parameters:
        root - The prefix
        path - The components.
        Returns:
        True if the property is known, false if not.
      • hasProperty

        public static boolean hasProperty​(Class<?> root,
                                          Object... path)
        Checks if a property is in the config or or set on the system. The property name is constructed as clazz.getName() + . + propertyComponent[0] + . + propertyComponent[1] + ...
        Parameters:
        root - The class with which the property is associated.
        path - The components.
        Returns:
        True if the property is known, false if not.
      • initialize

        public static String[] initialize​(String programName,
                                          String[] args,
                                          CommandLineParser parser,
                                          String... otherPackages)

        Initializes the configuration.

        Looks for a properties (programName.conf) in the classpath, the user home directory, and in the run directory to load

        The command line arguments are parsed and added to the configuration.

        Parameters:
        programName - the program name
        args - the command line arguments
        parser - the to use for parsing the arguments
        otherPackages - Other packages whose configs we should load
        Returns:
        Non config/option parameters from command line
      • initialize

        public static String[] initialize​(String programName,
                                          String[] args,
                                          String... otherPackages)

        Initializes the configuration.

        Looks for a properties (programName.conf) in the classpath, the user home directory, and in the run directory to load

        The command line arguments are parsed and added to the configuration.

        Parameters:
        programName - the program name
        args - the command line arguments
        otherPackages - Other packages whose configs we should load
        Returns:
        Non config/option parameters from command line
      • initializeTest

        public static void initializeTest()
        Initialize test.
      • isBean

        public static boolean isBean​(String property)
        Is bean boolean.
        Parameters:
        property - the property
        Returns:
        the boolean
      • isConfigLoaded

        public static boolean isConfigLoaded​(Resource configResource)
        Determines if a given configuration resource has been loaded
        Parameters:
        configResource - the config resource to check.
        Returns:
        True if the config has been loaded, False if not
      • loadApplicationConfig

        public static void loadApplicationConfig​(String applicationName)
      • loadConfig

        public static void loadConfig​(Resource resource)
        Loads a config file
        Parameters:
        resource - The config file
      • loadPackageConfig

        public static void loadPackageConfig​(String packageName)
        Load package config.
        Parameters:
        packageName - the package name
      • resolveVariables

        public static String resolveVariables​(String string)
        Resolve variables string.
        Parameters:
        string - the string
        Returns:
        the string
      • setAllCommandLine

        public static void setAllCommandLine​(String[] args)
        Sets all properties from the given array of arguments
        Parameters:
        args - the args
      • setAllCommandLine

        public static void setAllCommandLine​(CommandLineParser parser)
        Sets all properties from the given command line parser.
        Parameters:
        parser - the parser
      • setProperty

        public static void setProperty​(@NonNull
                                       @NonNull String name,
                                       Object value)
      • setProperty

        public static void setProperty​(@NonNull
                                       @NonNull String name,
                                       String value)
        Sets the value of a property.
        Parameters:
        name - the name of the property
        value - the value of the property