Class GuiceyBootstrap
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.module.installer.bundle.GuiceyBootstrap
-
- All Implemented Interfaces:
GuiceyCommonRegistration<GuiceyBootstrap>
public class GuiceyBootstrap extends java.lang.Object implements GuiceyCommonRegistration<GuiceyBootstrap>
Guicey initialization object. Provides almost the same configuration methods asGuiceBundle.Builder. Also, contains dropwizard bootstrap objects. May register pure dropwizard bundles.In contrast to main builder, guicey bundle can't:
- Disable bundles (because at this stage bundles already partly processed)
- Use generic disable predicates (to not allow bundles disable, moreover it's tests-oriented feature)
- Change options (because some bundles may already apply configuration based on changed option value which will mean inconsistent state)
- Register listener, implementing
GuiceyConfigurationHook(because it's too late - all hooks were processed) - Register some special objects like custom injector factory or custom bundles lookup
- Since:
- 01.08.2015
-
-
Constructor Summary
Constructors Constructor Description GuiceyBootstrap(ConfigurationContext context, java.util.List<java.lang.Class<? extends GuiceyBundle>> bundlesPath, java.util.List<GuiceyBundle> initOrder)Create bootstrap.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <K extends io.dropwizard.core.Configuration>
io.dropwizard.core.Application<K>application()Application instance may be useful for complex (half manual) integrations where access for injector is required.<K extends io.dropwizard.core.Configuration>
io.dropwizard.core.setup.Bootstrap<K>bootstrap()Note: for application in run phase (when called fromGuiceyEnvironment), it would be too late to configure dropwizard bootstrap object.GuiceyBootstrapbundles(GuiceyBundle... bundles)Register other guicey bundles for installation.GuiceyBootstrapdisableExtensions(java.lang.Class<?>... extensions)GuiceyBootstrapdisableInstallers(java.lang.Class<? extends FeatureInstaller>... installers)GuiceyBootstrapdisableModules(java.lang.Class<? extends com.google.inject.Module>... modules)Disable both usual and overriding guice modules.GuiceyBootstrapdropwizardBundles(io.dropwizard.core.ConfiguredBundle... bundles)Shortcut for dropwizard bundles registration (instead ofbootstrap().addBundle()), but with duplicates detection and tracking in diagnostic reporting.GuiceyBootstrapextensions(java.lang.Class<?>... extensionClasses)Bundle should not rely on auto-scan mechanism and so must declare all extensions manually (this better declares bundle content and speed ups startup).GuiceyBootstrapextensionsOptional(java.lang.Class<?>... extensionClasses)The same asGuiceyCommonRegistration.extensions(Class[]), but, in case if no installer recognize extension, will be automatically disabled instead of throwing error.GuiceyBootstrapinstallers(java.lang.Class<? extends FeatureInstaller>... installers)If bundle provides new installers then they must be declared here.GuiceyBootstraplisten(GuiceyLifecycleListener... listeners)Guicey broadcast a lot of events in order to indicate lifecycle phases (GuiceyLifecycle).GuiceyBootstrapmodules(com.google.inject.Module... modules)Register guice modules.GuiceyBootstrapmodulesOverride(com.google.inject.Module... modules)Override modules (using guiceModules.override(com.google.inject.Module...)).<V,K extends java.lang.Enum<? extends Option> & Option>
Voption(K option)Read option value.<K> java.util.Optional<K>sharedState(java.lang.Class<K> key)Access shared value.<K> KsharedState(java.lang.Class<K> key, java.util.function.Supplier<K> defaultValue)Alternative shared value initialization for cases when first accessed bundle should init state value and all others just use it.<K> KsharedStateOrFail(java.lang.Class<K> 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).<K> GuiceyBootstrapshareState(java.lang.Class<K> key, K value)Share global state to be used in other bundles (during configuration).<V> voidwhenSharedStateReady(java.lang.Class<V> key, java.util.function.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).
-
-
-
Constructor Detail
-
GuiceyBootstrap
public GuiceyBootstrap(ConfigurationContext context, java.util.List<java.lang.Class<? extends GuiceyBundle>> bundlesPath, java.util.List<GuiceyBundle> initOrder)
Create bootstrap.- Parameters:
context- configuration contextbundlesPath- paths to track bundles installation loopsinitOrder- holder for bundles initialization order
-
-
Method Detail
-
installers
@SafeVarargs public final GuiceyBootstrap installers(java.lang.Class<? extends FeatureInstaller>... installers)
If bundle provides new installers then they must be declared here. Optionally, core or other 3rd party installers may be declared also to indicate dependency (duplicate installers registrations will be removed).- Parameters:
installers- feature installer classes to register- Returns:
- bootstrap instance for chained calls
- See Also:
GuiceBundle.Builder.installers(Class[])
-
bundles
public GuiceyBootstrap bundles(GuiceyBundle... bundles)
Register other guicey bundles for installation. Bundles initialized immediately (same as transitive dropwizard bundles and guice modules).Equal instances of the same type will be considered as duplicate.
- Parameters:
bundles- guicey bundles- Returns:
- bootstrap instance for chained calls
- See Also:
GuiceBundle.Builder.bundles(GuiceyBundle...)
-
dropwizardBundles
public GuiceyBootstrap dropwizardBundles(io.dropwizard.core.ConfiguredBundle... bundles)
Shortcut for dropwizard bundles registration (instead ofbootstrap().addBundle()), but with duplicates detection and tracking in diagnostic reporting. Dropwizard bundle is immediately initialized.- Parameters:
bundles- dropwizard bundles to register- Returns:
- bootstrap instance for chained calls
- See Also:
GuiceBundle.Builder.dropwizardBundles(ConfiguredBundle...)
-
disableInstallers
@SafeVarargs public final GuiceyBootstrap disableInstallers(java.lang.Class<? extends FeatureInstaller>... installers)
- Parameters:
installers- feature installer types to disable- Returns:
- bootstrap instance for chained calls
- See Also:
GuiceBundle.Builder.disableInstallers(Class[])
-
bootstrap
public <K extends io.dropwizard.core.Configuration> io.dropwizard.core.setup.Bootstrap<K> bootstrap()
Note: for application in run phase (when called fromGuiceyEnvironment), it would be too late to configure dropwizard bootstrap object.- Specified by:
bootstrapin interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Type Parameters:
K- configuration type- Returns:
- dropwizard bootstrap instance
-
application
public <K extends io.dropwizard.core.Configuration> io.dropwizard.core.Application<K> application()
Application instance may be useful for complex (half manual) integrations where access for injector is required. For example, manually registeredManagedmay access injector in it's start method by callingInjectorLookup.getInjector(io.dropwizard.core.Application).NOTE: it will work in this example, because injector access will be after injector creation. Directly inside bundle initialization method injector could not be obtained as it's not exists yet.
- Specified by:
applicationin interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Type Parameters:
K- configuration type- Returns:
- dropwizard application instance
-
option
public <V,K extends java.lang.Enum<? extends Option> & Option> V option(K option)
Read option value. Options could be set only in application rootGuiceBundle.Builder.option(Enum, Object). If value wasn't set there then default value will be returned. Null may return only if it was default value and no new value were assigned.Option access is tracked as option usage (all tracked data is available through
OptionsInfo).- Specified by:
optionin interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Type Parameters:
V- option value typeK- helper type to define option- Parameters:
option- option enum- Returns:
- assigned option value or default value
- See Also:
more options info,options example,options definition
-
modules
public GuiceyBootstrap modules(com.google.inject.Module... modules)
Register guice modules.When registration called under initialization phase (
GuiceyBootstrap), neither configuration nor environment objects are available yet. If you need them for module, then you can wrap it withDropwizardAwareModuleor register modules in run phase (insideGuiceyBundle.run(GuiceyEnvironment)).When registration called under run phase (
GuiceyEnvironment), environment and configuration objects already available (no need to use Aware* interfaces, but if you will they will also work, of course). This may look like misconception because configuration appear not in configuration phase, but it's not: for example, in pure dropwizard you can register jersey configuration modules in run phase too. This brings the simplicity of use: 3rd party guice modules often require configuration values to be passed directly to constructor, which is impossible in initialization phase (and so you have to use Aware* workarounds).- Specified by:
modulesin interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Parameters:
modules- one or more guice modules- Returns:
- builder instance for chained calls
- See Also:
GuiceBundle.Builder.modules(com.google.inject.Module...),DropwizardAwareModule
-
modulesOverride
public GuiceyBootstrap modulesOverride(com.google.inject.Module... modules)
Override modules (using guiceModules.override(com.google.inject.Module...)).- Specified by:
modulesOverridein interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Parameters:
modules- overriding modules- Returns:
- builder instance for chained calls
- See Also:
GuiceBundle.Builder.modulesOverride(com.google.inject.Module...)
-
extensions
public GuiceyBootstrap extensions(java.lang.Class<?>... extensionClasses)
Bundle should not rely on auto-scan mechanism and so must declare all extensions manually (this better declares bundle content and speed ups startup).NOTE: startup will fail if bean not recognized by installers. Use
GuiceyCommonRegistration.extensionsOptional(Class[])to register optional extension.Alternatively, you can manually bind extensions in guice module and they would be recognized (
GuiceyOptions.AnalyzeGuiceModules).- Specified by:
extensionsin interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Parameters:
extensionClasses- extension bean classes to register- Returns:
- builder instance for chained calls
- See Also:
GuiceBundle.Builder.extensions(Class[])
-
extensionsOptional
public GuiceyBootstrap extensionsOptional(java.lang.Class<?>... extensionClasses)
The same asGuiceyCommonRegistration.extensions(Class[]), but, in case if no installer recognize extension, will be automatically disabled instead of throwing error. Useful for optional extensions declaration in 3rd party bundles (where it is impossible to be sure what other bundles will be used and so what installers will be available).Alternatively, you can manually bind extensions in guice module and they would be recognized (
GuiceyOptions.AnalyzeGuiceModules). Extensions with no available target installer will simply wouldn't be detected (because installers used for recognition) and so there is no need to mark them as optional in this case.- Specified by:
extensionsOptionalin interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Parameters:
extensionClasses- extension bean classes to register- Returns:
- builder instance for chained calls
-
disableExtensions
public GuiceyBootstrap disableExtensions(java.lang.Class<?>... extensions)
- Specified by:
disableExtensionsin interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Parameters:
extensions- extensions to disable (manually added, registered by bundles or with classpath scan)- Returns:
- builder instance for chained calls
- See Also:
GuiceBundle.Builder.disableExtensions(Class[])
-
disableModules
@SafeVarargs public final GuiceyBootstrap disableModules(java.lang.Class<? extends com.google.inject.Module>... modules)
Disable both usual and overriding guice modules.If bindings analysis is not disabled, could also disable inner (transitive) modules, but only inside normal modules.
- Specified by:
disableModulesin interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Parameters:
modules- guice module types to disable- Returns:
- builder instance for chained calls
- See Also:
GuiceBundle.Builder.disableModules(Class[])
-
listen
public GuiceyBootstrap listen(GuiceyLifecycleListener... listeners)
Guicey broadcast a lot of events in order to indicate lifecycle phases (GuiceyLifecycle). This could be useful for diagnostic logging (likeGuiceBundle.Builder.printLifecyclePhases()) or to implement special behaviours on installers, bundles, modules extensions (listeners have access to everything). For example,ConfigurationAwareModulelike support for guice modules could be implemented with listeners.Configuration items (modules, extensions, bundles) are not aware of each other and listeners could be used to tie them. For example, to tell bundle if some other bundles registered (limited applicability, but just for example).
You can also use
GuiceyLifecycleAdapterwhen you need to handle multiple events (it replaces direct events handling with simple methods).Listener is not registered if equal listener were already registered (
Setused as listeners storage), so if you need to be sure that only one instance of some listener will be used implementObject.equals(Object)andObject.hashCode().- Specified by:
listenin interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Parameters:
listeners- guicey lifecycle listeners- Returns:
- builder instance for chained calls
- See Also:
GuiceyLifecycle,GuiceyLifecycleAdapter,UniqueGuiceyLifecycleListener
-
shareState
public <K> GuiceyBootstrap shareState(java.lang.Class<K> key, K 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)!During application strartup, shared state could be requested with a static call
SharedConfigurationState.getStartupInstance(), but only from main thread.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(Application, Class).In some cases, it is preferred to use bundle 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
GuiceyCommonRegistration.sharedState(Class, java.util.function.Supplier)instead.- Specified by:
shareStatein interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Type Parameters:
K- shared object type- Parameters:
key- shared object keyvalue- shared object- Returns:
- builder instance for chained calls
- See Also:
SharedConfigurationState
-
sharedState
public <K> K sharedState(java.lang.Class<K> key, java.util.function.Supplier<K> defaultValue)Alternative shared value initialization for cases when first accessed bundle should init state value and all others 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.
- Specified by:
sharedStatein interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Type Parameters:
K- shared object type- Parameters:
key- shared object keydefaultValue- default object provider- Returns:
- shared object (possibly just created)
- See Also:
SharedConfigurationState
-
sharedState
public <K> java.util.Optional<K> sharedState(java.lang.Class<K> key)
Access shared value.- Specified by:
sharedStatein interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Type Parameters:
K- shared object type- Parameters:
key- shared object key- Returns:
- shared object
- See Also:
SharedConfigurationState
-
sharedStateOrFail
public <K> K sharedStateOrFail(java.lang.Class<K> 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).- Specified by:
sharedStateOrFailin interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Type Parameters:
K- 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
- See Also:
SharedConfigurationState
-
whenSharedStateReady
public <V> void whenSharedStateReady(java.lang.Class<V> key, java.util.function.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).- Specified by:
whenSharedStateReadyin interfaceGuiceyCommonRegistration<GuiceyBootstrap>- Type Parameters:
V- value type- Parameters:
key- shared object keyaction- action to execute when value would be set
-
-