From d3198ad8b75224921c0a8803ed52cd378362294b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Mon, 7 Aug 2023 09:51:08 +0200 Subject: [PATCH] move setup of web application to the build() method --- .../web/WebApplicationBuilder.java | 1 + .../server/application/BaseApplication.java | 2 +- .../application/BaseApplicationBuilder.java | 84 +++++++++++++------ 3 files changed, 59 insertions(+), 28 deletions(-) diff --git a/net-http-server-application-web/src/main/java/org/xbib/net/http/server/application/web/WebApplicationBuilder.java b/net-http-server-application-web/src/main/java/org/xbib/net/http/server/application/web/WebApplicationBuilder.java index 9934628..c1b8dcb 100644 --- a/net-http-server-application-web/src/main/java/org/xbib/net/http/server/application/web/WebApplicationBuilder.java +++ b/net-http-server-application-web/src/main/java/org/xbib/net/http/server/application/web/WebApplicationBuilder.java @@ -54,6 +54,7 @@ public class WebApplicationBuilder extends BaseApplicationBuilder { @Override public WebApplication build() { + super.build(); return new WebApplication(this); } } diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/application/BaseApplication.java b/net-http-server/src/main/java/org/xbib/net/http/server/application/BaseApplication.java index 3d636ce..041c9b6 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/application/BaseApplication.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/application/BaseApplication.java @@ -52,7 +52,7 @@ public class BaseApplication implements Application { protected BaseApplication(BaseApplicationBuilder builder) { this.builder = builder; - this.sessionName = getSettings().get("session.name", "SESS"); + this.sessionName = builder.settings.get("session.name", "SESS"); this.httpResponseRenderer = newResponseRenderer(); this.applicationModuleList = new ArrayList<>(); for (Map.Entry entry : builder.settings.getGroups("module").entrySet()) { diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/application/BaseApplicationBuilder.java b/net-http-server/src/main/java/org/xbib/net/http/server/application/BaseApplicationBuilder.java index 15557c2..00b5b26 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/application/BaseApplicationBuilder.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/application/BaseApplicationBuilder.java @@ -32,6 +32,10 @@ public class BaseApplicationBuilder implements ApplicationBuilder { private static final Set DEFAULT_SUFFIXES = Set.of("css", "js", "ico", "png", "jpg", "jpeg", "gif", "woff2"); + private String name; + + private String profile; + protected ClassLoader classLoader; protected Path home; @@ -61,34 +65,17 @@ public class BaseApplicationBuilder implements ApplicationBuilder { protected HttpRouter httpRouter; protected BaseApplicationBuilder() { - this.classLoader = getClass().getClassLoader(); - this.home = Paths.get(System.getProperties().containsKey("application.home") ? System.getProperty("application.home") : "."); - this.contextPath = "/"; - this.secret = "secret"; this.sessionsEnabled = true; - this.locale = Locale.getDefault(); - this.zoneId = ZoneId.systemDefault(); - this.mimeTypeService = new MimeTypeService(); - String name = System.getProperty("application.name"); - if (name == null) { - name = "application"; - } - String profile = System.getProperty("application.profile"); - if (profile == null) { - profile = "developer"; - } - this.configParams = new ConfigParams() - .withDirectoryName(name) - .withFileNamesWithoutSuffix(profile) - .withSystemEnvironment() - .withSystemProperties(); - this.configLoader = ConfigLoader.getInstance() - .withLogger(bootLogger); - this.settings = configLoader.load(configParams); - if (staticFileSuffixes == null) { - staticFileSuffixes = DEFAULT_SUFFIXES; - } - this.executor = BaseExecutor.builder().build(); + } + + public BaseApplicationBuilder setName(String name) { + this.name = name; + return this; + } + + public BaseApplicationBuilder setProfile(String profile) { + this.profile = profile; + return this; } public BaseApplicationBuilder setSettings(Settings settings) { @@ -159,6 +146,49 @@ public class BaseApplicationBuilder implements ApplicationBuilder { @Override public Application build() { Objects.requireNonNull(httpRouter, "http router must not be null"); + if (this.classLoader == null) { + this.classLoader = getClass().getClassLoader(); + } + if (this.home == null) { + this.home = Paths.get(System.getProperty("application.home", ".")); + } + if (this.contextPath == null) { + this.contextPath = "/"; + } + if (this.secret == null) { + this.secret = "secret"; + } + if (this.locale == null) { + this.locale = Locale.getDefault(); + } + if (this.zoneId == null) { + this.zoneId = ZoneId.systemDefault(); + } + if (name == null) { + name = System.getProperty("application.name", "application"); + } + if (profile == null) { + profile = System.getProperty("application.profile", "developer"); + } + if (this.settings == null) { + this.configParams = new ConfigParams() + .withDirectoryName(name) + .withFileNamesWithoutSuffix(profile) + .withSystemEnvironment() + .withSystemProperties(); + this.configLoader = ConfigLoader.getInstance() + .withLogger(bootLogger); + this.settings = configLoader.load(configParams); + } + if (this.mimeTypeService == null) { + this.mimeTypeService = new MimeTypeService(); + } + if (this.staticFileSuffixes == null) { + this.staticFileSuffixes = DEFAULT_SUFFIXES; + } + if (executor == null) { + this.executor = BaseExecutor.builder().build(); + } return new BaseApplication(this); } }