Class ConfigurationTree
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.module.yaml.ConfigurationTree
-
public class ConfigurationTree extends java.lang.Object
Detailed yaml configuration. This object provides direct access to sub configuration objects and configuration values by yaml path. This is handy in many cases when only one configuration value is required in class, but entire configuration object have to be bound to access it. Unique sub configuration objects may be used by re-usable parts: you don't need to know actual root configuration class, just be sure that sub configuration class is present somewhere inside only once (e.g. database configuration object).Used for configuration bindings in guice context.
Configuration is introspected using jersey serialization api: this means only values visible for jersey serialization will be presented.
Each configuration property descriptor provides access to root and child paths, so tree-like traversals are possible (see find* methods below for examples).
Object itself could be injected as guice bean
@Inject ConfigurationTree config
. Note that it did not contains root configuration instance, only properties tree.Also, object is accessible inside guicey bundles
GuiceyEnvironment.configurationTree()
and guice modules:ConfigurationTreeAwareModule
.Qualified configuration paths also detected: fields or getters annotated with qualifier annotation. Qualifier annotation is an annotation annotated with
BindingAnnotation
(e.g.Named
) orQualifier
(e.g.Named
).- Since:
- 04.05.2018
- See Also:
ConfigBindingModule
,GuiceyOptions.BindConfigurationByPath
-
-
Constructor Summary
Constructors Constructor Description ConfigurationTree(java.util.List<java.lang.Class> rootTypes)
ConfigurationTree(java.util.List<java.lang.Class> rootTypes, java.util.List<ConfigPath> paths, java.util.List<ConfigPath> uniqueTypePaths)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> T
annotatedValue(java.lang.annotation.Annotation annotation)
Search for exactly one qualified configuration value.<T> T
annotatedValue(java.lang.Class<? extends java.lang.annotation.Annotation> qualifierType)
Search for exactly one configuration value with qualifier annotation (without attributes).<T> java.util.Set<T>
annotatedValues(java.lang.annotation.Annotation annotation)
Search configuration values by qualifier annotation.<T> java.util.Set<T>
annotatedValues(java.lang.Class<? extends java.lang.annotation.Annotation> qualifierType)
Search configuration values by qualifier annotation (without attributes).java.util.List<ConfigPath>
findAllByAnnotation(java.lang.annotation.Annotation annotation)
Search configuration paths annotated by qualifier annotation.java.util.List<ConfigPath>
findAllByAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> qualifierType)
Search configuration paths annotated by qualifier annotation (without attributes).java.util.List<ConfigPath>
findAllByType(java.lang.Class<?> type)
Useful for searching multiple custom types.java.util.List<ConfigPath>
findAllFrom(java.lang.Class<? extends io.dropwizard.core.Configuration> confType)
Useful for getting all configurations for exact configuration class.java.util.List<ConfigPath>
findAllRootPaths()
For example, it would always contain logging, metrics and server paths from dropwizard configuration.java.util.List<ConfigPath>
findAllRootPathsFrom(java.lang.Class<? extends io.dropwizard.core.Configuration> confType)
The same asfindAllRootPaths()
, but returns only paths started in provided configuration.ConfigPath
findByAnnotation(java.lang.annotation.Annotation annotation)
Search for exactly one configuration path annotated with qualifier annotation.ConfigPath
findByAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> qualifierType)
Search for exactly one configuration path annotated with qualified annotation.ConfigPath
findByPath(java.lang.String path)
Case-insensitive exact match.java.util.List<ConfigPath>
getPaths()
Note that paths are computed using jackson serialization logic.java.util.List<java.lang.Class>
getRootTypes()
java.util.List<ConfigPath>
getUniqueTypePaths()
Such unique objects could be used for internal configurations instead of root configuration class (very handy for generic extensions).<T> T
valueByPath(java.lang.String path)
class Config extends Configuration { SubConfig sub = { // shown instance contents String val = "something" } }
<T,K extends T>
KvalueByType(java.lang.Class<T> type)
Behaviour is the same asvaluesByType(Class)
, but only first element is returned.<T,K extends T>
KvalueByUniqueDeclaredType(java.lang.Class<T> type)
Search value by unique type declaration.<T> java.util.List<? extends T>
valuesByType(java.lang.Class<T> type)
Useful to resolve sub configuration objects.
-
-
-
Constructor Detail
-
ConfigurationTree
public ConfigurationTree(java.util.List<java.lang.Class> rootTypes)
-
ConfigurationTree
public ConfigurationTree(java.util.List<java.lang.Class> rootTypes, java.util.List<ConfigPath> paths, java.util.List<ConfigPath> uniqueTypePaths)
-
-
Method Detail
-
getRootTypes
public java.util.List<java.lang.Class> getRootTypes()
- Returns:
- configuration hierarchy classes (including
Configuration
) and custom interfaces
-
getPaths
public java.util.List<ConfigPath> getPaths()
Note that paths are computed using jackson serialization logic. This means that not all deserializable properties could be serialized back, and so not present in the list. For example, if annotated configuration property setter performs immediate value transformation and bean did not provide getter then such value is impossible to read back from configuration.Paths are sorted by configuration class and path name (so more custom properties will be at the beginning and dropwizard properties at the end).
- Returns:
- all configuration value paths (including all steps, e.g. "sub", "sub.smth")
-
getUniqueTypePaths
public java.util.List<ConfigPath> getUniqueTypePaths()
Such unique objects could be used for internal configurations instead of root configuration class (very handy for generic extensions).Note that custom type is unique only if it does not appear anywhere else (on any level). Exact type matching: e.g. if some extending type is declared somewhere it would be considered as different (as class is different, hierarchy not counted).
- Returns:
- list of all configuration value paths, containing unique custom objects
-
findByPath
public ConfigPath findByPath(java.lang.String path)
Case-insensitive exact match.- Parameters:
path
- path to find descriptor for- Returns:
- path descriptor or null if not found
-
findAllByAnnotation
public java.util.List<ConfigPath> findAllByAnnotation(java.lang.annotation.Annotation annotation)
Search configuration paths annotated by qualifier annotation. It is not possible to provide the exact annotation instance, but you can create a class implementing annotation and use it for search. For example, guiceNamed
annotation hasNames.named(String)
: it is important that real annotation instance and "pseudo" annotation object would be equal (by equals).For annotations without attributes use annotation type:
findAllByAnnotation(Class)
.- Parameters:
annotation
- annotation instance (equal object) to search for annotated config paths- Returns:
- list of annotated (on field or getter) configuration paths
-
findAllByAnnotation
public java.util.List<ConfigPath> findAllByAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> qualifierType)
Search configuration paths annotated by qualifier annotation (without attributes). For cases when annotation with attributes used - usefindAllByAnnotation(java.lang.annotation.Annotation)
(current method would also work for all annotations because it compares by type only, but it might be not what was searched for).- Parameters:
qualifierType
- qualifier annotation type- Returns:
- list of annotated (on field or getter) configuration paths
-
findByAnnotation
public ConfigPath findByAnnotation(java.lang.annotation.Annotation annotation)
Search for exactly one configuration path annotated with qualifier annotation. It is not possible to provide the exact annotation instance, but you can create a class implementing annotation and use it for search. For example, guiceNamed
annotation hasNames.named(String)
: it is important that real annotation instance and "pseudo" annotation object would be equal (by equals).For annotations without attributes use annotation type:
findByAnnotation(Class)
.- Parameters:
annotation
- annotation instance (equal object) to search for an annotated config path- Returns:
- found configuration path or null
- Throws:
java.lang.IllegalStateException
- if multiple paths found
-
findByAnnotation
public ConfigPath findByAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> qualifierType)
Search for exactly one configuration path annotated with qualified annotation. For cases when annotation with attributes used - usefindByAnnotation(java.lang.annotation.Annotation)
(current method would search only by annotation type, ignoring any (possible) attributes).- Parameters:
qualifierType
- qualifier annotation type- Returns:
- found configuration path or null
- Throws:
java.lang.IllegalStateException
- if multiple paths found
-
findAllByType
public java.util.List<ConfigPath> findAllByType(java.lang.Class<?> type)
Useful for searching multiple custom types.class Config { SubConf field1; // just to show that value type taken into account Object field2 = new SubConfExt(); }
findAllByType(SubConf.class) == [filed1, field2]
because filed1 is declared with required type and field2 value is compatible with requested type.- Parameters:
type
- type to search for- Returns:
- all paths with the same or sub type for specified type or empty list
-
findAllFrom
public java.util.List<ConfigPath> findAllFrom(java.lang.Class<? extends io.dropwizard.core.Configuration> confType)
Useful for getting all configurations for exact configuration class.class MyConf extends Configuration { String val SubConf sub }
findAllFrom(MyConf) == [val, sub, sub.val1, sub.val2]
- all property paths, started in this class (all properties fromConfiguration
are ignored).- Parameters:
confType
- configuration type to get all properties from- Returns:
- all properties declared in (originated in for sub object paths) required configuration class.
-
findAllRootPaths
public java.util.List<ConfigPath> findAllRootPaths()
For example, it would always contain logging, metrics and server paths from dropwizard configuration.- Returns:
- all root objects and direct root values (only paths 1 level paths)
- See Also:
findAllRootPathsFrom(Class)
-
findAllRootPathsFrom
public java.util.List<ConfigPath> findAllRootPathsFrom(java.lang.Class<? extends io.dropwizard.core.Configuration> confType)
The same asfindAllRootPaths()
, but returns only paths started in provided configuration.- Parameters:
confType
- configuration type to get all properties from- Returns:
- all root objects and direct root values (only paths 1 level paths) declared in specified configuration class (directly)
- See Also:
findAllRootPaths()
-
valueByPath
public <T> T valueByPath(java.lang.String path)
.class Config extends Configuration { SubConfig sub = { // shown instance contents String val = "something" } }
valueByPath("sub.val") == "something"
Note: keep in mind that not all values could be accessible (read class javadoc)
- Type Parameters:
T
- value type- Parameters:
path
- yaml path (case insensitive)- Returns:
- configuration value on yaml path or null if value is null or path not found
-
valuesByType
public <T> java.util.List<? extends T> valuesByType(java.lang.Class<T> type)
Useful to resolve sub configuration objects.class Config extends Configuration { SubOne sub1 = ... SubTwo sub2 = ... SubTwo sub2_1 = ... SubTwoExt sub2_2 = ... // SubTwoExt extends SubTwo }
valuesByType(SubOne.class) == [<sub1>]
valuesByType(SubTwo.class) == [<sub2>, <sub2_1>, <sub2_2>]
Note that type matching is not exact: any extending types are also accepted. Type is compared with declaration type (inside configuration class).
- Type Parameters:
T
- value type- Parameters:
type
- type of required sub configuration objects- Returns:
- found sub configurations without nulls (properties with null value)
- See Also:
for uniqe objects access
-
valueByType
public <T,K extends T> K valueByType(java.lang.Class<T> type)
Behaviour is the same asvaluesByType(Class)
, but only first element is returned.Note: uniqueness not guaranteed!
- Type Parameters:
T
- value typeK
- actual expected sub type (may be the same)- Parameters:
type
- type of required sub configuration object- Returns:
- value of first compatible type occurrence (first not null value) or null
- See Also:
for guaranteed uniquness
-
valueByUniqueDeclaredType
public <T,K extends T> K valueByUniqueDeclaredType(java.lang.Class<T> type)
Search value by unique type declaration.class Config extends Configuration { SubOne sub1 = ... SubTwo sub2 = ... SubTwoExt sub3 = ... // SubTwoExt extends SubTwo }
valueByUniqueDeclaredType(SubOne.class) == <sub1>
,valueByUniqueDeclaredType(SubTwo.class) == <sub2>
valueByUniqueDeclaredType(SubTwoExt.class) == <sub3>
Note that direct declaration comparison used! For example,
valuesByType(SubTwo) == [<sub2>, <sub3>]
would consider sub2 and sub3 as the same type, butvalueByUniqueDeclaredType
will not!Type declaration is not unique if somewhere (maybe in some sub-sub configuration object) declaration with the same type exists. If you need to treat uniqueness only by first path level, then write search function yourself using
findAllRootPaths()
orfindAllRootPathsFrom(Class)
.- Type Parameters:
T
- value typeK
- actual expected sub type (may be the same)- Parameters:
type
- required target declaration type- Returns:
- uniquely declared sub configuration object or null if declaration not found or value null
-
annotatedValues
public <T> java.util.Set<T> annotatedValues(java.lang.annotation.Annotation annotation)
Search configuration values by qualifier annotation. It is not possible to provide the exact annotation instance, but you can create a class implementing annotation and use it for search. For example, guiceNamed
annotation hasNames.named(String)
: it is important that real annotation instance and "pseudo" annotation object would be equal (by equals).For annotations without attributes use annotation type:
annotatedValue(Class)
.- Type Parameters:
T
- target value type- Parameters:
annotation
- annotation instance (equal object) to search for annotated config paths- Returns:
- all non-null annotated configuration values
- See Also:
findAllByAnnotation(java.lang.annotation.Annotation)
-
annotatedValues
public <T> java.util.Set<T> annotatedValues(java.lang.Class<? extends java.lang.annotation.Annotation> qualifierType)
Search configuration values by qualifier annotation (without attributes). For cases when annotation with attributes used - useannotatedValues(java.lang.annotation.Annotation)
(current method would also work for all annotations because it compares by type only, but it might be not what was searched for).- Type Parameters:
T
- target value type- Parameters:
qualifierType
- qualifier annotation type- Returns:
- all non-null annotated configuration values
- See Also:
findAllByAnnotation(Class)
-
annotatedValue
public <T> T annotatedValue(java.lang.annotation.Annotation annotation)
Search for exactly one qualified configuration value. It is not possible to provide the exact annotation instance, but you can create a class implementing annotation and use it for search. For example, guiceNamed
annotation hasNames.named(String)
: it is important that real annotation instance and "pseudo" annotation object would be equal (by equals).For annotations without attributes use annotation type:
annotatedValue(Class)
.- Type Parameters:
T
- value type- Parameters:
annotation
- annotation instance (equal object) to search for an annotated config path- Returns:
- qualified configuration value or null
- Throws:
java.lang.IllegalStateException
- if multiple values found- See Also:
findByAnnotation(java.lang.annotation.Annotation)
-
annotatedValue
public <T> T annotatedValue(java.lang.Class<? extends java.lang.annotation.Annotation> qualifierType)
Search for exactly one configuration value with qualifier annotation (without attributes). For cases when annotation with attributes used - usefindByAnnotation(java.lang.annotation.Annotation)
(current method would search only by annotation type, ignoring any (possible) attributes).- Type Parameters:
T
- value type- Parameters:
qualifierType
- qualifier annotation type- Returns:
- qualified configuration value or null
- Throws:
java.lang.IllegalStateException
- if multiple values found- See Also:
findByAnnotation(Class)
-
-