Class 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.

    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
      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.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.Configuration> confType)
      The same as findAllRootPaths(), but returns only paths started in provided configuration.
      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>
      K
      valueByType​(java.lang.Class<T> type)
      Behaviour is the same as valuesByType(Class), but only first element is returned.
      <T,​K extends T>
      K
      valueByUniqueDeclaredType​(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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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
      • 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.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 from Configuration 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.Configuration> confType)
        The same as findAllRootPaths(), 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 as valuesByType(Class), but only first element is returned.

        Note: uniqueness not guaranteed!

        Type Parameters:
        T - value type
        K - 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, but valueByUniqueDeclaredType 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() or findAllRootPathsFrom(Class).

        Type Parameters:
        T - value type
        K - 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