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 |
modules(com.google.inject.Module... modules)
Register guice modules.
|
GuiceyEnvironment |
modulesOverride(com.google.inject.Module... modules)
Override modules (using guice
Modules.override(Module...)). |
<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. |
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 install