Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.yammer.dropwizard.config;
  
 
 import java.util.Map;
 
 // TODO: 11/7/11 <coda> -- document ServerFactory
 // TODO: 11/7/11 <coda> -- document ServerFactory
 
 public class ServerFactory {
     private static final Log LOG = Log.forClass(ServerFactory.class);
 
     private final HttpConfiguration config;
 
     public ServerFactory(HttpConfiguration config) {
         this. = config;
     }
 
     public Server buildServer(Environment envthrows ConfigurationException {
         HealthChecks.register(new DeadlockHealthCheck());
         for (HealthCheck healthCheck : env.getHealthChecks()) {
             HealthChecks.register(healthCheck);
         }
 
         if (env.getHealthChecks().isEmpty()) {
             .warn('\n' +
                              "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" +
                              "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" +
                              "!    THIS SERVICE HAS NO HEALTHCHECKS. THIS MEANS YOU WILL NEVER KNOW IF IT    !\n" +
                              "!    DIES IN PRODUCTION, WHICH MEANS YOU WILL NEVER KNOW IF YOU'RE LETTING     !\n" +
                              "!     YOUR USERS DOWN. YOU SHOULD ADD A HEALTHCHECK FOR EACH DEPENDENCY OF     !\n" +
                              "!     YOUR SERVICE WHICH FULLY (BUT LIGHTLY) TESTS YOUR SERVICE'S ABILITY TO   !\n" +
                              "!      USE THAT SERVICE. THINK OF IT AS A CONTINUOUS INTEGRATION TEST.         !\n" +
                              "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" +
                              "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
             );
         }
 
         final Server server = createServer();
         server.setHandler(createHandler(env));
         server.addBean(env);
         return server;
     }
 
     private Server createServer() {
         final Server server = new Server();
 
         server.addConnector(createExternalConnector());
 
         if (.getAdminPort() != .getPort() ) {
             server.addConnector(createInternalConnector());
         }
 
         server.addBean(new QuietErrorHandler());
 
         server.setSendDateHeader(.isDateHeaderEnabled());
 
        server.setThreadPool(createThreadPool());
        server.setStopAtShutdown(true);
        return server;
    }
    private Connector createExternalConnector() {
        final AbstractConnector connector = createConnector(.getPort());
        connector.setHost(.getBindHost().orNull());
        connector.setAcceptors(.getAcceptorThreadCount());
        connector.setForwarded(.useForwardedHeaders());
        connector.setMaxIdleTime((int.getMaxIdleTime().toMilliseconds());
                                                         .toMilliseconds());
        connector.setAcceptQueueSize(.getAcceptQueueSize());
        connector.setMaxBuffers(.getMaxBufferCount());
        connector.setRequestBufferSize((int.getRequestBufferSize().toBytes());
        connector.setRequestHeaderSize((int.getRequestHeaderBufferSize().toBytes());
        connector.setResponseBufferSize((int.getResponseBufferSize().toBytes());
        connector.setReuseAddress(.isReuseAddressEnabled());
        
        final Optional<DurationlingerTime = .getSoLingerTime();
        if (lingerTime.isPresent()) {
            connector.setSoLingerTime((intlingerTime.get().toMilliseconds());
        }
        connector.setPort(.getPort());
        connector.setName("main");
        return connector;
    }
    private AbstractConnector createConnector(int port) {
        final AbstractConnector connector;
        switch (.getConnectorType()) {
            case :
                connector = new InstrumentedBlockingChannelConnector(port);
                break;
            case :
                connector = new InstrumentedSocketConnector(port);
                break;
            case :
                connector = new InstrumentedSelectChannelConnector(port);
                break;
            default:
                throw new IllegalStateException("Invalid connector type: " + .getConnectorType());
        }
        if (connector instanceof AbstractNIOConnector) {
            ((AbstractNIOConnectorconnector).setUseDirectBuffers(.useDirectBuffers());
        }
        return connector;
    }
    private Handler createHandler(Environment env) {
        final HandlerCollection collection = new HandlerCollection();
        collection.addHandler(createInternalServlet(env));
        collection.addHandler(createExternalServlet(env.getServlets(), env.getFilters(), env.getServletListeners()));
        if (.isEnabled()) {
            collection.addHandler(.build());
        }
        return collection;
    }
    private Handler createInternalServlet(Environment env) {
        final ServletContextHandler handler = new ServletContextHandler();
        handler.addServlet(new ServletHolder(new TaskServlet(env.getTasks())), "/tasks/*");
        handler.addServlet(new ServletHolder(new AdminServlet()), "/*");
        if (.getAdminPort() == .getPort()) {
            handler.setContextPath("/admin");
            handler.setConnectorNames(new String[]{"main"});
        } else {
            handler.setConnectorNames(new String[]{"internal"});
        }
        }
        return handler;
    }
    private SecurityHandler basicAuthHandler(String usernameString password) {
        HashLoginService l = new HashLoginService();
        l.putUser(username, Credential.getCredential(password), new String[] {"user"});
        l.setName("admin");
        Constraint constraint = new Constraint();
        constraint.setName(.);
        constraint.setRoles(new String[]{"user"});
        constraint.setAuthenticate(true);
        ConstraintMapping cm = new ConstraintMapping();
        cm.setConstraint(constraint);
        cm.setPathSpec("/*");
        csh.setAuthenticator(new BasicAuthenticator());
        csh.setRealmName("admin");
        csh.addConstraintMapping(cm);
        csh.setLoginService(l);
        return csh;
    }
                                          ImmutableMultimap<StringFilterHolderfilters,
                                          ImmutableSet<EventListenerlisteners) {
        final ServletContextHandler handler = new ServletContextHandler();
        handler.addFilter(ThreadNameFilter.class"/*", EnumSet.of(.));
        handler.setBaseResource(Resource.newClassPathResource("."));
        for (ImmutableMap.Entry<StringServletHolderentry : servlets.entrySet()) {
            handler.addServlet(entry.getValue(), entry.getKey());
        }
        for (ImmutableMap.Entry<StringFilterHolderentry : filters.entries()) {
            handler.addFilter(entry.getValue(), entry.getKey(), EnumSet.of(.));
        }
        for (EventListener listener : listeners) {
            handler.addEventListener(listener);
        }
        for (Map.Entry<StringStringentry : .getContextParameters().entrySet()) {
            handler.setInitParameterentry.getKey(), entry.getValue() );
        }
        
        handler.setConnectorNames(new String[]{"main"});
        return wrapHandler(handler);
    }
    private Handler wrapHandler(ServletContextHandler handler) {
        final InstrumentedHandler instrumented = new InstrumentedHandler(handler);
        final GzipConfiguration gzip = .getGzipConfiguration();
        if (gzip.isEnabled()) {
            final BiDiGzipHandler gzipHandler = new BiDiGzipHandler(instrumented);
            final Optional<SizeminEntitySize = gzip.getMinimumEntitySize();
            if (minEntitySize.isPresent()) {
                gzipHandler.setMinGzipSize((intminEntitySize.get().toBytes());
            }
            final Optional<SizebufferSize = gzip.getBufferSize();
            if (bufferSize.isPresent()) {
                gzipHandler.setBufferSize((intbufferSize.get().toBytes());
            }
            final Optional<ImmutableSet<String>> userAgents = gzip.getExcludedUserAgents();
            if (userAgents.isPresent()) {
                gzipHandler.setExcluded(userAgents.get());
            }
            final Optional<ImmutableSet<String>> mimeTypes = gzip.getCompressedMimeTypes();
            if (mimeTypes.isPresent()) {
                gzipHandler.setMimeTypes(mimeTypes.get());
            }
            return gzipHandler;
        }
        return instrumented;
    }
    private ThreadPool createThreadPool() {
        final InstrumentedQueuedThreadPool pool = new InstrumentedQueuedThreadPool();
        pool.setMinThreads(.getMinThreads());
        pool.setMaxThreads(.getMaxThreads());
        return pool;
    }
    private Connector createInternalConnector() {
        final SocketConnector connector = new SocketConnector();
        connector.setPort(.getAdminPort());
        connector.setName("internal");
        connector.setThreadPool(new QueuedThreadPool(8));
        return connector;
    }
New to GrepCode? Check out our FAQ X