Package org.yamcs
Class YConfiguration
- java.lang.Object
-
- org.yamcs.YConfiguration
-
public class YConfiguration extends Object
A configuration object is a wrapper around a Map<String, Object> which keeps track to a parent and its original file (if any). This class loads yamcs configurations. There are a number of "subsystems", each using a corresponding subsystem.yaml file Configuration files are looked up in this order:- in the prefix/file.yaml via the classpath if the prefix is set in the setup method (used in the unittests)
- in the userConfigDirectory .yamcs/etc/file.yaml
- in the file.yaml via the classpath..
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
YConfiguration.ConfigurationNotFoundException
Introduced to be able to detect when a configuration file was not specified (as opposed to when there's a validation error inside).static class
YConfiguration.DefaultConfigurationResolver
Default config file resolver.
-
Field Summary
Fields Modifier and Type Field Description static File
configDirectory
-
Constructor Summary
Constructors Constructor Description YConfiguration(String subsystem, InputStream is, String confpath)
Constructs a new configuration object parsing the input streamYConfiguration(YConfiguration parent, String parentKey, Map<String,Object> root)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
containsKey(String key)
boolean
containsKey(String key, String key1)
static YConfiguration
emptyConfig()
Object
get(String key)
byte[]
getBinary(String key)
byte[]
getBinary(String key, byte[] defaultValue)
static byte[]
getBinary(Map<String,Object> m, String key)
boolean
getBoolean(String key)
boolean
getBoolean(String key, boolean defaultValue)
boolean
getBoolean(String key, String key1)
static boolean
getBoolean(Map<String,Object> m, String key)
static boolean
getBoolean(Map<String,Object> m, String key, boolean defaultValue)
Returns m.get(key) if it exists and is of type boolean, if m.get(key) exists and is not boolean, throw an exception. if m.get(key) does not exist, return the default value.YConfiguration
getConfig(String key)
If the key is pointing to a map, creates and returns a configuration object out of that mapList<YConfiguration>
getConfigList(String key)
YConfiguration
getConfigListIdx(String key, int idx)
If config.get(key) exists and is a list, and the list has the element idx and is a map, then return a configuration wrapper around that map.YConfiguration
getConfigOrEmpty(String key)
Same asgetConfig(String)
but return an empty config if the key does not exist.static YConfiguration
getConfiguration(String subsystem)
Loads (if not already loaded) and returns a configuration corresponding to a file <subsystem>.yaml This method does not reload the configuration file if it has changed.static YConfiguration
getConfiguration(String subsystem, boolean reload)
Loads and returns a configuration corresponding to a file <subsystem>.yaml This method reloads the configuration file always.double
getDouble(String key)
double
getDouble(String key, double defaultValue)
static double
getDouble(Map<String,Object> m, String key, double v)
<T extends Enum<T>>
TgetEnum(String key, Class<T> enumClass)
<T extends Enum<T>>
TgetEnum(String key, Class<T> enumClass, T defaultValue)
static <T extends Enum<T>>
TgetEnum(Map<String,Object> config, String key, Class<T> enumClass)
Returns a value of an enumeration that matches ignoring case the string obtained from the config with the given key.String
getFirstEntry()
returns the first entry(key) in the config file.Map<String,Object>
getFirstMap()
returns the first entry in the config file if it's a map.int
getInt(String key)
int
getInt(String key, int defaultValue)
int
getInt(String key, String key1)
int
getInt(String key, String key1, int defaultValue)
static int
getInt(Map<String,Object> m, String key)
static int
getInt(Map<String,Object> m, String key, int defaultValue)
return the m.get(key) as an int if it's present or v if it is not.Set<String>
getKeys()
<T> List<T>
getList(String key)
static <T> List<T>
getList(Map<String,Object> m, String key)
long
getLong(String key)
long
getLong(String key, long defaultValue)
static long
getLong(Map<String,Object> m, String key)
static long
getLong(Map<String,Object> m, String key, long v)
return the m.get(key) as an long if it's present or v if it is not.<K,V>
Map<K,V>getMap(String key)
Consider usinggetConfig(java.lang.String)
to get a child config instead of accessing the map directlystatic Map<String,Object>
getMap(Map<String,Object> m, String key)
String
getPath()
static YConfigurationResolver
getResolver()
Map<String,Object>
getRoot()
List<YConfiguration>
getServiceConfigList(String key)
This is the same like the method above but will create a {class: "string"} for strings rather than throwing an exception.String
getString(String key)
String
getString(String key, String defaultValue)
static String
getString(Map<String,Object> m, String key)
Returns m.get(key) if it exists and is of type string, otherwise throws an exceptionstatic String
getString(Map<String,Object> m, String key, String defaultValue)
<T> List<T>
getSubList(String key, String key1)
Map<String,Object>
getSubMap(String key, String key1)
String
getSubString(String key, String subkey)
static boolean
isDefined(String subsystem)
boolean
isList(String key)
static boolean
isList(Map<String,Object> m, String key)
static boolean
isNull(Map<?,?> m, String key)
static void
setResolver(YConfigurationResolver resolver)
static void
setupTest(String configPrefix)
Sets up the Yamcs configuration system and loads the UTC-TAI offsets.static void
setupTool()
Sets up the Yamcs configuration system and loads the UTC-TAI offsets.static void
setupTool(File configDirectory)
Sets up the Yamcs configuration system and loads the UTC-TAI offsets.Map<String,Object>
toMap()
String
toString()
static YConfiguration
wrap(Map<String,Object> m)
Create a new configuration wrapping around a map The resulting config will have no parent
-
-
-
Field Detail
-
configDirectory
public static File configDirectory
-
-
Constructor Detail
-
YConfiguration
public YConfiguration(String subsystem, InputStream is, String confpath)
Constructs a new configuration object parsing the input stream- Parameters:
is
- input stream where the configuration is loaded fromconfpath
- configuration path - it is remembered together with the configuration in case of error to indicate where it is coming from (i.e. which file)
-
YConfiguration
public YConfiguration(YConfiguration parent, String parentKey, Map<String,Object> root)
- Parameters:
parent
-parentKey
-root
-
-
-
Method Detail
-
setupTool
public static void setupTool()
Sets up the Yamcs configuration system and loads the UTC-TAI offsets.This method is intended for client tools and make store or use files from ~/.yamcs.
-
setupTool
public static void setupTool(File configDirectory)
Sets up the Yamcs configuration system and loads the UTC-TAI offsets.This method is intended for client tools that wish to customize the default config directory.
- Parameters:
configDirectory
-
-
setupTest
public static void setupTest(String configPrefix)
Sets up the Yamcs configuration system and loads the UTC-TAI offsets.This method is intended for use in unit and integration tests. It allows resolving configuration files from a specific subdirectory of the classpath.
- Parameters:
configPrefix
- the name of the subdirectory where to resolve configuration files. This is resolved from the classpath.
-
getConfiguration
public static YConfiguration getConfiguration(String subsystem) throws ConfigurationException
Loads (if not already loaded) and returns a configuration corresponding to a file <subsystem>.yaml This method does not reload the configuration file if it has changed.- Parameters:
subsystem
-- Returns:
- the loaded configuration
- Throws:
ConfigurationException
- if the configuration file could not be found or not loaded (e.g. error in yaml formatting)
-
getConfiguration
public static YConfiguration getConfiguration(String subsystem, boolean reload) throws ConfigurationException
Loads and returns a configuration corresponding to a file <subsystem>.yaml This method reloads the configuration file always.- Parameters:
subsystem
-reload
-- Returns:
- the loaded configuration
- Throws:
ConfigurationException
- if the configuration file could not be found or not loaded (e.g. error in yaml formatting)
-
isDefined
public static boolean isDefined(String subsystem) throws ConfigurationException
- Throws:
ConfigurationException
-
containsKey
public boolean containsKey(String key)
-
containsKey
public boolean containsKey(String key, String key1) throws ConfigurationException
- Throws:
ConfigurationException
-
getFirstMap
public Map<String,Object> getFirstMap() throws ConfigurationException
returns the first entry in the config file if it's a map. Otherwise throws an error- Throws:
ConfigurationException
-
getFirstEntry
public String getFirstEntry() throws ConfigurationException
returns the first entry(key) in the config file.- Returns:
- Throws:
ConfigurationException
-
getConfig
public YConfiguration getConfig(String key)
If the key is pointing to a map, creates and returns a configuration object out of that mapThe returned object will have its parent set to this object
If the key does not exist a ConfigurationException is thrown.
- Parameters:
key
-- Returns:
-
getConfigOrEmpty
public YConfiguration getConfigOrEmpty(String key)
Same asgetConfig(String)
but return an empty config if the key does not exist.- Parameters:
key
-- Returns:
-
getMap
public static Map<String,Object> getMap(Map<String,Object> m, String key) throws ConfigurationException
- Throws:
ConfigurationException
-
getMap
public <K,V> Map<K,V> getMap(String key) throws ConfigurationException
Consider usinggetConfig(java.lang.String)
to get a child config instead of accessing the map directly- Throws:
ConfigurationException
-
getSubMap
public Map<String,Object> getSubMap(String key, String key1) throws ConfigurationException
- Throws:
ConfigurationException
-
getString
public static String getString(Map<String,Object> m, String key) throws ConfigurationException
Returns m.get(key) if it exists and is of type string, otherwise throws an exception- Parameters:
m
-key
-- Returns:
- Throws:
ConfigurationException
-
getString
public static String getString(Map<String,Object> m, String key, String defaultValue) throws ConfigurationException
- Throws:
ConfigurationException
-
getString
public String getString(String key) throws ConfigurationException
- Throws:
ConfigurationException
-
getString
public String getString(String key, String defaultValue) throws ConfigurationException
- Throws:
ConfigurationException
-
getSubString
public String getSubString(String key, String subkey) throws ConfigurationException
- Throws:
ConfigurationException
-
getList
public <T> List<T> getList(String key) throws ConfigurationException
- Throws:
ConfigurationException
-
getConfigList
public List<YConfiguration> getConfigList(String key) throws ConfigurationException
- Throws:
ConfigurationException
-
getDouble
public double getDouble(String key) throws ConfigurationException
- Throws:
ConfigurationException
-
getDouble
public double getDouble(String key, double defaultValue) throws ConfigurationException
- Throws:
ConfigurationException
-
getServiceConfigList
public List<YConfiguration> getServiceConfigList(String key) throws ConfigurationException
This is the same like the method above but will create a {class: "string"} for strings rather than throwing an exception. It is to be used when loading service list which can be specified just by the class name.- Parameters:
key
-- Returns:
- Throws:
ConfigurationException
-
getSubList
public <T> List<T> getSubList(String key, String key1) throws ConfigurationException
- Throws:
ConfigurationException
-
getBoolean
public static boolean getBoolean(Map<String,Object> m, String key, boolean defaultValue) throws ConfigurationException
Returns m.get(key) if it exists and is of type boolean, if m.get(key) exists and is not boolean, throw an exception. if m.get(key) does not exist, return the default value.- Parameters:
m
-key
-defaultValue
- - the default value to return if m.get(key) does not exist.- Returns:
- the boolean config value
- Throws:
ConfigurationException
-
getBoolean
public static boolean getBoolean(Map<String,Object> m, String key) throws ConfigurationException
- Throws:
ConfigurationException
-
getBoolean
public boolean getBoolean(String key) throws ConfigurationException
- Throws:
ConfigurationException
-
getBoolean
public boolean getBoolean(String key, String key1) throws ConfigurationException
- Throws:
ConfigurationException
-
getBoolean
public boolean getBoolean(String key, boolean defaultValue)
-
getInt
public int getInt(String key) throws ConfigurationException
- Throws:
ConfigurationException
-
getInt
public int getInt(String key, int defaultValue) throws ConfigurationException
- Throws:
ConfigurationException
-
getInt
public int getInt(String key, String key1) throws ConfigurationException
- Throws:
ConfigurationException
-
getInt
public int getInt(String key, String key1, int defaultValue) throws ConfigurationException
- Throws:
ConfigurationException
-
getInt
public static int getInt(Map<String,Object> m, String key) throws ConfigurationException
- Throws:
ConfigurationException
-
getInt
public static int getInt(Map<String,Object> m, String key, int defaultValue) throws ConfigurationException
return the m.get(key) as an int if it's present or v if it is not. If the key is present but the value is not an integer, a ConfigurationException is thrown.- Parameters:
m
-key
-defaultValue
-- Returns:
- the value from the map or the passed value if the map does not contain the key
- Throws:
ConfigurationException
- if the key is present but it's not an int
-
getLong
public long getLong(String key)
-
getLong
public long getLong(String key, long defaultValue)
-
getLong
public static long getLong(Map<String,Object> m, String key) throws ConfigurationException
- Throws:
ConfigurationException
-
getBinary
public byte[] getBinary(String key)
-
getBinary
public byte[] getBinary(String key, byte[] defaultValue)
-
getBinary
public static byte[] getBinary(Map<String,Object> m, String key) throws ConfigurationException
- Throws:
ConfigurationException
-
getLong
public static long getLong(Map<String,Object> m, String key, long v) throws ConfigurationException
return the m.get(key) as an long if it's present or v if it is not.- Parameters:
m
-key
-v
-- Returns:
- the value from the map or the passed value if the map does not contain the key
- Throws:
ConfigurationException
- if the key is present but it's not an long
-
getDouble
public static double getDouble(Map<String,Object> m, String key, double v) throws ConfigurationException
- Throws:
ConfigurationException
-
isList
public boolean isList(String key)
-
setResolver
public static void setResolver(YConfigurationResolver resolver)
-
getResolver
public static YConfigurationResolver getResolver()
-
getEnum
public static <T extends Enum<T>> T getEnum(Map<String,Object> config, String key, Class<T> enumClass)
Returns a value of an enumeration that matches ignoring case the string obtained from the config with the given key. Throws an Configurationexception if the key does not exist in config or if it does not map to a valid enumeration value- Parameters:
config
-key
-enumClass
-- Returns:
-
wrap
public static YConfiguration wrap(Map<String,Object> m)
Create a new configuration wrapping around a map The resulting config will have no parent- Parameters:
m
-- Returns:
-
emptyConfig
public static YConfiguration emptyConfig()
-
getPath
public String getPath()
-
getList
public static <T> List<T> getList(Map<String,Object> m, String key) throws ConfigurationException
- Throws:
ConfigurationException
-
getConfigListIdx
public YConfiguration getConfigListIdx(String key, int idx)
If config.get(key) exists and is a list, and the list has the element idx and is a map, then return a configuration wrapper around that map.Otherwise throw a ConfigurationException
- Parameters:
key
-idx
-- Returns:
-
-