package io.dropwizard.setup;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.codahale.metrics.health.SharedHealthCheckRegistries;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

import io.dropwizard.jersey.DropwizardResourceConfig;
import io.dropwizard.jersey.setup.JerseyContainerHolder;
import io.dropwizard.jersey.setup.JerseyEnvironment;
import io.dropwizard.jersey.setup.JerseyServletContainer;
import io.dropwizard.jetty.MutableServletContextHandler;
import io.dropwizard.jetty.setup.ServletEnvironment;
import io.dropwizard.lifecycle.setup.LifecycleEnvironment;

import javax.annotation.Nullable;
import javax.servlet.Servlet;
import javax.validation.Validator;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

import static java.util.Objects.requireNonNull;

A Dropwizard application's environment.
/** * A Dropwizard application's environment. */
public class Environment { private final String name; private final MetricRegistry metricRegistry; private final HealthCheckRegistry healthCheckRegistry; private final ObjectMapper objectMapper; private Validator validator; private final JerseyContainerHolder jerseyServletContainer; private final JerseyEnvironment jerseyEnvironment; private final MutableServletContextHandler servletContext; private final ServletEnvironment servletEnvironment; private final LifecycleEnvironment lifecycleEnvironment; private final MutableServletContextHandler adminContext; private final AdminEnvironment adminEnvironment; private final ExecutorService healthCheckExecutorService;
Creates a new environment.
Params:
  • name – the name of the application
  • objectMapper – the ObjectMapper for the application
/** * Creates a new environment. * * @param name the name of the application * @param objectMapper the {@link ObjectMapper} for the application */
public Environment(String name, ObjectMapper objectMapper, Validator validator, MetricRegistry metricRegistry, @Nullable ClassLoader classLoader, HealthCheckRegistry healthCheckRegistry) { this.name = name; this.objectMapper = objectMapper; this.metricRegistry = metricRegistry; this.healthCheckRegistry = healthCheckRegistry; this.validator = validator; this.servletContext = new MutableServletContextHandler(); servletContext.setClassLoader(classLoader); this.servletEnvironment = new ServletEnvironment(servletContext); this.adminContext = new MutableServletContextHandler(); adminContext.setClassLoader(classLoader); this.adminEnvironment = new AdminEnvironment(adminContext, healthCheckRegistry, metricRegistry); this.lifecycleEnvironment = new LifecycleEnvironment(); final DropwizardResourceConfig jerseyConfig = new DropwizardResourceConfig(metricRegistry); jerseyConfig.setContextPath(servletContext.getContextPath()); this.jerseyServletContainer = new JerseyContainerHolder(new JerseyServletContainer(jerseyConfig)); this.jerseyEnvironment = new JerseyEnvironment(jerseyServletContainer, jerseyConfig); this.healthCheckExecutorService = this.lifecycle().executorService("TimeBoundHealthCheck-pool-%d") .workQueue(new ArrayBlockingQueue<>(1)) .minThreads(1) .maxThreads(4) .threadFactory(new ThreadFactoryBuilder().setDaemon(true).build()) .rejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()) .build(); try { SharedMetricRegistries.getDefault(); } catch (IllegalStateException e) { SharedMetricRegistries.setDefault("default", metricRegistry); } try { SharedHealthCheckRegistries.getDefault(); } catch (IllegalStateException e) { SharedHealthCheckRegistries.setDefault("default", healthCheckRegistry); } }
Creates an environment with default health check registry
/** * Creates an environment with default health check registry */
public Environment(String name, ObjectMapper objectMapper, Validator validator, MetricRegistry metricRegistry, @Nullable ClassLoader classLoader) { this(name, objectMapper, validator, metricRegistry, classLoader, new HealthCheckRegistry()); }
Returns the application's JerseyEnvironment.
/** * Returns the application's {@link JerseyEnvironment}. */
public JerseyEnvironment jersey() { return jerseyEnvironment; }
Returns an ExecutorService to run time bound health checks
/** * Returns an {@link ExecutorService} to run time bound health checks */
public ExecutorService getHealthCheckExecutorService() { return healthCheckExecutorService; }
Returns the application's AdminEnvironment.
/** * Returns the application's {@link AdminEnvironment}. */
public AdminEnvironment admin() { return adminEnvironment; }
Returns the application's LifecycleEnvironment.
/** * Returns the application's {@link LifecycleEnvironment}. */
public LifecycleEnvironment lifecycle() { return lifecycleEnvironment; }
Returns the application's ServletEnvironment.
/** * Returns the application's {@link ServletEnvironment}. */
public ServletEnvironment servlets() { return servletEnvironment; }
Returns the application's ObjectMapper.
/** * Returns the application's {@link ObjectMapper}. */
public ObjectMapper getObjectMapper() { return objectMapper; }
Returns the application's name.
/** * Returns the application's name. */
public String getName() { return name; }
Returns the application's Validator.
/** * Returns the application's {@link Validator}. */
public Validator getValidator() { return validator; }
Sets the application's Validator.
/** * Sets the application's {@link Validator}. */
public void setValidator(Validator validator) { this.validator = requireNonNull(validator); }
Returns the application's MetricRegistry.
/** * Returns the application's {@link MetricRegistry}. */
public MetricRegistry metrics() { return metricRegistry; }
Returns the application's HealthCheckRegistry.
/** * Returns the application's {@link HealthCheckRegistry}. */
public HealthCheckRegistry healthChecks() { return healthCheckRegistry; } /* * Internal Accessors */ public MutableServletContextHandler getApplicationContext() { return servletContext; } @Nullable public Servlet getJerseyServletContainer() { return jerseyServletContainer.getContainer(); } public MutableServletContextHandler getAdminContext() { return adminContext; } }