Class GuiceyBootstrap


  • public class GuiceyBootstrap
    extends java.lang.Object
    Guicey initialization object. Provides almost the same configuration methods as GuiceBundle.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
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      io.dropwizard.Application application()
      Application instance may be useful for complex (half manual) integrations where access for injector is required.
      <T extends io.dropwizard.Configuration>
      io.dropwizard.setup.Bootstrap<T>
      bootstrap()
      Note: application is already in run phase, so it's too late to configure dropwizard bootstrap object.
      GuiceyBootstrap bundles​(GuiceyBundle... bundles)
      Register other guicey bundles for installation.
      GuiceyBootstrap disableExtensions​(java.lang.Class<?>... extensions)  
      GuiceyBootstrap disableInstallers​(java.lang.Class<? extends FeatureInstaller>... installers)  
      GuiceyBootstrap disableModules​(java.lang.Class<? extends com.google.inject.Module>... modules)
      Disable both usual and overriding guice modules.
      GuiceyBootstrap dropwizardBundles​(io.dropwizard.ConfiguredBundle... bundles)
      Shortcut for dropwizard bundles registration (instead of bootstrap().addBundle()), but with duplicates detection and tracking in diagnostic reporting.
      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).
      GuiceyBootstrap extensionsOptional​(java.lang.Class<?>... extensionClasses)
      The same as extensions(Class[]), but, in case if no installer recognize extension, will be automatically disabled instead of throwing error.
      GuiceyBootstrap installers​(java.lang.Class<? extends FeatureInstaller>... installers)
      If bundle provides new installers then they must be declared here.
      GuiceyBootstrap listen​(GuiceyLifecycleListener... listeners)
      Guicey broadcast a lot of events in order to indicate lifecycle phases (GuiceyLifecycle).
      GuiceyBootstrap modules​(com.google.inject.Module... modules)
      Register guice modules.
      GuiceyBootstrap modulesOverride​(com.google.inject.Module... modules)
      Override modules (using guice Modules.override(Module...)).
      <V,​T extends java.lang.Enum & Option>
      V
      option​(T option)
      Read option value.
      <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.
      <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).
      GuiceyBootstrap shareState​(java.lang.Class<?> key, java.lang.Object value)
      Share global state to be used in other bundles (during configuration).
      • Methods inherited from class java.lang.Object

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

      • bootstrap

        public <T extends io.dropwizard.Configuration> io.dropwizard.setup.Bootstrap<T> bootstrap()
        Note: application is already in run phase, so it's too late to configure dropwizard bootstrap object. Object provided just for consultation.
        Type Parameters:
        T - configuration type
        Returns:
        dropwizard bootstrap instance
      • application

        public io.dropwizard.Application application()
        Application instance may be useful for complex (half manual) integrations where access for injector is required. For example, manually registered Managed may access injector in it's start method by calling InjectorLookup.getInjector(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.

        Returns:
        dropwizard application instance
      • option

        public <V,​T extends java.lang.Enum & Option> V option​(T option)
        Read option value. Options could be set only in application root GuiceBundle.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).

        Type Parameters:
        V - option value type
        T - 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
      • 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[])
      • 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 extensionsOptional(Class[]) to register optional extension.

        Alternatively, you can manually bind extensions in guice module and they would be recognized (GuiceyOptions.AnalyzeGuiceModules).

        Parameters:
        extensionClasses - extension bean classes to register
        Returns:
        bootstrap instance for chained calls
        See Also:
        GuiceBundle.Builder.extensions(Class[])
      • extensionsOptional

        public GuiceyBootstrap extensionsOptional​(java.lang.Class<?>... extensionClasses)
        The same as 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.

        Parameters:
        extensionClasses - extension bean classes to register
        Returns:
        bootstrap instance for chained calls
      • dropwizardBundles

        public GuiceyBootstrap dropwizardBundles​(io.dropwizard.ConfiguredBundle... bundles)
        Shortcut for dropwizard bundles registration (instead of bootstrap().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...)
      • disableExtensions

        public final GuiceyBootstrap disableExtensions​(java.lang.Class<?>... extensions)
        Parameters:
        extensions - extensions to disable (manually added, registered by bundles or with classpath scan)
        Returns:
        bootstrap 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.

        Parameters:
        modules - guice module types to disable
        Returns:
        bootstrap instance for chained calls
        See Also:
        GuiceBundle.Builder.disableModules(Class[])
      • shareState

        public GuiceyBootstrap 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)!

        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 sharedState(Class, Supplier) instead.

        Parameters:
        key - shared object key
        value - shared object
        Returns:
        bootstrap instance for chained calls
        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.
        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

        public <T> java.util.Optional<T> sharedState​(java.lang.Class<?> key)
        Access shared value.
        Type Parameters:
        T - shared object type
        Parameters:
        key - shared object key
        Returns:
        shared object
        See Also:
        SharedConfigurationState
      • sharedStateOrFail

        public <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 no value available
        See Also:
        SharedConfigurationState