diff --git a/README.md b/README.md index e67bc49..64ea1ef 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ the dropwizard environment upon service start. com.hubspot.dropwizard dropwizard-guice - 0.8.4.0 + ${current.version} ``` @@ -157,7 +157,7 @@ public void initialize(Bootstrap bootstrap) { As of Dropwizard 0.8.x, when writing Integration Tests using `DropwizardAppRule`, you need to reset [jersey2-guice](https://github.com/Squarespace/jersey2-guice) by running: - BootstrapUtils.reset(); + JerseyGuiceUtils.reset(); ## Examples Please fork [an example project](https://github.com/eliast/dropwizard-guice-example) if you'd like to get going right away. diff --git a/pom.xml b/pom.xml index 58e52c8..311e353 100755 --- a/pom.xml +++ b/pom.xml @@ -5,12 +5,12 @@ com.hubspot basepom - 12.7 + 15.6 com.hubspot.dropwizard dropwizard-guice - 0.8.4.1-SNAPSHOT + 1.0.0-SNAPSHOT Simple library for using Guice DI in a dropwizard service. https://github.com/HubSpot/dropwizard-guice @@ -30,59 +30,48 @@ - 0.8.4 - 18.0 - 9.2.9.v20150224 + 1.0.0 + 19.0 + 9.3.11.v20160721 5.1.3.Final - io.dropwizard.metrics - metrics-healthchecks - 3.1.2 - - - org.slf4j - slf4j-api - - + io.dropwizard + dropwizard-bom + ${dropwizard.version} + import + pom - org.glassfish.jersey.core - jersey-server - 2.21 - - - org.glassfish.hk2.external - javax.inject - - + org.mockito + mockito-core + test + 2.0.54-beta + + + org.assertj + assertj-core + test + 3.4.1 com.squarespace.jersey2-guice - jersey2-guice - 0.10 - - - com.google.code.findbugs - jsr305 - - - org.slf4j - slf4j-api - - - org.glassfish.jersey.containers - jersey-container-servlet-core - - + jersey2-guice-impl + 1.0.6 org.glassfish.hk2 hk2-api - 2.4.0-b31 + 2.4.0-b34 + + + org.glassfish.hk2.external + aopalliance-repackaged + + javax.ws.rs @@ -92,7 +81,7 @@ org.reflections reflections - 0.9.9 + 0.9.10 com.google.guava @@ -100,10 +89,15 @@ + + org.javassist + javassist + 3.19.0-GA + org.apache.httpcomponents httpclient - 4.5.1 + 4.5.2 commons-logging @@ -114,7 +108,13 @@ org.apache.httpcomponents httpcore - 4.4.3 + 4.4.4 + + + org.objenesis + objenesis + 2.3 + test @@ -148,6 +148,10 @@ io.dropwizard dropwizard-servlets + + io.dropwizard.metrics + metrics-core + io.dropwizard.metrics metrics-healthchecks @@ -155,12 +159,6 @@ com.google.inject guice - - - aopalliance - aopalliance - - com.google.inject.extensions @@ -172,7 +170,7 @@ com.squarespace.jersey2-guice - jersey2-guice + jersey2-guice-impl org.reflections diff --git a/src/main/java/com/hubspot/dropwizard/guice/GuiceBundle.java b/src/main/java/com/hubspot/dropwizard/guice/GuiceBundle.java index fe1c1dd..76085e2 100644 --- a/src/main/java/com/hubspot/dropwizard/guice/GuiceBundle.java +++ b/src/main/java/com/hubspot/dropwizard/guice/GuiceBundle.java @@ -7,16 +7,20 @@ import com.google.inject.Injector; import com.google.inject.Module; import com.google.inject.Stage; +import com.google.inject.servlet.ServletModule; +import com.squarespace.jersey2.guice.JerseyGuiceModule; +import com.squarespace.jersey2.guice.JerseyGuiceUtils; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import io.dropwizard.Configuration; import io.dropwizard.ConfiguredBundle; import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Environment; +import java.util.List; +import org.glassfish.hk2.api.ServiceLocator; +import org.glassfish.hk2.extension.ServiceLocatorGenerator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.List; - public class GuiceBundle implements ConfiguredBundle { final Logger logger = LoggerFactory.getLogger(GuiceBundle.class); @@ -24,7 +28,7 @@ public class GuiceBundle implements ConfiguredBundle private final AutoConfig autoConfig; private final List modules; private final InjectorFactory injectorFactory; - private Injector injector; + private Injector baseInjector; private DropwizardEnvironmentModule dropwizardEnvironmentModule; private Optional> configurationClass; private Stage stage; @@ -91,20 +95,31 @@ public void initialize(Bootstrap bootstrap) { } else { dropwizardEnvironmentModule = new DropwizardEnvironmentModule<>(Configuration.class); } - modules.add(new JerseyModule()); modules.add(dropwizardEnvironmentModule); + modules.add(new ServletModule()); initInjector(); + JerseyGuiceUtils.install(new ServiceLocatorGenerator() { + @Override + public ServiceLocator create(String name, ServiceLocator parent) { + if (!name.startsWith("__HK2_Generated_")) { + return null; + } + + return baseInjector.createChildInjector(new JerseyGuiceModule(name)) + .getInstance(ServiceLocator.class); + } + }); if (autoConfig != null) { - autoConfig.initialize(bootstrap, injector); + autoConfig.initialize(bootstrap, baseInjector.createChildInjector(new JerseyGuiceModule(JerseyGuiceUtils.newServiceLocator()))); } } @SuppressFBWarnings("DM_EXIT") private void initInjector() { try { - injector = injectorFactory.create(this.stage,ImmutableList.copyOf(this.modules)); + baseInjector = injectorFactory.create(this.stage,ImmutableList.copyOf(this.modules)); } catch(Exception ie) { logger.error("Exception occurred when creating Guice Injector - exiting", ie); System.exit(1); @@ -113,12 +128,12 @@ private void initInjector() { @Override public void run(final T configuration, final Environment environment) { - JerseyUtil.registerGuiceBound(injector, environment.jersey()); + JerseyUtil.registerGuiceBound(baseInjector, environment.jersey()); JerseyUtil.registerGuiceFilter(environment); setEnvironment(configuration, environment); if (autoConfig != null) { - autoConfig.run(environment, injector); + autoConfig.run(environment, baseInjector); } } @@ -128,6 +143,7 @@ private void setEnvironment(final T configuration, final Environment environment } public Injector getInjector() { - return injector; + Preconditions.checkState(baseInjector != null, "injector is only available after com.hubspot.dropwizard.guice.GuiceBundle.initialize() is called"); + return baseInjector.createChildInjector(new JerseyGuiceModule(JerseyGuiceUtils.newServiceLocator())); } } diff --git a/src/main/java/com/hubspot/dropwizard/guice/HK2Linker.java b/src/main/java/com/hubspot/dropwizard/guice/HK2Linker.java deleted file mode 100644 index 8873ae2..0000000 --- a/src/main/java/com/hubspot/dropwizard/guice/HK2Linker.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.hubspot.dropwizard.guice; - -import com.google.inject.Injector; -import com.squarespace.jersey2.guice.BootstrapUtils; -import org.glassfish.hk2.api.ServiceLocator; -import javax.inject.Inject; - -//Inspired by gwizard-jersey - https://github.com/stickfigure/gwizard -/** - * Binding this as an eager singleton provides the second step of linking Guice back into HK2. - * (the first step was to install the HK2 BootstrapModule in the Guice module). - * - * This needs to happen before anything else related to Jersey starts. - */ -public class HK2Linker { - @Inject - public HK2Linker(Injector injector, ServiceLocator locator) { - BootstrapUtils.link(locator, injector); - BootstrapUtils.install(locator); - } - -} diff --git a/src/main/java/com/hubspot/dropwizard/guice/JerseyModule.java b/src/main/java/com/hubspot/dropwizard/guice/JerseyModule.java deleted file mode 100644 index 445c47b..0000000 --- a/src/main/java/com/hubspot/dropwizard/guice/JerseyModule.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.hubspot.dropwizard.guice; - -import com.google.inject.servlet.ServletModule; -import com.squarespace.jersey2.guice.BootstrapModule; -import com.squarespace.jersey2.guice.BootstrapUtils; -import org.glassfish.hk2.api.ServiceLocator; - -//Inspired by gwizard-jersey - https://github.com/stickfigure/gwizard -public class JerseyModule extends ServletModule { - - @Override - protected void configureServlets() { - // The order these operations (including the steps in the linker) are important - ServiceLocator locator = new ServiceLocatorDecorator(BootstrapUtils.newServiceLocator()) { - - @Override - public void shutdown() { - // don't shutdown, see issue #67. Remove once jersey2-guice supports Jersey 2.21 - } - }; - install(new BootstrapModule(locator)); - - bind(HK2Linker.class).asEagerSingleton(); - } -} diff --git a/src/test/java/com/hubspot/dropwizard/guice/AutoConfigTest.java b/src/test/java/com/hubspot/dropwizard/guice/AutoConfigTest.java index 1bb6656..43eae59 100644 --- a/src/test/java/com/hubspot/dropwizard/guice/AutoConfigTest.java +++ b/src/test/java/com/hubspot/dropwizard/guice/AutoConfigTest.java @@ -1,5 +1,6 @@ package com.hubspot.dropwizard.guice; +import com.codahale.metrics.MetricRegistry; import com.google.inject.Guice; import com.google.inject.Injector; import com.hubspot.dropwizard.guice.objects.*; @@ -29,7 +30,7 @@ public class AutoConfigTest { private final Injector injector = Guice.createInjector(new TestModule()); @Spy - private Environment environment = new Environment("test env", Jackson.newObjectMapper(), null, null, null); + private Environment environment = new Environment("test env", Jackson.newObjectMapper(), null, new MetricRegistry(), null); private AutoConfig autoConfig; @Before diff --git a/src/test/java/com/hubspot/dropwizard/guice/GuiceBundleTest.java b/src/test/java/com/hubspot/dropwizard/guice/GuiceBundleTest.java index 0037b82..3d25713 100644 --- a/src/test/java/com/hubspot/dropwizard/guice/GuiceBundleTest.java +++ b/src/test/java/com/hubspot/dropwizard/guice/GuiceBundleTest.java @@ -1,8 +1,9 @@ package com.hubspot.dropwizard.guice; +import com.codahale.metrics.MetricRegistry; import com.google.inject.Injector; import com.hubspot.dropwizard.guice.objects.TestModule; -import com.squarespace.jersey2.guice.BootstrapUtils; +import com.squarespace.jersey2.guice.JerseyGuiceUtils; import io.dropwizard.Configuration; import io.dropwizard.jackson.Jackson; import io.dropwizard.setup.Bootstrap; @@ -30,13 +31,13 @@ public class GuiceBundleTest { @After public void tearDown() { - BootstrapUtils.reset(); + JerseyGuiceUtils.reset(); } @Before public void setUp() { //given - environment = new Environment("test env", Jackson.newObjectMapper(), null, null, null); + environment = new Environment("test env", Jackson.newObjectMapper(), null, new MetricRegistry(), null); guiceBundle = GuiceBundle.newBuilder() .addModule(new TestModule()) .build(); diff --git a/src/test/java/com/hubspot/dropwizard/guice/HK2LinkerTest.java b/src/test/java/com/hubspot/dropwizard/guice/HK2LinkerTest.java index d86f87c..973c983 100644 --- a/src/test/java/com/hubspot/dropwizard/guice/HK2LinkerTest.java +++ b/src/test/java/com/hubspot/dropwizard/guice/HK2LinkerTest.java @@ -1,13 +1,17 @@ package com.hubspot.dropwizard.guice; -import com.google.inject.Guice; import com.google.inject.Injector; import com.hubspot.dropwizard.guice.objects.ExplicitResource; import com.hubspot.dropwizard.guice.objects.JitResource; import com.hubspot.dropwizard.guice.objects.TestModule; -import com.squarespace.jersey2.guice.BootstrapUtils; +import com.squarespace.jersey2.guice.JerseyGuiceUtils; +import io.dropwizard.Application; +import io.dropwizard.Configuration; +import io.dropwizard.setup.Bootstrap; +import io.dropwizard.setup.Environment; import org.glassfish.hk2.api.ServiceLocator; import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import javax.servlet.ServletException; @@ -16,12 +20,26 @@ public class HK2LinkerTest { - final Injector injector = Guice.createInjector(new JerseyModule(), new TestModule()); + private static Injector injector; final ServiceLocator serviceLocator = injector.getInstance(ServiceLocator.class); + @BeforeClass + public static void setup() { + + final GuiceBundle bundle = new GuiceBundle.Builder().addModule(new TestModule()).build(); + bundle.initialize(new Bootstrap(new Application() { + @Override + public void run(Configuration configuration, Environment environment) throws Exception { + + } + })); + injector = bundle.getInjector(); + + } + @AfterClass public static void tearDown() { - BootstrapUtils.reset(); + JerseyGuiceUtils.reset(); } @Test diff --git a/src/test/java/com/hubspot/dropwizard/guice/InjectedIntegrationTest.java b/src/test/java/com/hubspot/dropwizard/guice/InjectedIntegrationTest.java index 77806e8..5c7c7fb 100644 --- a/src/test/java/com/hubspot/dropwizard/guice/InjectedIntegrationTest.java +++ b/src/test/java/com/hubspot/dropwizard/guice/InjectedIntegrationTest.java @@ -3,7 +3,7 @@ import com.google.common.io.Resources; import com.hubspot.dropwizard.guice.objects.InjectedBundle; import com.hubspot.dropwizard.guice.objects.TestApplication; -import com.squarespace.jersey2.guice.BootstrapUtils; +import com.squarespace.jersey2.guice.JerseyGuiceUtils; import io.dropwizard.Configuration; import io.dropwizard.client.JerseyClientBuilder; import io.dropwizard.testing.junit.DropwizardAppRule; @@ -32,7 +32,7 @@ public static void setUp() { @AfterClass public static void tearDown() { - BootstrapUtils.reset(); + JerseyGuiceUtils.reset(); } public static String resourceFilePath(String resourceClassPathLocation) {