public final class Config extends Context implements ConfigEventListener, Writable
Modifier and Type | Field and Description |
---|---|
static String |
CONFIG_binaryFormat
Configuration property: Binary value format.
|
static String |
CONFIG_binaryLineLength
Configuration property: Binary value line length.
|
static String |
CONFIG_encoder
Configuration property: Value encoder.
|
static String |
CONFIG_multiLineValuesOnSeparateLines
Configuration property: Multi-line values should always be on separate lines.
|
static String |
CONFIG_name
Configuration property: Configuration name.
|
static String |
CONFIG_parser
Configuration property: POJO parser.
|
static String |
CONFIG_readOnly
Configuration property: Read-only.
|
static String |
CONFIG_serializer
Configuration property: POJO serializer.
|
static String |
CONFIG_store
Configuration property: Configuration store.
|
static String |
CONFIG_varResolver
Configuration property: SVL variable resolver.
|
Constructor and Description |
---|
Config(PropertyStore ps)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
Config |
addListener(ConfigEventListener listener)
Add a listener to this config to react to modification events.
|
ObjectMap |
asMap()
Returns the values in this config map as a map of maps.
|
ConfigBuilder |
builder()
Creates a builder from this context object.
|
void |
close()
Closes this configuration object by unregistering it from the underlying config map.
|
Config |
commit()
Commit the changes in this config to the store.
|
static ConfigBuilder |
create()
Instantiates a new clean-slate
ConfigBuilder object. |
static ConfigBuilder |
create(String name)
Same as
create() but initializes the builder with the specified config name. |
SessionArgs |
createDefaultSessionArgs()
Unused.
|
Session |
createSession(SessionArgs args)
Unused.
|
Config |
encodeEntries()
Encodes and unencoded entries in this config.
|
boolean |
exists(String key)
Returns
|
protected void |
finalize() |
String |
get(String key)
Returns the specified value as a string from the config file.
|
boolean |
getBoolean(String key)
Convenience method for getting boolean config values.
|
boolean |
getBoolean(String key,
boolean def)
Convenience method for getting boolean config values.
|
byte[] |
getBytes(String key)
Convenience method for getting byte array config values.
|
byte[] |
getBytes(String key,
byte[] def)
Same as
getBytes(String) but with a default value if the entry doesn't exist. |
double |
getDouble(String key)
Convenience method for getting double config values.
|
double |
getDouble(String key,
double def)
Same as
getDouble(String) but returns a default value if not set. |
float |
getFloat(String key)
Convenience method for getting float config values.
|
float |
getFloat(String key,
float def)
Same as
getFloat(String) but returns a default value if not set. |
int |
getInt(String key)
Convenience method for getting int config values.
|
int |
getInt(String key,
int def)
Same as
getInt(String) but returns a default value if not set. |
Set<String> |
getKeys(String section)
Returns the keys of the entries in the specified section.
|
long |
getLong(String key)
Convenience method for getting long config values.
|
long |
getLong(String key,
long def)
Same as
getLong(String) but returns a default value if not set. |
MediaType |
getMediaType()
Returns the serialized media type for this resource (e.g.
|
<T> T |
getObject(String key,
Class<T> type)
Same as
getObject(String, Type, Type...) except optimized for a non-parameterized class. |
<T> T |
getObject(String key,
Parser parser,
Class<T> type)
Same as
getObject(String, Class) but allows you to specify the parser to use to parse the value. |
<T> T |
getObject(String key,
Parser parser,
Type type,
Type... args)
Same as
getObject(String, Type, Type...) but allows you to specify the parser to use to parse the value. |
<T> T |
getObject(String key,
Type type,
Type... args)
Gets the entry with the specified key and converts it to the specified value.
|
<T> T |
getObjectWithDefault(String key,
Parser parser,
T def,
Class<T> type)
Same as
getObjectWithDefault(String, Object, Class) but allows you to specify the parser to use to parse
the value. |
<T> T |
getObjectWithDefault(String key,
Parser parser,
T def,
Type type,
Type... args)
Same as
getObjectWithDefault(String, Object, Type, Type...) but allows you to specify the parser to use
to parse the value. |
<T> T |
getObjectWithDefault(String key,
T def,
Class<T> type)
Gets the entry with the specified key and converts it to the specified value.
|
<T> T |
getObjectWithDefault(String key,
T def,
Type type,
Type... args)
Gets the entry with the specified key and converts it to the specified value.
|
<T> T |
getSectionAsBean(String section,
Class<T> c)
Shortcut for calling
getSectionAsBean(sectionName, c, . |
<T> T |
getSectionAsBean(String section,
Class<T> c,
boolean ignoreUnknownProperties)
Converts this config file section to the specified bean instance.
|
<T> T |
getSectionAsInterface(String section,
Class<T> c)
Wraps a config file section inside a Java interface so that values in the section can be read and
write using getters and setters.
|
ObjectMap |
getSectionAsMap(String section)
Returns a section of this config copied into an
ObjectMap . |
String |
getString(String key)
Gets the entry with the specified key.
|
String |
getString(String key,
String def)
Gets the entry with the specified key.
|
String[] |
getStringArray(String key)
Gets the entry with the specified key, splits the value on commas, and returns the values as trimmed strings.
|
String[] |
getStringArray(String key,
String[] def)
Same as
getStringArray(String) but returns a default value if the value cannot be found. |
Config |
load(Map<String,Map<String,Object>> m)
Loads the contents of the specified map of maps into this config.
|
Config |
load(Reader contents,
boolean synchronous)
Overwrites the contents of the config file.
|
Config |
load(String contents,
boolean synchronous)
Overwrites the contents of the config file.
|
void |
onConfigChange(List<ConfigEvent> events)
Gets called immediately after a config file has been loaded.
|
Config |
remove(String key)
Removes an entry with the specified key.
|
Config |
removeListener(ConfigEventListener listener)
Removes a listener from this config.
|
Config |
removeSection(String name)
Removes the section with the specified name.
|
Config |
resolving(VarResolverSession varSession)
Creates a copy of this config using the specified var session for resolving variables.
|
Config |
rollback()
Does a rollback of any changes on this config currently in memory.
|
Config |
set(String key,
Object value)
Adds or replaces an entry with the specified key with a POJO serialized to a string using the registered
serializer.
|
Config |
set(String key,
Object value,
Serializer serializer)
Same as
set(String, Object) but allows you to specify the serializer to use to serialize the
value. |
Config |
set(String key,
Object value,
Serializer serializer,
ConfigMod[] modifiers,
String comment,
List<String> preLines)
Same as
set(String, Object) but allows you to specify all aspects of a value. |
Config |
set(String key,
Object value,
Serializer serializer,
ConfigMod modifier,
String comment,
List<String> preLines)
Same as
set(String, Object) but allows you to specify all aspects of a value. |
Config |
set(String key,
String value)
Sets a value in this config.
|
Config |
setSection(String name,
List<String> preLines)
Creates the specified section if it doesn't exist.
|
Config |
setSection(String name,
List<String> preLines,
Map<String,Object> contents)
Creates the specified section if it doesn't exist.
|
String |
toString() |
Config |
writeProperties(String section,
Object bean,
boolean ignoreUnknownProperties)
Copies the entries in a section to the specified bean by calling the public setters on that bean.
|
Writer |
writeTo(Writer w)
Saves this config file to the specified writer as an INI file.
|
createSession, equals, getArrayProperty, getArrayProperty, getBooleanProperty, getClassArrayProperty, getClassArrayProperty, getClassArrayProperty, getClassListProperty, getClassListProperty, getClassMapProperty, getClassMapProperty, getClassProperty, getClassSetProperty, getClassSetProperty, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceProperty, getInstanceProperty, getInstanceProperty, getIntegerProperty, getListProperty, getListProperty, getLongProperty, getMapProperty, getProperty, getProperty, getPropertyKeys, getPropertyStore, getSetProperty, getSetProperty, getStringProperty, hashCode
public static final String CONFIG_name
String
Specifies the configuration name.
This is typically the configuration file name, although
the name can be anything identifiable by the ConfigStore
used for retrieving and storing the configuration.
public static final String CONFIG_store
ConfigStore
ConfigFileStore.DEFAULT
The configuration store used for retrieving and storing configurations.
public static final String CONFIG_serializer
WriterSerializer
JsonSerializer.DEFAULT_LAX
The serializer to use for serializing POJO values.
public static final String CONFIG_parser
ReaderParser
JsonParser.DEFAULT
The parser to use for parsing values to POJOs.
public static final String CONFIG_encoder
ConfigEncoder
ConfigXorEncoder.INSTANCE
The encoder to use for encoding encoded configuration values.
public static final String CONFIG_varResolver
VarResolver
VarResolver.DEFAULT
The resolver to use for resolving SVL variables.
public static final String CONFIG_binaryLineLength
Integer
-1
When serializing binary values, lines will be split after this many characters.
Use -1
to represent no line splitting.
public static final String CONFIG_binaryFormat
String
The format to use when persisting byte arrays.
Possible values:
public static final String CONFIG_multiLineValuesOnSeparateLines
Boolean
When enabled, multi-line values will always be placed on a separate line from the key.
public static final String CONFIG_readOnly
Boolean
When enabled, attempts to call any setters on this object will throw an UnsupportedOperationException
.
public Config(PropertyStore ps) throws IOException
ps
- The property store containing all the settings for this object.IOException
public static ConfigBuilder create()
ConfigBuilder
object.
This is equivalent to simply calling
.
ConfigBuilder
object.public static ConfigBuilder create(String name)
create()
but initializes the builder with the specified config name.
This is equivalent to simply calling
.
name
- The configuration name.ConfigBuilder
object.public ConfigBuilder builder()
Context
Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
public Config resolving(VarResolverSession varSession)
This creates a shallow copy of the config but replacing the variable resolver.
varSession
- The var session used for resolving string variables.public String get(String key)
Unlike getString(String)
, this method doesn't replace SVL variables.
key
- The key.public Config set(String key, String value)
key
- The key.value
- The value.UnsupportedOperationException
- If configuration is read only.public Config set(String key, Object value) throws SerializeException
Equivalent to calling put(key, value, isEncoded(key))
.
key
- The key.value
- The new value POJO.SerializeException
- If serializer could not serialize the value or if a serializer is not registered with this config file.UnsupportedOperationException
- If configuration is read only.public Config set(String key, Object value, Serializer serializer) throws SerializeException
set(String, Object)
but allows you to specify the serializer to use to serialize the
value.key
- The key.value
- The new value.serializer
- The serializer to use for serializing the object.
If SerializeException
- If serializer could not serialize the value or if a serializer is not registered with this config file.UnsupportedOperationException
- If configuration is read only.public Config set(String key, Object value, Serializer serializer, ConfigMod modifier, String comment, List<String> preLines) throws SerializeException
set(String, Object)
but allows you to specify all aspects of a value.key
- The key.value
- The new value.serializer
- The serializer to use for serializing the object.
If modifier
- Optional modifier to apply to the value.
comment
- Optional same-line comment to add to this value.
preLines
- Optional comment or blank lines to add before this entry.
SerializeException
- If serializer could not serialize the value or if a serializer is not registered with this config file.UnsupportedOperationException
- If configuration is read only.public Config set(String key, Object value, Serializer serializer, ConfigMod[] modifiers, String comment, List<String> preLines) throws SerializeException
set(String, Object)
but allows you to specify all aspects of a value.key
- The key.value
- The new value.serializer
- The serializer to use for serializing the object.
If modifiers
- Optional modifiers to apply to the value.
comment
- Optional same-line comment to add to this value.
preLines
- Optional comment or blank lines to add before this entry.
SerializeException
- If serializer could not serialize the value or if a serializer is not registered with this config file.UnsupportedOperationException
- If configuration is read only.public Config remove(String key)
key
- The key.UnsupportedOperationException
- If configuration is read only.public Config encodeEntries()
If any entries in the config are marked as encoded but not actually encoded, this will encode them.
UnsupportedOperationException
- If configuration is read only.public String getString(String key)
The key can be in one of the following formats...
[section]
header).
key
- The key.public String getString(String key, String def)
The key can be in one of the following formats...
[section]
header).
key
- The key.def
- The default value if the value does not exist.public String[] getStringArray(String key)
key
- The key.public String[] getStringArray(String key, String[] def)
getStringArray(String)
but returns a default value if the value cannot be found.key
- The key.def
- The default value if the value does not exist.public int getInt(String key)
"100K" => 1024000
"100M" => 104857600
Uses Integer.decode(String)
underneath, so any of the following integer formats are supported:
key
- The key.0
if the value does not exist or the value is empty.public int getInt(String key, int def)
getInt(String)
but returns a default value if not set.key
- The key.def
- The default value if the value does not exist.public boolean getBoolean(String key)
key
- The key.public boolean getBoolean(String key, boolean def)
key
- The key.def
- The default value if the value does not exist.public long getLong(String key)
"100K" => 1024000
"100M" => 104857600
Uses Long.decode(String)
underneath, so any of the following number formats are supported:
key
- The key.0
if the value does not exist or the value is empty.public long getLong(String key, long def)
getLong(String)
but returns a default value if not set.key
- The key.def
- The default value if the value does not exist.public double getDouble(String key)
Uses Double.valueOf(String)
underneath, so any of the following number formats are supported:
key
- The key.0
if the value does not exist or the value is empty.public double getDouble(String key, double def)
getDouble(String)
but returns a default value if not set.key
- The key.def
- The default value if the value does not exist.public float getFloat(String key)
Uses Float.valueOf(String)
underneath, so any of the following number formats are supported:
key
- The key.0
if the value does not exist or the value is empty.public float getFloat(String key, float def)
getFloat(String)
but returns a default value if not set.key
- The key.def
- The default value if the value does not exist.public byte[] getBytes(String key) throws ParseException
This is equivalent to calling the following:
CONFIG_binaryFormat
setting.key
- The key.ParseException
- If value could not be converted to a byte array.public byte[] getBytes(String key, byte[] def) throws ParseException
getBytes(String)
but with a default value if the entry doesn't exist.key
- The key.def
- The default value if the value does not exist.ParseException
- If value could not be converted to a byte array.public <T> T getObject(String key, Type type, Type... args) throws ParseException
The key can be in one of the following formats...
[section]
header).
The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).
Config cf = Config.
Collection
classes are assumed to be followed by zero or one objects indicating the element type.
Map
classes are assumed to be followed by zero or two meta objects indicating the key and value
types.
The array can be arbitrarily long to indicate arbitrarily complex data structures.
getObject(String, Class)
method instead if you don't need a parameterized map/collection.
key
- The key.type
- The object type to create.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
args
- The type arguments of the class if it's a collection or map.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
ParseException
- If parser could not parse the value or if a parser is not registered with this config file.public <T> T getObject(String key, Parser parser, Type type, Type... args) throws ParseException
getObject(String, Type, Type...)
but allows you to specify the parser to use to parse the value.key
- The key.parser
- The parser to use for parsing the object.
If type
- The object type to create.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
args
- The type arguments of the class if it's a collection or map.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
ParseException
- If parser could not parse the value or if a parser is not registered with this config file.public <T> T getObject(String key, Class<T> type) throws ParseException
getObject(String, Type, Type...)
except optimized for a non-parameterized class.
This is the preferred parse method for simple types since you don't need to cast the results.
Config cf = Config.
T
- The class type of the object being created.key
- The key.type
- The object type to create.ParseException
- If the input contains a syntax error or is malformed, or is not valid for the specified type.for argument syntax for maps and collections.
public <T> T getObject(String key, Parser parser, Class<T> type) throws ParseException
getObject(String, Class)
but allows you to specify the parser to use to parse the value.T
- The class type of the object being created.key
- The key.parser
- The parser to use for parsing the object.
If type
- The object type to create.ParseException
- If the input contains a syntax error or is malformed, or is not valid for the specified type.for argument syntax for maps and collections.
public <T> T getObjectWithDefault(String key, T def, Class<T> type) throws ParseException
Same as getObject(String, Class)
, but with a default value.
key
- The key.def
- The default value if the value does not exist.type
- The class to convert the value to.ParseException
- If parser could not parse the value or if a parser is not registered with this config file.public <T> T getObjectWithDefault(String key, Parser parser, T def, Class<T> type) throws ParseException
getObjectWithDefault(String, Object, Class)
but allows you to specify the parser to use to parse
the value.key
- The key.parser
- The parser to use for parsing the object.
If def
- The default value if the value does not exist.type
- The class to convert the value to.ParseException
- If parser could not parse the value or if a parser is not registered with this config file.public <T> T getObjectWithDefault(String key, T def, Type type, Type... args) throws ParseException
Same as getObject(String, Type, Type...)
, but with a default value.
key
- The key.def
- The default value if the value does not exist.type
- The object type to create.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
args
- The type arguments of the class if it's a collection or map.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
ParseException
- If parser could not parse the value or if a parser is not registered with this config file.public <T> T getObjectWithDefault(String key, Parser parser, T def, Type type, Type... args) throws ParseException
getObjectWithDefault(String, Object, Type, Type...)
but allows you to specify the parser to use
to parse the value.key
- The key.parser
- The parser to use for parsing the object.
If def
- The default value if the value does not exist.type
- The object type to create.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
args
- The type arguments of the class if it's a collection or map.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
ParseException
- If parser could not parse the value or if a parser is not registered with this config file.public Set<String> getKeys(String section)
section
- The section name to write from.
public Config writeProperties(String section, Object bean, boolean ignoreUnknownProperties) throws ParseException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
section
- The section name to write from.
bean
- The bean to set the properties on.ignoreUnknownProperties
- If IllegalArgumentException
if this section contains a key that doesn't
correspond to a setter method.ParseException
- If parser was not set on this config file or invalid properties were found in the section.IllegalArgumentException
IllegalAccessException
InvocationTargetException
UnsupportedOperationException
- If configuration is read only.public <T> T getSectionAsBean(String section, Class<T> c) throws ParseException
getSectionAsBean(sectionName, c, false )
.section
- The section name to write from.
c
- The bean class to create.ParseException
public <T> T getSectionAsBean(String section, Class<T> c, boolean ignoreUnknownProperties) throws ParseException
Key/value pairs in the config file section get copied as bean property values to the specified bean class.
Config cf = Config.
section
- The section name to write from.
c
- The bean class to create.ignoreUnknownProperties
- If ParseException
if the section contains an entry that isn't a bean property
name.ParseException
public ObjectMap getSectionAsMap(String section) throws ParseException
ObjectMap
.section
- The section name to write from.
ObjectMap
, or ParseException
public <T> T getSectionAsInterface(String section, Class<T> c)
Config cf = Config.
UnsupportedOperationException
to be thrown.
section
- The section name to write from.
c
- The proxy interface class.public boolean exists(String key)
key
- The key.public Config setSection(String name, List<String> preLines)
Returns the existing section if it already exists.
name
- The section name.
preLines
- Optional comment and blank lines to add immediately before the section.
UnsupportedOperationException
- If configuration is read only.public Config setSection(String name, List<String> preLines, Map<String,Object> contents) throws SerializeException
name
- The section name.
preLines
- Optional comment and blank lines to add immediately before the section.
contents
- Values to set in the new section.
SerializeException
UnsupportedOperationException
- If configuration is read only.public Config removeSection(String name)
name
- The name of the section to removeUnsupportedOperationException
- If configuration is read only.public Config load(Map<String,Map<String,Object>> m) throws SerializeException
m
- The maps to load.SerializeException
public Config commit() throws IOException
IOException
UnsupportedOperationException
- If configuration is read only.public Writer writeTo(Writer w) throws IOException
The writer will automatically be closed.
writeTo
in interface Writable
w
- The writer to send the output to.IOException
- If a problem occurred trying to send contents to the writer.public Config addListener(ConfigEventListener listener)
Listeners should be removed using removeListener(ConfigEventListener)
.
listener
- The new listener to add.public Config removeListener(ConfigEventListener listener)
listener
- The listener to remove.public void close() throws IOException
IOException
public Config load(Reader contents, boolean synchronous) throws IOException, InterruptedException
contents
- The new contents of the config file.synchronous
- Wait until the change has been persisted before returning this map.IOException
InterruptedException
UnsupportedOperationException
- If configuration is read only.public Config load(String contents, boolean synchronous) throws IOException, InterruptedException
contents
- The new contents of the config file.synchronous
- Wait until the change has been persisted before returning this map.IOException
InterruptedException
UnsupportedOperationException
- If configuration is read only.public Config rollback()
UnsupportedOperationException
- If configuration is read only.public ObjectMap asMap()
This is considered a snapshot copy of the config map.
The returned map is modifiable, but modifications to the returned map are not reflected in the config map.
public Session createSession(SessionArgs args)
createSession
in class Context
args
- The session arguments.public SessionArgs createDefaultSessionArgs()
createDefaultSessionArgs
in class Context
public void onConfigChange(List<ConfigEvent> events)
ConfigEventListener
onConfigChange
in interface ConfigEventListener
events
- The change events.public MediaType getMediaType()
Writable
getMediaType
in interface Writable
Copyright © 2018 Apache. All rights reserved.