33import java .util .List ;
44
55import com .google .inject .*;
6+ import com .hubspot .dropwizard .guice .ConfigData .ConfigDataModule ;
67import io .dropwizard .setup .Bootstrap ;
78import org .slf4j .Logger ;
89import org .slf4j .LoggerFactory ;
@@ -30,6 +31,7 @@ public class GuiceBundle<T extends Configuration> implements ConfiguredBundle<T>
3031 private final List <Module > modules ;
3132 private final List <Module > initModules ;
3233 private final List <Function <Injector , ServletContextListener >> contextListenerGenerators ;
34+ private final String [] configurationPackages ;
3335 private final InjectorFactory injectorFactory ;
3436
3537 private Injector initInjector ;
@@ -45,6 +47,7 @@ public static class Builder<T extends Configuration> {
4547 private List <Function <Injector , ServletContextListener >> contextListenerGenerators = Lists .newArrayList ();
4648 private Optional <Class <T >> configurationClass = Optional .absent ();
4749 private InjectorFactory injectorFactory = new InjectorFactoryImpl ();
50+ List <String > configurationPackages = new ArrayList <>();
4851
4952 /**
5053 * Add a module to the bundle.
@@ -81,6 +84,17 @@ public Builder<T> setConfigClass(Class<T> clazz) {
8184 configurationClass = Optional .of (clazz );
8285 return this ;
8386 }
87+
88+ /**
89+ * Sets a list of base packages that may contain configuration objects.
90+ * When config data is bound in the injector, classes within these
91+ * packages will be recursed into.
92+ */
93+ public Builder <T > addConfigPackages (String ... basePackages ) {
94+ Preconditions .checkNotNull (basePackages .length > 0 );
95+ configurationPackages .addAll (Arrays .asList (basePackages ));
96+ return this ;
97+ }
8498
8599 public Builder <T > setInjectorFactory (InjectorFactory factory ) {
86100 Preconditions .checkNotNull (factory );
@@ -101,7 +115,7 @@ public GuiceBundle<T> build() {
101115
102116 public GuiceBundle <T > build (Stage s ) {
103117 return new GuiceBundle <>(s , autoConfig , modules , initModules , contextListenerGenerators , injectorFactory ,
104- configurationClass );
118+ configurationClass , configurationPackages . toArray ( new String [ 0 ]) );
105119 }
106120
107121 }
@@ -116,17 +130,20 @@ private GuiceBundle(Stage stage,
116130 List <Module > initModules ,
117131 List <Function <Injector , ServletContextListener >> contextListenerGenerators ,
118132 InjectorFactory injectorFactory ,
119- Optional <Class <T >> configurationClass ) {
133+ Optional <Class <T >> configurationClass ,
134+ String [] configurationPackages ) {
120135 Preconditions .checkNotNull (modules );
121136 Preconditions .checkArgument (!modules .isEmpty ());
122137 Preconditions .checkNotNull (contextListenerGenerators );
123138 Preconditions .checkNotNull (stage );
139+ Preconditions .checkNotNull (configurationPackages );
124140 this .modules = modules ;
125141 this .initModules = initModules ;
126142 this .contextListenerGenerators = contextListenerGenerators ;
127143 this .autoConfig = autoConfig ;
128144 this .configurationClass = configurationClass ;
129145 this .injectorFactory = injectorFactory ;
146+ this .configurationPackages = configurationPackages ;
130147 this .stage = stage ;
131148 }
132149
@@ -165,10 +182,7 @@ public void run(final T configuration, final Environment environment) {
165182 void run (Bootstrap <T > bootstrap , Environment environment , final T configuration ) {
166183 initEnvironmentModule ();
167184 setEnvironment (bootstrap , environment , configuration );
168- //The secondary injected modules generally use config data. If we are starting up a command
169- //that doesn't have a configuration, loading these modules is useless at best.
170- boolean addModules = configuration != null ;
171- initGuice (environment , addModules );
185+ initGuice (environment , configuration );
172186 Injector injector = getInjector ().get ();
173187
174188 if (environment != null ) {
@@ -203,10 +217,16 @@ private void initEnvironmentModule() {
203217 }
204218 }
205219
206- private void initGuice (final Environment environment , boolean addModules ) {
207- Injector environmentInjector = initInjector .createChildInjector (dropwizardEnvironmentModule );
220+ @ SuppressWarnings ("unchecked" )
221+ private void initGuice (final Environment environment , T configuration ) {
222+ List <Module > envModules = new ArrayList <>();
223+ envModules .add (dropwizardEnvironmentModule );
224+ if (configuration != null ) envModules .add (new ConfigDataModule (configuration , configurationPackages ));
225+ Injector environmentInjector = initInjector .createChildInjector (envModules );
208226
209- if (addModules ) {
227+ //The secondary injected modules generally use config data. If we are starting up a command
228+ //that doesn't have a configuration, loading these modules is useless at best.
229+ if (configuration != null ) {
210230 for (Module module : modules )
211231 environmentInjector .injectMembers (module );
212232
0 commit comments