<commit_before>package com.hubspot.dropwizard.guice;

import com.google.common.base.Joiner;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.inject.*;
import com.google.inject.name.Names;
import io.dropwizard.Configuration;
import io.dropwizard.setup.Environment;
import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.reflect.FieldUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.google.common.base.Throwables.propagate;
import static java.lang.String.format;

public class DropwizardEnvironmentModule<T extends Configuration> extends AbstractModule {
	private static final String ILLEGAL_DROPWIZARD_MODULE_STATE = "The dropwizard environment has not yet been set. This is likely caused by trying to access the dropwizard environment during the bootstrap phase.";
	private T configuration;
	private Environment environment;
	private Class<? super T> configurationClass;

	public DropwizardEnvironmentModule(Class<T> configurationClass) {
		this.configurationClass = configurationClass;
	}

	@Override
	protected void configure() {
		Provider<T> provider = new CustomConfigurationProvider();
		bind(configurationClass).toProvider(provider);
		if (configurationClass != Configuration.class) {
			bind(Configuration.class).toProvider(provider);
		}

        bindConfigs(configurationClass, new String[]{}, Lists.<Class<?>>newArrayList());
	}

    private void bindConfigs(Class<?> config, String[] path, List<Class<?>> visited) {
        List<Class<?>> classes = Lists.newArrayList(ClassUtils.getAllSuperclasses(config));
        classes.add(config);<commit_msg>Adding initialization injector binding of ServletContext (primaraly to support Shiro)<commit_after>package com.hubspot.dropwizard.guice;

import com.google.common.base.Joiner;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.inject.*;
import com.google.inject.name.Names;
import io.dropwizard.Configuration;
import io.dropwizard.jetty.MutableServletContextHandler;
import io.dropwizard.setup.Environment;
import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.reflect.FieldUtils;

import javax.servlet.ServletContext;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.google.common.base.Throwables.propagate;
import static java.lang.String.format;

public class DropwizardEnvironmentModule<T extends Configuration> extends AbstractModule {
	private static final String ILLEGAL_DROPWIZARD_MODULE_STATE = "The dropwizard environment has not yet been set. This is likely caused by trying to access the dropwizard environment during the bootstrap phase.";
	private T configuration;
	private Environment environment;
	private Class<? super T> configurationClass;

	public DropwizardEnvironmentModule(Class<T> configurationClass) {
		this.configurationClass = configurationClass;
	}

	@Override
	protected void configure() {
		Provider<T> provider = new CustomConfigurationProvider();
		bind(configurationClass).toProvider(provider);
		if (configurationClass != Configuration.class) {
			bind(Configuration.class).toProvider(provider);
		}

        bindConfigs(configurationClass, new String[]{}, Lists.<Class<?>>newArrayList());

        bindContext("application", environment.getApplicationContext());
	}

    /**
     * Bind some of the context objects to be injectable. Annotated with a {@link com.google.inject.name.Names} to
     * prevent collisions for any that the {@link com.google.inject.servlet.ServletModule} may bind later.
     */
    private void bindContext(String name, MutableServletContextHandler context) {
        bind(ServletContext.class)
            .annotatedWith(Names.named(name))
            .toInstance(context.getServletContext());
    }

    private void bindConfigs(Class<?> config, String[] path, List<Class<?>> visited) {
        List<Class<?>> classes = Lists.newArrayList(ClassUtils.getAllSuperclasses(config));
        classes.add(config);
{'fraction_non_alphanumeric': 0.10085632730732635, 'fraction_numerical': 0.0, 'mean_word_length': 5.419847328244275, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3405365', 'n_tokens_mistral': 1153, 'n_tokens_neox': 1122, 'n_words': 281}