Class Config
- All Implemented Interfaces:
ConfigEventListener
Notes:
- This class is thread safe and reusable.
See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final BeanSessionprotected final BinaryFormatprotected final intprotected final booleanprotected final Stringprotected final ReaderParserprotected final booleanprotected final WriterSerializerprotected final ConfigStoreprotected final VarResolverprotected final VarResolverSessionFields inherited from class org.apache.juneau.Context
CONTEXT_APPLY_FILTER -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddListener(ConfigEventListener listener) Add a listener to this config to react to modification events.Encodes and unencoded entries in this config.voidclose()Closes this configuration object by unregistering it from the underlying config map.commit()Commit the changes in this config to the store.copy()Creates a builder from this context object.static Config.Buildercreate()Creates a new builder for this object.static Config.BuilderSame ascreate()but initializes the builder with the specified config name.booleanReturnstrue if this section contains the specified key and the key has a non-blank value.Gets the entry with the specified key.Returns the list of candidate system default configuration file names.Returns the keys of the entries in the specified section.getName()Returns the name associated with this config (usually a file name).getSection(String name) Gets the section with the specified name.Returns the section names defined in this config.Gets the entry with the specified key.static ConfigReturns the system default configuration.Overwrites the contents of the config file.Overwrites the contents of the config file.Loads the contents of the specified map of maps into this config.voidonConfigChange(ConfigEvents events) Gets called immediately after a config file has been loaded.Returns the properties on this bean as a map for debugging.Removes an entry with the specified key.removeImport(String sectionName, String importName) Removes the import statement with the specified name from the specified section.removeListener(ConfigEventListener listener) Removes a listener from this config.removeSection(String name) Removes the section with the specified name.resolving(VarResolverSession varSession) Creates a copy of this config using the specified var session for resolving variables.rollback()Does a rollback of any changes on this config currently in memory.Adds or replaces an entry with the specified key with a POJO serialized to a string using the registered serializer.set(String key, Object value, Serializer serializer) Same asset(String, Object)but allows you to specify the serializer to use to serialize the value.set(String key, Object value, Serializer serializer, String modifiers, String comment, List<String> preLines) Same asset(String, Object)but allows you to specify all aspects of a value.Sets a value in this config.Creates the specified import statement if it doesn't exist.setSection(String name, List<String> preLines) Creates the specified section if it doesn't exist.Creates the specified section if it doesn't exist.static voidsetSystemDefault(Config systemDefault) Sets a system default configuration.Takes the settings defined in this configuration and sets them as system properties.toMap()Returns the contents of this config as a simple map.toString()Saves this config file to the specified writer as an INI file.Methods inherited from class org.apache.juneau.Context
createBuilder, createSession, getAnnotationProvider, getSession, init, isDebug
-
Field Details
-
multiLineValuesOnSeparateLines
-
readOnly
-
binaryLineLength
-
binaryFormat
-
beanSession
-
store
-
mods
-
parser
-
name
-
varResolver
-
varSession
-
serializer
-
-
Constructor Details
-
Config
Constructor.- Parameters:
builder- The builder for this object.- Throws:
IOException- Thrown by underlying stream.
-
-
Method Details
-
create
Creates a new builder for this object.- Returns:
- A new builder.
-
create
Same ascreate()but initializes the builder with the specified config name.- Parameters:
name- The configuration name.- Returns:
- A new builder.
-
getCandidateSystemDefaultConfigNames
Returns the list of candidate system default configuration file names.If the
"juneau.configFile" system property is set, returns a singleton of that value.
Otherwise, returns a list consisting of the following values:- File with same name as jar file but with
".cfg" extension. (e.g."myjar.cfg" ) - Any file ending in
".cfg" in the home directory (names ordered alphabetically). "juneau.cfg" "default.cfg" "application.cfg" "app.cfg" "settings.cfg" "application.properties"
- Returns:
- A list of candidate file names.
The returned list is modifiable.
Each call constructs a new list.
- File with same name as jar file but with
-
getSystemDefault
Returns the system default configuration.- Returns:
- The system default configuration, or
null if it doesn't exist.
-
setSystemDefault
Sets a system default configuration.- Parameters:
systemDefault- The new system default configuration.
-
addListener
Add a listener to this config to react to modification events.Listeners should be removed using
removeListener(ConfigEventListener).- Parameters:
listener- The new listener to add.- Returns:
- This object.
-
applyMods
Encodes and unencoded entries in this config.If any entries in the config are marked as encoded but not actually encoded, this will encode them.
- Returns:
- This object.
- Throws:
UnsupportedOperationException- If configuration is read only.
-
close
Closes this configuration object by unregistering it from the underlying config map. -
commit
Commit the changes in this config to the store.- Returns:
- This object.
- Throws:
IOException- Thrown by underlying stream.UnsupportedOperationException- If configuration is read only.
-
copy
Description copied from class:ContextCreates a builder from this context object.Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
-
exists
Returnstrue if this section contains the specified key and the key has a non-blank value.- Parameters:
key- The key.- Returns:
true if this section contains the specified key and the key has a non-blank value.
-
get
Gets the entry with the specified key.The key can be in one of the following formats...
-
"key" - A value in the default section (i.e. defined above any[section] header). -
"section/key" - A value from the specified section.
If entry does not exist, returns an empty
Entryobject.- Parameters:
key- The key.- Returns:
- The entry bean, never
null .
-
-
getKeys
Returns the keys of the entries in the specified section.- Parameters:
section- The section name to write from.
If empty, refers to the default section.
Must not benull .- Returns:
- An unmodifiable set of keys, or an empty set if the section doesn't exist.
-
getName
Returns the name associated with this config (usually a file name).- Returns:
- The name associated with this config, or
null if it has no name.
-
getSection
Gets the section with the specified name.If section does not exist, returns an empty
Sectionobject.- Parameters:
name- The section name.null and blank refer to the default section.- Returns:
- The section bean, never
null .
-
getSectionNames
Returns the section names defined in this config.- Returns:
- The section names defined in this config.
-
getString
Gets the entry with the specified key.The key can be in one of the following formats...
-
"key" - A value in the default section (i.e. defined above any[section] header). -
"section/key" - A value from the specified section.
If entry does not exist, returns
null .Notes:
- This method is equivalent to calling
get( .key ).orElse(null );
- Parameters:
key- The key.- Returns:
- The entry value, or
null if it doesn't exist.
-
-
load
Loads the contents of the specified map of maps into this config.- Parameters:
m- The maps to load.- Returns:
- This object.
- Throws:
SerializeException- Value could not be serialized.
-
load
Overwrites the contents of the config file.- Parameters:
contents- The new contents of the config file.synchronous- Wait until the change has been persisted before returning this map.- Returns:
- This object.
- Throws:
IOException- Thrown by underlying stream.InterruptedException- Thread was interrupted.UnsupportedOperationException- If configuration is read only.
-
load
Overwrites the contents of the config file.- Parameters:
contents- The new contents of the config file.synchronous- Wait until the change has been persisted before returning this map.- Returns:
- This object.
- Throws:
IOException- Thrown by underlying stream.InterruptedException- Thread was interrupted.UnsupportedOperationException- If configuration is read only.
-
onConfigChange
Description copied from interface:ConfigEventListenerGets called immediately after a config file has been loaded.- Specified by:
onConfigChangein interfaceConfigEventListener- Parameters:
events- The change events.
-
remove
Removes an entry with the specified key.- Parameters:
key- The key.- Returns:
- The previous value, or
null if the section or key did not previously exist. - Throws:
UnsupportedOperationException- If configuration is read only.
-
removeImport
Removes the import statement with the specified name from the specified section.- Parameters:
sectionName- The section name where to place the import statement.
Must not benull .
Use blank for the default section.importName- The import name.
Must not benull .- Returns:
- This object.
- Throws:
UnsupportedOperationException- If configuration is read only.
-
removeListener
Removes a listener from this config.- Parameters:
listener- The listener to remove.- Returns:
- This object.
-
removeSection
Removes the section with the specified name.- Parameters:
name- The name of the section to remove- Returns:
- This object.
- Throws:
UnsupportedOperationException- If configuration is read only.
-
resolving
Creates a copy of this config using the specified var session for resolving variables.This creates a shallow copy of the config but replacing the variable resolver.
- Parameters:
varSession- The var session used for resolving string variables.- Returns:
- A new config object.
-
rollback
Does a rollback of any changes on this config currently in memory.- Returns:
- This object.
- Throws:
UnsupportedOperationException- If configuration is read only.
-
set
Adds or replaces an entry with the specified key with a POJO serialized to a string using the registered serializer.Equivalent to calling
put(key, value, isEncoded(key)) .- Parameters:
key- The key.value- The new value POJO.- Returns:
- The previous value, or
null if the section or key did not previously exist. - Throws:
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.
-
set
Same asset(String, Object)but allows you to specify the serializer to use to serialize the value.- Parameters:
key- The key.value- The new value.serializer- The serializer to use for serializing the object. Ifnull , then uses the predefined serializer on the config file.- Returns:
- The previous value, or
null if the section or key did not previously exist. - Throws:
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.
-
set
public Config set(String key, Object value, Serializer serializer, String modifiers, String comment, List<String> preLines) throws SerializeException Same asset(String, Object)but allows you to specify all aspects of a value.- Parameters:
key- The key.value- The new value.serializer- The serializer to use for serializing the object. Ifnull , then uses the predefined serializer on the config file.modifiers- Optional modifiers to apply to the value.
Ifnull , then previous value will not be replaced.comment- Optional same-line comment to add to this value.
Ifnull , then previous value will not be replaced.preLines- Optional comment or blank lines to add before this entry.
Ifnull , then previous value will not be replaced.- Returns:
- The previous value, or
null if the section or key did not previously exist. - Throws:
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.
-
set
Sets a value in this config.- Parameters:
key- The key.value- The value.- Returns:
- This object.
- Throws:
UnsupportedOperationException- If configuration is read only.
-
setImport
Creates the specified import statement if it doesn't exist.- Parameters:
sectionName- The section name where to place the import statement.
Must not benull .
Use blank for the default section.importName- The import name.
Must not benull .preLines- Optional comment and blank lines to add immediately before the import statement.
Ifnull , previous pre-lines will not be replaced.- Returns:
- The appended or existing import statement.
- Throws:
UnsupportedOperationException- If configuration is read only.
-
setSection
Creates the specified section if it doesn't exist.Returns the existing section if it already exists.
- Parameters:
name- The section name.
Must not benull .
Use blank for the default section.preLines- Optional comment and blank lines to add immediately before the section.
Ifnull , previous pre-lines will not be replaced.- Returns:
- The appended or existing section.
- Throws:
UnsupportedOperationException- If configuration is read only.
-
setSection
public Config setSection(String name, List<String> preLines, Map<String, Object> contents) throws SerializeExceptionCreates the specified section if it doesn't exist.- Parameters:
name- The section name.
Must not benull .
Use blank for the default section.preLines- Optional comment and blank lines to add immediately before the section.
Ifnull , previous pre-lines will not be replaced.contents- Values to set in the new section.
Can benull .- Returns:
- The appended or existing section.
- Throws:
SerializeException- Contents could not be serialized.UnsupportedOperationException- If configuration is read only.
-
setSystemProperties
Takes the settings defined in this configuration and sets them as system properties.- Returns:
- This object.
-
toMap
Returns the contents of this config as a simple map.- Returns:
- The contents of this config as a simple map.
-
properties
Description copied from class:ContextReturns the properties on this bean as a map for debugging.- Overrides:
propertiesin classContext- Returns:
- The properties on this bean as a map for debugging.
-
toString
-
writeTo
Saves this config file to the specified writer as an INI file.The writer will automatically be closed.
- Parameters:
w- The writer to send the output to.- Returns:
- This object.
- Throws:
IOException- If a problem occurred trying to send contents to the writer.
-