Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.yammer.dropwizard.config;
  
 
 import  javax.annotation.Nullable;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 // TODO: 10/12/11 <coda> -- test Environment
 /*
     REVIEW: 11/12/11 <coda> -- Probably better to invert this code.
     Instead of letting it collect intermediate results and then exposing those via package-private
     getters, it might be better to pass this a ServletContextHandler, etc., and have it modify
     those directly. That's easier to test.
 */

A Dropwizard service's environment.
 
 public class Environment extends AbstractLifeCycle {
     private static final Log LOG = Log.forClass(Environment.class);
 
     private final AbstractService<?> service;
     private final DropwizardResourceConfig config;
     private final ImmutableSet.Builder<HealthCheckhealthChecks;
     private final ImmutableMap.Builder<StringServletHolderservlets;
     private final ImmutableMultimap.Builder<StringFilterHolderfilters;
     private final ImmutableSet.Builder<Tasktasks;
     private final AggregateLifeCycle lifeCycle;

    
Creates a new environment.

Parameters:
configuration the service's Configuration
service the service
 
     public Environment(Configuration configurationAbstractService<?> service) {
         this. = service;
         this. = new DropwizardResourceConfig() {
             @Override
             public void validate() {
                 super.validate();
                 logResources();
                 logProviders();
                 logHealthChecks();
                 logManagedObjects();
                 logEndpoints();
             }
         };
         this. = ImmutableSet.builder();
         this. = ImmutableMap.builder();
         this. = ImmutableMultimap.builder();
         this. = ImmutableSet.builder();
         this. = ImmutableSet.builder();
         this. = new AggregateLifeCycle();
         
         final HttpServlet jerseyContainer = service.getJerseyContainer();
         if (jerseyContainer != null) {
             addServlet(jerseyContainerconfiguration.getHttpConfiguration().getRootPath()).setInitOrder(.);
         }
         addTask(new GarbageCollectionTask());
     }
 
     @Override
     protected void doStart() throws Exception {
         .start();
     }
 
     @Override
     protected void doStop() throws Exception {
         .stop();
    }

    
Adds the given object as a Jersey singleton resource.

Parameters:
resource a Jersey singleton resource
    public void addResource(Object resource) {
        .getSingletons().add(checkNotNull(resource));
    }

    
Adds the given class as a Jersey resource.

N.B.: This class must either have a no-args constructor or use Jersey's built-in dependency injection.

Parameters:
klass a Jersey resource class
    public void addResource(Class<?> klass) {
        .getClasses().add(checkNotNull(klass));
    }

    
Adds the given object as a Jersey provider.

Parameters:
provider a Jersey provider
    public void addProvider(Object provider) {
        .getSingletons().add(checkNotNull(provider));
    }

    
Adds the given class as a Jersey provider.

N.B.: This class must either have a no-args constructor or use Jersey's built-in dependency injection.

Parameters:
klass a Jersey provider class
    public void addProvider(Class<?> klass) {
        .getClasses().add(checkNotNull(klass));
    }

    
Adds the given health check to the set of health checks exposed on the admin port.

Parameters:
healthCheck a health check
    public void addHealthCheck(HealthCheck healthCheck) {
        .add(checkNotNull(healthCheck));
    }

    
Adds the given Managed instance to the set of objects managed by the server's lifecycle. When the server starts, managed will be started. When the server stops, managed will be stopped.

Parameters:
managed a managed object
    public void manage(Managed managed) {
        .addBean(new JettyManaged(checkNotNull(managed)));
    }

    
Adds the given Jetty LifeCycle instances to the server's lifecycle.

Parameters:
managed a Jetty-managed object
    public void manage(LifeCycle managed) {
        .addBean(checkNotNull(managed));
    }

    
Add a servlet instance.

Parameters:
servlet the servlet instance
urlPattern the URL pattern for requests that should be handled by servlet
Returns:
a ServletConfiguration instance allowing for further configuration
    public ServletConfiguration addServlet(Servlet servlet,
                                           String urlPattern) {
        final ServletHolder holder = new NonblockingServletHolder(checkNotNull(servlet));
        final ServletConfiguration configuration = new ServletConfiguration(holder);
        configuration.addUrlPattern(checkNotNull(urlPattern));
        return configuration;
    }

    
Add a servlet class.

Parameters:
klass the servlet class
urlPattern the URL pattern for requests that should be handled by instances of klass
Returns:
a ServletConfiguration instance allowing for further configuration
    public ServletConfiguration addServlet(Class<? extends Servletklass,
                                           String urlPattern) {
        final ServletHolder holder = new ServletHolder(checkNotNull(klass));
        final ServletConfiguration configuration = new ServletConfiguration(holder);
        configuration.addUrlPattern(checkNotNull(urlPattern));
        return configuration;
    }

    
Add a filter instance.

Parameters:
filter the filter instance
urlPattern the URL pattern for requests that should be handled by filter
Returns:
a FilterConfiguration instance allowing for further configuration
    public FilterConfiguration addFilter(Filter filter,
                                         String urlPattern) {
        final FilterHolder holder = new FilterHolder(checkNotNull(filter));
        final FilterConfiguration configuration = new FilterConfiguration(holder);
        configuration.addUrlPattern(checkNotNull(urlPattern));
        return configuration;
    }

    
Add a filter class.

Parameters:
klass the filter class
urlPattern the URL pattern for requests that should be handled by instances of klass
Returns:
a FilterConfiguration instance allowing for further configuration
    public FilterConfiguration addFilter(Class<? extends Filterklass,
                                         String urlPattern) {
        final FilterHolder holder = new FilterHolder(checkNotNull(klass));
        final FilterConfiguration configuration = new FilterConfiguration(holder);
        configuration.addUrlPattern(checkNotNull(urlPattern));
        return configuration;
    }

    
Add one or more servlet event listeners.

Parameters:
listeners one or more listener instances that implement javax.servlet.ServletContextListener, javax.servlet.ServletContextAttributeListener, javax.servlet.ServletRequestListener or javax.servlet.ServletRequestAttributeListener
    public void addServletListeners(EventListener... listeners) {
        this..addlisteners );
    }
    
    
Adds a Task instance.

Parameters:
task a Task
    public void addTask(Task task) {
        .add(checkNotNull(task));
    }

    
Enables the Jersey feature with the given name.

Parameters:
name the name of the feature to be enabled
See also:
ResourceConfig
    public void enableJerseyFeature(String name) {
        .getFeatures().put(checkNotNull(name), .);
    }

    
Disables the Jersey feature with the given name.

Parameters:
name the name of the feature to be disabled
See also:
ResourceConfig
    public void disableJerseyFeature(String name) {
        .getFeatures().put(checkNotNull(name), .);
    }

    
Sets the given Jersey property.

Parameters:
name the name of the Jersey property
value the value of the Jersey property
See also:
ResourceConfig
    public void setJerseyProperty(String name,
                                  @Nullable Object value) {
        .getProperties().put(checkNotNull(name), value);
    }

    
Creates a new ExecutorService instance with the given parameters whose lifecycle is managed by the service.

Parameters:
nameFormat a String.format(String, Object...)-compatible format String, to which a unique integer (0, 1, etc.) will be supplied as the single parameter.
corePoolSize the number of threads to keep in the pool, even if they are idle.
maximumPoolSize the maximum number of threads to allow in the pool.
keepAliveTime when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
unit the time unit for the keepAliveTime argument.
Returns:
a new ExecutorService instance
    public ExecutorService managedExecutorService(String nameFormat,
                                                  int corePoolSize,
                                                  int maximumPoolSize,
                                                  long keepAliveTime,
                                                  TimeUnit unit) {
        final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(nameFormat)
                                                                      .build();
        final ExecutorService executor = new ThreadPoolExecutor(corePoolSize,
                                                                maximumPoolSize,
                                                                keepAliveTime,
                                                                unit,
                                                                new LinkedBlockingQueue<Runnable>(),
                                                                threadFactory);
        manage(new ExecutorServiceManager(executor, 5, .));
        return executor;
    }

    
Creates a new ScheduledExecutorService instance with the given parameters whose lifecycle is managed by the service.

Parameters:
nameFormat a String.format(String, Object...)-compatible format String, to which a unique integer (0, 1, etc.) will be supplied as the single parameter.
corePoolSize the number of threads to keep in the pool, even if they are idle.
Returns:
a new ScheduledExecutorService instance
                                                                    int corePoolSize) {
        final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(nameFormat)
                                                                      .build();
        final ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(corePoolSize,
                                                                                  threadFactory);
        manage(new ExecutorServiceManager(executor, 5, .));
        return executor;
    }
    /*
     * Internal Accessors
     */
        return .build();
    }
        return .build();
    }
        return .build();
    }
        return .build();
    }
        return .build();
    }
    
    private void logManagedObjects() {
        final ImmutableSet.Builder<Stringbuilder = ImmutableSet.builder();
        for (Object bean : .getBeans()) {
            builder.add(bean.getClass().getCanonicalName());
        }
        .debug("managed objects = {}"builder.build());
    }
    private void logHealthChecks() {
        final ImmutableSet.Builder<Stringbuilder = ImmutableSet.builder();
        for (HealthCheck healthCheck : .build()) {
            builder.add(healthCheck.getClass().getCanonicalName());
        }
        .debug("health checks = {}"builder.build());
    }
    private void logResources() {
        final ImmutableSet.Builder<Stringbuilder = ImmutableSet.builder();
        for (Class<?> klass : .getClasses()) {
            if (klass.isAnnotationPresent(Path.class)) {
                builder.add(klass.getCanonicalName());
            }
        }
        for (Object o : .getSingletons()) {
            if (o.getClass().isAnnotationPresent(Path.class)) {
                builder.add(o.getClass().getCanonicalName());
            }
        }
        .debug("resources = {}"builder.build());
    }
    private void logProviders() {
        final ImmutableSet.Builder<Stringbuilder = ImmutableSet.builder();
        for (Class<?> klass : .getClasses()) {
            if (klass.isAnnotationPresent(Provider.class)) {
                builder.add(klass.getCanonicalName());
            }
        }
        for (Object o : .getSingletons()) {
            if (o.getClass().isAnnotationPresent(Provider.class)) {
                builder.add(o.getClass().getCanonicalName());
            }
        }
        .debug("providers = {}"builder.build());
    }
    private void logEndpoints() {
        final StringBuilder stringBuilder = new StringBuilder(1024).append("\n\n");
        final ImmutableList.Builder<Class<?>> builder = ImmutableList.builder();
        for (Object o : .getSingletons()) {
            if (o.getClass().isAnnotationPresent(Path.class)) {
                builder.add(o.getClass());
            }
        }
        for (Class<?> klass : .getClasses()) {
            if (klass.isAnnotationPresent(Path.class)) {
                builder.add(klass);
            }
        }
        for (Class<?> klass : builder.build()) {
            final String path = klass.getAnnotation(Path.class).value();
            final ImmutableList.Builder<Stringendpoints = ImmutableList.builder();
            for (AnnotatedMethod method : annotatedMethods(klass)) {
                for (HttpMethod verb : method.getMetaMethodAnnotations(HttpMethod.class)) {
                    endpoints.add(String.format("    %-7s %s (%s)",
                                                verb.value(),
                                                path,
                                                klass.getCanonicalName()));
                }
            }
            for (String line : Ordering.natural()
                                       .sortedCopy(endpoints.build())) {
                stringBuilder.append(line).append('\n');
            }
        }
        .info(stringBuilder.toString());
    }
    private MethodList annotatedMethods(Class<?> resource) {
        return new MethodList(resourcetrue).hasMetaAnnotation(HttpMethod.class);
    }
    public AbstractService<?> getService() {
        return ;
    }
New to GrepCode? Check out our FAQ X