Skip to content

Commit 28cc2d7

Browse files
committed
server: allow app to set serverOptions (add it back)
fix #4029
1 parent 8779484 commit 28cc2d7

8 files changed

Lines changed: 94 additions & 31 deletions

File tree

‎jooby/src/main/java/io/jooby/Jooby.java‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ public class Jooby implements Router, Registry {
7373

7474
private final transient AtomicBoolean stopped = new AtomicBoolean(false);
7575

76-
private static Jooby owner;
76+
private static @Nullable Jooby owner;
7777
private static ExecutionMode BOOT_EXECUTION_MODE = ExecutionMode.DEFAULT;
78-
private static Server BOOT_SERVER;
78+
private static @Nullable Server BOOT_SERVER = null;
7979

8080
private RouterImpl router;
8181

@@ -126,8 +126,7 @@ public Jooby() {
126126
Optional.ofNullable(BOOT_SERVER)
127127
.map(Server::getOutputFactory)
128128
.orElseGet(OutputFactory::create));
129-
router.setServerOptions(
130-
Optional.ofNullable(BOOT_SERVER).map(Server::getOptions).orElseGet(ServerOptions::new));
129+
Optional.ofNullable(BOOT_SERVER).map(Server::getOptions).ifPresent(router::setServerOptions);
131130
} else {
132131
copyState(owner, this);
133132
}
@@ -443,6 +442,17 @@ public ServerOptions getServerOptions() {
443442
return router.getServerOptions();
444443
}
445444

445+
/**
446+
* Sets the server options.
447+
*
448+
* @param serverOptions The server options to configure. Must not be {@code null}.
449+
* @return This instance.
450+
*/
451+
public Jooby setServerOptions(ServerOptions serverOptions) {
452+
router.setServerOptions(serverOptions);
453+
return this;
454+
}
455+
446456
@Override
447457
public boolean isStarted() {
448458
return started.get();
@@ -1220,24 +1230,29 @@ public static void runApp(
12201230
String[] args, Server server, ExecutionMode executionMode, List<Supplier<Jooby>> provider) {
12211231
/* Dump command line as system properties. */
12221232
parseArguments(args).forEach(System::setProperty);
1223-
ServerOptions appServerOptions = null;
12241233
var apps = new ArrayList<Jooby>();
1234+
var serverOptionList = new ArrayList<@Nullable ServerOptions>();
1235+
serverOptionList.add(server.getOptions());
12251236
var targetServer = server.getLoggerOff().isEmpty() ? server : MutedServer.mute(server);
12261237
try {
12271238
for (var factory : provider) {
12281239
var app = createApp(server, executionMode, factory);
12291240
/*
12301241
When running a single app instance, there is no issue with server options.
1231-
When multiple apps pick first in the provided order.
1242+
On multiple apps pick first in the provided order.
12321243
*/
1233-
if (appServerOptions == null) {
1234-
appServerOptions = ServerOptions.from(app.getConfig()).orElse(null);
1235-
}
1244+
serverOptionList.add(ServerOptions.from(app.getConfig()).orElseGet(app::getServerOptions));
12361245
apps.add(app);
12371246
}
12381247
// Override when there is something and server options were not set
1239-
if (server.getOptions().defaults && appServerOptions != null) {
1240-
server.setOptions(appServerOptions);
1248+
var overrideServerOptions =
1249+
serverOptionList.stream()
1250+
.filter(Objects::nonNull)
1251+
.filter(it -> !it.defaults)
1252+
.findFirst()
1253+
.orElse(server.getOptions());
1254+
if (server.getOptions() != overrideServerOptions) {
1255+
server.setOptions(overrideServerOptions);
12411256
}
12421257
for (var app : apps) {
12431258
server.init(app);

‎jooby/src/main/java/io/jooby/Router.java‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,16 @@ default Router setAttribute(String key, Object value) {
188188
ServiceRegistry getServices();
189189

190190
/**
191-
* Server options.
191+
* Retrieves the server options.
192192
*
193-
* @return Server options.
193+
* <p>An application can provide custom server options, which will be used to configure the server
194+
* provided that the server has not been explicitly configured elsewhere.
195+
*
196+
* <p><strong>Multi-application behavior:</strong> In a setup with multiple applications, the
197+
* first application that provides server options takes precedence. Any server options provided by
198+
* subsequent applications are ignored.
199+
*
200+
* @return The server options.
194201
*/
195202
ServerOptions getServerOptions();
196203

‎jooby/src/main/java/io/jooby/Server.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,13 @@ public Server setOptions(ServerOptions options) {
156156
* @return This instance.
157157
*/
158158
default Server init(Jooby application) {
159-
var registry = application.getServices();
160159
var options = getOptions();
161160
options.setServer(getName());
162161

162+
// set final options
163+
application.setServerOptions(options);
164+
// update registry with server options and server instance
165+
var registry = application.getServices();
163166
registry.put(ServerOptions.class, options);
164167
registry.put(Server.class, this);
165168

‎jooby/src/main/java/io/jooby/ServerOptions.java‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public static Optional<ServerOptions> from(Config conf) {
205205
// ssl
206206
SslOptions.from(conf, "server.ssl").ifPresent(options::setSsl);
207207
if (conf.hasPath("server.httpsOnly")) {
208-
options.httpsOnly = conf.getBoolean("server.httpsOnly");
208+
options.setHttpsOnly(conf.getBoolean("server.httpsOnly"));
209209
}
210210
if (conf.hasPath("server.http2")) {
211211
options.setHttp2(conf.getBoolean("server.http2"));
@@ -241,6 +241,7 @@ public String toString() {
241241
*/
242242
public ServerOptions setServer(String server) {
243243
this.server = server;
244+
this.defaults = false;
244245
return this;
245246
}
246247

@@ -270,6 +271,7 @@ public int getPort() {
270271
*/
271272
public ServerOptions setPort(int port) {
272273
this.port = Math.max(0, port);
274+
this.defaults = false;
273275
return this;
274276
}
275277

@@ -303,6 +305,7 @@ public ServerOptions setSecurePort(@Nullable Integer securePort) {
303305
} else {
304306
this.securePort = Math.max(0, securePort);
305307
}
308+
this.defaults = false;
306309
return this;
307310
}
308311

@@ -323,6 +326,7 @@ public boolean isHttpsOnly() {
323326
*/
324327
public ServerOptions setHttpsOnly(boolean httpsOnly) {
325328
this.httpsOnly = httpsOnly;
329+
this.defaults = false;
326330
return this;
327331
}
328332

@@ -343,6 +347,7 @@ public int getIoThreads() {
343347
*/
344348
public ServerOptions setIoThreads(int ioThreads) {
345349
this.ioThreads = ioThreads;
350+
this.defaults = false;
346351
return this;
347352
}
348353

@@ -366,6 +371,7 @@ public int getWorkerThreads() {
366371
*/
367372
public ServerOptions setWorkerThreads(int workerThreads) {
368373
this.workerThreads = workerThreads;
374+
this.defaults = false;
369375
return this;
370376
}
371377

@@ -388,6 +394,7 @@ public ServerOptions setWorkerThreads(int workerThreads) {
388394
*/
389395
public ServerOptions setCompressionLevel(@Nullable Integer compressionLevel) {
390396
this.compressionLevel = compressionLevel;
397+
this.defaults = false;
391398
return this;
392399
}
393400

@@ -411,6 +418,7 @@ public boolean getDefaultHeaders() {
411418
*/
412419
public ServerOptions setDefaultHeaders(boolean defaultHeaders) {
413420
this.defaultHeaders = defaultHeaders;
421+
this.defaults = false;
414422
return this;
415423
}
416424

@@ -431,6 +439,7 @@ public OutputOptions getOutput() {
431439
*/
432440
public ServerOptions setOutput(OutputOptions output) {
433441
this.output = output;
442+
this.defaults = false;
434443
return this;
435444
}
436445

@@ -452,6 +461,7 @@ public int getMaxRequestSize() {
452461
*/
453462
public ServerOptions setMaxRequestSize(int maxRequestSize) {
454463
this.maxRequestSize = maxRequestSize;
464+
this.defaults = false;
455465
return this;
456466
}
457467

@@ -472,6 +482,7 @@ public int getMaxFormFields() {
472482
*/
473483
public ServerOptions setMaxFormFields(int maxFormFields) {
474484
this.maxFormFields = maxFormFields;
485+
this.defaults = false;
475486
return this;
476487
}
477488

@@ -500,6 +511,7 @@ public int getMaxHeaderSize() {
500511
*/
501512
public ServerOptions setMaxHeaderSize(int maxHeaderSize) {
502513
this.maxHeaderSize = maxHeaderSize;
514+
this.defaults = false;
503515
return this;
504516
}
505517

@@ -519,11 +531,12 @@ public String getHost() {
519531
* @return This options.
520532
*/
521533
public ServerOptions setHost(String host) {
522-
if (host == null || host.trim().length() == 0 || "localhost".equalsIgnoreCase(host.trim())) {
534+
if (host == null || host.trim().isEmpty() || "localhost".equalsIgnoreCase(host.trim())) {
523535
this.host = LOCAL_HOST;
524536
} else {
525537
this.host = host;
526538
}
539+
this.defaults = false;
527540
return this;
528541
}
529542

@@ -544,6 +557,7 @@ public ServerOptions setHost(String host) {
544557
*/
545558
public ServerOptions setSsl(@Nullable SslOptions ssl) {
546559
this.ssl = ssl;
560+
this.defaults = false;
547561
return this;
548562
}
549563

@@ -567,6 +581,7 @@ public ServerOptions setSsl(@Nullable SslOptions ssl) {
567581
*/
568582
public ServerOptions setHttp2(@Nullable Boolean http2) {
569583
this.http2 = http2;
584+
this.defaults = false;
570585
return this;
571586
}
572587

@@ -589,6 +604,7 @@ public ServerOptions setHttp2(@Nullable Boolean http2) {
589604
*/
590605
public ServerOptions setExpectContinue(@Nullable Boolean expectContinue) {
591606
this.expectContinue = expectContinue;
607+
this.defaults = false;
592608
return this;
593609
}
594610

‎jooby/src/main/java/io/jooby/internal/RouterImpl.java‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,7 @@
1212
import java.nio.file.NoSuchFileException;
1313
import java.nio.file.Path;
1414
import java.nio.file.Paths;
15-
import java.util.ArrayList;
16-
import java.util.Collections;
17-
import java.util.HashMap;
18-
import java.util.LinkedHashMap;
19-
import java.util.LinkedList;
20-
import java.util.List;
21-
import java.util.Locale;
22-
import java.util.Map;
23-
import java.util.NoSuchElementException;
24-
import java.util.Optional;
15+
import java.util.*;
2516
import java.util.concurrent.ConcurrentHashMap;
2617
import java.util.concurrent.Executor;
2718
import java.util.function.BiConsumer;
@@ -794,8 +785,7 @@ public ServerOptions getServerOptions() {
794785
}
795786

796787
public void setServerOptions(ServerOptions serverOptions) {
797-
this.serverOptions = serverOptions;
798-
services.put(ServerOptions.class, serverOptions);
788+
this.serverOptions = Objects.requireNonNull(serverOptions, "serverOptions cannot be null");
799789
}
800790

801791
@Override

‎jooby/src/test/java/io/jooby/JoobyApiUnitTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void routerOptions() {
136136
options.setIgnoreCase(true);
137137
app.setRouterOptions(options);
138138
assertTrue(app.getRouterOptions().isIgnoreCase());
139-
assertNotNull(app.getServerOptions());
139+
assertNull(app.getServerOptions());
140140
}
141141

142142
@Test

‎jooby/src/test/java/io/jooby/ServerOptionsTest.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Collections;
1212
import java.util.Map;
1313
import java.util.Optional;
14+
import java.util.function.Consumer;
1415

1516
import javax.net.ssl.SSLContext;
1617

@@ -272,4 +273,36 @@ void testPackagePrivateConstructor() {
272273
ServerOptions options = new ServerOptions(true);
273274
assertTrue(options.defaults);
274275
}
276+
277+
@Test
278+
public void settersShouldClearDefaultsFlag() {
279+
assertSetterClearsDefault(options -> options.setServer("Netty"));
280+
assertSetterClearsDefault(options -> options.setPort(8081));
281+
assertSetterClearsDefault(options -> options.setSecurePort(8443));
282+
assertSetterClearsDefault(options -> options.setHttpsOnly(true));
283+
assertSetterClearsDefault(options -> options.setIoThreads(4));
284+
assertSetterClearsDefault(options -> options.setWorkerThreads(16));
285+
assertSetterClearsDefault(options -> options.setCompressionLevel(6));
286+
assertSetterClearsDefault(options -> options.setDefaultHeaders(false));
287+
assertSetterClearsDefault(options -> options.setOutput(new OutputOptions()));
288+
assertSetterClearsDefault(options -> options.setMaxRequestSize(1024));
289+
assertSetterClearsDefault(options -> options.setMaxFormFields(500));
290+
assertSetterClearsDefault(options -> options.setMaxHeaderSize(4096));
291+
assertSetterClearsDefault(options -> options.setHost("localhost"));
292+
assertSetterClearsDefault(options -> options.setSsl(new SslOptions()));
293+
assertSetterClearsDefault(options -> options.setHttp2(true));
294+
assertSetterClearsDefault(options -> options.setExpectContinue(true));
295+
}
296+
297+
private void assertSetterClearsDefault(Consumer<ServerOptions> setter) {
298+
// 1. Create with defaults = true
299+
ServerOptions options = new ServerOptions(true);
300+
assertTrue(options.defaults, "defaults flag should be true initially");
301+
302+
// 2. Invoke the setter
303+
setter.accept(options);
304+
305+
// 3. Assert defaults flag was toggled to false
306+
assertFalse(options.defaults, "defaults flag should be false after calling setter");
307+
}
275308
}

‎tests/src/test/java/io/jooby/test/HttpsTest.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ public void customSslContext(ServerTestRunner runner) {
245245
.setSsl(SslOptions.selfSigned()))
246246
.define(
247247
app -> {
248-
var options = app.require(ServerOptions.class);
249-
options.setSsl(SslOptions.selfSigned());
248+
var options = app.getServerOptions();
250249
// a fresh context is created every time based on config
251250
var ctx1 = options.getSSLContext(this.getClass().getClassLoader());
252251
var ctx2 = options.getSSLContext(this.getClass().getClassLoader());

0 commit comments

Comments
 (0)