diff --git a/.gitignore b/.gitignore
index e0483b8..ca92ae7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,4 @@ target/
.project
.settings
.idea
-*.iml
\ No newline at end of file
+*.iml
diff --git a/README.md b/README.md
index dd2754b..534033b 100644
--- a/README.md
+++ b/README.md
@@ -7,60 +7,175 @@ the dropwizard environment upon service start.
### Usage
+```xml
+
+
+ com.hubspot.dropwizard
+ dropwizard-guice
+ 0.7.0.2
+
+
+```
+
Simply install a new instance of the bundle during your service initialization
```java
-public class HelloWorldService extends Application {
+public class HelloWorldApplication extends Application {
+
+ private GuiceBundle guiceBundle;
public static void main(String[] args) throws Exception {
- new HelloWorldService().run(args);
- }
-
- @Override
- public String getName() {
- return "hello-world";
- }
-
- @Override
- public void initialize(Bootstrap bootstrap) {
- bootstrap.addBundle(GuiceBundle.newBuilder()
- .addModule(new HelloWorldModule())
- .build()
- );
- }
-
- @Override
- public void run(HelloWorldConfiguration configuration, final Environment environment) {
- environment.jersey().register(HelloWorldResource.class);
- environment.healthChecks().register("Template", TemplateHealthCheck.class);
- }
+ new HelloWorldApplication().run(args);
+ }
+
+ @Override
+ public void initialize(Bootstrap bootstrap) {
+
+ guiceBundle = GuiceBundle.newBuilder()
+ .addModule(new HelloWorldModule())
+ .setConfigClass(HelloWorldConfiguration.class)
+ .build();
+
+ bootstrap.addBundle(guiceBundle);
+ }
+ @Override
+ public String getName() {
+ return "hello-world";
+ }
+
+ @Override
+ public void run(HelloWorldConfiguration helloWorldConfiguration, Environment environment) throws Exception {
+ environment.jersey().register(HelloWorldResource.class);
+ environment.lifecycle().manage(guiceBundle.getInjector().getInstance(TemplateHealthCheck.class));
+ }
}
```
-Lastly, you can enable auto configuration via package scanning.
+You can enable auto configuration via package scanning.
+```java
+public class HelloWorldApplication extends Application {
+
+ public static void main(String[] args) throws Exception {
+ new HelloWorldApplication().run(args);
+ }
+
+ @Override
+ public void initialize(Bootstrap bootstrap) {
+
+ GuiceBundle guiceBundle = GuiceBundle.newBuilder()
+ .addModule(new HelloWorldModule())
+ .enableAutoConfig(getClass().getPackage().getName())
+ .setConfigClass(HelloWorldConfiguration.class)
+ .build();
+
+ bootstrap.addBundle(guiceBundle);
+ // with AutoConfig enabled you don't need to add bundles or commands explicitly here.
+ // inherit from one of InjectedCommand, InjectedConfiguredCommand, or InjectedEnvironmentCommand
+ // to get access to all modules during injection.
+ }
+
+ @Override
+ public String getName() {
+ return "hello-world";
+ }
+
+ @Override
+ public void run(HelloWorldConfiguration helloWorldConfiguration, Environment environment) throws Exception {
+ // now you don't need to add resources, tasks, healthchecks or providers
+ // you must have your health checks inherit from InjectableHealthCheck in order for them to be injected
+ }
+}
+
+Modules will also be injected before being added. Field injections only, constructor based injections will not be available.
+Configuration data and initialization module data will be available for injecting into modules.
```java
-public class HelloWorldService extends Service {
+
+
+public class HelloWorldApplication extends Application {
public static void main(String[] args) throws Exception {
- new HelloWorldService().run(args);
- }
-
- @Override
- public void initialize(Bootstrap bootstrap) {
- bootstrap.setName("hello-world");
- bootstrap.addBundle(GuiceBundle.newBuilder()
- .addModule(new HelloWorldModule())
- .enableAutoConfig(getClass().getPackage().getName())
- .build()
- );
- }
-
- @Override
- public void run(HelloWorldConfiguration configuration, final Environment environment) {
- // now you don't need to add resources, tasks, healthchecks or providers
- // you must have your health checks inherit from InjectableHealthCheck in order for them to be injected
- }
+ new HelloWorldApplication().run(args);
+ }
+
+ @Override
+ public void initialize(Bootstrap bootstrap) {
+
+ GuiceBundle guiceBundle = GuiceBundle.newBuilder()
+ .addInitModule(new BaseModule())
+ // bindings defined in the BaseModule or any configuration data is available for
+ // injection into HelloWorldModule fields
+ .addModule(new HelloWorldModule())
+ //Any resource, task, bundle, etc within this class path will be included automatically.
+ .enableAutoConfig(getClass().getPackage().getName())
+ //The contents of any config objects within this package path will be auto-injected.
+ .addConfigPackages(getClass().getPackage().getName())
+ .setConfigClass(HelloWorldConfiguration.class)
+ .build();
+
+ bootstrap.addBundle(guiceBundle);
+ }
+
+ @Override
+ public String getName() {
+ return "hello-world";
+ }
+
+ @Override
+ public void run(HelloWorldConfiguration helloWorldConfiguration, Environment environment) throws Exception {
+ }
+}
+```
+
+If you are having trouble accessing your Configuration or Environment inside a Guice Module, you could try using a provider.
+
+```java
+public class HelloWorldModule extends AbstractModule {
+
+ @Override
+ protected void configure() {
+ // anything you'd like to configure
+ }
+
+ @Provides
+ public SomePool providesSomethingThatNeedsConfiguration(HelloWorldConfiguration configuration) {
+ return new SomePool(configuration.getPoolName());
+ }
+
+ @Provides
+ public SomeManager providesSomenthingThatNeedsEnvironment(Environment env) {
+ return new SomeManager(env.getSomethingFromHere()));
+ }
+}
+```
+
+You can also replace the default Guice `Injector` by implementing your own `InjectorFactory`. For example if you want
+to use [Governator](https://github.com/Netflix/governator) you can create the following implementation:
+
+```java
+public class GovernatorInjectorFactory implements InjectorFactory {
+
+ @Override
+ public Injector create( final Stage stage, final List modules ) {
+ return LifecycleInjector.builder().inStage( stage ).withModules( modules ).build()
+ .createInjector();
+ }
+}
+```
+
+and then set the InjectorFactory when initializing the GuiceBundle:
+
+```java
+@Override
+public void initialize(Bootstrap bootstrap) {
+
+ GuiceBundle guiceBundle = GuiceBundle.newBuilder()
+ .addModule(new HelloWorldModule())
+ .enableAutoConfig(getClass().getPackage().getName())
+ .setConfigClass(HelloWorldConfiguration.class)
+ .setInjectorFactory( new GovernatorInjectorFactory() )
+ .build();
+ bootstrap.addBundle(guiceBundle);
}
```
diff --git a/pom.xml b/pom.xml
index f12ae5a..9733521 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,13 +1,12 @@
-
- 4.0.0
+
+ 4.0.0
-
- org.sonatype.oss
- oss-parent
- 7
-
+
+ org.sonatype.oss
+ oss-parent
+ 7
+
@@ -17,63 +16,118 @@
- com.hubspot.dropwizard
- dropwizard-guice
- 0.7.0-sskrla-SNAPSHOT
- Dropwizard Guice Support
- Simple library for using Guice DI in a dropwizard service.
- https://github.com/HubSpot/dropwizard-guice
+ com.hubspot.dropwizard
+ dropwizard-guice
+ 0.8.1-SNAPSHOT
+ Dropwizard Guice Support
+ Simple library for using Guice DI in a dropwizard service.
+ https://github.com/HubSpot/dropwizard-guice
-
-
- The Apache Software License, Version 2.0
- http://www.apache.org/licenses/LICENSE-2.0.txt
- repo
-
-
+
+
+ The Apache Software License, Version 2.0
+ http://www.apache.org/licenses/LICENSE-2.0.txt
+ repo
+
+
-
- scm:git:git@github.com:HubSpot/dropwizard-guice.git
- scm:git:git@github.com:HubSpot/dropwizard-guice.git
- git@github.com:HubSpot/dropwizard-guice.git
-
+
+ scm:git:git@github.com:HubSpot/dropwizard-guice.git
+ scm:git:git@github.com:HubSpot/dropwizard-guice.git
+ git@github.com:HubSpot/dropwizard-guice.git
+
-
-
- eliast
- Elias Torres
- elias@hubspot.com
-
-
+
+
+ eliast
+ Elias Torres
+ elias@hubspot.com
+
+
-
-
- io.dropwizard
- dropwizard-core
- 0.7.0-rc1
-
-
- com.google.inject.extensions
- guice-servlet
- 3.0
-
-
- com.sun.jersey.contribs
- jersey-guice
- 1.17.1
-
-
- org.reflections
- reflections
- 0.9.8
-
-
- com.google.guava
- guava
-
-
-
-
+
+ 0.8.0-rc3
+
+
+
+
+
+ com.google.code.findbugs
+ annotations
+ 3.0.0
+
+
+ io.dropwizard
+ dropwizard-core
+ ${io.dropwizard.version}
+
+
+ com.google.code.findbugs
+ jsr305
+
+
+
+
+ com.google.inject
+ guice
+ 4.0-beta5
+
+
+ com.google.guava
+ guava
+
+
+
+
+ com.google.inject.extensions
+ guice-servlet
+ 4.0-beta5
+
+
+ com.squarespace.jersey2-guice
+ jersey2-guice
+ 0.5
+
+
+ org.glassfish.jersey.containers
+ jersey-container-servlet-core
+
+
+ org.glassfish.jersey.containers
+ jersey-container-servlet
+
+
+
+
+ org.reflections
+ reflections
+ 0.9.9
+
+
+ com.google.guava
+ guava
+
+
+
+
+ com.jayway.restassured
+ rest-assured
+ 2.4.0
+ test
+
+
+ io.dropwizard
+ dropwizard-testing
+ ${io.dropwizard.version}
+ test
+
+
+ io.dropwizard
+ dropwizard-client
+ ${io.dropwizard.version}
+ test
+
+
@@ -83,12 +137,11 @@
2.5
-
- 1.6
+
+ 1.7
-
org.apache.maven.plugins
diff --git a/src/main/java/com/hubspot/dropwizard/guice/AutoConfig.java b/src/main/java/com/hubspot/dropwizard/guice/AutoConfig.java
index 87a2d16..03b57ea 100644
--- a/src/main/java/com/hubspot/dropwizard/guice/AutoConfig.java
+++ b/src/main/java/com/hubspot/dropwizard/guice/AutoConfig.java
@@ -1,13 +1,21 @@
package com.hubspot.dropwizard.guice;
+import com.google.inject.ConfigurationException;
+import com.google.common.base.Function;
+import com.google.common.collect.Collections2;
import io.dropwizard.Bundle;
+import io.dropwizard.ConfiguredBundle;
+import io.dropwizard.cli.Command;
+import io.dropwizard.cli.ConfiguredCommand;
+import io.dropwizard.cli.EnvironmentCommand;
import io.dropwizard.lifecycle.Managed;
import io.dropwizard.servlets.tasks.Task;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
+
import com.google.common.base.Preconditions;
import com.google.inject.Injector;
-import com.sun.jersey.spi.inject.InjectableProvider;
+import org.glassfish.jersey.server.model.Resource;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
@@ -18,7 +26,9 @@
import org.slf4j.LoggerFactory;
import javax.ws.rs.Path;
+import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;
+import java.util.Collection;
import java.util.Set;
public class AutoConfig {
@@ -44,23 +54,28 @@ public AutoConfig(String... basePackages) {
public void run(Environment environment, Injector injector) {
addHealthChecks(environment, injector);
- addProviders(environment, injector);
- addInjectableProviders(environment, injector);
- addResources(environment, injector);
+ addProviders(environment);
+ addResources(environment);
addTasks(environment, injector);
addManaged(environment, injector);
+ addParamConverterProviders(environment);
}
public void initialize(Bootstrap> bootstrap, Injector injector) {
addBundles(bootstrap, injector);
+ addCommands(bootstrap, injector);
}
private void addManaged(Environment environment, Injector injector) {
Set> managedClasses = reflections
.getSubTypesOf(Managed.class);
for (Class extends Managed> managed : managedClasses) {
- environment.lifecycle().manage(injector.getInstance(managed));
- logger.info("Added managed: {}", managed);
+ try {
+ environment.lifecycle().manage(injector.getInstance(managed));
+ logger.info("Added managed: {}", managed);
+ } catch (ConfigurationException e) {
+ logger.warn("Could not get instance of managed: {}", managed);
+ }
}
}
@@ -68,8 +83,12 @@ private void addTasks(Environment environment, Injector injector) {
Set> taskClasses = reflections
.getSubTypesOf(Task.class);
for (Class extends Task> task : taskClasses) {
- environment.admin().addTask(injector.getInstance(task));
- logger.info("Added task: {}", task);
+ try {
+ environment.admin().addTask(injector.getInstance(task));
+ logger.info("Added task: {}", task);
+ } catch (ConfigurationException e) {
+ logger.warn("Could not get instance of task: {}", task);
+ }
}
}
@@ -77,24 +96,17 @@ private void addHealthChecks(Environment environment, Injector injector) {
Set> healthCheckClasses = reflections
.getSubTypesOf(InjectableHealthCheck.class);
for (Class extends InjectableHealthCheck> healthCheck : healthCheckClasses) {
- InjectableHealthCheck instance = injector.getInstance(healthCheck);
- environment.healthChecks().register(instance.getName(), instance);
- logger.info("Added injectableHealthCheck: {}", healthCheck);
- }
- }
-
- @SuppressWarnings("rawtypes")
- private void addInjectableProviders(Environment environment,
- Injector injector) {
- Set> injectableProviders = reflections
- .getSubTypesOf(InjectableProvider.class);
- for (Class extends InjectableProvider> injectableProvider : injectableProviders) {
- environment.jersey().register(injectableProvider);
- logger.info("Added injectableProvider: {}", injectableProvider);
+ try {
+ InjectableHealthCheck instance = injector.getInstance(healthCheck);
+ environment.healthChecks().register(instance.getName(), instance);
+ logger.info("Added injectableHealthCheck: {}", healthCheck);
+ } catch (ConfigurationException e) {
+ logger.warn("Could not get instance of InjectableHealthCheck: {}", healthCheck);
+ }
}
}
- private void addProviders(Environment environment, Injector injector) {
+ private void addProviders(Environment environment) {
Set> providerClasses = reflections
.getTypesAnnotatedWith(Provider.class);
for (Class> provider : providerClasses) {
@@ -103,21 +115,80 @@ private void addProviders(Environment environment, Injector injector) {
}
}
- private void addResources(Environment environment, Injector injector) {
+ private void addResources(Environment environment) {
Set> resourceClasses = reflections
.getTypesAnnotatedWith(Path.class);
for (Class> resource : resourceClasses) {
- environment.jersey().register(resource);
- logger.info("Added resource class: {}", resource);
+ if(Resource.isAcceptable(resource)) {
+ environment.jersey().register(resource);
+ logger.info("Added resource class: {}", resource);
+ }
}
}
+ @SuppressWarnings("rawtypes")
private void addBundles(Bootstrap> bootstrap, Injector injector) {
Set> bundleClasses = reflections
.getSubTypesOf(Bundle.class);
for (Class extends Bundle> bundle : bundleClasses) {
- bootstrap.addBundle(injector.getInstance(bundle));
- logger.info("Added bundle class {} during bootstrap", bundle);
+ try {
+ bootstrap.addBundle(injector.getInstance(bundle));
+ logger.info("Added bundle class {} during bootstrap", bundle);
+ } catch (ConfigurationException e) {
+ logger.warn("Could not get instance of bundle: {}", bundle);
+ }
+ }
+ Set> configuredBundleClasses = reflections.getSubTypesOf(ConfiguredBundle.class);
+ for(Class extends ConfiguredBundle> bundle : configuredBundleClasses)
+ {
+ if(!bundle.equals(GuiceBundle.class))
+ {
+ try {
+ bootstrap.addBundle(injector.getInstance(bundle));
+ logger.info("Added configured bundle class {} during bootstrap", bundle);
+ } catch (ConfigurationException e) {
+ logger.warn("Could not get instance of configured bundle: {}", bundle);
+ }
+ }
+ }
+ }
+
+ private void addCommands(Bootstrap> bootstrap, Injector injector) {
+ Collection