public class GuiceyEnvironment
extends java.lang.Object
GuiceBundle.Builder. Also, contains dropwizard configuration and
environment objects.
As it is called on run phase, it does not allow to install or disable new bundles and installers.
| Constructor and Description |
|---|
GuiceyEnvironment(ConfigurationContext context) |
| Modifier and Type | Method and 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> |
configuration() |
<T,K extends T> |
configuration(java.lang.Class<T> type)
May be used to access unique sub configuration object.
|
<T> T |
configuration(java.lang.String yamlPath)
May be used to access current configuration value by exact path.
|
<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. |
ConfigurationTree |
configurationTree()
Raw configuration introspection info.
|
GuiceyEnvironment |
disableExtensions(java.lang.Class<?>... extensions) |
GuiceyEnvironment |
disableModules(java.lang.Class<? extends com.google.inject.Module>... modules)
Disable both usual and overriding guice modules.
|
io.dropwizard.setup.Environment |
environment() |
GuiceyEnvironment |
listen(GuiceyLifecycleListener... listeners)
Guicey broadcast a lot of events in order to indicate lifecycle phases
(GuiceyLifecycle).
|
GuiceyEnvironment |
listenJetty(org.eclipse.jetty.util.component.LifeCycle.Listener listener)
Shortcut for jetty lifecycle listener
listener registration. |
GuiceyEnvironment |
listenServer(io.dropwizard.lifecycle.ServerLifecycleListener listener)
Shortcut for
ServerLifecycleListener registration. |
GuiceyEnvironment |
manage(io.dropwizard.lifecycle.Managed managed)
Shortcut for manual registration of
Managed objects. |
GuiceyEnvironment |
modules(com.google.inject.Module... modules)
Register guice modules.
|
GuiceyEnvironment |
modulesOverride(com.google.inject.Module... modules)
Override modules (using guice
Modules.override(Module...)). |
GuiceyEnvironment |
onApplicationStartup(ApplicationStartupListener listener)
Code to execute after complete application startup.
|
GuiceyEnvironment |
onGuiceyStartup(GuiceyStartupListener listener)
Code to execute after guice injector creation (but still under run phase).
|
<V,T extends java.lang.Enum & Option> |
option(T option)
Read option value.
|
GuiceyEnvironment |
register(java.lang.Class<?>... items)
Shortcut for
environment().jersey().register() for direct registration of jersey extensions. |
GuiceyEnvironment |
register(java.lang.Object... items)
Shortcut for
environment().jersey().register() for direct registration of jersey extensions. |
<T> java.util.Optional<T> |
sharedState(java.lang.Class<?> key)
Access shared value.
|
<T> T |
sharedStateOrFail(java.lang.Class<?> key,
java.lang.String message,
java.lang.Object... args)
Use to access shared state value and immediately fail if value not yet set (most likely due to incorrect
configuration order).
|
public GuiceyEnvironment(ConfigurationContext context)
public <T extends io.dropwizard.Configuration> T configuration()
T - configuration typepublic <T> T configuration(java.lang.String yamlPath)
new X("sub.config.path").T - value typeyamlPath - target value yaml pathfor custom configuration searchespublic <T,K extends T> K configuration(java.lang.Class<T> type)
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).
T - declaration typeK - required value type (may be the same or extending type)type - target configuration declaration typefor custom configuration searchespublic <T> java.util.List<? extends T> configurations(java.lang.Class<T> type)
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 bundle.
Note: only custom types may be used (sub configuration objects), not Integer, Boolean, List, etc.
T - value typetype - target configuration typefor custom configuration searchespublic ConfigurationTree configurationTree()
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.
for configuration introspection details,
for available guice configuration bindingspublic io.dropwizard.setup.Environment environment()
public io.dropwizard.Application application()
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.
public <V,T extends java.lang.Enum & Option> V option(T option)
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).
V - option value typeT - helper type to define optionoption - option enummore options info,
options example,
options definitionpublic GuiceyEnvironment modules(com.google.inject.Module... modules)
Note that this registration appear in run phase and so you already have access to environment and configuration (and don't 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).
modules - one or more guice modulesGuiceBundle.Builder.modules(com.google.inject.Module...)public GuiceyEnvironment modulesOverride(com.google.inject.Module... modules)
Modules.override(Module...)).modules - overriding modulesGuiceBundle.Builder.modulesOverride(Module...)public final GuiceyEnvironment disableExtensions(java.lang.Class<?>... extensions)
extensions - extensions to disable (manually added, registered by bundles or with classpath scan)GuiceBundle.Builder.disableExtensions(Class[])@SafeVarargs public final GuiceyEnvironment disableModules(java.lang.Class<? extends com.google.inject.Module>... modules)
If bindings analysis is not disabled, could also disable inner (transitive) modules, but only inside normal modules.
modules - guice module types to disableGuiceBundle.Builder.disableModules(Class[])public GuiceyEnvironment register(java.lang.Object... items)
environment().jersey().register() for direct registration of jersey extensions.
For the most cases prefer automatic installation of jersey extensions with guicey installer.items - jersey extension instances to installpublic GuiceyEnvironment register(java.lang.Class<?>... items)
environment().jersey().register() for direct registration of jersey extensions.
For the most cases prefer automatic installation of jersey extensions with guicey installer.items - jersey extension instances to installpublic GuiceyEnvironment manage(io.dropwizard.lifecycle.Managed managed)
Managed objects.
Pay attention that managed objects are not called for commands.
managed - managed to registerpublic GuiceyEnvironment listen(GuiceyLifecycleListener... listeners)
GuiceyLifecycle.BundlesStarted.
Listener is not registered if equal listener was already registered (Set used as
listeners storage), so if you need to be sure that only one instance of some listener will be used
implement Object.equals(Object).
listeners - guicey lifecycle listenersGuiceBundle.Builder.listen(GuiceyLifecycleListener...)public GuiceyEnvironment listenServer(io.dropwizard.lifecycle.ServerLifecycleListener listener)
ServerLifecycleListener registration.
Note that server listener is called only when jetty starts up and so will no be called with lightweight
guicey test helpers GuiceyAppRule or
UseGuiceyApp. Prefer using
onApplicationStartup(ApplicationStartupListener) to be correctly called in tests (of course, if not
server only execution is desired).
Obviously not called for custom command execution.
listener - server startup listener.public GuiceyEnvironment listenJetty(org.eclipse.jetty.util.component.LifeCycle.Listener listener)
listener registration.
Lifecycle listeners are called with lightweight guicey test helpers
GuiceyAppRule or
UseGuiceyApp which makes them perfectly suitable for reporting.
If only startup event is required, prefer onApplicationStartup(ApplicationStartupListener) method
as more expressive and easier to use.
Listeners are not called on custom command execution.
listener - jettyadapterpublic <T> T sharedStateOrFail(java.lang.Class<?> key,
java.lang.String message,
java.lang.Object... args)
Note: shared state value assumed to be initialized under initialization phase (but you can workaround this limitation by accessing shared state statically)
T - shared object typekey - shared object keymessage - exception message (could use String.format(String, Object...) placeholders)args - placeholder arguments for error messagejava.lang.IllegalStateException - if not value availableSharedConfigurationStatepublic <T> java.util.Optional<T> sharedState(java.lang.Class<?> key)
T - shared object typekey - shared object keySharedConfigurationStatepublic GuiceyEnvironment onGuiceyStartup(GuiceyStartupListener listener)
Listener will be called on environment command start too.
Note: there is no registration method for this listener in main guice bundle builder
(GuiceBundle.Builder) because it is assumed, that such blocks would
always be wrapped with bundles to improve application readability.
listener - listener to call after injector creationpublic GuiceyEnvironment onApplicationStartup(ApplicationStartupListener listener)
GuiceyAppRule or
UseGuiceyApp) - after guicey start (as jetty not started in this
case). In both cases application completely started at this moment. Suitable for reporting.
If you need to listen only for real server startup then use listenServer(ServerLifecycleListener)
instead.
Not called on custom command execution (because no lifecycle involved in this case). In this case you can use
onGuiceyStartup(GuiceyStartupListener) as always executed point.
listener - listener to call on server startup