Class Configuration


  • public final class Configuration
    extends Object
    Immutable wrapper around a map with properties, providing useful access helper methods.
    • Method Detail

      • enableSecureModeGlobally

        public static void enableSecureModeGlobally()
        Enable a secure mode, i.e., allow only injection of configuration options marked as secure. Once enabled, this can not be disabled.
      • defaultConfiguration

        public static Configuration defaultConfiguration()
        Creates a configuration with all values set to default.
      • copyWithNewPrefix

        public static Configuration copyWithNewPrefix​(Configuration oldConfig,
                                                      String newPrefix)
        Creates a copy of a configuration with just the prefix set to a new value.
      • getDefaultConverters

        public static Map<Class<?>,​TypeConverter> getDefaultConverters()
        Get the map of registered default TypeConverters. These type converters are used whenever a new Configuration instance is created, except when the ConfigurationBuilder.copyFrom(Configuration) method is used.

        For all instances in this map the method TypeConverter.getInstanceForNewConfiguration(Configuration) will be called before the type converter is actually added to a Configuration instance.

        The returned map is mutable and changes have immediate effect on this class! Callers are free to add and remove mappings as they wish. However, as this is static state, this will affect all other callers as well! Thus, it should be used only with caution, for example to add default type converters in a large project at startup. It is discouraged to change this map, if the same effect can easily be achieved using ConfigurationBuilder.addConverter(Class, TypeConverter).

        Returns:
        A reference to the map of type converters used by this class.
      • enableLogging

        public void enableLogging​(LogManager pLogger)
      • dumpUsedOptionsTo

        public void dumpUsedOptionsTo​(PrintStream out)
        Let this instance write human-readable information about every option that is used to the given stream.
      • getProperty

        @Deprecated
        public @Nullable String getProperty​(String key)
        Deprecated.
        Get the value of an option. USE OF THIS METHOD IS NOT RECOMMENDED!

        Use configuration injection with Option and inject(Object) instead. This provides type safety, documentation, logging etc.

      • hasProperty

        @Deprecated
        public boolean hasProperty​(String key)
        Deprecated.
        Check whether an option has a specified value. USE OF THIS METHOD IS NOT RECOMMENDED!

        Use configuration injection with Option and inject(Object) instead. This provides type safety, documentation, logging, default values, etc.

      • getUnusedProperties

        public Set<String> getUnusedProperties()
      • getDeprecatedProperties

        public Set<String> getDeprecatedProperties()
      • asPropertiesString

        public String asPropertiesString()
      • inject

        public void inject​(Object obj)
                    throws InvalidConfigurationException
        Inject the values of configuration options into an object. The class of the object has to have a Options annotation, and each field to set / method to call has to have a Option annotation.

        Supported types for configuration options:

        For the collection types an immutable instance will be created and injected. Their type parameter has to be one of the other supported types. For collection types and arrays the values of the configuration option are assumed to be comma separated.

        Parameters:
        obj - The object in which the configuration options should be injected.
        Throws:
        InvalidConfigurationException - If the user specified configuration is wrong.
      • injectWithDefaults

        public <T> void injectWithDefaults​(T obj,
                                           Class<T> cls,
                                           T defaultsObject)
                                    throws InvalidConfigurationException
        Same as inject(Object, Class), but if this Configuration instance does not contain a value for a requested configuration option, use the value that is set in the given defaultsInstance instead of the value that is set as default in the to-be-injected object. This can be used to create a copy of an object but with some options changed according to this Configuration instance.

        Note that this only works for configuration options that are specified as fields, not for those specified as setters.

        Parameters:
        obj - The to-be-injected instance.
        cls - The static class type of the object to inject.
        defaultsObject - The instance from which default values should be read.
        Throws:
        InvalidConfigurationException
      • fromCmdLineArguments

        public static Configuration fromCmdLineArguments​(String[] args)
                                                  throws InvalidConfigurationException
        Construct a configuration object from the array of command line arguments.

        The input format is as follows:

         
           --option=Value
         
         
        Parameters:
        args - Command line arguments
        Returns:
        Constructed Configuration instance
        Throws:
        InvalidConfigurationException - On incorrect format or when configuration options for Configurations class are invalid