introduce settings for module instantiation
This commit is contained in:
parent
9fb8da8a55
commit
760e83c035
23 changed files with 158 additions and 106 deletions
|
@ -1,6 +1,3 @@
|
||||||
import org.xbib.net.http.server.ApplicationModule;
|
|
||||||
import org.xbib.net.http.server.application.config.ConfigApplicationModule;
|
|
||||||
|
|
||||||
module org.xbib.net.http.server.application.config {
|
module org.xbib.net.http.server.application.config {
|
||||||
uses org.xbib.config.ConfigLogger;
|
uses org.xbib.config.ConfigLogger;
|
||||||
exports org.xbib.net.http.server.application.config;
|
exports org.xbib.net.http.server.application.config;
|
||||||
|
@ -8,5 +5,4 @@ module org.xbib.net.http.server.application.config {
|
||||||
requires org.xbib.net.http.server;
|
requires org.xbib.net.http.server;
|
||||||
requires org.xbib.config;
|
requires org.xbib.config;
|
||||||
requires java.logging;
|
requires java.logging;
|
||||||
provides ApplicationModule with ConfigApplicationModule;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,16 +30,12 @@ public class ConfigApplicationModule extends BaseApplicationModule {
|
||||||
|
|
||||||
private Settings settings;
|
private Settings settings;
|
||||||
|
|
||||||
public ConfigApplicationModule() {
|
public ConfigApplicationModule(Application application, String name, Settings settings) {
|
||||||
|
super(application, name, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public void onOpen(Application application, Settings settings) {
|
||||||
return "config";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onOpen(Application application) throws Exception {
|
|
||||||
String profile = System.getProperty("application.profile");
|
String profile = System.getProperty("application.profile");
|
||||||
if (profile == null) {
|
if (profile == null) {
|
||||||
profile = "developer";
|
profile = "developer";
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
import org.xbib.net.http.server.ApplicationModule;
|
|
||||||
import org.xbib.net.http.server.application.database.DatabaseApplicationModule;
|
|
||||||
|
|
||||||
module org.xbib.net.http.server.application.database {
|
module org.xbib.net.http.server.application.database {
|
||||||
requires org.xbib.net;
|
requires org.xbib.net;
|
||||||
requires org.xbib.net.http;
|
requires org.xbib.net.http;
|
||||||
requires org.xbib.net.http.server;
|
requires org.xbib.net.http.server;
|
||||||
|
requires org.xbib.config;
|
||||||
requires org.xbib.jdbc.query;
|
requires org.xbib.jdbc.query;
|
||||||
requires org.xbib.jdbc.connection.pool;
|
requires org.xbib.jdbc.connection.pool;
|
||||||
requires org.xbib.datastructures.tiny;
|
requires org.xbib.datastructures.tiny;
|
||||||
requires java.logging;
|
requires java.logging;
|
||||||
requires java.sql;
|
requires java.sql;
|
||||||
provides ApplicationModule with DatabaseApplicationModule;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,13 +9,14 @@ import org.xbib.net.http.server.BaseApplicationModule;
|
||||||
import org.xbib.net.http.server.HttpRequest;
|
import org.xbib.net.http.server.HttpRequest;
|
||||||
import org.xbib.net.http.server.HttpServerContext;
|
import org.xbib.net.http.server.HttpServerContext;
|
||||||
import org.xbib.net.http.server.HttpService;
|
import org.xbib.net.http.server.HttpService;
|
||||||
|
import org.xbib.settings.Settings;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class DatabaseApplicationModule extends BaseApplicationModule {
|
public class DatabaseApplicationModule<A extends Application> extends BaseApplicationModule {
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(DatabaseApplicationModule.class.getName());
|
private static final Logger logger = Logger.getLogger(DatabaseApplicationModule.class.getName());
|
||||||
|
|
||||||
|
@ -23,16 +24,12 @@ public class DatabaseApplicationModule extends BaseApplicationModule {
|
||||||
|
|
||||||
private DatabaseProvider databaseProvider;
|
private DatabaseProvider databaseProvider;
|
||||||
|
|
||||||
public DatabaseApplicationModule() {
|
public DatabaseApplicationModule(Application application, String name, Settings settings) {
|
||||||
|
super(application, name, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public void onOpen(Application application, Settings settings) throws Exception {
|
||||||
return "database";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onOpen(Application application) throws Exception {
|
|
||||||
this.dataSource = createDataSource();
|
this.dataSource = createDataSource();
|
||||||
String flavor = System.getProperty("database.flavor");
|
String flavor = System.getProperty("database.flavor");
|
||||||
this.databaseProvider = flavor != null ?
|
this.databaseProvider = flavor != null ?
|
||||||
|
|
|
@ -5,8 +5,6 @@ dependencies {
|
||||||
api project(':net-http-server-application-config')
|
api project(':net-http-server-application-config')
|
||||||
api project(':net-http-template-groovy')
|
api project(':net-http-template-groovy')
|
||||||
api libs.jdbc.query
|
api libs.jdbc.query
|
||||||
implementation libs.settings.datastructures.json
|
|
||||||
implementation libs.settings.datastructures.yaml
|
|
||||||
implementation libs.webjars.bootstrap
|
implementation libs.webjars.bootstrap
|
||||||
implementation libs.webjars.jquery
|
implementation libs.webjars.jquery
|
||||||
implementation libs.webjars.fontawesome
|
implementation libs.webjars.fontawesome
|
||||||
|
|
|
@ -9,7 +9,6 @@ module org.xbib.net.http.server.application.web {
|
||||||
requires org.xbib.net.http.server.netty.secure;
|
requires org.xbib.net.http.server.netty.secure;
|
||||||
requires org.xbib.net.http.template.groovy;
|
requires org.xbib.net.http.template.groovy;
|
||||||
requires org.xbib.datastructures.tiny;
|
requires org.xbib.datastructures.tiny;
|
||||||
requires org.xbib.datastructures.json.tiny;
|
|
||||||
requires org.xbib.jdbc.query;
|
requires org.xbib.jdbc.query;
|
||||||
requires org.xbib.config;
|
requires org.xbib.config;
|
||||||
requires java.logging;
|
requires java.logging;
|
||||||
|
|
|
@ -10,11 +10,15 @@ import org.xbib.net.http.server.session.IncomingSessionHandler;
|
||||||
import org.xbib.net.http.server.session.OutgoingSessionHandler;
|
import org.xbib.net.http.server.session.OutgoingSessionHandler;
|
||||||
import org.xbib.net.http.server.session.Session;
|
import org.xbib.net.http.server.session.Session;
|
||||||
import org.xbib.net.http.server.session.file.FileJsonSessionCodec;
|
import org.xbib.net.http.server.session.file.FileJsonSessionCodec;
|
||||||
|
import org.xbib.settings.Settings;
|
||||||
|
|
||||||
public class WebApplication extends BaseApplication {
|
public class WebApplication extends BaseApplication {
|
||||||
|
|
||||||
|
private final WebApplicationBuilder builder;
|
||||||
|
|
||||||
protected WebApplication(WebApplicationBuilder builder) {
|
protected WebApplication(WebApplicationBuilder builder) {
|
||||||
super(builder);
|
super(builder);
|
||||||
|
this.builder = builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static WebApplicationBuilder builder() {
|
public static WebApplicationBuilder builder() {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package org.xbib.net.http.server.application.web;
|
package org.xbib.net.http.server.application.web;
|
||||||
|
|
||||||
import org.xbib.net.http.server.BaseApplicationBuilder;
|
import org.xbib.net.http.server.BaseApplicationBuilder;
|
||||||
import org.xbib.net.http.server.Application;
|
|
||||||
|
|
||||||
import org.xbib.settings.Settings;
|
import org.xbib.settings.Settings;
|
||||||
|
|
||||||
|
@ -11,14 +10,18 @@ public class WebApplicationBuilder extends BaseApplicationBuilder {
|
||||||
|
|
||||||
protected String name;
|
protected String name;
|
||||||
|
|
||||||
protected Settings settings;
|
protected WebApplicationBuilder() {
|
||||||
|
|
||||||
protected WebApplicationBuilder() throws Exception {
|
|
||||||
super();
|
super();
|
||||||
this.profile = System.getProperty("application.profile");
|
this.profile = System.getProperty("application.profile");
|
||||||
this.name = System.getProperty("application.name");
|
this.name = System.getProperty("application.name");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WebApplicationBuilder setSettings(Settings settings) {
|
||||||
|
super.setSettings(settings);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public WebApplicationBuilder setProfile(String profile) {
|
public WebApplicationBuilder setProfile(String profile) {
|
||||||
this.profile = profile;
|
this.profile = profile;
|
||||||
return this;
|
return this;
|
||||||
|
@ -29,13 +32,8 @@ public class WebApplicationBuilder extends BaseApplicationBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public WebApplicationBuilder setSettings(Settings settings) {
|
|
||||||
this.settings = settings;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Application build() {
|
public WebApplication build() {
|
||||||
WebApplication webApplication = new WebApplication(this);
|
WebApplication webApplication = new WebApplication(this);
|
||||||
setupApplication(webApplication);
|
setupApplication(webApplication);
|
||||||
return webApplication;
|
return webApplication;
|
||||||
|
|
|
@ -23,13 +23,11 @@ public class HttpResponse extends BaseHttpResponse {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
//logger.log(Level.FINE, "close");
|
builder.close();
|
||||||
//builder.internalClose();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void flush() throws IOException {
|
public void flush() throws IOException {
|
||||||
//logger.log(Level.FINE, "flush");
|
builder.flush();
|
||||||
//builder.internalFlush();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,12 +124,17 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void flush() {
|
||||||
|
internalBufferWrite(Unpooled.buffer(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
ctx.close();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public HttpResponse build() {
|
public HttpResponse build() {
|
||||||
Objects.requireNonNull(ctx);
|
Objects.requireNonNull(ctx);
|
||||||
//if (shouldFlush()) {
|
|
||||||
// internalFlush();
|
|
||||||
//}
|
|
||||||
if (body != null) {
|
if (body != null) {
|
||||||
internalWrite(body);
|
internalWrite(body);
|
||||||
} else if (charBuffer != null && charset != null) {
|
} else if (charBuffer != null && charset != null) {
|
||||||
|
@ -141,19 +146,13 @@ public class HttpResponseBuilder extends BaseHttpResponseBuilder {
|
||||||
} else if (inputStream != null) {
|
} else if (inputStream != null) {
|
||||||
internalWrite(inputStream, bufferSize, true);
|
internalWrite(inputStream, bufferSize, true);
|
||||||
}
|
}
|
||||||
|
if (shouldFlush()) {
|
||||||
|
// really server flush?
|
||||||
|
//flush();
|
||||||
|
}
|
||||||
return new HttpResponse(this);
|
return new HttpResponse(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
//void internalFlush() {
|
|
||||||
// logger.log(Level.FINE, "internal flush");
|
|
||||||
// internalBufferWrite(Unpooled.buffer(0));
|
|
||||||
//}
|
|
||||||
|
|
||||||
//void internalClose() {
|
|
||||||
// logger.log(Level.FINE, "internal close");
|
|
||||||
// ctx.close();
|
|
||||||
//}
|
|
||||||
|
|
||||||
private void internalWrite(String body) {
|
private void internalWrite(String body) {
|
||||||
internalWrite(dataBufferFactory.wrap(StandardCharsets.UTF_8.encode(body)));
|
internalWrite(dataBufferFactory.wrap(StandardCharsets.UTF_8.encode(body)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
dependencies {
|
dependencies {
|
||||||
api project(':net-http')
|
api project(':net-http')
|
||||||
|
api libs.config
|
||||||
|
implementation libs.settings.datastructures.json
|
||||||
|
implementation libs.settings.datastructures.yaml
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ import org.xbib.net.http.server.ApplicationModule;
|
||||||
|
|
||||||
module org.xbib.net.http.server {
|
module org.xbib.net.http.server {
|
||||||
uses ApplicationModule;
|
uses ApplicationModule;
|
||||||
|
uses org.xbib.config.ConfigLogger;
|
||||||
exports org.xbib.net.http.server;
|
exports org.xbib.net.http.server;
|
||||||
exports org.xbib.net.http.server.auth;
|
exports org.xbib.net.http.server.auth;
|
||||||
exports org.xbib.net.http.server.cookie;
|
exports org.xbib.net.http.server.cookie;
|
||||||
|
@ -23,6 +24,7 @@ module org.xbib.net.http.server {
|
||||||
requires org.xbib.net.mime;
|
requires org.xbib.net.mime;
|
||||||
requires org.xbib.net.http;
|
requires org.xbib.net.http;
|
||||||
requires org.xbib.datastructures.common;
|
requires org.xbib.datastructures.common;
|
||||||
|
requires org.xbib.config;
|
||||||
requires java.logging;
|
requires java.logging;
|
||||||
requires java.naming;
|
requires java.naming;
|
||||||
requires java.sql;
|
requires java.sql;
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.xbib.net.http.server;
|
||||||
import org.xbib.net.http.HttpAddress;
|
import org.xbib.net.http.HttpAddress;
|
||||||
import org.xbib.net.http.HttpResponseStatus;
|
import org.xbib.net.http.HttpResponseStatus;
|
||||||
import org.xbib.net.http.server.session.SessionListener;
|
import org.xbib.net.http.server.session.SessionListener;
|
||||||
|
import org.xbib.settings.Settings;
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -26,6 +27,8 @@ public interface Application extends SessionListener, Resolver<Path>, Closeable
|
||||||
|
|
||||||
String getContextPath();
|
String getContextPath();
|
||||||
|
|
||||||
|
Settings getSettings();
|
||||||
|
|
||||||
Collection<ApplicationModule> getModules();
|
Collection<ApplicationModule> getModules();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -26,7 +26,5 @@ public interface ApplicationBuilder {
|
||||||
|
|
||||||
ApplicationBuilder setZoneId(ZoneId zoneId);
|
ApplicationBuilder setZoneId(ZoneId zoneId);
|
||||||
|
|
||||||
ApplicationBuilder addModule(ApplicationModule module);
|
|
||||||
|
|
||||||
Application build();
|
Application build();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
package org.xbib.net.http.server;
|
package org.xbib.net.http.server;
|
||||||
|
|
||||||
import org.xbib.net.http.server.session.Session;
|
import org.xbib.net.http.server.session.Session;
|
||||||
|
import org.xbib.settings.Settings;
|
||||||
|
|
||||||
public interface ApplicationModule extends Comparable<ApplicationModule> {
|
public interface ApplicationModule {
|
||||||
|
|
||||||
String getName();
|
String getName();
|
||||||
|
|
||||||
void onOpen(Application application) throws Exception;
|
void onOpen(Application application, Settings settings) throws Exception;
|
||||||
|
|
||||||
void onOpen(Application application, HttpServerContext httpServerContext);
|
void onOpen(Application application, HttpServerContext httpServerContext);
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@ import org.xbib.net.http.server.session.Session;
|
||||||
import org.xbib.net.http.server.util.BlockingThreadPoolExecutor;
|
import org.xbib.net.http.server.util.BlockingThreadPoolExecutor;
|
||||||
import org.xbib.net.http.server.validate.HttpRequestValidator;
|
import org.xbib.net.http.server.validate.HttpRequestValidator;
|
||||||
import org.xbib.net.util.NamedThreadFactory;
|
import org.xbib.net.util.NamedThreadFactory;
|
||||||
|
import org.xbib.settings.Settings;
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -55,7 +56,7 @@ public class BaseApplication implements Application {
|
||||||
protected BaseApplication(BaseApplicationBuilder builder) {
|
protected BaseApplication(BaseApplicationBuilder builder) {
|
||||||
this.builder = builder;
|
this.builder = builder;
|
||||||
this.executor = new BlockingThreadPoolExecutor(builder.blockingThreadCount, builder.blockingQueueCount,
|
this.executor = new BlockingThreadPoolExecutor(builder.blockingThreadCount, builder.blockingQueueCount,
|
||||||
new NamedThreadFactory("org-xbib-net-hhtp-server-application"));
|
new NamedThreadFactory("org-xbib-net-http-server-application"));
|
||||||
this.executor.setRejectedExecutionHandler((runnable, threadPoolExecutor) ->
|
this.executor.setRejectedExecutionHandler((runnable, threadPoolExecutor) ->
|
||||||
logger.log(Level.SEVERE, "rejected " + runnable + " for thread pool executor = " + threadPoolExecutor));
|
logger.log(Level.SEVERE, "rejected " + runnable + " for thread pool executor = " + threadPoolExecutor));
|
||||||
this.httpRequestValidator = buildRequestValidator();
|
this.httpRequestValidator = buildRequestValidator();
|
||||||
|
@ -86,6 +87,11 @@ public class BaseApplication implements Application {
|
||||||
return builder.contextPath;
|
return builder.contextPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Settings getSettings() {
|
||||||
|
return builder.settings;
|
||||||
|
}
|
||||||
|
|
||||||
public HttpRouter getRouter() {
|
public HttpRouter getRouter() {
|
||||||
return builder.router;
|
return builder.router;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,12 @@
|
||||||
package org.xbib.net.http.server;
|
package org.xbib.net.http.server;
|
||||||
|
|
||||||
|
import org.xbib.config.ConfigLoader;
|
||||||
|
import org.xbib.config.ConfigLogger;
|
||||||
|
import org.xbib.config.ConfigParams;
|
||||||
|
import org.xbib.config.SystemConfigLogger;
|
||||||
import org.xbib.datastructures.common.ImmutableSet;
|
import org.xbib.datastructures.common.ImmutableSet;
|
||||||
import org.xbib.net.http.server.route.HttpRouter;
|
import org.xbib.net.http.server.route.HttpRouter;
|
||||||
|
import org.xbib.settings.Settings;
|
||||||
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
@ -9,6 +14,8 @@ import java.time.ZoneId;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
@ -18,6 +25,17 @@ public class BaseApplicationBuilder implements ApplicationBuilder {
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(BaseApplicationBuilder.class.getName());
|
private static final Logger logger = Logger.getLogger(BaseApplicationBuilder.class.getName());
|
||||||
|
|
||||||
|
private static final ConfigLogger bootLogger;
|
||||||
|
|
||||||
|
static {
|
||||||
|
// early loading of boot logger during static initialization block
|
||||||
|
ServiceLoader<ConfigLogger> serviceLoader = ServiceLoader.load(ConfigLogger.class);
|
||||||
|
Optional<ConfigLogger> optionalBootLogger = serviceLoader.findFirst();
|
||||||
|
bootLogger = optionalBootLogger.orElse(new SystemConfigLogger());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ClassLoader classLoader;
|
||||||
|
|
||||||
protected int blockingThreadCount;
|
protected int blockingThreadCount;
|
||||||
|
|
||||||
protected int blockingQueueCount;
|
protected int blockingQueueCount;
|
||||||
|
@ -36,11 +54,18 @@ public class BaseApplicationBuilder implements ApplicationBuilder {
|
||||||
|
|
||||||
protected ZoneId zoneId;
|
protected ZoneId zoneId;
|
||||||
|
|
||||||
protected List<ApplicationModule> applicationModuleList;
|
|
||||||
|
|
||||||
protected Set<String> staticFileSuffixes;
|
protected Set<String> staticFileSuffixes;
|
||||||
|
|
||||||
|
protected ConfigParams configParams;
|
||||||
|
|
||||||
|
protected ConfigLoader configLoader;
|
||||||
|
|
||||||
|
protected Settings settings;
|
||||||
|
|
||||||
|
protected List<ApplicationModule> applicationModuleList;
|
||||||
|
|
||||||
protected BaseApplicationBuilder() {
|
protected BaseApplicationBuilder() {
|
||||||
|
this.classLoader = getClass().getClassLoader();
|
||||||
this.blockingThreadCount = Runtime.getRuntime().availableProcessors();
|
this.blockingThreadCount = Runtime.getRuntime().availableProcessors();
|
||||||
this.blockingQueueCount = Integer.MAX_VALUE;
|
this.blockingQueueCount = Integer.MAX_VALUE;
|
||||||
this.home = Paths.get(System.getProperties().containsKey("application.home") ? System.getProperty("application.home") : ".");
|
this.home = Paths.get(System.getProperties().containsKey("application.home") ? System.getProperty("application.home") : ".");
|
||||||
|
@ -52,6 +77,11 @@ public class BaseApplicationBuilder implements ApplicationBuilder {
|
||||||
this.applicationModuleList = new ArrayList<>();
|
this.applicationModuleList = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BaseApplicationBuilder setSettings(Settings settings) {
|
||||||
|
this.settings = settings;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BaseApplicationBuilder setThreadCount(int blockingThreadCount) {
|
public BaseApplicationBuilder setThreadCount(int blockingThreadCount) {
|
||||||
this.blockingThreadCount = blockingThreadCount;
|
this.blockingThreadCount = blockingThreadCount;
|
||||||
|
@ -77,19 +107,19 @@ public class BaseApplicationBuilder implements ApplicationBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BaseApplicationBuilder setSecret(String secret) {
|
public ApplicationBuilder setSecret(String secret) {
|
||||||
this.secret = secret;
|
this.secret = secret;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BaseApplicationBuilder setSessionsEnabled(boolean sessionsEnabled) {
|
public ApplicationBuilder setSessionsEnabled(boolean sessionsEnabled) {
|
||||||
this.sessionsEnabled = sessionsEnabled;
|
this.sessionsEnabled = sessionsEnabled;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BaseApplicationBuilder setRouter(HttpRouter router) {
|
public ApplicationBuilder setRouter(HttpRouter router) {
|
||||||
this.router = router;
|
this.router = router;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -106,15 +136,6 @@ public class BaseApplicationBuilder implements ApplicationBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public ApplicationBuilder addModule(ApplicationModule module) {
|
|
||||||
if (module != null) {
|
|
||||||
applicationModuleList.add(module);
|
|
||||||
logger.log(Level.FINE, "module " + module + " added");
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ApplicationBuilder addStaticSuffixes(String... suffixes) {
|
public ApplicationBuilder addStaticSuffixes(String... suffixes) {
|
||||||
ImmutableSet.Builder<String> builder = ImmutableSet.builder();
|
ImmutableSet.Builder<String> builder = ImmutableSet.builder();
|
||||||
for (String suffix : suffixes) {
|
for (String suffix : suffixes) {
|
||||||
|
@ -124,6 +145,11 @@ public class BaseApplicationBuilder implements ApplicationBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ApplicationBuilder registerModule(ApplicationModule applicationModule) {
|
||||||
|
applicationModuleList.add(applicationModule);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Application build() {
|
public Application build() {
|
||||||
Application application = new BaseApplication(this);
|
Application application = new BaseApplication(this);
|
||||||
|
@ -132,19 +158,41 @@ public class BaseApplicationBuilder implements ApplicationBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setupApplication(Application application) {
|
protected void setupApplication(Application application) {
|
||||||
ServiceLoader<ApplicationModule> serviceLoader = ServiceLoader.load(ApplicationModule.class);
|
String profile = System.getProperty("application.profile");
|
||||||
for (ApplicationModule module : serviceLoader) {
|
if (profile == null) {
|
||||||
applicationModuleList.add(module);
|
profile = "developer";
|
||||||
logger.log(Level.FINE, "module " + module + " added");
|
|
||||||
}
|
}
|
||||||
applicationModuleList.forEach(module -> {
|
String[] args = profile.split(";");
|
||||||
try {
|
this.configParams = new ConfigParams()
|
||||||
module.onOpen(application);
|
.withArgs(args)
|
||||||
logger.log(Level.FINE, "module " + module + " opened");
|
.withDirectoryName("application")
|
||||||
} catch (Exception e) {
|
.withFileNamesWithoutSuffix(args[0])
|
||||||
throw new IllegalStateException(e);
|
.withSystemEnvironment()
|
||||||
|
.withSystemProperties();
|
||||||
|
this.configLoader = ConfigLoader.getInstance()
|
||||||
|
.withLogger(bootLogger);
|
||||||
|
this.settings = configLoader.load(configParams);
|
||||||
|
for (Map.Entry<String, Settings> entry : settings.getGroups("module").entrySet()) {
|
||||||
|
String moduleName = entry.getKey();
|
||||||
|
Settings moduleSettings = entry.getValue();
|
||||||
|
if (moduleSettings.getAsBoolean("enabled", true)) {
|
||||||
|
try {
|
||||||
|
String className = moduleSettings.get("class");
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Class<ApplicationModule> clazz =
|
||||||
|
(Class<ApplicationModule>) Class.forName(className, true, classLoader);
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logger.log(Level.WARNING, "disabled module: " + moduleName);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
if (router != null) {
|
if (router != null) {
|
||||||
router.setApplication(application);
|
router.setApplication(application);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,29 @@
|
||||||
package org.xbib.net.http.server;
|
package org.xbib.net.http.server;
|
||||||
|
|
||||||
import org.xbib.net.http.server.session.Session;
|
import org.xbib.net.http.server.session.Session;
|
||||||
|
import org.xbib.settings.Settings;
|
||||||
|
|
||||||
public abstract class BaseApplicationModule implements ApplicationModule {
|
public abstract class BaseApplicationModule implements ApplicationModule {
|
||||||
|
|
||||||
public BaseApplicationModule() {
|
protected final Application application;
|
||||||
|
|
||||||
|
protected final String name;
|
||||||
|
|
||||||
|
protected final Settings settings;
|
||||||
|
|
||||||
|
public BaseApplicationModule(Application application, String name, Settings settings) {
|
||||||
|
this.application = application;
|
||||||
|
this.name = name;
|
||||||
|
this.settings = settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onOpen(Application application) throws Exception {
|
public String getName() {
|
||||||
|
return name != null ? name : settings.get("name", "undefined");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOpen(Application application, Settings settings) throws Exception {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -38,9 +53,4 @@ public abstract class BaseApplicationModule implements ApplicationModule {
|
||||||
@Override
|
@Override
|
||||||
public void onClose(Application application) {
|
public void onClose(Application application) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compareTo(ApplicationModule o) {
|
|
||||||
return getName().compareTo(o.getName());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,7 +80,7 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder {
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
this.version = HttpVersion.HTTP_1_1;
|
this.version = HttpVersion.HTTP_1_1;
|
||||||
this.status = null;
|
this.status = HttpResponseStatus.OK;
|
||||||
this.headers = new HttpHeaders();
|
this.headers = new HttpHeaders();
|
||||||
this.trailingHeaders = new HttpHeaders();
|
this.trailingHeaders = new HttpHeaders();
|
||||||
this.contentType = HttpHeaderValues.APPLICATION_OCTET_STREAM;
|
this.contentType = HttpHeaderValues.APPLICATION_OCTET_STREAM;
|
||||||
|
@ -302,6 +302,7 @@ public abstract class BaseHttpResponseBuilder implements HttpResponseBuilder {
|
||||||
if (httpServerConfig != null && httpServerConfig.getServerName() != null) {
|
if (httpServerConfig != null && httpServerConfig.getServerName() != null) {
|
||||||
headers.add(HttpHeaderNames.SERVER, httpServerConfig.getServerName());
|
headers.add(HttpHeaderNames.SERVER, httpServerConfig.getServerName());
|
||||||
}
|
}
|
||||||
|
logger.log(Level.FINER, "headers built: " + headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CharBuffer wrapHeaders() {
|
public CharBuffer wrapHeaders() {
|
||||||
|
|
|
@ -82,12 +82,14 @@ public abstract class AbstractResourceHandler implements HttpHandler {
|
||||||
context.response()
|
context.response()
|
||||||
.addHeader(HttpHeaderNames.LOCATION, resource.getIndexFileName())
|
.addHeader(HttpHeaderNames.LOCATION, resource.getIndexFileName())
|
||||||
.setResponseStatus(HttpResponseStatus.TEMPORARY_REDIRECT)
|
.setResponseStatus(HttpResponseStatus.TEMPORARY_REDIRECT)
|
||||||
.build().flush(); // flush is important
|
.build()
|
||||||
|
.flush(); // write headers
|
||||||
} else {
|
} else {
|
||||||
// send forbidden, we do not allow directory access
|
// send forbidden, we do not allow directory access
|
||||||
context.response()
|
context.response()
|
||||||
.setResponseStatus(HttpResponseStatus.FORBIDDEN)
|
.setResponseStatus(HttpResponseStatus.FORBIDDEN)
|
||||||
.build().flush(); // fluish is important
|
.build()
|
||||||
|
.flush(); // write status
|
||||||
}
|
}
|
||||||
context.done();
|
context.done();
|
||||||
} else {
|
} else {
|
||||||
|
@ -291,7 +293,7 @@ public abstract class AbstractResourceHandler implements HttpHandler {
|
||||||
HttpServerContext context,
|
HttpServerContext context,
|
||||||
long offset, long size) throws IOException {
|
long offset, long size) throws IOException {
|
||||||
if (resource instanceof HttpServerResource) {
|
if (resource instanceof HttpServerResource) {
|
||||||
logger.log(Level.FINE, "we have a server resource, render by resource");
|
logger.log(Level.FINER, "we have a server resource, render by resource");
|
||||||
((HttpServerResource) resource).render(context);
|
((HttpServerResource) resource).render(context);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,10 @@
|
||||||
import org.xbib.net.http.server.ApplicationModule;
|
|
||||||
import org.xbib.net.http.template.groovy.GroovyTemplateApplicationModule;
|
|
||||||
|
|
||||||
module org.xbib.net.http.template.groovy {
|
module org.xbib.net.http.template.groovy {
|
||||||
exports org.xbib.net.http.template.groovy;
|
exports org.xbib.net.http.template.groovy;
|
||||||
requires org.xbib.net;
|
requires org.xbib.net;
|
||||||
requires org.xbib.net.http;
|
requires org.xbib.net.http;
|
||||||
requires org.xbib.net.http.server;
|
requires org.xbib.net.http.server;
|
||||||
|
requires org.xbib.config;
|
||||||
requires org.apache.groovy;
|
requires org.apache.groovy;
|
||||||
requires org.apache.groovy.templates;
|
requires org.apache.groovy.templates;
|
||||||
requires java.logging;
|
requires java.logging;
|
||||||
provides ApplicationModule with GroovyTemplateApplicationModule;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import org.xbib.net.http.server.HttpServerContext;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UncheckedIOException;
|
import java.io.UncheckedIOException;
|
||||||
import org.xbib.net.http.server.HttpService;
|
import org.xbib.net.http.server.HttpService;
|
||||||
|
import org.xbib.settings.Settings;
|
||||||
|
|
||||||
public class GroovyTemplateApplicationModule extends BaseApplicationModule {
|
public class GroovyTemplateApplicationModule extends BaseApplicationModule {
|
||||||
|
|
||||||
|
@ -15,16 +16,12 @@ public class GroovyTemplateApplicationModule extends BaseApplicationModule {
|
||||||
|
|
||||||
private GroovyTemplateRenderer groovyTemplateRenderer;
|
private GroovyTemplateRenderer groovyTemplateRenderer;
|
||||||
|
|
||||||
public GroovyTemplateApplicationModule() {
|
public GroovyTemplateApplicationModule(Application application, String name, Settings settings) {
|
||||||
|
super(application, name, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public void onOpen(Application application, Settings settings) {
|
||||||
return "groovy-template";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onOpen(Application application) {
|
|
||||||
this.groovyMarkupTemplateHandler = new GroovyMarkupTemplateHandler(application);
|
this.groovyMarkupTemplateHandler = new GroovyMarkupTemplateHandler(application);
|
||||||
this.groovyTemplateRenderer = new GroovyTemplateRenderer();
|
this.groovyTemplateRenderer = new GroovyTemplateRenderer();
|
||||||
}
|
}
|
||||||
|
@ -40,7 +37,9 @@ public class GroovyTemplateApplicationModule extends BaseApplicationModule {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onOpen(Application application, HttpServerContext httpServerContext, HttpService httpService, HttpRequest httpRequest) {
|
public void onOpen(Application application, HttpServerContext httpServerContext, HttpService httpService, HttpRequest httpRequest) {
|
||||||
|
httpServerContext.attributes().put("request", httpRequest);
|
||||||
httpServerContext.attributes().put("params", httpRequest.getParameter().asSingleValuedMap());
|
httpServerContext.attributes().put("params", httpRequest.getParameter().asSingleValuedMap());
|
||||||
|
application.getModules().forEach(module -> httpServerContext.attributes().put(module.getName(), module));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -98,10 +98,10 @@ public class GroovyTemplateResource extends HtmlTemplateResource {
|
||||||
Template template = templates.get(templatePath);
|
Template template = templates.get(templatePath);
|
||||||
Logger templateLogger = Logger.getLogger("template." + getName().replace('/', '.'));
|
Logger templateLogger = Logger.getLogger("template." + getName().replace('/', '.'));
|
||||||
Binding binding = new Binding();
|
Binding binding = new Binding();
|
||||||
binding.setVariable("variables", binding.getVariables());
|
|
||||||
httpServerContext.attributes().forEach(binding::setVariable);
|
httpServerContext.attributes().forEach(binding::setVariable);
|
||||||
binding.setVariable("logger", templateLogger);
|
binding.setVariable("logger", templateLogger);
|
||||||
binding.setVariable("log", templateLogger);
|
binding.setVariable("log", templateLogger);
|
||||||
|
application.getModules().forEach(m -> binding.setVariable(m.getName(), m));
|
||||||
DefaultTemplateResolver templateResolver = httpServerContext.attributes().get(DefaultTemplateResolver.class, "templateresolver");
|
DefaultTemplateResolver templateResolver = httpServerContext.attributes().get(DefaultTemplateResolver.class, "templateresolver");
|
||||||
if (templateResolver != null) {
|
if (templateResolver != null) {
|
||||||
// handle programmatic locale change plus template making under lock so no other request/response can interrupt us
|
// handle programmatic locale change plus template making under lock so no other request/response can interrupt us
|
||||||
|
|
Loading…
Reference in a new issue