
zone.dragon.dropwizard.EnvironmentBinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dropwizard-hk2 Show documentation
Show all versions of dropwizard-hk2 Show documentation
Adds support for Health Checks, LifeCycles, Metrics, and Tasks to be injected by HK2
package zone.dragon.dropwizard;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.dropwizard.Configuration;
import io.dropwizard.lifecycle.setup.LifecycleEnvironment;
import io.dropwizard.setup.Environment;
import lombok.NonNull;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener;
import org.eclipse.jetty.util.component.LifeCycle;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import javax.validation.Validator;
/**
* This binder makes much of the DropWizard environment available to HK2 to be injected into components that request it at runtime.
* Specifically, the following components are bound: - {@link Environment}
- {@link HealthCheckRegistry}
- {@link
* LifecycleEnvironment}
- {@link MetricRegistry}
- {@link Configuration}
* - {@link ObjectMapper}
- {@link Server}
- {@link Validator}
*/
public class EnvironmentBinder extends AbstractBinder {
private final T configuration;
private final Environment environment;
private Server server;
/**
* Creates a new binder that exposes the DropWizard environment to HK2
*
* @param configuration
* DropWizard configuration
* @param environment
* DropWizard environment
*/
public EnvironmentBinder(@NonNull T configuration, @NonNull Environment environment) {
this.configuration = configuration;
this.environment = environment;
environment.lifecycle().addLifeCycleListener(new AbstractLifeCycleListener() {
@Override
public void lifeCycleStarting(LifeCycle event) {
if (event instanceof Server) {
server = (Server) event;
}
}
});
}
@SuppressWarnings("unchecked")
@Override
protected void configure() {
bind(environment).to(Environment.class);
bind(environment.healthChecks()).to(HealthCheckRegistry.class);
bind(environment.lifecycle()).to(LifecycleEnvironment.class);
bind(environment.metrics()).to(MetricRegistry.class);
bind(environment.getValidator()).to(Validator.class);
bind(configuration).to((Class) configuration.getClass()).to(Configuration.class);
bind(server).to(Server.class);
bind(environment.getObjectMapper()).to(ObjectMapper.class);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy