move setup of web application to the build() method

This commit is contained in:
Jörg Prante 2023-08-07 09:51:08 +02:00
parent d3140e330e
commit d3198ad8b7
3 changed files with 59 additions and 28 deletions

View file

@ -54,6 +54,7 @@ public class WebApplicationBuilder extends BaseApplicationBuilder {
@Override
public WebApplication build() {
super.build();
return new WebApplication(this);
}
}

View file

@ -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<String, Settings> entry : builder.settings.getGroups("module").entrySet()) {

View file

@ -32,6 +32,10 @@ public class BaseApplicationBuilder implements ApplicationBuilder {
private static final Set<String> 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";
public BaseApplicationBuilder setName(String name) {
this.name = name;
return this;
}
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 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);
}
}