Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.dropwizard.server;
  
 
 import java.util.Set;
 
 // TODO: 5/15/13 <coda> -- add tests for AbstractServerFactory
 
A base class for ServerFactory implementations.

Configuration Parameters:

NameDefaultDescription
requestLogThe request log configuration.
gzipThe GZIP configuration.
maxThreads1024The maximum number of threads to use for requests.
minThreads8The minimum number of threads to use for requests.
maxQueuedRequests1024The maximum number of requests to queue before blocking the acceptors.
idleThreadTimeout1 minuteThe amount of time a worker thread can be idle before being stopped.
nofileSoftLimit(none) The number of open file descriptors before a soft error is issued. Requires Jetty's libsetuid.so on java.library.path.
nofileHardLimit(none) The number of open file descriptors before a hard error is issued. Requires Jetty's libsetuid.so on java.library.path.
gid(none) The group ID to switch to once the connectors have started. Requires Jetty's libsetuid.so on java.library.path.
uid(none) The user ID to switch to once the connectors have started. Requires Jetty's libsetuid.so on java.library.path.
user(none) The username to switch to once the connectors have started. Requires Jetty's libsetuid.so on java.library.path.
group(none) The group to switch to once the connectors have started. Requires Jetty's libsetuid.so on java.library.path.
umask(none) The umask to switch to once the connectors have started. Requires Jetty's libsetuid.so on java.library.path.
startsAsRoot(none) Whether or not the Dropwizard application is started as a root user. Requires Jetty's libsetuid.so on java.library.path.
shutdownGracePeriod30 seconds The maximum time to wait for Jetty, and all Managed instances, to cleanly shutdown before forcibly terminating them.
allowedMethodsGET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH The set of allowed HTTP methods. Others will be rejected with a 405 Method Not Allowed response.

public abstract class AbstractServerFactory implements ServerFactory {
    private static final Logger LOGGER = LoggerFactory.getLogger(ServerFactory.class);
    private static final Pattern WINDOWS_NEWLINE = Pattern.compile("\\r\\n?");
    @Valid
    @NotNull
    private RequestLogFactory requestLog = new RequestLogFactory();
    @Valid
    @NotNull
    private GzipFilterFactory gzip = new GzipFilterFactory();
    @Min(2)
    private int maxThreads = 1024;
    @Min(1)
    private int minThreads = 8;
    private int maxQueuedRequests = 1024;
    @MinDuration(1)
    private Duration idleThreadTimeout = Duration.minutes(1);
    @Min(1)
    private Integer nofileSoftLimit;
    @Min(1)
    private Integer nofileHardLimit;
    private Integer gid;
    private Integer uid;
    private String user;
    private String group;
    private String umask;
    private Boolean startsAsRoot;
    private Duration shutdownGracePeriod = Duration.seconds(30);
    @NotNull
    @NotEmpty
    private String jerseyRootPath = "/*";
    @JsonIgnore
    @ValidationMethod(message = "must have a smaller minThreads than maxThreads")
    public boolean isThreadPoolSizedCorrectly() {
        return  <= ;
    }
    @JsonProperty("requestLog")
        return ;
    }
    @JsonProperty("requestLog")
    public void setRequestLogFactory(RequestLogFactory requestLog) {
        this. = requestLog;
    }
    @JsonProperty("gzip")
        return ;
    }
    @JsonProperty("gzip")
    public void setGzipFilterFactory(GzipFilterFactory gzip) {
        this. = gzip;
    }
    public int getMaxThreads() {
        return ;
    }
    public void setMaxThreads(int count) {
        this. = count;
    }
    public int getMinThreads() {
        return ;
    }
    public void setMinThreads(int count) {
        this. = count;
    }
    public int getMaxQueuedRequests() {
        return ;
    }
    public void setMaxQueuedRequests(int maxQueuedRequests) {
        this. = maxQueuedRequests;
    }
    public Duration getIdleThreadTimeout() {
        return ;
    }
    public void setIdleThreadTimeout(Duration idleThreadTimeout) {
        this. = idleThreadTimeout;
    }
    public Integer getNofileSoftLimit() {
        return ;
    }
    public void setNofileSoftLimit(Integer nofileSoftLimit) {
        this. = nofileSoftLimit;
    }
    public Integer getNofileHardLimit() {
        return ;
    }
    public void setNofileHardLimit(Integer nofileHardLimit) {
        this. = nofileHardLimit;
    }
    public Integer getGid() {
        return ;
    }
    public void setGid(Integer gid) {
        this. = gid;
    }
    public Integer getUid() {
        return ;
    }
    public void setUid(Integer uid) {
        this. = uid;
    }
    public String getUser() {
        return ;
    }
    public void setUser(String user) {
        this. = user;
    }
    public String getGroup() {
        return ;
    }
    public void setGroup(String group) {
        this. = group;
    }
    public String getUmask() {
        return ;
    }
    public void setUmask(String umask) {
        this. = umask;
    }
    public Boolean getStartsAsRoot() {
        return ;
    }
    public void setStartsAsRoot(Boolean startsAsRoot) {
        this. = startsAsRoot;
    }
        return ;
    }
    public void setRegisterDefaultExceptionMappers(Boolean registerDefaultExceptionMappers) {
        this. = registerDefaultExceptionMappers;
    }
    public Duration getShutdownGracePeriod() {
        return ;
    }
    public void setShutdownGracePeriod(Duration shutdownGracePeriod) {
        this. = shutdownGracePeriod;
    }
    public Set<StringgetAllowedMethods() {
        return ;
    }
    public void setAllowedMethods(Set<StringallowedMethods) {
        this. = allowedMethods;
    }
    @JsonProperty("rootPath")
    public String getJerseyRootPath() {
        return ;
    }
    @JsonProperty("rootPath")
    public void setJerseyRootPath(String jerseyRootPath) {
        this. = jerseyRootPath;
    }
    protected Handler createAdminServlet(Server server,
                                         MutableServletContextHandler handler,
                                         MetricRegistry metrics,
                                         HealthCheckRegistry healthChecks) {
        configureSessionsAndSecurity(handlerserver);
        handler.setServer(server);
        handler.addServlet(new NonblockingServletHolder(new AdminServlet()), "/*");
        handler.addFilter(AllowedMethodsFilter.class"/*", EnumSet.of(.))
                .setInitParameter(., Joiner.on(',').join());
        return handler;
    }
    private void configureSessionsAndSecurity(MutableServletContextHandler handlerServer server) {
        handler.setServer(server);
        if (handler.isSecurityEnabled()) {
            handler.getSecurityHandler().setServer(server);
        }
        if (handler.isSessionsEnabled()) {
            handler.getSessionHandler().setServer(server);
        }
    }
    protected Handler createAppServlet(Server server,
                                       JerseyEnvironment jersey,
                                       ObjectMapper objectMapper,
                                       Validator validator,
                                       MutableServletContextHandler handler,
                                       @Nullable Servlet jerseyContainer,
                                       MetricRegistry metricRegistry) {
        configureSessionsAndSecurity(handlerserver);
        handler.addFilter(AllowedMethodsFilter.class"/*", EnumSet.of(.))
                .setInitParameter(., Joiner.on(',').join());
        handler.addFilter(ThreadNameFilter.class"/*", EnumSet.of(.));
        if (.isEnabled()) {
            final FilterHolder holder = new FilterHolder(.build());
            handler.addFilter(holder"/*", EnumSet.allOf(DispatcherType.class));
        }
        if (jerseyContainer != null) {
            jersey.setUrlPattern();
            jersey.register(new JacksonMessageBodyProvider(objectMappervalidator));
            if ( == null || ) {
                jersey.register(new LoggingExceptionMapper<Throwable>() {
                });
                jersey.register(new ConstraintViolationExceptionMapper());
                jersey.register(new JsonProcessingExceptionMapper());
                jersey.register(new EarlyEofExceptionMapper());
            }
            handler.addServlet(new NonblockingServletHolder(jerseyContainer), jersey.getUrlPattern());
        }
        final InstrumentedHandler instrumented = new InstrumentedHandler(metricRegistry);
        instrumented.setServer(server);
        instrumented.setHandler(handler);
        return instrumented;
    }
    protected ThreadPool createThreadPool(MetricRegistry metricRegistry) {
        final BlockingQueue<Runnablequeue = new BlockingArrayQueue<>();
        final InstrumentedQueuedThreadPool threadPool =
                new InstrumentedQueuedThreadPool(metricRegistry,
                                                 (int.toMilliseconds(), queue);
        threadPool.setName("dw");
        return threadPool;
    }
    protected Server buildServer(LifecycleEnvironment lifecycle,
                                 ThreadPool threadPool) {
        final Server server = new Server(threadPool);
        server.addLifeCycleListener(buildSetUIDListener());
        lifecycle.attach(server);
        final ErrorHandler errorHandler = new ErrorHandler();
        errorHandler.setServer(server);
        errorHandler.setShowStacks(false);
        server.addBean(errorHandler);
        server.setStopAtShutdown(true);
        return server;
    }
    protected SetUIDListener buildSetUIDListener() {
        final SetUIDListener listener = new SetUIDListener();
        if ( != null) {
            listener.setStartServerAsPrivileged();
        }
        if ( != null) {
            listener.setGid();
        }
        if ( != null) {
            listener.setUid();
        }
        if ( != null) {
            listener.setUsername();
        }
        if ( != null) {
            listener.setGroupname();
        }
        if ( != null ||  != null) {
            final RLimit rlimit = new RLimit();
            if ( != null) {
                rlimit.setHard();
            }
            if ( != null) {
                rlimit.setSoft();
            }
            listener.setRLimitNoFiles(rlimit);
        }
        if ( != null) {
            listener.setUmaskOctal();
        }
        return listener;
    }
    protected Handler addRequestLog(Server serverHandler handlerString name) {
        if (.isEnabled()) {
            final RequestLogHandler requestLogHandler = new RequestLogHandler();
            requestLogHandler.setRequestLog(.build(name));
            // server should own the request log's lifecycle since it's already started,
            // the handler might not become managed in case of an error which would leave
            // the request log stranded
            server.addBean(requestLogHandler.getRequestLog(), true);
            requestLogHandler.setHandler(handler);
            return requestLogHandler;
        }
        return handler;
    }
    protected Handler addStatsHandler(Handler handler) {
        // Graceful shutdown is implemented via the statistics handler,
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=420142
        StatisticsHandler statisticsHandler = new StatisticsHandler();
        statisticsHandler.setHandler(handler);
        return statisticsHandler;
    }
    protected void printBanner(String name) {
        try {
            final String banner = .matcher(Resources.toString(Resources.getResource("banner.txt"),
                                                                             .))
                                                 .replaceAll("\n")
                                                 .replace("\n", String.format("%n"));
            .info(String.format("Starting {}%n{}"), namebanner);
        } catch (IllegalArgumentException | IOException ignored) {
            // don't display the banner if there isn't one
            .info("Starting {}"name);
        }
    }
New to GrepCode? Check out our FAQ X