<commit_before>package com.metrink.croquet;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.DispatcherType;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener;
import org.eclipse.jetty.util.component.LifeCycle;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.servlet.ServletContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.name.Names;
import com.google.inject.persist.PersistFilter;
import com.google.inject.servlet.GuiceFilter;
import com.google.inject.servlet.ServletModule;
import com.google.inject.util.Providers;
import com.metrink.croquet.hibernate.DataSourceHibernateModule;
import com.metrink.croquet.hibernate.PersistanceUnitHibernateModule;
import com.metrink.croquet.hibernate.QueryRunnerModule;
import com.metrink.croquet.inject.CroquetRestModule;
import com.metrink.croquet.modules.ManagedModule;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
 * This is the main class of Croquet.
 *
 * You must create an instance of this class to setup and run the framework.
 * @param <T> The class used when loading the settings YAML
 */
public class CroquetRest<T extends RestSettings> {
    protected static final EnumSet<DispatcherType> DISPATCHER_TYPES =
            EnumSet.of(DispatcherType.ASYNC,
                       DispatcherType.REQUEST,
                       DispatcherType.FORWARD,
                       DispatcherType.INCLUDE);

    private static final Logger LOG = LoggerFactory.getLogger(CroquetRest.class);

    private volatile CroquetStatus status = CroquetStatus.STOPPED;

    private Injector injector;

    private final List<Class<? extends ManagedModule>> managedModules = new ArrayList<Class<? extends ManagedModule>>();
    private final List<Module> guiceModules = new ArrayList<Module>();

    private final T settings;
    private Server jettyServer;
    private final ServletContextHandler sch;

    /**
     * Constructs a Croquet instance given the arguments pass into the program. The {@link WicketSettings} type T is passed
     * as the first argument to this constructor to avoid ugly reflection.
     *
     * @param clazz the settings class
     * @param args the arguments passed into the program.
     */
    CroquetRest(final Class<T> clazz, final T settings) {
        this.settings = settings;

        sch = new ServletContextHandler(ServletContextHandler.SESSIONS);

        // add the croquet module to our list of guice modules
        guiceModules.add(new CroquetRestModule<T>(clazz, settings));

        // The user can pick between: no db, a persistence.xml file, or using application.yml file.
        // If you're not using a db, then we skip this.
        // If you're using the application.yml file, then we install the DataSource Hibernate module.
        // If you're using the persistence.xml file, then we install the JpaPersistenceModule.
        if(settings.getDatabaseSettings().getNotUsed()) {
            LOG.info("Not configuring a database. " +
                    "If you get an error about no binding for an EntityManager, you need to configure a database.");
        } else if(settings.getDatabaseSettings().getPersistenceUnit() == null) {
            LOG.info("Using YAML file to configure Hibernate");

            final DataSourceFactory dataSourceFactory = new DataSourceFactory(settings.getDatabaseSettings());

            guiceModules.add(new DataSourceHibernateModule(dataSourceFactory));
            guiceModules.add(new QueryRunnerModule(dataSourceFactory));
        } else {
            LOG.info("Using persistence.xml file to configure Hibernate");

            guiceModules.add(new PersistanceUnitHibernateModule());
        }

        // this sets the name of the peristence unit
        // we need to jump through these hoops because it has to be
        // null when we're doing a unit test
        guiceModules.add(new AbstractModule() {
            @Override
            protected void configure() {
                bind(String.class).annotatedWith(Names.named("jpa-unit-name"))
                                  .toProvider(getPUNameProvider());
            }
        });

        guiceModules.add(new ServletModule() {
            @Override
            protected void configureServlets() {
                final Map<String, String> params = new HashMap<String, String>();

                params.put(ServerProperties.PROVIDER_PACKAGES, "com.metrink.api");

                bind(ServletContainer.class).in(Singleton.class);
                serve("/*").with(ServletContainer.class, params);
            }
        });
    }

    /**
     * Provides the name of the persistence unit.
     * @return the name of the persistence unit.
     */
    protected Provider<String> getPUNameProvider() {
        final String puName = settings.getDatabaseSettings().getPersistenceUnit();
<commit_msg>Updated provider package<commit_after>package com.metrink.croquet;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.DispatcherType;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener;
import org.eclipse.jetty.util.component.LifeCycle;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.servlet.ServletContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.name.Names;
import com.google.inject.persist.PersistFilter;
import com.google.inject.servlet.GuiceFilter;
import com.google.inject.servlet.ServletModule;
import com.google.inject.util.Providers;
import com.metrink.croquet.hibernate.DataSourceHibernateModule;
import com.metrink.croquet.hibernate.PersistanceUnitHibernateModule;
import com.metrink.croquet.hibernate.QueryRunnerModule;
import com.metrink.croquet.inject.CroquetRestModule;
import com.metrink.croquet.modules.ManagedModule;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
 * This is the main class of Croquet.
 *
 * You must create an instance of this class to setup and run the framework.
 * @param <T> The class used when loading the settings YAML
 */
public class CroquetRest<T extends RestSettings> {
    protected static final EnumSet<DispatcherType> DISPATCHER_TYPES =
            EnumSet.of(DispatcherType.ASYNC,
                       DispatcherType.REQUEST,
                       DispatcherType.FORWARD,
                       DispatcherType.INCLUDE);

    private static final Logger LOG = LoggerFactory.getLogger(CroquetRest.class);

    private volatile CroquetStatus status = CroquetStatus.STOPPED;

    private Injector injector;

    private final List<Class<? extends ManagedModule>> managedModules = new ArrayList<Class<? extends ManagedModule>>();
    private final List<Module> guiceModules = new ArrayList<Module>();

    private final T settings;
    private Server jettyServer;
    private final ServletContextHandler sch;

    /**
     * Constructs a Croquet instance given the arguments pass into the program. The {@link WicketSettings} type T is passed
     * as the first argument to this constructor to avoid ugly reflection.
     *
     * @param clazz the settings class
     * @param args the arguments passed into the program.
     */
    CroquetRest(final Class<T> clazz, final T settings) {
        this.settings = settings;

        sch = new ServletContextHandler(ServletContextHandler.SESSIONS);

        // add the croquet module to our list of guice modules
        guiceModules.add(new CroquetRestModule<T>(clazz, settings));

        // The user can pick between: no db, a persistence.xml file, or using application.yml file.
        // If you're not using a db, then we skip this.
        // If you're using the application.yml file, then we install the DataSource Hibernate module.
        // If you're using the persistence.xml file, then we install the JpaPersistenceModule.
        if(settings.getDatabaseSettings().getNotUsed()) {
            LOG.info("Not configuring a database. " +
                    "If you get an error about no binding for an EntityManager, you need to configure a database.");
        } else if(settings.getDatabaseSettings().getPersistenceUnit() == null) {
            LOG.info("Using YAML file to configure Hibernate");

            final DataSourceFactory dataSourceFactory = new DataSourceFactory(settings.getDatabaseSettings());

            guiceModules.add(new DataSourceHibernateModule(dataSourceFactory));
            guiceModules.add(new QueryRunnerModule(dataSourceFactory));
        } else {
            LOG.info("Using persistence.xml file to configure Hibernate");

            guiceModules.add(new PersistanceUnitHibernateModule());
        }

        // this sets the name of the peristence unit
        // we need to jump through these hoops because it has to be
        // null when we're doing a unit test
        guiceModules.add(new AbstractModule() {
            @Override
            protected void configure() {
                bind(String.class).annotatedWith(Names.named("jpa-unit-name"))
                                  .toProvider(getPUNameProvider());
            }
        });

        // make sure we have at least one package
        if(settings.getProviderPackages().isEmpty()) {
            throw new RuntimeException("No provider packages specified");
        }

        guiceModules.add(new ServletModule() {
            @Override
            protected void configureServlets() {
                final StringBuilder sb = new StringBuilder();

                for(String path:settings.getProviderPackages()) {
                    sb.append(path);
                    sb.append(";");
                }

                final Map<String, String> params = new HashMap<String, String>();

                params.put(ServerProperties.PROVIDER_PACKAGES, sb.toString());

                bind(ServletContainer.class).in(Singleton.class);
                serve("/*").with(ServletContainer.class, params);
            }
        });
    }

    /**
     * Provides the name of the persistence unit.
     * @return the name of the persistence unit.
     */
    protected Provider<String> getPUNameProvider() {
        final String puName = settings.getDatabaseSettings().getPersistenceUnit();

{'fraction_non_alphanumeric': 0.08854440357330531, 'fraction_numerical': 0.0003503240497460151, 'mean_word_length': 3.0811293781272338, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '373788', 'n_tokens_mistral': 3077, 'n_tokens_neox': 2956, 'n_words': 816}