io.elsci.mocks.web.tomcat.TomcatProperties Maven / Gradle / Ivy
package io.elsci.mocks.web.tomcat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static io.elsci.mocks.util.PropertyReader.readProperty;
public class TomcatProperties {
private static final Logger LOGGER = LoggerFactory.getLogger(TomcatProperties.class);
private static final int DEFAULT_PORT = 8080;
private static final int DEFAULT_TIMEOUT = 30;
static final boolean DAEMON = true;
static final int THREAD_PRIORITY = Thread.NORM_PRIORITY;
private int port;
private int minThreads;
private int maxThreads;
private int sessionTimeoutMin;
private String contextPath;
private String baseDirPath;
private TomcatProperties() {
}
public int getPort() {
return port;
}
int getMinThreads() {
return minThreads;
}
int getMaxThreads() {
return maxThreads;
}
String getContextPath() {
return contextPath;
}
String getBaseDirPath() {
return baseDirPath;
}
String getThreadPrefix() {
return String.format("http-%s-", getPort());
}
int getSessionTimeoutMin() {
return sessionTimeoutMin;
}
public TomcatProperties setPort(int port) {
this.port = port;
return this;
}
private TomcatProperties setBaseDirPath(String baseDirPath) {
this.baseDirPath = baseDirPath;
return this;
}
private TomcatProperties setMinThreads(int minThreads) {
this.minThreads = minThreads;
return this;
}
private TomcatProperties setMaxThreads(int maxThreads) {
this.maxThreads = maxThreads;
return this;
}
private TomcatProperties setSessionTimeoutMin(int sessionTimeoutMin) {
this.sessionTimeoutMin = sessionTimeoutMin;
return this;
}
private TomcatProperties setContextPath(String contextPath) {
this.contextPath = contextPath;
return this;
}
static TomcatProperties fromSystemProperties() {
String baseDirPathProp = "server.baseDirPath";
String tmpDirProp = "java.io.tmpdir";
String tmpDir = System.getProperty(tmpDirProp);
String baseDir = readProperty(baseDirPathProp).stringValueOr(tmpDir);
LOGGER.info("Tomcat base dir: {}. To override set {} or {} properties", baseDir, baseDirPathProp, tmpDirProp);
String minThreadsProp = "server.minThreads";
String maxThreadsProp = "server.maxThreads";
int minThreads = readProperty(minThreadsProp).intValueOr(Runtime.getRuntime().availableProcessors());
int maxThreads = readProperty(maxThreadsProp).intValueOr(Runtime.getRuntime().availableProcessors() * 3);
LOGGER.info("Thread pool min={}, max={}. To override set {} and {}", minThreads, maxThreads, minThreadsProp, maxThreadsProp);
String sessionTimeoutProp = "server.servlet.session.timeout_min";
int sessionTimeoutMin = readProperty(sessionTimeoutProp).intValueOr(DEFAULT_TIMEOUT);
LOGGER.info("Session timeout: {}. To override set {}", sessionTimeoutMin, sessionTimeoutProp);
int port = readProperty("server.port").intValueOr(DEFAULT_PORT);
String contextPath = readProperty("server.contextPath").stringValueOr("");
return new TomcatProperties()
.setPort(port)
.setBaseDirPath(baseDir)
.setMinThreads(minThreads)
.setMaxThreads(maxThreads)
.setContextPath(contextPath)
.setSessionTimeoutMin(sessionTimeoutMin);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy