Class Config
- All Implemented Interfaces:
AnnotationProvider
,ConfigEventListener
Notes:
- This class is thread safe and reusable.
See Also:
-
Nested Class Summary
-
Field Summary
Fields inherited from class org.apache.juneau.Context
CONTEXT_APPLY_FILTER
Fields inherited from interface org.apache.juneau.AnnotationProvider
DEFAULT, DISABLE_ANNOTATION_CACHING
-
Constructor Summary
-
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.void
close()
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.Builder
create()
Creates a new builder for this object.static Config.Builder
Same ascreate()
but initializes the builder with the specified config name.boolean
Returnstrue if this section contains the specified key and the key has a non-blank value.protected void
finalize()
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 Config
Returns 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.void
onConfigChange
(ConfigEvents events) Gets called immediately after a config file has been loaded.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 void
setSystemDefault
(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, firstAnnotation, firstAnnotation, firstAnnotation, firstAnnotation, firstDeclaredAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachDeclaredAnnotation, getSession, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, init, isDebug, lastAnnotation, lastAnnotation, lastAnnotation, lastAnnotation, lastDeclaredAnnotation, properties
-
Constructor Details
-
Config
Constructor.- Parameters:
builder
- The builder for this object.- Throws:
IOException
- Thrown by underlying stream.
-
-
Method Details
-
setSystemDefault
Sets a system default configuration.- Parameters:
systemDefault
- The new system default configuration.
-
getSystemDefault
Returns the system default configuration.- Returns:
- The system default configuration, or
null if it doesn't exist.
-
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
-
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.
-
copy
Description copied from class:Context
Creates a builder from this context object.Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
-
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.
-
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.
-
setSystemProperties
Takes the settings defined in this configuration and sets them as system properties.- Returns:
- This object.
-
set
Sets a value in this config.- Parameters:
key
- The key.value
- The value.- 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.
-
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.
-
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.
-
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
Entry
object.- Parameters:
key
- The key.- Returns:
- The entry bean, never
null .
-
-
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.
-
-
getSection
Gets the section with the specified name.If section does not exist, returns an empty
Section
object.- Parameters:
name
- The section name.null and blank refer to the default section.- Returns:
- The section 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.
-
getSectionNames
Returns the section names defined in this config.- Returns:
- The section names defined in this config.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
removeListener
Removes a listener from this config.- Parameters:
listener
- The listener to remove.- Returns:
- This object.
-
close
Closes this configuration object by unregistering it from the underlying config map.- Throws:
IOException
- Thrown by underlying stream.
-
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.
-
rollback
Does a rollback of any changes on this config currently in memory.- Returns:
- This object.
- Throws:
UnsupportedOperationException
- If configuration is read only.
-
toMap
Returns the contents of this config as a simple map.- Returns:
- The contents of this config as a simple map.
-
onConfigChange
Description copied from interface:ConfigEventListener
Gets called immediately after a config file has been loaded.- Specified by:
onConfigChange
in interfaceConfigEventListener
- Parameters:
events
- The change events.
-
toString
-
finalize
-