public final class Config extends Object implements Closeable, Config, Serializable, TypeConverter
Config
,
Serialized FormConstructor and Description |
---|
Config()
Creates a new
Config with a default set of ConfigSource s and a
default set of Converter s
(including discovered Converter s). |
Config(Collection<? extends ConfigSource> sources,
TypeConverter typeConverter)
Creates a new
Config instance. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this
Config using a best-effort strategy. |
<T> T |
convert(String rawValue,
Type type)
Implements the
TypeConverter.convert(String, Type) method
by invoking the same method on this Config 's affiliated
TypeConverter supplied at construction time and returning
the result. |
Iterable<ConfigSource> |
getConfigSources()
Returns an
Iterable representing a snapshot at a moment
in time of this Config 's affiliated ConfigSource s
as they existed at that time. |
<T> Optional<T> |
getOptionalValue(String name,
Class<T> type)
Returns an
Optional representing an optional
configuration property value for the supplied name , as
converted to an object of the supplied type . |
<T> Optional<T> |
getOptionalValue(String name,
Type type)
Returns an
Optional representing an optional
configuration property value for the supplied name , as
converted to an object of the supplied type . |
Iterable<String> |
getPropertyNames()
Returns an
Iterable representing a snapshot at a moment
in time of the configuration property names as they existed at
that time. |
<T> T |
getValue(String name,
Class<T> type)
Returns the value for the configuration property named by the
supplied
name , as converted to an object of the supplied
type . |
<T> T |
getValue(String name,
Type type)
Returns the value for the configuration property named by the
supplied
name , as converted to an object of the supplied
type . |
boolean |
isClosed()
|
public Config() throws IOException
Config
with a default set of ConfigSource
s and a
default set of Converter
s
(including discovered Converter
s).IOException
- if an error occurs while reading a
META-INF/microprofile-config.properties
resourceServiceConfigurationError
- if there is a
problem interacting with a ServiceLoader
public Config(Collection<? extends ConfigSource> sources, TypeConverter typeConverter)
Config
instance.
The MicroProfile Config specification wants Config
implementations to be
acquired using ConfigProvider.getConfig()
invocations. This constructor exists primarily for
convenience.
sources
will be synchronized on and iterated
over by this constructor, which may have implications on
the type of Collection
supplied.
sources
- a Collection
of ConfigSource
s; may
be null
; will be synchronized on and iterated
over; copied by value; no reference is retained to this
objecttypeConverter
- a TypeConverter
implementation used
for type conversion; must not be null
; if it implements
Closeable
it will be closed by this Config
's close()
methodNullPointerException
- of typeConverter
is
null
ConfigProviderResolver
,
ConfigProvider.getConfig()
public final void close() throws IOException
Config
using a best-effort strategy.
An attempt is made to close each ConfigSource
that is
itself an instance of Closeable
and that was supplied to
this Config
at construction time. If any errors occur
along the way, they are added as suppressed exceptions to an
IOException
which is thrown as a kind of
aggregate.
When this method finishes normally, all of this Config
's associated Closeable
ConfigSource
s will
be closed. In addition,
the TypeConverter
supplied at construction time will be
closed as well if it implements Closeable
.
This method is idempotent and safe for concurrent use by multiple threads.
close
in interface Closeable
close
in interface AutoCloseable
IOException
- if at least one Closeable
ConfigSource
was not successfully closed or if the TypeConverter
supplied at construction time implements Closeable
and threw an IOException
public final boolean isClosed()
true
if this Config
has been closed.
A closed Config
will throw IllegalStateException
from most of its methods.
This method is safe for use by multiple threads.
public final Iterable<ConfigSource> getConfigSources()
Iterable
representing a snapshot at a moment
in time of this Config
's affiliated ConfigSource
s
as they existed at that time.
This method never returns null
.
The underlying collection of ConfigSource
s the
returned Iterable
is capable of iterating over is an
immutable copy of the internal collection of ConfigSource
s managed by this Config
.
The Iterable
returned by this method creates Iterator
s that do not
support the Iterator.remove()
method.
The MicroProfile Config specification implies a state of
affairs that permits ConfigSource
s "inside" a Config
to come and go. Consequently, this method returns what
is effectively a dissociated snapshot at a moment in time of a
collection of ConfigSource
s that were once managed by
this Config
at that moment in time.
This method is safe for use by multiple threads.
The Iterable
returned by this method is safe for use
by multiple threads.
getConfigSources
in interface Config
Iterable
of ConfigSource
s; never
null
IllegalStateException
- if this Config
has been
closedpublic final Iterable<String> getPropertyNames()
Iterable
representing a snapshot at a moment
in time of the configuration property names as they existed at
that time.
This method never returns null
.
The underlying collection of property names the returned
Iterable
is capable of iterating over is an immutable
copy of the internal collection of configuration property names
managed by this Config
.
The Iterable
returned by this method creates Iterator
s that do not
support the Iterator.remove()
method.
The MicroProfile Config specification implies a state of
affairs that permits ConfigSource
s "inside" a Config
to come and go. Consequently, this method returns what
is effectively a dissociated snapshot at a moment in time of a
collection of the configuration property names that were once
managed by this Config
at that moment in time.
No caching is performed by this method. Property names are
harvested from calls to ConfigSource.getPropertyNames()
on each ConfigSource
present in the Iterable
returned by the getConfigSources()
method.
This method is safe for use by multiple threads.
The Iterable
returned by this method is safe for use
by multiple threads.
getPropertyNames
in interface Config
Iterable
representing a snapshot of a
collection of configuration property names which this Config
manages; never null
IllegalStateException
- if this Config
has been
closedpublic final <T> Optional<T> getOptionalValue(String name, Class<T> type)
Optional
representing an optional
configuration property value for the supplied name
, as
converted to an object of the supplied type
.
This method never returns null
.
This method does not cache the value it returns.
It is worth noting that the MicroProfile Config
specification does not say anything about whether Converter
s are allowed to attempt to "convert" null
values from ConfigSource.getValue(String)
invocations
into non-null
objects. The MicroProfile Config TCK will fail if Converter
s _do_ attempt to work on null
values, so this
implementation never uses a null
value for
conversion.
This method is safe for use by multiple threads.
getOptionalValue
in interface Config
T
- the type of the value being requestedname
- the name of the configuration property whose
converted value should be returned; may be null
; passed
unaltered to ConfigSource.getValue(String)
so subject to
that method's (unspecified) preconditionstype
- the Class
representing the type any
non-null
value should be converted to if possible; must
not be null
Optional
representing an optional
configuration property value for the supplied name
, as
converted to an object of the supplied type
; never null
IllegalArgumentException
- if the value could not be
converted to the requested typeIllegalStateException
- if this Config
has been
closedgetOptionalValue(String, Type)
,
getValue(String, Class)
,
getValue(String, Type)
,
ConfigSource.getValue(String)
,
TypeConverter.convert(String, Type)
,
Converter.convert(String)
public final <T> Optional<T> getOptionalValue(String name, Type type)
Optional
representing an optional
configuration property value for the supplied name
, as
converted to an object of the supplied type
.
This method never returns null
.
This method does not cache the value it returns.
This method is safe for use by multiple threads.
T
- the type of the value being requestedname
- the name of the configuration property whose
converted value should be returned; may be null
; passed
unaltered to ConfigSource.getValue(String)
so subject to
that method's (unspecified) preconditionstype
- the Type
representing the type any
non-null
value should be converted to if possible; must
not be null
Optional
representing an optional
configuration property value for the supplied name
, as
converted to an object of the supplied type
; never null
IllegalArgumentException
- if the value could not be
converted to the requested typeIllegalStateException
- if this Config
has been
closedgetOptionalValue(String, Class)
,
getValue(String, Class)
,
getValue(String, Type)
,
ConfigSource.getValue(String)
,
TypeConverter.convert(String, Type)
,
Converter.convert(String)
public final <T> T getValue(String name, Class<T> type)
name
, as converted to an object of the supplied
type
.
This method never returns null
.
This method does not cache the value it returns.
The MicroProfile Config specification does not say whether
implementations of the Config.getValue(String, Class)
method may or may not return null
. null
values
from ConfigSource.getValue(String)
invocations are taken
to indicate a given configuration property value's absence,
however. Coupled with the fact that all Config.getValue(String, Class)
are required to throw NoSuchElementException
s when "the
property isn't present in the configuration", this implementation
chooses never to return null
.
It is also worth noting that the MicroProfile Config
specification does not say anything about whether Converter
s are allowed to attempt to "convert" null
values from ConfigSource.getValue(String)
invocations
into non-null
objects. The MicroProfile Config TCK will fail if Converter
s _do_ attempt to work on null
values, so this
implementation never uses a null
value for
conversion.
This method is safe for use by multiple threads.
getValue
in interface Config
T
- the type of the values returned by this methodname
- the name of the configuration property whose value
should be returned; may be null
; passed unaltered to
ConfigSource.getValue(String)
so subject to that method's
(unspecified) preconditionstype
- the Class
representing the type any
non-null
value should be converted to if possible; must
not be null
null
NoSuchElementException
- if the requested configuration
property value does not existIllegalStateException
- if this Config
has been
closedConfigSource.getValue(String)
,
getValue(String, Type)
,
getOptionalValue(String, Class)
,
getOptionalValue(String, Type)
public final <T> T getValue(String name, Type type)
name
, as converted to an object of the supplied
type
.
This method never returns null
.
This method does not cache the value it returns.
It is also worth noting that the MicroProfile Config
specification does not say anything about whether Converter
s are allowed to attempt to "convert" null
values from ConfigSource.getValue(String)
invocations
into non-null
objects. The MicroProfile Config TCK will fail if Converter
s _do_ attempt to work on null
values, so this
implementation never uses a null
value for
conversion.
This method is safe for use by multiple threads.
T
- the type of the values returned by this methodname
- the name of the configuration property whose value
should be returned; may be null
; passed unaltered to
ConfigSource.getValue(String)
so subject to that method's
(unspecified) preconditionstype
- the Type
representing the type any
non-null
value should be converted to if possible; must
not be null
null
NoSuchElementException
- if the requested configuration
property value does not existIllegalStateException
- if this Config
has been
closedConfigSource.getValue(String)
,
getValue(String, Class)
,
getOptionalValue(String, Class)
,
getOptionalValue(String, Type)
public final <T> T convert(String rawValue, Type type)
TypeConverter.convert(String, Type)
method
by invoking the same method on this Config
's affiliated
TypeConverter
supplied at construction time and returning
the result.
This method may return null
.
This method is safe for use by multiple threads.
convert
in interface TypeConverter
T
- the type of the return value, assumed to be assignable
to references of the supplied typerawValue
- the value to convert; may be null
type
- the Type
to which the current invocation of
this method's return value should be assignable; must not be
null
type
, or
null
NullPointerException
- if type
is null
IllegalArgumentException
- if conversion could not occur
for any reasonIllegalStateException
- if this Config
has been
closedTypeConverter.convert(String, Type)
Copyright © 2019–2021, microBean™. All rights reserved.