Class DropwizardAwareModule<C extends io.dropwizard.Configuration>

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.String appPackage()  
      protected io.dropwizard.setup.Bootstrap<C> bootstrap()  
      protected C configuration()  
      protected <T,​K extends T>
      K
      configuration​(java.lang.Class<T> type)
      May be used to access unique sub configuration object.
      protected <T> T configuration​(java.lang.String yamlPath)
      May be used to access current configuration value by exact path.
      protected <T> java.util.List<? extends T> configurations​(java.lang.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.
      protected ConfigurationTree configurationTree()
      Raw configuration introspection info.
      protected io.dropwizard.setup.Environment environment()  
      protected Options options()  
      void setBootstrap​(io.dropwizard.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 setConfigurationTree​(ConfigurationTree configurationTree)
      Mathod will be called just before injector initialization.
      void setEnvironment​(io.dropwizard.setup.Environment environment)
      Method will be called just before injector initialization.
      void setOptions​(Options options)
      Method will be called just before injector initialization.
      protected <T> java.util.Optional<T> sharedState​(java.lang.Class<?> key)
      Access shared value.
      <T> T sharedState​(java.lang.Class<?> key, java.util.function.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​(java.lang.Class<?> key, java.lang.String message, java.lang.Object... args)
      Used to access shared state value and immediately fail if value not yet set (most likely, due to incorrect configuration order).
      void shareState​(java.lang.Class<?> key, java.lang.Object value)
      Share global state to be used in other bundles (during configuration).
      • 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, requestStaticInjection, requireBinding, requireBinding
      • Methods inherited from class java.lang.Object

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

      • DropwizardAwareModule

        public DropwizardAwareModule()
    • Method Detail

      • 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.Configuration>
        Parameters:
        configuration - configuration object
      • setBootstrap

        public void setBootstrap​(io.dropwizard.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.Configuration>
        Parameters:
        bootstrap - bootstrap object
      • setEnvironment

        public void setEnvironment​(io.dropwizard.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
      • bootstrap

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

        protected C configuration()
        Returns:
        application configuration
      • configuration

        protected <T> T configuration​(java.lang.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:
        for custom configuration searches
      • configuration

        protected <T,​K extends T> K configuration​(java.lang.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:
        for custom configuration searches
      • configurations

        protected <T> java.util.List<? extends T> configurations​(java.lang.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:
        for custom configuration searches
      • 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:
        for configuration introspection details, for available guice configuration bindings
      • environment

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

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

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

        public void shareState​(java.lang.Class<?> key,
                               java.lang.Object 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.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.

        Parameters:
        key - shared object key
        value - shared object
        See Also:
        SharedConfigurationState
      • sharedState

        public <T> T sharedState​(java.lang.Class<?> key,
                                 java.util.function.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:
        SharedConfigurationState
      • sharedState

        protected <T> java.util.Optional<T> sharedState​(java.lang.Class<?> 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:
        SharedConfigurationState
      • sharedStateOrFail

        protected <T> T sharedStateOrFail​(java.lang.Class<?> key,
                                          java.lang.String message,
                                          java.lang.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:
        java.lang.IllegalStateException - if not value available
        See Also:
        SharedConfigurationState