CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-dropwizard--dropwizard-core

Core components of the Dropwizard framework for building Java web applications

Pending
Overview
Eval results
Files

environment-setup.mddocs/

Environment and Setup

The Environment and Bootstrap classes provide the runtime context and pre-start setup for Dropwizard applications, offering access to Jersey, servlet container, admin interface, lifecycle management, and health checks.

Capabilities

Environment Class

The runtime environment providing unified access to all application subsystems including Jersey, servlets, admin interface, lifecycle management, and health checks.

/**
 * A Dropwizard application's environment.
 */
public class Environment {
    /**
     * Creates a new environment.
     * @param name the application name
     * @param objectMapper the ObjectMapper for the application
     * @param validatorFactory the validator factory
     * @param metricRegistry the metric registry
     * @param classLoader the class loader (nullable)
     * @param healthCheckRegistry the health check registry
     * @param configuration the application configuration
     */
    public Environment(String name,
                      ObjectMapper objectMapper,
                      ValidatorFactory validatorFactory,
                      MetricRegistry metricRegistry,
                      ClassLoader classLoader,
                      HealthCheckRegistry healthCheckRegistry,
                      Configuration configuration);
    
    /**
     * Convenience constructor for tests.
     * @param name the application name
     */
    public Environment(String name);
    
    /**
     * Returns the application's JerseyEnvironment.
     * @return the Jersey environment for JAX-RS configuration
     */
    public JerseyEnvironment jersey();
    
    /**
     * Returns the application's ServletEnvironment.
     * @return the servlet environment for servlet registration
     */
    public ServletEnvironment servlets();
    
    /**
     * Returns the application's AdminEnvironment.
     * @return the admin environment for admin interface
     */
    public AdminEnvironment admin();
    
    /**
     * Returns the application's LifecycleEnvironment.
     * @return the lifecycle environment for managed objects
     */
    public LifecycleEnvironment lifecycle();
    
    /**
     * Returns the application's HealthEnvironment.
     * @return the health environment for health checks
     */
    public HealthEnvironment health();
    
    /**
     * Returns the application's name.
     * @return the application name
     */
    public String getName();
    
    /**
     * Returns the application's ObjectMapper.
     * @return the JSON object mapper
     */
    public ObjectMapper getObjectMapper();
    
    /**
     * Returns the application's Validator.
     * @return the bean validator
     */
    public Validator getValidator();
    
    /**
     * Sets the application's Validator.
     * @param validator the bean validator
     */
    public void setValidator(Validator validator);
    
    /**
     * Returns the application's MetricRegistry.
     * @return the metrics registry
     */
    public MetricRegistry metrics();
    
    /**
     * Returns the application's HealthCheckRegistry.
     * @return the health check registry
     */
    public HealthCheckRegistry healthChecks();
    
    /**
     * Returns an ExecutorService to run time bound health checks.
     * @return the health check executor service
     */
    public ExecutorService getHealthCheckExecutorService();
    
    // Internal accessors
    public MutableServletContextHandler getApplicationContext();
    public Servlet getJerseyServletContainer();
    public MutableServletContextHandler getAdminContext();
}

Usage Examples:

@Override
public void run(MyConfiguration configuration, Environment environment) {
    // Register JAX-RS resources
    environment.jersey().register(new UserResource(userService));
    environment.jersey().register(new OrderResource(orderService));
    
    // Register servlets
    environment.servlets().addServlet("metrics", new AdminServlet())
        .addMapping("/admin/metrics/*");
    
    // Register health checks
    environment.healthChecks().register("database", 
        new DatabaseHealthCheck(configuration.getDatabaseUrl()));
    environment.healthChecks().register("redis", 
        new RedisHealthCheck(configuration.getRedis()));
    
    // Register managed objects (lifecycle)
    environment.lifecycle().manage(new DatabaseManager(configuration));
    
    // Register admin tasks
    environment.admin().addTask(new CacheFlushTask(cacheService));
    
    // Access metrics
    final Timer requestTimer = environment.metrics().timer("requests");
    final Counter errorCounter = environment.metrics().counter("errors");
}

Bootstrap Class

The pre-start application environment containing everything required to bootstrap a Dropwizard command. Used during application initialization to register bundles, commands, and configure core services.

/**
 * The pre-start application environment for bootstrapping.
 * @param <T> the configuration type
 */
public class Bootstrap<T extends Configuration> {
    /**
     * Creates a new Bootstrap for the given application.
     * @param application a Dropwizard Application
     */
    public Bootstrap(Application<T> application);
    
    /**
     * Returns the bootstrap's Application.
     * @return the application instance
     */
    public Application<T> getApplication();
    
    /**
     * Adds the given bundle to the bootstrap.
     * @param bundle a ConfiguredBundle
     */
    public void addBundle(ConfiguredBundle<? super T> bundle);
    
    /**
     * Adds the given command to the bootstrap.
     * @param command a Command
     */
    public void addCommand(Command command);
    
    /**
     * Adds the given configured command to the bootstrap.
     * @param command a ConfiguredCommand
     */
    public void addCommand(ConfiguredCommand<T> command);
    
    /**
     * Returns the bootstrap's ObjectMapper.
     * @return the JSON object mapper
     */
    public ObjectMapper getObjectMapper();
    
    /**
     * Sets the ObjectMapper for the bootstrap.
     * @param objectMapper an ObjectMapper (should be created by Jackson.newObjectMapper())
     */
    public void setObjectMapper(ObjectMapper objectMapper);
    
    /**
     * Returns the application metrics.
     * @return the metric registry
     */
    public MetricRegistry getMetricRegistry();
    
    /**
     * Sets a custom registry for the application metrics.
     * @param metricRegistry a custom metric registry
     */
    public void setMetricRegistry(MetricRegistry metricRegistry);
    
    /**
     * Returns the application's validator factory.
     * @return the validator factory
     */
    public ValidatorFactory getValidatorFactory();
    
    /**
     * Sets the validator factory.
     * @param validatorFactory the validator factory
     */
    public void setValidatorFactory(ValidatorFactory validatorFactory);
    
    /**
     * Returns the configuration source provider.
     * @return the configuration source provider
     */
    public ConfigurationSourceProvider getConfigurationSourceProvider();
    
    /**
     * Sets the configuration source provider.
     * @param provider the configuration source provider
     */
    public void setConfigurationSourceProvider(ConfigurationSourceProvider provider);
    
    /**
     * Returns the bootstrap's class loader.
     * @return the class loader
     */
    public ClassLoader getClassLoader();
    
    /**
     * Sets the bootstrap's class loader.
     * @param classLoader the class loader
     */
    public void setClassLoader(ClassLoader classLoader);
    
    /**
     * Returns the configuration factory factory.
     * @return the configuration factory factory
     */
    public ConfigurationFactoryFactory<T> getConfigurationFactoryFactory();
    
    /**
     * Sets the configuration factory factory.
     * @param configurationFactoryFactory the configuration factory factory
     */
    public void setConfigurationFactoryFactory(ConfigurationFactoryFactory<T> configurationFactoryFactory);
    
    /**
     * Returns the health check registry.
     * @return the health check registry
     */
    public HealthCheckRegistry getHealthCheckRegistry();
    
    /**
     * Sets the health check registry.
     * @param healthCheckRegistry the health check registry
     */
    public void setHealthCheckRegistry(HealthCheckRegistry healthCheckRegistry);
    
    /**
     * Returns the JMX reporter for metrics.
     * @return the JmxReporter (nullable)
     */
    public JmxReporter getJmxReporter();
    
    /**
     * Returns the application's commands.
     * @return list of registered commands
     */
    public List<Command> getCommands();
    
    /**
     * Registers JVM metrics and starts JMX reporting.
     */
    public void registerMetrics();
    
    /**
     * Runs the bootstrap's bundles with the given configuration and environment.
     * @param configuration the parsed configuration
     * @param environment the application environment
     * @throws Exception if a bundle throws an exception
     */
    public void run(T configuration, Environment environment) throws Exception;
}

Usage Examples:

@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
    // Add bundles for additional functionality
    bootstrap.addBundle(new AssetsBundle("/assets/", "/ui/", "index.html"));
    bootstrap.addBundle(new SslReloadBundle());
    bootstrap.addBundle(new MigrationsBundle<MyConfiguration>() {
        @Override
        public DataSourceFactory getDataSourceFactory(MyConfiguration configuration) {
            return configuration.getDataSourceFactory();
        }
    });
    
    // Add custom commands
    bootstrap.addCommand(new RenderCommand());
    bootstrap.addCommand(new DbMigrateCommand<>("db migrate", this, "migrations.xml"));
    
    // Customize object mapper
    bootstrap.getObjectMapper().disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    
    // Set custom configuration source provider (e.g., for environment variables)
    bootstrap.setConfigurationSourceProvider(
        new SubstitutingSourceProvider(
            bootstrap.getConfigurationSourceProvider(),
            new EnvironmentVariableSubstitutor(false)
        )
    );
}

AdminEnvironment Class

Administrative environment extending ServletEnvironment for the admin interface, providing task registration and health check management.

/**
 * The administrative environment of a Dropwizard application.
 */
public class AdminEnvironment extends ServletEnvironment {
    /**
     * Creates a new AdminEnvironment.
     * @param handler a servlet context handler
     * @param healthChecks a health check registry
     * @param metricRegistry a metric registry
     * @param adminFactory admin factory configuration
     */
    public AdminEnvironment(MutableServletContextHandler handler,
                           HealthCheckRegistry healthChecks,
                           MetricRegistry metricRegistry,
                           AdminFactory adminFactory);
    
    /**
     * Adds the given task to the admin environment.
     * @param task an administrative task
     */
    public void addTask(Task task);
    
    /**
     * Returns the health check registry.
     * @return the health check registry
     */
    public HealthCheckRegistry getHealthChecks();
    
    /**
     * Returns the task servlet.
     * @return the task servlet for admin tasks
     */
    public TaskServlet getTasks();
}

Usage Examples:

// Register admin tasks
environment.admin().addTask(new GarbageCollectionTask());
environment.admin().addTask(new LogConfigurationTask());
environment.admin().addTask(new CacheFlushTask(cacheManager));

// Register admin servlets
environment.admin().addServlet("custom-admin", new CustomAdminServlet())
    .addMapping("/admin/custom/*");

Types

// Core environment interfaces from related modules
public interface JerseyEnvironment {
    void register(Object component);
    void register(Class<?> componentClass);
}

public interface ServletEnvironment {
    ServletRegistration.Dynamic addServlet(String name, Servlet servlet);
    FilterRegistration.Dynamic addFilter(String name, Filter filter);
}

public interface LifecycleEnvironment {
    void manage(Managed managed);
    ExecutorServiceBuilder executorService(String nameFormat);
}

public interface HealthEnvironment {
    void register(String name, HealthCheck healthCheck);
}

Install with Tessl CLI

npx tessl i tessl/maven-io-dropwizard--dropwizard-core

docs

bundles.md

cli.md

core-application.md

environment-setup.md

index.md

server-config.md

tile.json