Class DropwizardAwareModule<C extends io.dropwizard.core.Configuration>

java.lang.Object
com.google.inject.AbstractModule
ru.vyarus.dropwizard.guice.module.support.DropwizardAwareModule<C>
Type Parameters:
C - configuration type
All Implemented Interfaces:
com.google.inject.Module, BootstrapAwareModule<C>, ConfigurationAwareModule<C>, ConfigurationTreeAwareModule, EnvironmentAwareModule, OptionsAwareModule
Direct Known Subclasses:
GuiceBootstrapModule, UniqueDropwizardAwareModule

public abstract class DropwizardAwareModule<C extends io.dropwizard.core.Configuration> extends com.google.inject.AbstractModule implements EnvironmentAwareModule, BootstrapAwareModule<C>, ConfigurationAwareModule<C>, ConfigurationTreeAwareModule, OptionsAwareModule
Base module to avoid boilerplate. It's not required to extend it, but useful if dropwizard objects required in module: no need to manually implement interfaces.
Since:
06.06.2015
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected <T> T
    Search for exactly one annotated configuration value.
    protected <T> T
    annotatedConfiguration(Class<? extends Annotation> qualifierType)
    Search for exactly one configuration value with qualifier annotation (without attributes).
    protected String
     
    protected io.dropwizard.core.setup.Bootstrap<C>
     
    protected C
     
    protected <T, K extends T>
    K
    May be used to access unique sub configuration object.
    protected <T> T
    May be used to access current configuration value by exact path.
    protected <T> List<? extends T>
    IMPORTANT: method semantic is different from configuration(Class), which use direct class declaration match, whereas this method searches by all assignable types.
    Raw configuration introspection info.
    protected io.dropwizard.core.setup.Environment
     
    protected Options
     
    void
    setBootstrap(io.dropwizard.core.setup.Bootstrap<C> bootstrap)
    Method will be called just before injector initialization.
    void
    setConfiguration(C configuration)
    Method will be called just before injector initialization.
    void
    Mathod will be called just before injector initialization.
    void
    setEnvironment(io.dropwizard.core.setup.Environment environment)
    Method will be called just before injector initialization.
    void
    Method will be called just before injector initialization.
    protected <T> Optional<T>
    Access shared value.
    <T> T
    sharedState(Class<T> key, Supplier<T> defaultValue)
    Alternative shared value initialization for cases when first accessed bundle should init state value and all other just use it.
    protected <T> T
    sharedStateOrFail(Class<T> key, String message, Object... args)
    Used to access shared state value and immediately fail if value not yet set (most likely, due to incorrect configuration order).
    <V> void
    shareState(Class<V> key, V value)
    Share global state to be used in other bundles (during configuration).
    protected <V> void
    whenSharedStateReady(Class<V> key, Consumer<V> action)
    Reactive shared value access: if value already available action called immediately, otherwise action would be called when value set (note that value could be set only once).

    Methods inherited from class com.google.inject.AbstractModule

    addError, addError, addError, bind, bind, bind, bindConstant, binder, bindInterceptor, bindListener, bindListener, bindScope, configure, configure, convertToTypes, currentStage, getMembersInjector, getMembersInjector, getProvider, getProvider, install, requestInjection, requestInjection, requestStaticInjection, requireBinding, requireBinding

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • DropwizardAwareModule

      public DropwizardAwareModule()
  • Method Details

    • setConfiguration

      public void setConfiguration(C configuration)
      Description copied from interface: ConfigurationAwareModule
      Method will be called just before injector initialization.
      Specified by:
      setConfiguration in interface ConfigurationAwareModule<C extends io.dropwizard.core.Configuration>
      Parameters:
      configuration - configuration object
    • setBootstrap

      public void setBootstrap(io.dropwizard.core.setup.Bootstrap<C> bootstrap)
      Description copied from interface: BootstrapAwareModule
      Method will be called just before injector initialization.
      Specified by:
      setBootstrap in interface BootstrapAwareModule<C extends io.dropwizard.core.Configuration>
      Parameters:
      bootstrap - bootstrap object
    • setEnvironment

      public void setEnvironment(io.dropwizard.core.setup.Environment environment)
      Description copied from interface: EnvironmentAwareModule
      Method will be called just before injector initialization.
      Specified by:
      setEnvironment in interface EnvironmentAwareModule
      Parameters:
      environment - environment object
    • setOptions

      public void setOptions(Options options)
      Description copied from interface: OptionsAwareModule
      Method will be called just before injector initialization.
      Specified by:
      setOptions in interface OptionsAwareModule
      Parameters:
      options - options object
    • setConfigurationTree

      public void setConfigurationTree(ConfigurationTree configurationTree)
      Description copied from interface: ConfigurationTreeAwareModule
      Mathod will be called just before injector initialization.
      Specified by:
      setConfigurationTree in interface ConfigurationTreeAwareModule
      Parameters:
      configurationTree - introspected configuration object
    • bootstrap

      protected io.dropwizard.core.setup.Bootstrap<C> bootstrap()
      Returns:
      application bootstrap object
    • configuration

      protected C configuration()
      Returns:
      application configuration
    • configuration

      protected <T> T configuration(String yamlPath)
      May be used to access current configuration value by exact path. This is helpful for modules universality: suppose bundle X requires configuration object XConf, which is configured somewhere inside application configuration. We can require configuration path in module constructor and use it to access required configuration object: new X("sub.config.path").
      Type Parameters:
      T - value type
      Parameters:
      yamlPath - target value yaml path
      Returns:
      configuration value by path or null if value is null or path not exists
      See Also:
    • configuration

      protected <T, K extends T> K configuration(Class<T> type)
      May be used to access unique sub configuration object. This is helpful for modules universality: suppose module X requires configuration object XConf and we are sure that only one declaration of XConf would be used in target configuration class, then we can simply request it: configuration(XConf.class) == <instance of XConf or null>.

      Note that uniqueness is checked by declaration class:

      class Config extends Configuration {
           Sub sub;
           SubExt ext; // SubExt extends Sub
       }
      are unique declarations (declaration of the same type never appears in configuration on any level). configuration(Sub.class) == sub and configuration(SubExt.class) == ext.

      Example of accessing server config from dropwizard configuration: configuration(ServerFactory.class) == <DefaultServerFactory> (or SimpleServerFactory) (see dropwizard Configuration class).

      Type Parameters:
      T - declaration type
      K - required value type (may be the same or extending type)
      Parameters:
      type - target configuration declaration type
      Returns:
      unique configuration value or null if value is null or no declaration found
      See Also:
    • configurations

      protected <T> List<? extends T> configurations(Class<T> type)
      IMPORTANT: method semantic is different from configuration(Class), which use direct class declaration match, whereas this method searches by all assignable types.
      class Config extends Configuration {
           Sub sub;
           SubExt ext; // SubExt extends Sub
       }
      configurations(Sub.class) == [sub, ext], but configurations(SubExt.class) == [ext].

      Useful when multiple sub configuration objects could be used and all of them are required in some universal module.

      Note: only custom types may be used (sub configuration objects), not Integer, Boolean, List, etc.

      Type Parameters:
      T - value type
      Parameters:
      type - target configuration type
      Returns:
      list of configuration values with required type or empty list
      See Also:
    • annotatedConfiguration

      protected <T> T annotatedConfiguration(Annotation annotation)
      Search for exactly one annotated 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, guice Named annotation has Names.named(String): it is important that real annotation instance and "pseudo" annotation object would be equal.

      For annotations without attributes use annotation type: annotatedConfiguration(Class).

      For multiple values use configurationTree().annotatedValues().

      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:
      IllegalStateException - if multiple values found
    • annotatedConfiguration

      protected <T> T annotatedConfiguration(Class<? extends Annotation> qualifierType)
      Search for exactly one configuration value with qualifier annotation (without attributes). For cases when annotation with attributes used - use annotatedConfiguration(java.lang.annotation.Annotation) (current method would search only by annotation type, ignoring any (possible) attributes).

      For multiple values use configurationTree().annotatedValues().

      Type Parameters:
      T - value type
      Parameters:
      qualifierType - qualifier annotation type
      Returns:
      qualified configuration value or null
      Throws:
      IllegalStateException - if multiple values found
    • configurationTree

      protected ConfigurationTree configurationTree()
      Raw configuration introspection info. Could be used for more sophisticated configuration searches then provided in shortcut methods.

      Note that configuration is analyzed using jackson serialization api, so not all configured properties could be visible (when property getter is not exists or field not annotated).

      Returned object contains all resolved configuration paths. Any path element could be traversed like a tree. See find* and value* methods as an examples of how stored paths could be traversed.

      Returns:
      detailed configuration object
      See Also:
    • environment

      protected io.dropwizard.core.setup.Environment environment()
      Returns:
      application environment
    • appPackage

      protected String appPackage()
      Returns:
      application class package (most likely root package for entire application)
    • options

      protected Options options()
      Returns:
      options accessor object
    • shareState

      public <V> void shareState(Class<V> key, V value)
      Share global state to be used in other bundles (during configuration). This was added for very special cases when shared state is unavoidable (to not re-invent the wheel each time)!

      It is preferred to initialize shared state under initialization phase to avoid problems related to initialization order (assuming state is used under run phase). But, in some cases, it is not possible.

      Internally, state is linked to application instance, so it would be safe to use with concurrent tests. Value could be accessed statically with application instance: SharedConfigurationState.lookup(io.dropwizard.core.Application, Class).

      During application strartup, shared state could be requested with a static call SharedConfigurationState.getStartupInstance(), but only from main thread.

      In some cases, it is preferred to use module class as key. Value could be set only once (to prevent hard to track situations)

      If initialization point could vary (first access should initialize it) use sharedState(Class, java.util.function.Supplier) instead.

      Type Parameters:
      V - shared object type
      Parameters:
      key - shared object key
      value - shared object
      See Also:
    • sharedState

      public <T> T sharedState(Class<T> key, Supplier<T> defaultValue)
      Alternative shared value initialization for cases when first accessed bundle should init state value and all other just use it.

      It is preferred to initialize shared state under initialization phase to avoid problems related to initialization order (assuming state is used under run phase). But, in some cases, it is not possible.

      Type Parameters:
      T - shared object type
      Parameters:
      key - shared object key
      defaultValue - default object provider
      Returns:
      shared object (possibly just created)
      See Also:
    • sharedState

      protected <T> Optional<T> sharedState(Class<T> key)
      Access shared value. Shared state value assumed to be initialized under initialization phase by bundle (but you can workaround this limitation by accessing shared state statically with SharedConfigurationState)
      Type Parameters:
      T - shared object type
      Parameters:
      key - shared object key
      Returns:
      shared object
      See Also:
    • sharedStateOrFail

      protected <T> T sharedStateOrFail(Class<T> key, String message, Object... args)
      Used to access shared state value and immediately fail if value not yet set (most likely, due to incorrect configuration order).
      Type Parameters:
      T - shared object type
      Parameters:
      key - shared object key
      message - exception message (could use String.format(String, Object...) placeholders)
      args - placeholder arguments for error message
      Returns:
      shared object
      Throws:
      IllegalStateException - if not value available
      See Also:
    • whenSharedStateReady

      protected <V> void whenSharedStateReady(Class<V> key, Consumer<V> action)
      Reactive shared value access: if value already available action called immediately, otherwise action would be called when value set (note that value could be set only once).
      Type Parameters:
      V - value type
      Parameters:
      key - shared object key
      action - action to execute when value would be set