Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ the dropwizard environment upon service start.
<dependency>
<groupId>com.hubspot.dropwizard</groupId>
<artifactId>dropwizard-guice</artifactId>
<version>0.8.4.0</version>
<version>${current.version}</version>
</dependency>
</dependencies>
```
Expand Down Expand Up @@ -157,7 +157,7 @@ public void initialize(Bootstrap<HelloWorldConfiguration> 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.
Expand Down
98 changes: 48 additions & 50 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>com.hubspot</groupId>
<artifactId>basepom</artifactId>
<version>12.7</version>
<version>15.6</version>
</parent>

<groupId>com.hubspot.dropwizard</groupId>
<artifactId>dropwizard-guice</artifactId>
<version>0.8.4.1-SNAPSHOT</version>
<version>1.0.0-SNAPSHOT</version>
<description>Simple library for using Guice DI in a dropwizard service.</description>
<url>https://github.com/HubSpot/dropwizard-guice</url>

Expand All @@ -30,59 +30,48 @@
</developers>

<properties>
<dropwizard.version>0.8.4</dropwizard.version>
<dep.guava.version>18.0</dep.guava.version>
<dep.jetty.version>9.2.9.v20150224</dep.jetty.version>
<dropwizard.version>1.0.0</dropwizard.version>
<dep.guava.version>19.0</dep.guava.version>
<dep.jetty.version>9.3.11.v20160721</dep.jetty.version>
<hibernate.version>5.1.3.Final</hibernate.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-healthchecks</artifactId>
<version>3.1.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-bom</artifactId>
<version>${dropwizard.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.21</version>
<exclusions>
<exclusion>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>javax.inject</artifactId>
</exclusion>
</exclusions>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
<version>2.0.54-beta</version><!--match expectation by artifact: io.dropwizard:dropwizard-testing-->
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
<version>3.4.1</version><!--match expectation by artifact io.dropwizard:dropwizard-testing-->
</dependency>
<dependency>
<groupId>com.squarespace.jersey2-guice</groupId>
<artifactId>jersey2-guice</artifactId>
<version>0.10</version>
<exclusions>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</exclusion>
</exclusions>
<artifactId>jersey2-guice-impl</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-api</artifactId>
<version>2.4.0-b31</version>
<version>2.4.0-b34</version>
<exclusions>
<exclusion>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>aopalliance-repackaged</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
Expand All @@ -92,18 +81,23 @@
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.9</version>
<version>0.9.10</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.19.0-GA</version><!--match expectation from artifact: org.reflections:reflections-->
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
<version>4.5.2</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
Expand All @@ -114,7 +108,13 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.3</version>
<version>4.4.4</version>
</dependency>
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>2.3</version><!--match expectation from artifact: io.dropwizard:dropwizard-testing-->
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down Expand Up @@ -148,19 +148,17 @@
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-servlets</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-healthchecks</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<exclusions>
<exclusion>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
Expand All @@ -172,7 +170,7 @@
</dependency>
<dependency>
<groupId>com.squarespace.jersey2-guice</groupId>
<artifactId>jersey2-guice</artifactId>
<artifactId>jersey2-guice-impl</artifactId>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
Expand Down
34 changes: 25 additions & 9 deletions src/main/java/com/hubspot/dropwizard/guice/GuiceBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@
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<T extends Configuration> implements ConfiguredBundle<T> {

final Logger logger = LoggerFactory.getLogger(GuiceBundle.class);

private final AutoConfig autoConfig;
private final List<Module> modules;
private final InjectorFactory injectorFactory;
private Injector injector;
private Injector baseInjector;
private DropwizardEnvironmentModule dropwizardEnvironmentModule;
private Optional<Class<T>> configurationClass;
private Stage stage;
Expand Down Expand Up @@ -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() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having trouble injecting Provider<SecurityContext> in my services, I think it's because the install call here is after the initInjector call.

@jhaber @heldeen is there any way you see to make the Jersey services like SecurityContext available?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vvondra, I am able to inject Provider<SecurityContext> in my quick tests. Would you want to create an issue w/ a simple example project that can duplicate the issue you are seeing?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to sum up an example in #87

https://github.com/HubSpot/dropwizard-guice/pull/87/files#r75596536

Maybe it's because I'm not injecting the Provider to a resource, but to a service that is a dependecy of the resource?

@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);
Expand All @@ -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);
}
}

Expand All @@ -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()));
}
}
22 changes: 0 additions & 22 deletions src/main/java/com/hubspot/dropwizard/guice/HK2Linker.java

This file was deleted.

25 changes: 0 additions & 25 deletions src/main/java/com/hubspot/dropwizard/guice/JerseyModule.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Loading