Class Configuration
-
Method Summary
Modifier and TypeMethodDescriptionstatic ConfigurationBuilderbuilder()Create a new Builder instance.static ConfigurationcopyWithNewPrefix(Configuration oldConfig, String newPrefix) Creates a copy of a configuration with just the prefix set to a new value.static ConfigurationCreates a configuration with all values set to default.voidLet this instance write human-readable information about every option that is used to the given stream.voidenableLogging(LogManager pLogger) static voidEnable a secure mode, i.e., allow only injection of configuration options marked as secure.static ConfigurationfromCmdLineArguments(String[] args) Construct a configuration object from the array of command line arguments.static Map<Class<?>,TypeConverter> Get the map of registered defaultTypeConverters.@Nullable StringgetProperty(String key) Deprecated.booleanhasProperty(String key) Deprecated.voidInject the values of configuration options into an object.voidUse this method if the calling class is likely to be sub-classed, so that the options of the calling class get injected, not the options of the dynamic class type of the object.<T> voidinjectWithDefaults(T obj, Class<T> cls, T defaultsObject) Same asinject(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 givendefaultsInstanceinstead of the value that is set as default in the to-be-injected object.voidrecursiveInject(Object obj) Callinject(Object, Class)for this object with its actual class and all super class that have anOptionsannotation.toString()
-
Method Details
-
builder
Create a new Builder instance. -
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
Creates a configuration with all values set to default. -
copyWithNewPrefix
Creates a copy of a configuration with just the prefix set to a new value. -
getDefaultConverters
Get the map of registered defaultTypeConverters. These type converters are used whenever a new Configuration instance is created, except when theConfigurationBuilder.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 aConfigurationinstance.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
-
dumpUsedOptionsTo
Let this instance write human-readable information about every option that is used to the given stream. -
getProperty
Deprecated.Get the value of an option. USE OF THIS METHOD IS NOT RECOMMENDED!Use configuration injection with
Optionandinject(Object)instead. This provides type safety, documentation, logging etc. -
hasProperty
Deprecated.Check whether an option has a specified value. USE OF THIS METHOD IS NOT RECOMMENDED!Use configuration injection with
Optionandinject(Object)instead. This provides type safety, documentation, logging, default values, etc. -
getUnusedProperties
-
getDeprecatedProperties
-
asPropertiesString
-
inject
Inject the values of configuration options into an object. The class of the object has to have aOptionsannotation, and each field to set / method to call has to have aOptionannotation.Supported types for configuration options:
- all primitive types and their wrapper types
- all enum types
Stringand arrays of itFileandPath(the fieldFileOption.value()is required in this case!)Class<Something>CharsetLevelPattern- arbitrary factory interfaces as supported by
Classes.createFactory(TypeToken, Class) - arrays of the above types
AnnotatedValuewith types of the above as value type (users can specify an annotation string after a "::" separator)- collection types
Iterable,Collection,List,Set,SortedSet,Multiset, andEnumSetof the above types
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.
-
inject
Use this method if the calling class is likely to be sub-classed, so that the options of the calling class get injected, not the options of the dynamic class type of the object.- Parameters:
cls- The static class type of the object to inject.- Throws:
InvalidConfigurationException- See Also:
-
injectWithDefaults
public <T> void injectWithDefaults(T obj, Class<T> cls, T defaultsObject) throws InvalidConfigurationException Same asinject(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 givendefaultsInstanceinstead 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
-
recursiveInject
Callinject(Object, Class)for this object with its actual class and all super class that have anOptionsannotation.- Parameters:
obj- The object in which the configuration options should be injected.- Throws:
InvalidConfigurationException- If the user specified configuration is wrong.
-
toString
-
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
Configurationinstance - Throws:
InvalidConfigurationException- On incorrect format or when configuration options for Configurations class are invalid
-