-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathDropwizardEnvironmentModule.java
More file actions
103 lines (90 loc) · 3.67 KB
/
Copy pathDropwizardEnvironmentModule.java
File metadata and controls
103 lines (90 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.hubspot.dropwizard.guice;
import com.google.common.base.Optional;
import com.google.inject.*;
import com.google.inject.name.Names;
import io.dropwizard.Configuration;
import io.dropwizard.jetty.MutableServletContextHandler;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import net.sourceforge.argparse4j.inf.Namespace;
import javax.servlet.ServletContext;
public class DropwizardEnvironmentModule<T extends Configuration> extends AbstractModule {
private static final String ILLEGAL_DROPWIZARD_MODULE_STATE = "The dropwizard environment has not been set. This is likely caused by trying to access the dropwizard environment during the bootstrap phase or during a non-configured command.";
private Optional<T> configuration;
private Optional<Environment> environment;
private Optional<Namespace> namespace = Optional.absent();
private Optional<Bootstrap<T>> bootstrap;
private Class<? super T> configurationClass;
public DropwizardEnvironmentModule(Class<T> configurationClass) {
this.configurationClass = configurationClass;
}
@Override
protected void configure() {
Provider<T> provider = new CustomConfigurationProvider();
if(configuration.isPresent()){
bind(configurationClass).toProvider(provider);
if (configurationClass != Configuration.class) {
bind(Configuration.class).toProvider(provider);
}
}
if(environment.isPresent()) {
bindContext("application", environment.get().getApplicationContext());
}
}
/**
* Bind some of the context objects to be injectable. Annotated with a {@link com.google.inject.name.Names} to
* prevent collisions for any that the {@link com.google.inject.servlet.ServletModule} may bind later.
*/
private void bindContext(String name, MutableServletContextHandler context) {
bind(ServletContext.class)
.annotatedWith(Names.named(name))
.toInstance(context.getServletContext());
}
@Deprecated
public void setEnvironmentData(T configuration, Environment environment) {
setEnvironmentData(null, environment, configuration);
}
public void setEnvironmentData(Bootstrap<T> bootstrap,
Environment environment,
T configuration) {
this.bootstrap = Optional.fromNullable(bootstrap);
this.configuration = Optional.fromNullable(configuration);
this.environment = Optional.fromNullable(environment);
}
public void setNamespace(Namespace namespace) {
this.namespace = Optional.fromNullable(namespace);
}
@Provides
public Environment providesEnvironment() {
if (environment == null || !environment.isPresent()) {
throw new ProvisionException(ILLEGAL_DROPWIZARD_MODULE_STATE);
}
return environment.get();
}
@Provides
public Namespace providesNamespace() {
if (namespace == null || !namespace.isPresent()) {
throw new ProvisionException(ILLEGAL_DROPWIZARD_MODULE_STATE);
}
return namespace.get();
}
/**
* Note: This is a raw type. Guice cannot inject the full type due to type erasure
*/
@Provides
public Bootstrap providesBootstrap() {
if (bootstrap == null || !bootstrap.isPresent()) {
throw new ProvisionException(ILLEGAL_DROPWIZARD_MODULE_STATE);
}
return bootstrap.get();
}
private class CustomConfigurationProvider implements Provider<T> {
@Override
public T get() {
if (configuration == null || !configuration.isPresent()) {
throw new ProvisionException(ILLEGAL_DROPWIZARD_MODULE_STATE);
}
return configuration.get();
}
}
}