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:
for uniquness
-
-
Constructor Summary
Constructors Constructor Description DropwizardAwareModule()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <T> T
annotatedConfiguration(java.lang.annotation.Annotation annotation)
Search for exactly one annotated configuration value.protected <T> T
annotatedConfiguration(java.lang.Class<? extends java.lang.annotation.Annotation> qualifierType)
Search for exactly one configuration value with qualifier annotation (without attributes).protected java.lang.String
appPackage()
protected io.dropwizard.core.setup.Bootstrap<C>
bootstrap()
protected C
configuration()
protected <T,K extends T>
Kconfiguration(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 fromconfiguration(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.core.setup.Environment
environment()
protected Options
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
setConfigurationTree(ConfigurationTree configurationTree)
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
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, requestInjection, requestStaticInjection, requireBinding, requireBinding
-
-
-
-
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 interfaceConfigurationAwareModule<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 interfaceBootstrapAwareModule<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 interfaceEnvironmentAwareModule
- 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 interfaceOptionsAwareModule
- 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 interfaceConfigurationTreeAwareModule
- 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(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:
are unique declarations (declaration of the same type never appears in configuration on any level).class Config extends Configuration { Sub sub; SubExt ext; // SubExt extends Sub }
configuration(Sub.class) == sub
andconfiguration(SubExt.class) == ext
.Example of accessing server config from dropwizard configuration:
configuration(ServerFactory.class) == <DefaultServerFactory> (or SimpleServerFactory)
(see dropwizardConfiguration
class).- Type Parameters:
T
- declaration typeK
- 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 fromconfiguration(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]
, butconfigurations(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
-
annotatedConfiguration
protected <T> T annotatedConfiguration(java.lang.annotation.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, guiceNamed
annotation hasNames.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:
java.lang.IllegalStateException
- if multiple values found
-
annotatedConfiguration
protected <T> T annotatedConfiguration(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 - useannotatedConfiguration(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:
java.lang.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:
for configuration introspection details
,for available guice configuration bindings
-
environment
protected io.dropwizard.core.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.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.- Parameters:
key
- shared object keyvalue
- 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 keydefaultValue
- 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 withSharedConfigurationState
)- 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 keymessage
- exception message (could useString.format(String, Object...)
placeholders)args
- placeholder arguments for error message- Returns:
- shared object
- Throws:
java.lang.IllegalStateException
- if not value available- See Also:
SharedConfigurationState
-
-