Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
173 changes: 103 additions & 70 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,160 +5,194 @@
<parent>
<groupId>com.hubspot</groupId>
<artifactId>basepom</artifactId>
<version>63.7</version>
<version>65.1</version>
</parent>

<groupId>com.hubspot.dropwizard</groupId>
<artifactId>dropwizard-guicier</artifactId>
<version>1.3.5.4-SNAPSHOT</version>

<name>dropwizard-guicier</name>
<description>Integrates Dropwizard REST framework with Guice dependency injection</description>
<version>3.0.0-SNAPSHOT</version>

<properties>
<basepom.check.skip-dependency-versions-check>true</basepom.check.skip-dependency-versions-check>
<dep.dropwizard.version>3.0.16</dep.dropwizard.version>
<dep.dropwizard-metrics.version>4.2.36</dep.dropwizard-metrics.version>
<dep.guava.version>33.4.8-jre</dep.guava.version>
<dep.hk2.version>2.6.1</dep.hk2.version>
<dep.jackson.version>2.19.2</dep.jackson.version>
<dep.jackson-databind.version>${dep.jackson.version}</dep.jackson-databind.version>
<dep.jersey2.version>2.47</dep.jersey2.version>
<dep.jetty.version>10.0.26</dep.jetty.version>
<dep.slf4j.version>2.0.17</dep.slf4j.version>

<project.build.targetJdk>11</project.build.targetJdk>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>com.squarespace.jersey2-guice</groupId>
<artifactId>jersey2-guice-impl</artifactId>
<version>1.0.6</version>
<!-- should probably hoist this to basepom -->
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${dep.jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${dep.jetty.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-multibindings</artifactId>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- Put this dep first so the ServiceLoader returns its classes first -->
<dependency>
<groupId>com.squarespace.jersey2-guice</groupId>
<artifactId>jersey2-guice-impl</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-servlet</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-servlets</artifactId>
<artifactId>dropwizard-jersey</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<artifactId>dropwizard-jetty</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-lifecycle</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-jackson</artifactId>
<artifactId>dropwizard-servlets</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-jersey</artifactId>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-healthchecks</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-jetty</artifactId>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-healthchecks</artifactId>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>guice-bridge</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-api</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-utils</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-client</artifactId>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.basepom.maven</groupId>
<artifactId>duplicate-finder-maven-plugin</artifactId>
<configuration>
<exceptions combine.children="append">
<exception>
<conflictingDependencies>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard.logback</groupId>
<artifactId>logback-throttling-appender</artifactId>
</dependency>
</conflictingDependencies>
<classes>
<class>org.jspecify.annotations.Nullable</class>
</classes>
</exception>
</exceptions>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<url>https://github.com/HubSpot/dropwizard-guicier</url>

<licenses>
Expand All @@ -184,4 +218,3 @@
<tag>HEAD</tag>
</scm>
</project>

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.core.setup.Bootstrap;

/**
* Dropwizard makes it really hard to allow unknown fields in the {@link io.dropwizard.Configuration}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import static com.google.common.base.Preconditions.checkState;

import com.google.inject.Module;
import io.dropwizard.Configuration;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.core.Configuration;
import io.dropwizard.core.setup.Bootstrap;
import io.dropwizard.core.setup.Environment;

public abstract class DropwizardAwareModule<C extends Configuration> implements Module {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import com.google.inject.Module;
import com.google.inject.matcher.Matchers;
import com.google.inject.spi.ProvisionListener;
import com.hubspot.dropwizard.guicier.injection.InjectorProvider;
import io.dropwizard.core.setup.Environment;
import io.dropwizard.lifecycle.Managed;
import io.dropwizard.lifecycle.ServerLifecycleListener;
import io.dropwizard.servlets.tasks.Task;
import io.dropwizard.setup.Environment;
import java.lang.reflect.Type;
import javax.ws.rs.Path;
import javax.ws.rs.ext.Provider;
Expand Down Expand Up @@ -59,6 +60,7 @@ public <T> void onProvision(ProvisionInvocation<T> provision) {
}

public void register(Injector injector) {
InjectorProvider.set(injector);
registerResourcesAndProviders(environment.jersey().getResourceConfig(), injector);
}

Expand Down
48 changes: 7 additions & 41 deletions src/main/java/com/hubspot/dropwizard/guicier/GuiceBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@
import com.google.inject.servlet.GuiceFilter;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.squarespace.jersey2.guice.JerseyGuiceModule;
import com.squarespace.jersey2.guice.JerseyGuiceUtils;
import io.dropwizard.Configuration;
import io.dropwizard.ConfiguredBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import java.lang.reflect.Field;
import com.hubspot.dropwizard.guicier.injection.InjectionManagerProviderFeature;
import io.dropwizard.core.Configuration;
import io.dropwizard.core.ConfiguredBundle;
import io.dropwizard.core.setup.Bootstrap;
import io.dropwizard.core.setup.Environment;
import java.util.Arrays;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.internal.ServiceLocatorFactoryImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -84,38 +80,25 @@ public void run(final T configuration, final Environment environment) throws Exc
}

final DropwizardModule dropwizardModule = new DropwizardModule(environment);
// We assume that the next service locator will be the main application one
final String serviceLocatorName = getNextServiceLocatorName();

ImmutableSet.Builder<Module> modulesBuilder = ImmutableSet
.<Module>builder()
.addAll(guiceModules)
.addAll(dropwizardAwareModules)
.add(new ServletModule())
.add(dropwizardModule)
.add(new JerseyGuiceModule(serviceLocatorName))
.add(new JerseyGuicierModule())
.add(binder -> {
binder.bind(Environment.class).toInstance(environment);
binder.bind(configClass).toInstance(configuration);
binder.bind(InjectionManagerProviderFeature.class);
});
if (enableGuiceEnforcer) {
modulesBuilder.add(new GuiceEnforcerModule());
}
this.injector = injectorFactory.create(guiceStage, modulesBuilder.build());

JerseyGuiceUtils.install((name, parent) -> {
if (!name.startsWith("__HK2_")) {
return null;
} else if (serviceLocatorName.equals(name)) {
return injector.getInstance(ServiceLocator.class);
} else {
LOG.debug("Returning a new ServiceLocator for name '{}'", name);
return JerseyGuiceUtils.newServiceLocator(name, parent);
}
});

dropwizardModule.register(injector);

environment
.servlets()
.addFilter("Guice Filter", GuiceFilter.class)
Expand Down Expand Up @@ -211,21 +194,4 @@ public final GuiceBundle<U> build() {
);
}
}

private static String getNextServiceLocatorName() {
Class<ServiceLocatorFactoryImpl> factoryClass = ServiceLocatorFactoryImpl.class;
try {
Field nameCountField = factoryClass.getDeclaredField("name_count");
nameCountField.setAccessible(true);
int count = (int) nameCountField.get(null);

Field namePrefixField = factoryClass.getDeclaredField("GENERATED_NAME_PREFIX");
namePrefixField.setAccessible(true);
String prefix = (String) namePrefixField.get(null);

return prefix + count;
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
Loading