Package com.gengoai.config
Class Config
- java.lang.Object
-
- com.gengoai.config.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
-
-
Field Summary
Fields Modifier and Type Field Description static String
CONF_EXTENSION
File extensions for config files.
-
Constructor Summary
Constructors Constructor Description Config()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
clear()
Clears all set propertiesstatic String
findKey(Class<?> root, Object... path)
Finds the best key for the given class and child path.static String
findKey(String root, Object... path)
Finds the best key for the given class and child path.static Config
fromJson(JsonEntry entry, Type... params)
Static method allowing the Config to be deserialized from Jsonstatic Val
get(Class<?> clazz, Object... propertyComponents)
Gets the value of a property for a given classstatic Val
get(String propertyPrefix, Object... propertyComponents)
Gets the value of a property for a given class and language (the language is optional)static ClassLoader
getDefaultClassLoader()
Gets default class loader.static Config
getInstance()
Gets the singleton instance of the config.static List<String>
getPropertiesMatching(Predicate<? super String> matcher)
Gets alist
of properties whose name is matched using the suppliedStringMatcher
.static boolean
hasProperty(Class<?> root, Object... path)
Checks if a property is in the config or or set on the system.static boolean
hasProperty(String root, Object... path)
Checks if a property is in the config or or set on the system.static String[]
initialize(String programName, String[] args, CommandLineParser parser, String... otherPackages)
Initializes the configuration.static String[]
initialize(String programName, String[] args, String... otherPackages)
Initializes the configuration.static void
initializeTest()
Initialize test.static boolean
isBean(String property)
Is bean boolean.static boolean
isConfigLoaded(Resource configResource)
Determines if a given configuration resource has been loadedstatic void
loadApplicationConfig(String applicationName)
static void
loadConfig(Resource resource)
Loads a config filestatic void
loadPackageConfig(String packageName)
Load package config.static String
resolveVariables(String string)
Resolve variables string.static void
setAllCommandLine(CommandLineParser parser)
Sets all properties from the given command line parser.static void
setAllCommandLine(String[] args)
Sets all properties from the given array of argumentsstatic void
setDefaultClassLoader(ClassLoader newDefaultClassLoader)
Sets the defaultClassLoader
to use with Classpath resourcesstatic Config
setInstance(Config config)
Sets the singleton instance of the config.static void
setProperty(@NonNull String name, Object value)
static void
setProperty(@NonNull String name, String value)
Sets the value of a property.JsonEntry
toJson()
-
-
-
Field Detail
-
CONF_EXTENSION
public static final String CONF_EXTENSION
File extensions for config files.- See Also:
- Constant Field Values
-
-
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 rootpath
- 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 rootpath
- 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 entryparams
- 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 classpropertyComponents
- 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 prefixpropertyComponents
- 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 defaultClassLoader
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 suppliedStringMatcher
.- Parameters:
matcher
- TheStringMatcher
to use for matching property names.- Returns:
- A
List
of properties matched using theStringMatcher
.
-
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 aspropertyPrefix + . + propertyComponent[0] + . + propertyComponent[1] + ...
- Parameters:
root
- The prefixpath
- 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 asclazz.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 nameargs
- the command line argumentsparser
- the to use for parsing the argumentsotherPackages
- 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 nameargs
- the command line argumentsotherPackages
- 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, String value)
Sets the value of a property.- Parameters:
name
- the name of the propertyvalue
- the value of the property
-
toJson
public JsonEntry toJson()
-
-