upgrade to Gradle 8.0.2, simplify module methods, simplify thread pool, cleanup router

This commit is contained in:
Jörg Prante 2023-04-05 21:55:24 +02:00
parent 93ac72ea0c
commit 43558479e7
28 changed files with 194 additions and 259 deletions

View file

@ -1,22 +1,13 @@
buildscript {
repositories {
maven {
url 'https://xbib.org/repository'
}
}
dependencies {
classpath "org.xbib.gradle.plugin:gradle-plugin-shadow:1.1.1"
}
}
plugins {
id "de.marcphilipp.nexus-publish" version "0.4.0"
id "io.codearte.nexus-staging" version "0.21.1"
id "org.xbib.gradle.plugin.asciidoctor" version "2.5.2.1"
id "org.cyclonedx.bom" version "1.7.2"
id "com.github.spotbugs" version "5.0.13"
id "checkstyle"
id "pmd"
id 'maven-publish'
id 'signing'
id "io.github.gradle-nexus.publish-plugin" version "1.3.0"
id "com.github.spotbugs" version "5.0.14"
id "org.cyclonedx.bom" version "1.7.2"
id "org.xbib.gradle.plugin.asciidoctor" version "2.5.2.2"
}
wrapper {

View file

@ -1,5 +1,5 @@
group = org.xbib
name = net-http
version = 3.1.0
version = 3.2.0
org.gradle.warning.mode = ALL

View file

@ -2,6 +2,8 @@ apply plugin: 'java-library'
java {
modularity.inferModulePath.set(true)
withSourcesJar()
withJavadocJar()
}
compileJava {
@ -21,20 +23,6 @@ jar {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar, javadocJar
}
tasks.withType(JavaCompile) {
options.compilerArgs.add('-Xlint:all,-exports')
options.encoding = 'UTF-8'

View file

@ -1,13 +1,10 @@
apply plugin: "de.marcphilipp.nexus-publish"
publishing {
publications {
mavenJava(MavenPublication) {
"${project.name}"(MavenPublication) {
from components.java
artifact sourcesJar
artifact javadocJar
pom {
artifactId = project.name
name = project.name
description = rootProject.ext.description
url = rootProject.ext.url
@ -49,18 +46,6 @@ publishing {
if (project.hasProperty("signing.keyId")) {
apply plugin: 'signing'
signing {
sign publishing.publications.mavenJava
}
}
if (project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword')) {
nexusPublishing {
repositories {
sonatype {
username = project.property('ossrhUsername')
password = project.property('ossrhPassword')
packageGroup = "org.xbib"
}
}
sign publishing.publications."${project.name}"
}
}

View file

@ -1,11 +1,11 @@
if (project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword')) {
apply plugin: 'io.codearte.nexus-staging'
nexusStaging {
username = project.property('ossrhUsername')
password = project.property('ossrhPassword')
packageGroup = "org.xbib"
nexusPublishing {
repositories {
sonatype {
username = project.property('ossrhUsername')
password = project.property('ossrhPassword')
packageGroup = "org.xbib"
}
}
}
}

Binary file not shown.

View file

@ -1,5 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-all.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

12
gradlew vendored
View file

@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
@ -80,10 +80,10 @@ do
esac
done
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
APP_NAME="Gradle"
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
@ -143,12 +143,16 @@ fi
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac

1
gradlew.bat vendored
View file

@ -26,6 +26,7 @@ if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

View file

@ -6,9 +6,9 @@ import org.xbib.config.ConfigLoader;
import org.xbib.config.ConfigLogger;
import org.xbib.config.ConfigParams;
import org.xbib.config.SystemConfigLogger;
import org.xbib.net.http.server.HttpRequest;
import org.xbib.net.http.server.application.Application;
import org.xbib.net.http.server.application.BaseApplicationModule;
import org.xbib.net.http.server.HttpRequest;
import org.xbib.net.http.server.HttpServerContext;
import org.xbib.net.http.server.service.HttpService;
import org.xbib.settings.Settings;
@ -24,27 +24,21 @@ public class ConfigApplicationModule extends BaseApplicationModule {
bootLogger = optionalBootLogger.orElse(new SystemConfigLogger());
}
private ConfigParams configParams;
private final ConfigParams configParams;
private ConfigLoader configLoader;
private final ConfigLoader configLoader;
private Settings settings;
private final Settings settings;
public ConfigApplicationModule(Application application, String name, Settings settings) {
super(application, name, settings);
}
@Override
public void onOpen(Application application, Settings settings) {
String profile = System.getProperty("application.profile");
if (profile == null) {
profile = "developer";
}
String[] args = profile.split(";");
this.configParams = new ConfigParams()
.withArgs(args)
.withDirectoryName("application")
.withFileNamesWithoutSuffix(args[0])
.withDirectoryName(name)
.withFileNamesWithoutSuffix(profile)
.withSystemEnvironment()
.withSystemProperties();
this.configLoader = ConfigLoader.getInstance()
@ -53,14 +47,9 @@ public class ConfigApplicationModule extends BaseApplicationModule {
}
@Override
public void onOpen(Application application, HttpServerContext httpServerContext, HttpService httpService) {
public void onOpen(HttpServerContext httpServerContext, HttpService httpService, HttpRequest httpRequest) {
httpServerContext.getAttributes().put("configparams", configParams);
httpServerContext.getAttributes().put("configloader", configLoader);
httpServerContext.getAttributes().put("settings", settings);
}
@Override
public void onOpen(Application application, HttpServerContext httpServerContext, HttpService httpService, HttpRequest httpRequest) {
// do nothing
}
}

View file

@ -16,20 +16,16 @@ import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DatabaseApplicationModule<A extends Application> extends BaseApplicationModule {
public class DatabaseApplicationModule extends BaseApplicationModule {
private static final Logger logger = Logger.getLogger(DatabaseApplicationModule.class.getName());
private DataSource dataSource;
private final DataSource dataSource;
private DatabaseProvider databaseProvider;
private final DatabaseProvider databaseProvider;
public DatabaseApplicationModule(Application application, String name, Settings settings) {
super(application, name, settings);
}
@Override
public void onOpen(Application application, Settings settings) throws Exception {
this.dataSource = createDataSource();
String flavor = System.getProperty("database.flavor");
this.databaseProvider = flavor != null ?
@ -37,7 +33,7 @@ public class DatabaseApplicationModule<A extends Application> extends BaseApplic
}
@Override
public void onOpen(Application application, HttpServerContext httpServerContext, HttpService httpService) {
public void onOpen(HttpServerContext httpServerContext, HttpService httpService, HttpRequest httpRequest) {
if (dataSource != null) {
httpServerContext.getAttributes().put("datasource", dataSource);
}
@ -46,12 +42,7 @@ public class DatabaseApplicationModule<A extends Application> extends BaseApplic
}
}
@Override
public void onOpen(Application application, HttpServerContext httpServerContext, HttpService httpService, HttpRequest httpRequest) {
// nothing
}
private DataSource createDataSource() throws Exception {
private DataSource createDataSource() {
Properties properties = new Properties();
System.getProperties().forEach((key, value) -> {
if (key.toString().startsWith("database.") && !key.toString().equals("database.flavor")) {
@ -72,7 +63,11 @@ public class DatabaseApplicationModule<A extends Application> extends BaseApplic
config.setConnectionTimeout(getAsLong(properties, "timeout", 15L * 1000L)); // 15 seconds
config.setHousekeepingPeriodMs(getAsLong(properties, "housekeeping", 600L * 1000L)); // 10 minutes
config.setAutoCommit(getAsBoolean(properties, "autocommit", true));
return new PoolDataSource(config);
try {
return new PoolDataSource(config);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
private int getAsInt(Properties properties, String key, int defaultValue) {

View file

@ -67,7 +67,7 @@ public class NettyHttpServer implements HttpServer {
logger.log(Level.FINE, "parent event loop group = " + parentEventLoopGroup +
" child event loop group = " + childEventLoopGroup +
" socket channel class = " + socketChannelClass +
" router addresses = " + getApplication().getAddresses());
" router addresses = " + builder.application.getAddresses());
}
public static NettyHttpServerBuilder builder() {
@ -84,8 +84,8 @@ public class NettyHttpServer implements HttpServer {
@Override
public void bind() throws BindException {
Set<HttpAddress> httpAddressSet = getApplication().getAddresses();
logger.log(Level.FINE, "http adresses = " + httpAddressSet);
Set<HttpAddress> httpAddressSet = builder.application.getAddresses();
logger.log(Level.FINE, "http addresses = " + httpAddressSet);
for (HttpAddress httpAddress : httpAddressSet) {
SocketConfig socketConfig = httpAddress.getSocketConfig();
ServerBootstrap bootstrap = new ServerBootstrap()
@ -198,8 +198,7 @@ public class NettyHttpServer implements HttpServer {
channelFuture.cancel(true);
}
}
// close application
getApplication().close();
builder.application.close();
logger.log(Level.INFO, "server shutdown complete");
}

View file

@ -26,8 +26,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
@ -39,13 +37,10 @@ public class NioHttpServer implements HttpServer {
private final NioHttpServerBuilder builder;
private final ExecutorService workerPool;
private final Map<HttpAddress, ServerSocketChannel> serverSockets;
NioHttpServer(NioHttpServerBuilder builder) {
this.builder = builder;
this.workerPool = Executors.newCachedThreadPool();
this.serverSockets = new HashMap<>();
}
@ -55,7 +50,7 @@ public class NioHttpServer implements HttpServer {
@Override
public void bind() throws BindException {
for (HttpAddress httpAddress : getApplication().getAddresses()) {
for (HttpAddress httpAddress : builder.application.getAddresses()) {
try {
logger.log(Level.INFO, () -> "trying to bind to " + httpAddress);
ServerSocketChannel channel = ServerSocketChannel.open();
@ -106,7 +101,7 @@ public class NioHttpServer implements HttpServer {
httpAddress,
(InetSocketAddress) socketChannel.getLocalAddress(),
(InetSocketAddress) socketChannel.getRemoteAddress());
handle(requestBuilder, responseBuilder);
builder.application.dispatch(requestBuilder, responseBuilder);
socketChannel.close();
} catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
@ -137,10 +132,6 @@ public class NioHttpServer implements HttpServer {
return builder.application;
}
public void handle(HttpRequestBuilder httpRequestBuilder, HttpResponseBuilder httpResponseBuilder) throws IOException {
getApplication().dispatch(httpRequestBuilder, httpResponseBuilder);
}
@Override
public void close() throws IOException {
for (Map.Entry<HttpAddress, ServerSocketChannel> entry : serverSockets.entrySet()) {

View file

@ -55,7 +55,7 @@ public class SimpleHttpServer implements HttpServer {
@Override
public void bind() throws BindException {
// bind only once per HttpAddress in all domains
for (HttpAddress httpAddress : getApplication().getAddresses()) {
for (HttpAddress httpAddress : builder.application.getAddresses()) {
logger.log(Level.INFO, () -> "trying to bind to " + httpAddress);
try {
InetSocketAddress inetSocketAddress = httpAddress.getInetSocketAddress();
@ -76,7 +76,7 @@ public class SimpleHttpServer implements HttpServer {
if (serverSocket.isBound()) {
serverSockets.put(httpAddress, serverSocket);
logger.log(Level.INFO, () -> "server socket = " + serverSocket +
" domains = " + getApplication().getAddresses() + " bound, listening on " + inetSocketAddress);
" domains = " + builder.application.getAddresses() + " bound, listening on " + inetSocketAddress);
} else {
logger.log(Level.WARNING, "server socket " + serverSocket + " not bound, something is wrong");
}
@ -108,7 +108,7 @@ public class SimpleHttpServer implements HttpServer {
httpAddress,
(InetSocketAddress) socket.getLocalSocketAddress(),
(InetSocketAddress) socket.getRemoteSocketAddress());
getApplication().dispatch(httpRequestBuilder, httpResponseBuilder);
builder.application.dispatch(httpRequestBuilder, httpResponseBuilder);
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
} finally {

View file

@ -56,8 +56,7 @@ public class HttpRouterTest {
.setMethod(HttpMethod.DELETE)
.setRequestURI("/demo")
.addHeader(HttpHeaderNames.HOST, httpAddress.hostAndPort());
router.setApplication(BaseApplication.builder().build());
router.route(httpRequest, httpResponse);
router.route(BaseApplication.builder().build(), httpRequest, httpResponse);
String string = outputStream.toString(StandardCharsets.UTF_8);
Logger.getAnonymousLogger().log(Level.INFO, "the response string is = " + string);
assertTrue(string.contains("/demo"));

View file

@ -38,13 +38,19 @@ public interface Application extends SessionListener, Resolver<Path>, Closeable
Collection<ApplicationModule> getModules();
/**
* Dispatch a regular request.
* Dispatch a request.
* @param requestBuilder the request
* @param responseBuilder the response
*/
void dispatch(HttpRequestBuilder requestBuilder,
HttpResponseBuilder responseBuilder);
/**
* Dispatch a status.
* @param requestBuilder the request
* @param responseBuilder the response
* @param httpResponseStatus the status
*/
void dispatch(HttpRequestBuilder requestBuilder,
HttpResponseBuilder responseBuilder,
HttpResponseStatus httpResponseStatus);

View file

@ -3,6 +3,7 @@ package org.xbib.net.http.server.application;
import java.nio.file.Path;
import java.time.ZoneId;
import java.util.Locale;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.xbib.net.http.server.route.HttpRouter;
import org.xbib.net.mime.MimeTypeService;
@ -17,6 +18,8 @@ public interface ApplicationBuilder {
ApplicationBuilder setKeepAliveTimeUnit(TimeUnit keepAliveTimeUnit);
ApplicationBuilder setExecutor(ThreadPoolExecutor executor);
ApplicationBuilder setHome(Path home);
ApplicationBuilder setContextPath(String contextPath);

View file

@ -3,5 +3,5 @@ package org.xbib.net.http.server.application;
import java.util.concurrent.Callable;
import org.xbib.net.buffer.Releasable;
public interface RouterCallable extends Callable<Boolean>, Releasable {
public interface ApplicationCallable<T> extends Callable<T>, Releasable {
}

View file

@ -4,25 +4,20 @@ import org.xbib.net.http.server.HttpRequest;
import org.xbib.net.http.server.HttpServerContext;
import org.xbib.net.http.server.service.HttpService;
import org.xbib.net.http.server.session.Session;
import org.xbib.settings.Settings;
public interface ApplicationModule {
String getName();
void onOpen(Application application, Settings settings) throws Exception;
void onOpen(HttpServerContext httpServerContext);
void onOpen(Application application, HttpServerContext httpServerContext);
void onOpen(HttpServerContext httpServerContext, HttpService httpService, HttpRequest httpRequest);
void onOpen(Application application, HttpServerContext httpServerContext, HttpService httpService);
void onClose(HttpServerContext httpServerContext);
void onOpen(Application application, HttpServerContext httpServerContext, HttpService httpService, HttpRequest httpRequest);
void onOpen(Session session);
void onClose(Application application, HttpServerContext httpServerContext);
void onClose(Session session);
void onOpen(Application application, Session session);
void onClose(Application application, Session session);
void onClose(Application application);
void onClose();
}

View file

@ -3,11 +3,11 @@ package org.xbib.net.http.server.application;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class RouterTask<T> extends FutureTask<T> {
public class ApplicationTask<T> extends FutureTask<T> {
private final Callable<T> callable;
public RouterTask(Callable<T> callable) {
public ApplicationTask(Callable<T> callable) {
super(callable);
this.callable = callable;
}

View file

@ -15,8 +15,10 @@ public class ApplicationThreadPoolExecutor extends ThreadPoolExecutor {
private final Logger logger = Logger.getLogger(ApplicationThreadPoolExecutor.class.getName());
public ApplicationThreadPoolExecutor(int nThreads, int maxQueue,
long keepAliveTime, TimeUnit timeUnit,
public ApplicationThreadPoolExecutor(int nThreads,
int maxQueue,
long keepAliveTime,
TimeUnit timeUnit,
ThreadFactory threadFactory) {
super(nThreads, nThreads, keepAliveTime, timeUnit, createBlockingQueue(maxQueue), threadFactory);
logger.log(Level.FINE, () -> "threadpool executor up with nThreads = " + nThreads +
@ -26,13 +28,13 @@ public class ApplicationThreadPoolExecutor extends ThreadPoolExecutor {
" threadFactory = " + threadFactory);
}
private static BlockingQueue<Runnable> createBlockingQueue(int max) {
return max == Integer.MAX_VALUE ? new SynchronousQueue<>(true) : new ArrayBlockingQueue<>(max);
private static BlockingQueue<Runnable> createBlockingQueue(int maxQueue) {
return maxQueue == 0 ? new SynchronousQueue<>(true) : new ArrayBlockingQueue<>(maxQueue);
}
@Override
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
return new RouterTask<>(callable);
return new ApplicationTask<>(callable);
}
@Override
@ -43,10 +45,10 @@ public class ApplicationThreadPoolExecutor extends ThreadPoolExecutor {
logger.log(Level.SEVERE, terminationCause.getMessage(), terminationCause);
return;
}
if (runnable instanceof RouterTask<?> routerTask) {
RouterCallable routerCallable = (RouterCallable) routerTask.getCallable();
logger.log(Level.FINEST, () -> "releasing " + routerCallable);
routerCallable.release();
if (runnable instanceof ApplicationTask<?> applicationTask) {
ApplicationCallable<?> applicationCallable = (ApplicationCallable<?>) applicationTask.getCallable();
logger.log(Level.FINEST, () -> "releasing " + applicationCallable);
applicationCallable.release();
}
}
}

View file

@ -35,7 +35,6 @@ import org.xbib.net.http.server.session.Session;
import org.xbib.net.http.server.session.memory.MemoryPropertiesSessionCodec;
import org.xbib.net.http.server.validate.HttpRequestValidator;
import org.xbib.net.mime.MimeTypeService;
import org.xbib.net.util.NamedThreadFactory;
import org.xbib.net.util.RandomUtil;
import org.xbib.settings.Settings;
@ -45,8 +44,6 @@ public class BaseApplication implements Application {
protected BaseApplicationBuilder builder;
private final ApplicationThreadPoolExecutor executor;
private final HttpRequestValidator httpRequestValidator;
protected final String sessionName;
@ -65,11 +62,6 @@ public class BaseApplication implements Application {
protected BaseApplication(BaseApplicationBuilder builder) {
this.builder = builder;
this.executor = new ApplicationThreadPoolExecutor(builder.blockingThreadCount, builder.blockingThreadQueueCount,
builder.blockingThreadKeepAliveTime, builder.blockingThreadKeepAliveTimeUnit,
new NamedThreadFactory("org-xbib-net-http-server-application"));
this.executor.setRejectedExecutionHandler((runnable, threadPoolExecutor) ->
logger.log(Level.SEVERE, "rejected " + runnable + " for thread pool executor = " + threadPoolExecutor));
this.sessionName = getSettings().get("session.name", "SESS");
this.httpRequestValidator = newRequestValidator();
this.incomingCookieHandler = newIncomingCookieHandler();
@ -139,10 +131,11 @@ public class BaseApplication implements Application {
@Override
public void dispatch(HttpRequestBuilder httpRequestBuilder,
HttpResponseBuilder httpResponseBuilder) {
RouterCallable routerCallable = new RouterCallable() {
final Application application = this;
ApplicationCallable<?> applicationCallable = new ApplicationCallable<>() {
@Override
public Boolean call() {
getRouter().route(httpRequestBuilder, httpResponseBuilder);
public Object call() {
getRouter().route(application, httpRequestBuilder, httpResponseBuilder);
return true;
}
@ -152,7 +145,7 @@ public class BaseApplication implements Application {
httpResponseBuilder.release();
}
};
Future<?> future = executor.submit(routerCallable);
Future<?> future = builder.executor.submit(applicationCallable);
logger.log(Level.FINEST, "dispatched " + future);
}
@ -161,9 +154,9 @@ public class BaseApplication implements Application {
HttpResponseBuilder httpResponseBuilder,
HttpResponseStatus httpResponseStatus) {
HttpServerContext httpServerContext = createContext(null, httpRequestBuilder, httpResponseBuilder);
RouterCallable routerCallable = new RouterCallable() {
ApplicationCallable<?> applicationCallable = new ApplicationCallable<>() {
@Override
public Boolean call() {
public Object call() {
getRouter().routeStatus(httpResponseStatus, httpServerContext);
return true;
}
@ -174,7 +167,7 @@ public class BaseApplication implements Application {
httpResponseBuilder.release();
}
};
Future<?> future = executor.submit(routerCallable);
Future<?> future = builder.executor.submit(applicationCallable);
logger.log(Level.FINEST, "dispatched status " + future);
}
@ -249,13 +242,13 @@ public class BaseApplication implements Application {
@Override
public void onCreated(Session session) {
logger.log(Level.FINER, "session name = " + sessionName + " created = " + session);
builder.applicationModuleList.forEach(module -> module.onOpen(this, session));
builder.applicationModuleList.forEach(module -> module.onOpen(session));
}
@Override
public void onDestroy(Session session) {
logger.log(Level.FINER, "session name = " + sessionName + " destroyed = " + session);
builder.applicationModuleList.forEach(module -> module.onClose(this, session));
builder.applicationModuleList.forEach(module -> module.onClose(session));
}
@Override
@ -271,7 +264,7 @@ public class BaseApplication implements Application {
incomingSessionHandler.handle(httpServerContext);
}
// call modules after request/cookie/session setup
builder.applicationModuleList.forEach(module -> module.onOpen(this, httpServerContext));
builder.applicationModuleList.forEach(module -> module.onOpen(httpServerContext));
} catch (HttpException e) {
getRouter().routeException(e);
httpServerContext.fail();
@ -285,7 +278,7 @@ public class BaseApplication implements Application {
public void onClose(HttpServerContext httpServerContext) {
try {
// call modules before session/cookie
builder.applicationModuleList.forEach(module -> module.onClose(this, httpServerContext));
builder.applicationModuleList.forEach(module -> module.onClose(httpServerContext));
if (builder.sessionsEnabled && outgoingSessionHandler != null) {
outgoingSessionHandler.handle(httpServerContext);
}
@ -329,19 +322,19 @@ public class BaseApplication implements Application {
public void close() throws IOException {
logger.log(Level.INFO, "application closing");
// stop dispatching and stop dispatched requests
executor.shutdown();
builder.executor.shutdown();
try {
if (!executor.awaitTermination(10, TimeUnit.SECONDS)) {
List<Runnable> list = executor.shutdownNow();
if (!builder.executor.awaitTermination(10, TimeUnit.SECONDS)) {
List<Runnable> list = builder.executor.shutdownNow();
logger.log(Level.WARNING, "unable to stop runnables " + list);
}
} catch (InterruptedException e) {
List<Runnable> list = executor.shutdownNow();
List<Runnable> list = builder.executor.shutdownNow();
logger.log(Level.WARNING, "unable to stop runnables " + list);
}
builder.applicationModuleList.forEach(module -> {
logger.log(Level.FINE, "application closing module " + module);
module.onClose(this);
module.onClose();
});
if (outgoingSessionHandler != null && (outgoingSessionHandler instanceof Closeable)) {
logger.log(Level.FINE, "application closing outgoing session handler");

View file

@ -10,6 +10,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -19,6 +20,7 @@ import org.xbib.config.ConfigParams;
import org.xbib.config.SystemConfigLogger;
import org.xbib.net.http.server.route.HttpRouter;
import org.xbib.net.mime.MimeTypeService;
import org.xbib.net.util.NamedThreadFactory;
import org.xbib.settings.Settings;
public class BaseApplicationBuilder implements ApplicationBuilder {
@ -44,6 +46,8 @@ public class BaseApplicationBuilder implements ApplicationBuilder {
protected TimeUnit blockingThreadKeepAliveTimeUnit;
protected ThreadPoolExecutor executor;
protected Path home;
protected String contextPath;
@ -73,7 +77,7 @@ public class BaseApplicationBuilder implements ApplicationBuilder {
protected BaseApplicationBuilder() {
this.classLoader = getClass().getClassLoader();
this.blockingThreadCount = Runtime.getRuntime().availableProcessors();
this.blockingThreadQueueCount = Integer.MAX_VALUE;
this.blockingThreadQueueCount = 0; // use fair synchronous queue
this.blockingThreadKeepAliveTime = 60;
this.blockingThreadKeepAliveTimeUnit = TimeUnit.SECONDS;
this.home = Paths.get(System.getProperties().containsKey("application.home") ? System.getProperty("application.home") : ".");
@ -115,6 +119,12 @@ public class BaseApplicationBuilder implements ApplicationBuilder {
return this;
}
@Override
public BaseApplicationBuilder setExecutor(ThreadPoolExecutor executor) {
this.executor = executor;
return this;
}
@Override
public BaseApplicationBuilder setHome(Path home) {
this.home = home;
@ -205,12 +215,16 @@ public class BaseApplicationBuilder implements ApplicationBuilder {
if (staticFileSuffixes == null) {
staticFileSuffixes = DEFAULT_SUFFIXES;
}
if (this.executor == null) {
this.executor = new ApplicationThreadPoolExecutor(blockingThreadCount, blockingThreadQueueCount,
blockingThreadKeepAliveTime, blockingThreadKeepAliveTimeUnit,
new NamedThreadFactory("org-xbib-net-http-server-application"));
this.executor.setRejectedExecutionHandler((runnable, threadPoolExecutor) ->
logger.log(Level.SEVERE, "rejected " + runnable + " for thread pool executor = " + threadPoolExecutor));
}
}
protected void setupApplication(Application application) {
if (router != null) {
router.setApplication(application);
}
for (Map.Entry<String, Settings> entry : settings.getGroups("module").entrySet()) {
String moduleName = entry.getKey();
Settings moduleSettings = entry.getValue();
@ -223,7 +237,6 @@ public class BaseApplicationBuilder implements ApplicationBuilder {
ApplicationModule applicationModule = clazz.getConstructor(Application.class, String.class, Settings.class)
.newInstance(application, moduleName, moduleSettings);
applicationModuleList.add(applicationModule);
applicationModule.onOpen(application, moduleSettings);
} catch (Exception e) {
logger.log(Level.WARNING, e.getMessage(), e);
throw new IllegalArgumentException("class not found or not loadable: " + e.getMessage());

View file

@ -26,34 +26,26 @@ public abstract class BaseApplicationModule implements ApplicationModule {
}
@Override
public void onOpen(Application application, Settings settings) throws Exception {
public void onOpen(HttpServerContext httpServerContext) {
}
@Override
public void onOpen(Application application, HttpServerContext httpServerContext) {
public void onOpen(HttpServerContext httpServerContext, HttpService httpService, HttpRequest httpRequest) {
}
@Override
public void onOpen(Application application, HttpServerContext httpServerContext, HttpService httpService) {
public void onClose(HttpServerContext httpServerContext) {
}
@Override
public void onOpen(Application application, HttpServerContext httpServerContext, HttpService httpService, HttpRequest httpRequest) {
public void onOpen(Session session) {
}
@Override
public void onClose(Application application, HttpServerContext httpServerContext) {
public void onClose(Session session) {
}
@Override
public void onOpen(Application application, Session session) {
}
@Override
public void onClose(Application application, Session session) {
}
@Override
public void onClose(Application application) {
public void onClose() {
}
}

View file

@ -39,8 +39,6 @@ public class BaseHttpRouter implements HttpRouter {
private final HttpRouteResolver<HttpService> httpRouteResolver;
private Application application;
protected BaseHttpRouter(BaseHttpRouterBuilder builder) {
this.builder = builder;
HttpRouteResolver.Builder<HttpService> httpRouteResolverBuilder = newHttpRouteResolverBuilder();
@ -69,12 +67,6 @@ public class BaseHttpRouter implements HttpRouter {
return BaseHttpRouteResolver.builder();
}
@Override
public void setApplication(Application application) {
Objects.requireNonNull(application);
this.application = application;
}
@Override
public Collection<HttpDomain> getDomains() {
return builder.domains;
@ -86,7 +78,8 @@ public class BaseHttpRouter implements HttpRouter {
}
@Override
public void route(HttpRequestBuilder requestBuilder,
public void route(Application application,
HttpRequestBuilder requestBuilder,
HttpResponseBuilder responseBuilder) {
Objects.requireNonNull(requestBuilder);
Objects.requireNonNull(requestBuilder.getRequestURI());
@ -104,64 +97,64 @@ public class BaseHttpRouter implements HttpRouter {
true);
httpRouteResolver.resolve(httpRoute, httpRouteResolverResults::add);
HttpServerContext httpServerContext = application.createContext(httpDomain, requestBuilder, responseBuilder);
route(httpServerContext, httpRouteResolverResults);
}
protected void route(HttpServerContext httpServerContext,
List<HttpRouteResolver.Result<HttpService>> httpRouteResolverResults) {
application.onOpen(httpServerContext);
try {
if (httpServerContext.isFailed()) {
return;
}
if (httpRouteResolverResults.isEmpty()) {
logger.log(Level.FINE, "route resolver results is empty, generating a not found message");
httpServerContext.setResolverResult(null);
routeStatus(NOT_FOUND, httpServerContext);
return;
}
for (HttpRouteResolver.Result<HttpService> httpRouteResolverResult : httpRouteResolverResults) {
try {
// first: create the final request
httpServerContext.setResolverResult(httpRouteResolverResult);
HttpService httpService = httpRouteResolverResult.getValue();
HttpRequest httpRequest = httpServerContext.httpRequest();
application.getModules().forEach(module -> module.onOpen(application, httpServerContext, httpService, httpRequest));
// second: security check, authentication etc.
if (httpService.getSecurityDomain() != null) {
logger.log(Level.FINEST, () -> "handling security domain service " + httpService);
for (HttpHandler httpHandler : httpService.getSecurityDomain().getHandlers()) {
logger.log(Level.FINEST, () -> "handling security domain handler " + httpHandler);
httpHandler.handle(httpServerContext);
}
}
if (httpServerContext.isDone() || httpServerContext.isFailed()) {
break;
}
// after security checks, accept service, open and execute service
httpServerContext.getAttributes().put("service", httpService);
application.getModules().forEach(module -> module.onOpen(application, httpServerContext, httpService));
logger.log(Level.FINEST, () -> "handling service " + httpService);
httpService.handle(httpServerContext);
// if service signals that work is done, break
if (httpServerContext.isDone() || httpServerContext.isFailed()) {
break;
}
} catch (HttpException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
routeException(e);
break;
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
routeToErrorHandler(httpServerContext, t);
break;
}
}
route(application, httpServerContext, httpRouteResolverResults);
} finally {
application.onClose(httpServerContext);
}
}
protected void route(Application application,
HttpServerContext httpServerContext,
List<HttpRouteResolver.Result<HttpService>> httpRouteResolverResults) {
if (httpServerContext.isFailed()) {
return;
}
if (httpRouteResolverResults.isEmpty()) {
logger.log(Level.FINE, "route resolver results is empty, generating a not found message");
httpServerContext.setResolverResult(null);
routeStatus(NOT_FOUND, httpServerContext);
return;
}
for (HttpRouteResolver.Result<HttpService> httpRouteResolverResult : httpRouteResolverResults) {
try {
// first: create the final request
httpServerContext.setResolverResult(httpRouteResolverResult);
HttpService httpService = httpRouteResolverResult.getValue();
HttpRequest httpRequest = httpServerContext.httpRequest();
application.getModules().forEach(module -> module.onOpen(httpServerContext, httpService, httpRequest));
// second: security check, authentication etc.
if (httpService.getSecurityDomain() != null) {
logger.log(Level.FINEST, () -> "handling security domain service " + httpService);
for (HttpHandler httpHandler : httpService.getSecurityDomain().getHandlers()) {
logger.log(Level.FINEST, () -> "handling security domain handler " + httpHandler);
httpHandler.handle(httpServerContext);
}
}
if (httpServerContext.isDone() || httpServerContext.isFailed()) {
break;
}
// after security checks, accept service, open and execute service
httpServerContext.getAttributes().put("service", httpService);
logger.log(Level.FINEST, () -> "handling service " + httpService);
httpService.handle(httpServerContext);
// if service signals that work is done, break
if (httpServerContext.isDone() || httpServerContext.isFailed()) {
break;
}
} catch (HttpException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
routeException(e);
break;
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
routeToErrorHandler(httpServerContext, t);
break;
}
}
}
@Override
public void routeException(HttpException e) {
routeStatus(e.getResponseStatus(), e.getHttpServerContext());

View file

@ -15,13 +15,12 @@ public interface HttpRouter {
DomainsByAddress getDomainsByAddress();
void setApplication(Application application);
void route(HttpRequestBuilder requestBuilder, HttpResponseBuilder responseBuilder);
void routeException(HttpException e);
void route(Application application, HttpRequestBuilder requestBuilder, HttpResponseBuilder responseBuilder);
void routeStatus(HttpResponseStatus httpResponseStatus, HttpServerContext httpServerContext);
void routeToErrorHandler(HttpServerContext httpServerContext, Throwable t);
void routeException(HttpException e);
}

View file

@ -18,16 +18,12 @@ public class GroovyTemplateApplicationModule extends BaseApplicationModule {
private static final Logger logger = Logger.getLogger(GroovyTemplateApplicationModule.class.getName());
private GroovyMarkupTemplateHandler groovyMarkupTemplateHandler;
private final GroovyMarkupTemplateHandler groovyMarkupTemplateHandler;
private GroovyTemplateRenderer groovyTemplateRenderer;
private final GroovyTemplateRenderer groovyTemplateRenderer;
public GroovyTemplateApplicationModule(Application application, String name, Settings settings) {
super(application, name, settings);
}
@Override
public void onOpen(Application application, Settings settings) {
ClassLoader classLoader = GroovyMarkupTemplateHandler.class.getClassLoader();
String defaultMarkupTemplate = settings.get("markup.templateClass",
"org.xbib.net.http.template.DefaultMarkupTemplate");
@ -54,7 +50,7 @@ public class GroovyTemplateApplicationModule extends BaseApplicationModule {
}
@Override
public void onOpen(Application application, HttpServerContext httpServerContext) {
public void onOpen(HttpServerContext httpServerContext) {
try {
groovyMarkupTemplateHandler.handle(httpServerContext);
} catch (IOException e) {
@ -63,14 +59,14 @@ public class GroovyTemplateApplicationModule extends BaseApplicationModule {
}
@Override
public void onOpen(Application application, HttpServerContext httpServerContext, HttpService httpService, HttpRequest httpRequest) {
public void onOpen(HttpServerContext httpServerContext, HttpService httpService, HttpRequest httpRequest) {
httpServerContext.getAttributes().put("request", httpRequest);
httpServerContext.getAttributes().put("params", httpRequest.getParameter().asSingleValuedMap());
application.getModules().forEach(module -> httpServerContext.getAttributes().put(module.getName(), module));
}
@Override
public void onClose(Application application, HttpServerContext httpServerContext) {
public void onClose(HttpServerContext httpServerContext) {
try {
groovyTemplateRenderer.handle(httpServerContext);
} catch (IOException e) {

View file

@ -1,14 +1,14 @@
dependencyResolutionManagement {
versionCatalogs {
libs {
version('gradle', '7.5.1')
version('gradle', '8.0.2')
version('junit', '5.9.2')
version('groovy', '4.0.10')
version('groovy', '4.0.11')
version('netty', '4.1.90.Final')
version('netty-tcnative', '2.0.59.Final')
version('datastructures', '2.0.0')
version('config', '5.0.2')
version('net', '3.1.0')
version('net', '3.2.0')
library('junit-jupiter-api', 'org.junit.jupiter', 'junit-jupiter-api').versionRef('junit')
library('junit-jupiter-params', 'org.junit.jupiter', 'junit-jupiter-params').versionRef('junit')
library('junit-jupiter-engine', 'org.junit.jupiter', 'junit-jupiter-engine').versionRef('junit')