Skip to content

Commit 0e596b5

Browse files
committed
Fix kotlin ambiguos call on runApp + serverOptions
1 parent 0704eed commit 0e596b5

13 files changed

Lines changed: 81 additions & 59 deletions

File tree

jooby/src/main/java/io/jooby/Env.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public static Config defaults() {
202202
defaultMap.put("application.tmpdir", tmpdir.toString());
203203
defaultMap.put("application.env", "dev");
204204
defaultMap.put("application.charset", "UTF-8");
205-
defaultMap.put("server.maxRequestSize", Integer.toString(Server._10MB));
205+
defaultMap.put("server.maxRequestSize", Integer.toString(ServerOptions._10MB));
206206
String pid = pid();
207207
if (pid != null) {
208208
System.setProperty("PID", pid);

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ private <T> T require(AttributeKey<T> key) {
410410
log.info(" app dir: {}", System.getProperty("user.dir"));
411411
log.info(" tmp dir: {}", tmpdir);
412412

413-
log.info("routes: \n\n{}\n\nlistening on:\n http://localhost:{}{}\n", router, server.getOptions().getPort(),
413+
log.info("routes: \n\n{}\n\nlistening on:\n http://localhost:{}{}\n", router,
414+
server.getOptions().getPort(),
414415
router.getContextPath());
415416
return this;
416417
}
@@ -429,6 +430,23 @@ private <T> T require(AttributeKey<T> key) {
429430
return router.toString();
430431
}
431432

433+
public static void runApp(String[] args, @Nonnull Class<? extends Jooby> applicationType) {
434+
runApp(ExecutionMode.DEFAULT, args, applicationType);
435+
}
436+
437+
public static void runApp(ExecutionMode executionMode, String[] args,
438+
@Nonnull Class<? extends Jooby> applicationType) {
439+
configurePackage(applicationType);
440+
runApp(executionMode, args, () ->
441+
(Jooby) Stream.of(applicationType.getDeclaredConstructors())
442+
.filter(it -> it.getParameterCount() == 0)
443+
.findFirst()
444+
.map(Throwing.throwingFunction(c -> c.newInstance()))
445+
.orElseThrow(() -> new IllegalArgumentException(
446+
"Default constructor for: " + applicationType.getName()))
447+
);
448+
}
449+
432450
public static void runApp(String[] args, @Nonnull Supplier<Jooby> provider) {
433451
runApp(ExecutionMode.DEFAULT, args, provider);
434452
}
@@ -465,7 +483,10 @@ public static void runApp(@Nonnull ExecutionMode mode, @Nonnull String[] args,
465483
}
466484

467485
private static void configurePackage(@Nonnull Object provider) {
468-
Class providerClass = provider.getClass();
486+
configurePackage(provider.getClass());
487+
}
488+
489+
private static void configurePackage(@Nonnull Class providerClass) {
469490
if (!providerClass.getName().contains("KoobyKt")) {
470491
System.setProperty(DEF_PCKG,
471492
System.getProperty(DEF_PCKG, providerClass.getPackage().getName()));

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,6 @@ protected void addShutdownHook() {
5555
}
5656
}
5757

58-
int _4KB = 4096;
59-
60-
int _8KB = 8192;
61-
62-
/** 16KB constant. */
63-
int _16KB = 16384;
64-
65-
int _10MB = 20971520;
66-
6758
@Nonnull Server setOptions(@Nonnull ServerOptions options);
6859

6960
@Nonnull ServerOptions getOptions();

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@
2222

2323
public class ServerOptions {
2424

25+
public static final int _4KB = 4096;
26+
27+
public static final int _8KB = 8192;
28+
29+
/** 16KB constant. */
30+
public static final int _16KB = 16384;
31+
32+
public static final int _10MB = 20971520;
33+
34+
private int bufferSize = _16KB;
35+
2536
public static final int IO_THREADS = Math.max(Runtime.getRuntime().availableProcessors(), 2);
2637

2738
public static final int WORKER_THREADS = IO_THREADS * 8;
@@ -40,9 +51,7 @@ public class ServerOptions {
4051

4152
private String server;
4253

43-
private int bufferSize = Server._16KB;
44-
45-
private int maxRequestSize = Server._10MB;
54+
private int maxRequestSize = _10MB;
4655

4756
@Override public String toString() {
4857
StringBuilder buff = new StringBuilder();
@@ -52,7 +61,7 @@ public class ServerOptions {
5261
buff.append(", ioThreads: ").append(ioThreads);
5362
}
5463
buff.append(", workerThreads: ").append(getWorkerThreads());
55-
if ("netty".equals(server) && singleLoop != null) {
64+
if ("netty".equals(server)) {
5665
buff.append(", singleLoop: ").append(singleLoop);
5766
}
5867
buff.append(", bufferSize: ").append(bufferSize);
@@ -145,8 +154,12 @@ public int getMaxRequestSize() {
145154
return this;
146155
}
147156

148-
public @Nullable Boolean getSingleLoop() {
149-
return singleLoop;
157+
public boolean getSingleLoop() {
158+
return getSingleLoop(true);
159+
}
160+
161+
public boolean getSingleLoop(boolean defaultValue) {
162+
return singleLoop == null ? defaultValue : singleLoop.booleanValue();
150163
}
151164

152165
public @Nonnull ServerOptions setSingleLoop(boolean singleLoop) {

jooby/src/main/java/io/jooby/internal/InputStreamBody.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package io.jooby.internal;
1717

1818
import io.jooby.Body;
19-
import io.jooby.Server;
19+
import io.jooby.ServerOptions;
2020
import io.jooby.Throwing;
2121
import io.jooby.Value;
2222

@@ -42,7 +42,7 @@ public InputStreamBody(InputStream stream, long contentLength) {
4242

4343
public byte[] bytes() {
4444
try (InputStream stream = in) {
45-
int bufferSize = Server._16KB;
45+
int bufferSize = ServerOptions._16KB;
4646
ByteArrayOutputStream out = new ByteArrayOutputStream(bufferSize);
4747
int len;
4848
byte[] buffer = new byte[bufferSize];

jooby/src/main/kotlin/io/jooby/Kooby.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import java.util.function.Consumer
1010
import java.util.function.Supplier
1111
import kotlin.coroutines.CoroutineContext
1212
import kotlin.reflect.KClass
13+
import kotlin.reflect.full.primaryConstructor
1314

1415
@DslMarker
1516
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPEALIAS, AnnotationTarget.TYPE, AnnotationTarget.FUNCTION)
@@ -75,7 +76,7 @@ open class Kooby constructor() : Jooby() {
7576
this.init()
7677
}
7778

78-
fun <T:Any> mvc(router: KClass<T>): Kooby {
79+
fun <T : Any> mvc(router: KClass<T>): Kooby {
7980
super.mvc(router.java)
8081
return this
8182
}
@@ -208,6 +209,13 @@ open class Kooby constructor() : Jooby() {
208209
return super.route(method, pattern, handler)
209210
}
210211

212+
fun serverOptions(options: ServerOptions.() -> Unit): Kooby {
213+
val serverOptions = ServerOptions()
214+
options(serverOptions)
215+
setServerOptions(serverOptions)
216+
return this
217+
}
218+
211219
companion object {
212220
private val ContextCoroutineName = CoroutineName("ctx")
213221
}
@@ -227,14 +235,13 @@ fun runApp(args: Array<String>, init: Kooby.() -> Unit) {
227235

228236
// ::App
229237
@RouterDsl
230-
fun runApp(init: () -> Kooby, args: Array<String>) {
231-
runApp(ExecutionMode.DEFAULT, args, init)
238+
fun <T : Jooby> runApp(args: Array<String>, application: KClass<T>) {
239+
runApp(ExecutionMode.DEFAULT, args, application)
232240
}
233241

234242
@RouterDsl
235-
fun runApp(mode: ExecutionMode, args: Array<String>, init: () -> Kooby) {
236-
configurePackage(init)
237-
Jooby.runApp( mode, args, init)
243+
fun <T : Jooby> runApp(mode: ExecutionMode, args: Array<String>, application: KClass<T>) {
244+
Jooby.runApp(mode, args, application.java)
238245
}
239246

240247
internal fun configurePackage(value: Any) {

jooby/src/test/java/io/jooby/MockContext.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import java.util.HashMap;
2222
import java.util.Map;
2323
import java.util.concurrent.Executor;
24-
import java.util.stream.IntStream;
25-
import java.util.stream.Stream;
2624

2725
public class MockContext implements Context {
2826

@@ -255,7 +253,7 @@ public String getResultText() {
255253

256254
@Nonnull @Override public OutputStream responseStream() {
257255
responseStarted = true;
258-
ByteArrayOutputStream out = new ByteArrayOutputStream(Server._16KB);
256+
ByteArrayOutputStream out = new ByteArrayOutputStream(ServerOptions._16KB);
259257
result = out;
260258
return out;
261259
}

jooby/src/test/kotlin/apps/Apps.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ class App : Kooby({
2727

2828
/** run class: */
2929
fun runClass(args: Array<String>) {
30-
runApp(::App, args)
30+
runApp(args, App::class)
3131
}
3232

3333
/** run class with mode: */
3434
fun runWithMode(args: Array<String>) {
35-
runApp(ExecutionMode.DEFAULT, args, ::App)
35+
runApp(ExecutionMode.DEFAULT, args, App::class)
3636
}
3737

3838
/** run inline: */

modules/server/jooby-netty/src/main/java/io/jooby/internal/netty/NettyPipeline.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,16 @@
1919
import io.netty.channel.ChannelInitializer;
2020
import io.netty.channel.ChannelPipeline;
2121
import io.netty.channel.socket.SocketChannel;
22-
import io.netty.handler.codec.Headers;
2322
import io.netty.handler.codec.http.HttpContentCompressor;
2423
import io.netty.handler.codec.http.HttpHeaders;
2524
import io.netty.handler.codec.http.HttpRequestDecoder;
2625
import io.netty.handler.codec.http.HttpResponseEncoder;
2726
import io.netty.handler.codec.http.multipart.HttpDataFactory;
28-
import io.netty.handler.flush.FlushConsolidationHandler;
29-
import io.netty.handler.stream.ChunkedWriteHandler;
3027

3128
import java.util.function.Consumer;
3229

33-
import static io.jooby.Server._4KB;
34-
import static io.jooby.Server._8KB;
30+
import static io.jooby.ServerOptions._4KB;
31+
import static io.jooby.ServerOptions._8KB;
3532

3633
public class NettyPipeline extends ChannelInitializer<SocketChannel> {
3734

modules/server/jooby-netty/src/main/java/io/jooby/netty/Netty.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,19 @@ public class Netty extends Server.Base {
5858

5959
private ServerOptions options = new ServerOptions()
6060
.setSingleLoop(false)
61-
.setIoThreads(ServerOptions.IO_THREADS)
6261
.setServer("netty");
6362

6463
@Override public Netty setOptions(@Nonnull ServerOptions options) {
65-
Boolean singleLoop = options.getSingleLoop();
6664
this.options = options
67-
.setSingleLoop(singleLoop == null ? false : singleLoop.booleanValue())
68-
.setIoThreads(options.getIoThreads());
65+
.setSingleLoop(options.getSingleLoop(false));
6966
return this;
7067
}
7168

7269
@Nonnull @Override public ServerOptions getOptions() {
7370
return options;
7471
}
7572

76-
@Nonnull @Override public Server start(Jooby application) {
73+
@Nonnull @Override public Server start(@Nonnull Jooby application) {
7774
try {
7875
applications.add(application);
7976

@@ -94,7 +91,7 @@ public class Netty extends Server.Base {
9491
/** Acceptor: Accepts connections */
9592
this.acceptor = provider.group("netty-acceptor", options.getIoThreads());
9693

97-
if (options.getSingleLoop().booleanValue()) {
94+
if (options.getSingleLoop()) {
9895
this.ioLoop = this.acceptor;
9996
} else {
10097
/** IO: processing connections, parsing messages and doing engine's internal work */
@@ -139,7 +136,7 @@ public Server stop() {
139136
acceptor.shutdownGracefully();
140137
acceptor = null;
141138
}
142-
if (!options.getSingleLoop().booleanValue() && ioLoop != null) {
139+
if (!options.getSingleLoop() && ioLoop != null) {
143140
ioLoop.shutdownGracefully();
144141
ioLoop = null;
145142
}

0 commit comments

Comments
 (0)