Skip to content

Commit 02dbd23

Browse files
committed
ServiceLoad JexPlugins
1 parent 223a4a9 commit 02dbd23

File tree

6 files changed

+50
-74
lines changed

6 files changed

+50
-74
lines changed

avaje-jex/src/main/java/io/avaje/jex/DJex.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.avaje.jex;
22

33
import io.avaje.inject.BeanScope;
4+
import io.avaje.jex.core.CoreServiceLoader;
45
import io.avaje.jex.core.CoreServiceManager;
56
import io.avaje.jex.core.HealthPlugin;
67
import io.avaje.jex.jdk.JdkServerStart;
@@ -111,6 +112,11 @@ public Server start() {
111112
if (config.health()) {
112113
plugin(new HealthPlugin());
113114
}
115+
116+
if (!config.useSpiPlugins()) {
117+
CoreServiceLoader.plugins().forEach(p -> p.apply(this));
118+
}
119+
114120
final SpiRoutes routes =
115121
new RoutesBuilder(
116122
this.routing, this.config.ignoreTrailingSlashes())

avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ final class DJexConfig implements JexConfig {
2222
private boolean health = true;
2323
private boolean ignoreTrailingSlashes = true;
2424
private Executor executor;
25-
26-
private boolean preCompressStaticFiles;
2725
private JsonService jsonService;
2826
private final Map<String, TemplateRender> renderers = new HashMap<>();
2927
private SSLContext sslContext;
30-
private final CompressionConfig compression= new CompressionConfig();
28+
private boolean useJexSpi = true;
29+
private final CompressionConfig compression = new CompressionConfig();
3130

3231
@Override
3332
public JexConfig port(int port) {
@@ -59,12 +58,6 @@ public JexConfig ignoreTrailingSlashes(boolean ignoreTrailingSlashes) {
5958
return this;
6059
}
6160

62-
@Override
63-
public JexConfig preCompressStaticFiles(boolean preCompressStaticFiles) {
64-
this.preCompressStaticFiles = preCompressStaticFiles;
65-
return this;
66-
}
67-
6861
@Override
6962
public JexConfig jsonService(JsonService jsonService) {
7063
this.jsonService = jsonService;
@@ -118,11 +111,6 @@ public boolean ignoreTrailingSlashes() {
118111
return ignoreTrailingSlashes;
119112
}
120113

121-
@Override
122-
public boolean preCompressStaticFiles() {
123-
return preCompressStaticFiles;
124-
}
125-
126114
@Override
127115
public JsonService jsonService() {
128116
return jsonService;
@@ -154,4 +142,14 @@ public JexConfig compression(Consumer<CompressionConfig> consumer) {
154142
public CompressionConfig compression() {
155143
return compression;
156144
}
145+
146+
@Override
147+
public DJexConfig disableSpiPlugins() {
148+
useJexSpi = false;
149+
return this;
150+
}
151+
152+
boolean useSpiPlugins() {
153+
return useJexSpi;
154+
}
157155
}

avaje-jex/src/main/java/io/avaje/jex/JexConfig.java

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.Map;
44
import java.util.concurrent.Executor;
55
import java.util.concurrent.Executors;
6-
import java.util.concurrent.ThreadFactory;
76
import java.util.function.Consumer;
87

98
import javax.net.ssl.SSLContext;
@@ -12,97 +11,60 @@
1211
import io.avaje.jex.spi.JsonService;
1312
import io.avaje.jex.spi.TemplateRender;
1413

15-
/**
16-
* Jex configuration.
17-
*/
14+
/** Jex configuration. */
1815
public sealed interface JexConfig permits DJexConfig {
1916

20-
/**
21-
* Set the port to use. Defaults to 7001.
22-
*/
17+
/** Set the port to use. Defaults to 7001. */
2318
JexConfig port(int port);
2419

25-
/**
26-
* Set the host to bind to.
27-
*/
20+
/** Set the host to bind to. */
2821
JexConfig host(String host);
2922

30-
/**
31-
* Set the contextPath.
32-
*/
23+
/** Set the contextPath. */
3324
JexConfig contextPath(String contextPath);
3425

35-
/**
36-
* Set to true to include the health endpoint. Defaults to true.
37-
*/
26+
/** Set to true to include the health endpoint. Defaults to true. */
3827
JexConfig health(boolean health);
3928

40-
/**
41-
* Set to true to ignore trailing slashes. Defaults to true.
42-
*/
29+
/** Set to true to ignore trailing slashes. Defaults to true. */
4330
JexConfig ignoreTrailingSlashes(boolean ignoreTrailingSlashes);
4431

45-
/**
46-
* Set to true to pre compress static files. Defaults to false.
47-
*/
48-
JexConfig preCompressStaticFiles(boolean preCompressStaticFiles);
49-
50-
/**
51-
* Set the JsonService to use.
52-
*/
32+
/** Set the JsonService to use. */
5333
JexConfig jsonService(JsonService jsonService);
5434

5535
/**
5636
* Register a template renderer explicitly.
5737
*
5838
* @param extension The extension the renderer applies to.
59-
* @param renderer The template render to use for the given extension.
39+
* @param renderer The template render to use for the given extension.
6040
*/
6141
JexConfig renderer(String extension, TemplateRender renderer);
6242

63-
/**
64-
* Set executor for serving requests.
65-
*/
43+
/** Set executor for serving requests. */
6644
JexConfig executor(Executor executor);
6745

6846
/**
69-
* Executor for serving requests. Defaults to a {@link Executors#newVirtualThreadPerTaskExecutor()}
47+
* Executor for serving requests. Defaults to a {@link
48+
* Executors#newVirtualThreadPerTaskExecutor()}
7049
*/
7150
Executor executor();
7251

73-
/**
74-
* Return the port to use.
75-
*/
52+
/** Return the port to use. */
7653
int port();
7754

78-
/**
79-
* Return the host to bind to.
80-
*/
55+
/** Return the host to bind to. */
8156
String host();
8257

83-
/**
84-
* Return the contextPath to use.
85-
*/
58+
/** Return the contextPath to use. */
8659
String contextPath();
8760

88-
/**
89-
* Return true to include the health endpoint.
90-
*/
61+
/** Return true to include the health endpoint. */
9162
boolean health();
9263

93-
/**
94-
* Return true to ignore trailing slashes.
95-
*/
64+
/** Return true to ignore trailing slashes. */
9665
boolean ignoreTrailingSlashes();
9766

98-
/**
99-
* Return true if static files should be pre compressed.
100-
*/
101-
boolean preCompressStaticFiles();
102-
103-
/**
104-
* Return the JsonService.
105-
*/
67+
/** Return the JsonService. */
10668
JsonService jsonService();
10769

10870
/** Return the ssl context if https is enabled. */
@@ -111,13 +73,15 @@ public sealed interface JexConfig permits DJexConfig {
11173
/** Enable https with the provided SSLContext. */
11274
JexConfig sslContext(SSLContext ssl);
11375

114-
/**
115-
* Return the template renderers registered by extension.
116-
*/
76+
/** Return the template renderers registered by extension. */
11777
Map<String, TemplateRender> renderers();
11878

79+
/** configure compression via consumer */
11980
JexConfig compression(Consumer<CompressionConfig> consumer);
12081

82+
/** get compression configuration */
12183
CompressionConfig compression();
12284

85+
/** whether to disable JexPlugins loaded from ServiceLoader */
86+
JexConfig disableSpiPlugins();
12387
}

avaje-jex/src/main/java/io/avaje/jex/core/CoreServiceLoader.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.ServiceLoader;
77

88
import io.avaje.jex.spi.JexExtension;
9+
import io.avaje.jex.spi.JexPlugin;
910
import io.avaje.jex.spi.JsonService;
1011
import io.avaje.jex.spi.TemplateRender;
1112

@@ -16,13 +17,15 @@ public final class CoreServiceLoader {
1617

1718
private final JsonService jsonService;
1819
private final List<TemplateRender> renders = new ArrayList<>();
20+
private final List<JexPlugin> plugins = new ArrayList<>();
1921

2022
CoreServiceLoader() {
2123
JsonService spiJsonService = null;
2224
for (var spi : ServiceLoader.load(JexExtension.class)) {
2325
switch (spi) {
2426
case JsonService s -> spiJsonService = s;
2527
case TemplateRender r -> renders.add(r);
28+
case JexPlugin p -> plugins.add(p);
2629
}
2730
}
2831
jsonService = spiJsonService;
@@ -35,4 +38,8 @@ public static Optional<JsonService> jsonService() {
3538
public static List<TemplateRender> getRenders() {
3639
return INSTANCE.renders;
3740
}
41+
42+
public static List<JexPlugin> plugins() {
43+
return INSTANCE.plugins;
44+
}
3845
}

avaje-jex/src/main/java/io/avaje/jex/spi/JexExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
* META-INF/services/io.avaje.jex.spi.JexExtension } for it to be loaded by Jex
1010
*/
1111
@Service
12-
public sealed interface JexExtension permits JsonService, TemplateRender {}
12+
public sealed interface JexExtension permits JsonService, TemplateRender, JexPlugin {}

avaje-jex/src/main/java/io/avaje/jex/spi/JexPlugin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
/**
66
* A plugin that can register things like routes, exception handlers etc.
77
*/
8-
public interface JexPlugin {
8+
@FunctionalInterface
9+
public non-sealed interface JexPlugin extends JexExtension{
910

1011
/**
1112
* Register the plugin features with jex.

0 commit comments

Comments
 (0)