Skip to content

Commit 8bbe3ab

Browse files
fussel178pklaschka
andcommitted
STASH: Revert this commit if you want to continue
Co-authored-by: pklaschka <[email protected]>
1 parent e41e1db commit 8bbe3ab

34 files changed

+241
-214
lines changed

telestion-api/src/main/java/de/wuespace/telestion/api/verticle/TelestionVerticle.java

Lines changed: 18 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -21,88 +21,19 @@
2121
* Ludwig Richter (@fussel178)
2222
*/
2323
public abstract class TelestionVerticle<T extends TelestionConfiguration> extends AbstractVerticle {
24-
/**
25-
* The default verticle configuration in a generic format.
26-
*/
27-
private JsonObject defaultGenericConfig = new JsonObject();
28-
/**
29-
* The default verticle configuration in the Configuration type format.<p>
30-
* Is <code>null</code> when no type via {@link #getConfigType()} is given.
31-
*/
32-
private T defaultConfig;
33-
34-
/**
35-
* The verticle configuration in a generic format.
36-
*/
37-
private JsonObject genericConfig = new JsonObject();
38-
/**
39-
* The verticle configuration in the Configuration type format.<p>
40-
* Is <code>null</code> when no type via {@link #getConfigType()} is given.
41-
*/
42-
private T config;
24+
private VerticleConfigStrategy<T> config;
4325

4426
/**
4527
* The default logger instance.
4628
*/
4729
protected final Logger logger = LoggerFactory.getLogger(getClass());
4830

49-
/**
50-
* Get the Configuration Class type from the inheriting class.
51-
*
52-
* @return the Configuration Class type
53-
*/
54-
@SuppressWarnings("unchecked")
55-
protected Class<T> getConfigType() {
56-
try {
57-
String className = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName();
58-
Class<?> clazz = Class.forName(className);
59-
//noinspection unchecked
60-
return (Class<T>) clazz;
61-
} catch (Exception e) {
62-
logger.warn("Cannot get Class type from generic: {}", e.getMessage());
63-
return null;
64-
}
65-
}
66-
67-
/**
68-
* Creates a new Telestion verticle and tries to load the default configuration
69-
* from the specified configuration class.
70-
*
71-
* @param skipDefaultConfigLoading when {@code true} the loading of the default configuration is skipped
72-
*/
73-
public TelestionVerticle(boolean skipDefaultConfigLoading) {
74-
if (skipDefaultConfigLoading) {
75-
return;
76-
}
77-
var configType = getConfigType();
78-
if (Objects.isNull(configType)) {
79-
return;
80-
}
81-
82-
try {
83-
var defaultConfig = configType.getConstructor().newInstance();
84-
this.defaultConfig = defaultConfig;
85-
this.defaultGenericConfig = defaultConfig.toJsonObject();
86-
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
87-
// no default configuration on configuration class found, ignoring
88-
logger.info("No default configuration found for {}. " +
89-
"Expected constructor with no arguments to exist on {}. " +
90-
"Continuing without default configuration.",
91-
getClass().getSimpleName(), getConfigType().getSimpleName());
92-
}
93-
}
94-
95-
/**
96-
* Same as {@link TelestionVerticle#TelestionVerticle(boolean)}
97-
* but enables loading of default configuration if possible.
98-
*/
99-
public TelestionVerticle() {
100-
this(false);
101-
}
102-
10331
@Override
10432
public final void start(Promise<Void> startPromise) throws Exception {
105-
updateConfigs();
33+
var verticleClazz = this.getClass();
34+
35+
this.config = new VerticleConfigStrategy<T>(config(), getClass());
36+
10637
// put general startup steps here
10738
onStart(startPromise);
10839
}
@@ -187,9 +118,7 @@ public void onStop() throws Exception {
187118
* @param defaultConfig the new default verticle configuration
188119
*/
189120
public void setDefaultConfig(JsonObject defaultConfig) {
190-
this.defaultGenericConfig = defaultConfig;
191-
this.defaultConfig = mapToConfiguration(defaultConfig);
192-
updateConfigs();
121+
this.config = new VerticleConfigStrategy<T>(config(), defaultConfig, getClass());
193122
}
194123

195124
/**
@@ -198,47 +127,35 @@ public void setDefaultConfig(JsonObject defaultConfig) {
198127
* @param defaultConfig the new default verticle configuration
199128
*/
200129
public void setDefaultConfig(T defaultConfig) {
201-
this.defaultConfig = defaultConfig;
202-
this.defaultGenericConfig = defaultConfig.toJsonObject();
203-
updateConfigs();
130+
this.config = new VerticleConfigStrategy<T>(config(), defaultConfig, getClass());
204131
}
205132

206133
/**
207-
* Get the default verticle configuration in the Configuration type format.<p>
208-
* Returns <code>null</code> when no type via {@link #getConfigType()} is given.
209-
*
210-
* @return the default verticle configuration
134+
* @see VerticleConfigStrategy#getDefaultConfig()
211135
*/
212136
public T getDefaultConfig() {
213-
return defaultConfig;
137+
return config.getDefaultConfig();
214138
}
215139

216140
/**
217-
* Get the default verticle configuration in a generic format.
218-
*
219-
* @return the default verticle configuration
141+
* @see VerticleConfigStrategy#getUntypedDefaultConfig()
220142
*/
221-
public JsonObject getGenericDefaultConfig() {
222-
return defaultGenericConfig;
143+
public JsonObject getUntypedDefaultConfig() {
144+
return config.getUntypedDefaultConfig();
223145
}
224146

225147
/**
226-
* Get the verticle configuration in the Configuration type format.<p>
227-
* Returns <code>null</code> when no type via {@link #getConfigType()} is given.
228-
*
229-
* @return the verticle configuration
148+
* @see VerticleConfigStrategy#getConfig()
230149
*/
231150
public T getConfig() {
232-
return config;
151+
return config.getConfig();
233152
}
234153

235154
/**
236-
* Get the verticle configuration in a generic format.
237-
*
238-
* @return the verticle configuration
155+
* @see VerticleConfigStrategy#getUntypedConfig()
239156
*/
240-
public JsonObject getGenericConfig() {
241-
return genericConfig;
157+
public JsonObject getUntypedConfig() {
158+
return config.getUntypedConfig();
242159
}
243160

244161
/**
@@ -248,26 +165,6 @@ public JsonObject getGenericConfig() {
248165
*/
249166
@Override
250167
public final JsonObject config() {
251-
return defaultGenericConfig.mergeIn(super.config());
252-
}
253-
254-
/**
255-
* Update the config representations based on the default verticle configuration.
256-
*/
257-
private void updateConfigs() {
258-
genericConfig = config();
259-
config = mapToConfiguration(genericConfig);
260-
}
261-
262-
/**
263-
* Map a generic JSON object to the Configuration type.<p>
264-
* Returns <code>null</code> when no type via {@link #getConfigType()} is given.
265-
*
266-
* @param object the generic JSON object to map
267-
* @return the JSON object in the Configuration type format
268-
*/
269-
private T mapToConfiguration(JsonObject object) {
270-
var type = getConfigType();
271-
return type != null ? object.mapTo(type) : null;
168+
return super.config();
272169
}
273170
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package de.wuespace.telestion.api.verticle;
2+
3+
import io.vertx.core.json.JsonObject;
4+
5+
import java.lang.reflect.InvocationTargetException;
6+
import java.lang.reflect.ParameterizedType;
7+
import java.util.Objects;
8+
import java.util.Optional;
9+
10+
public class VerticleConfigStrategy<T extends TelestionConfiguration> {
11+
12+
/**
13+
* The default verticle configuration in a generic format.
14+
*/
15+
private final JsonObject untypedDefaultConfig;
16+
17+
/**
18+
* The default verticle configuration in the Configuration type format.<p>
19+
* Is <code>null</code> when no type for the configuration is given.
20+
*/
21+
private final T defaultConfig;
22+
23+
/**
24+
* The verticle configuration in a generic format.
25+
*/
26+
private final JsonObject untypedConfig;
27+
28+
/**
29+
* The verticle configuration in the Configuration type format.<p>
30+
* Is <code>null</code> when no type for the configuration is given.
31+
*/
32+
private final T config;
33+
34+
public VerticleConfigStrategy(JsonObject verticleConfig, JsonObject untypedDefaultConfig, Class<? extends TelestionVerticle<?>> verticleClazz) {
35+
var combined = new JsonObject();
36+
if (untypedDefaultConfig != null) {
37+
combined.mergeIn(untypedDefaultConfig);
38+
39+
this.untypedDefaultConfig = untypedDefaultConfig;
40+
this.defaultConfig = verticleClazz != null ? untypedDefaultConfig.mapTo(verticleClazz) : null;
41+
} else {
42+
this.untypedDefaultConfig = null;
43+
this.defaultConfig = null;
44+
}
45+
46+
combined.mergeIn(verticleConfig);
47+
this.untypedConfig = combined;
48+
this.config = verticleClazz != null ? combined.mapTo(verticleClazz) : null;
49+
}
50+
51+
public VerticleConfigStrategy(JsonObject verticleConfig, T defaultConfig, Class<? extends TelestionVerticle<?>> verticleClazz) {
52+
this(verticleConfig, defaultConfig != null ? defaultConfig.toJsonObject() : null, configType);
53+
}
54+
55+
public VerticleConfigStrategy(JsonObject verticleConfig, Class<? extends TelestionVerticle<?>> verticleClazz) {
56+
this(verticleConfig, getDefaultConfigFromConfigType(configType), configType);
57+
}
58+
59+
/**
60+
* Get the default verticle configuration in the Configuration type format.
61+
*
62+
* @return the default verticle configuration
63+
*/
64+
public T getDefaultConfig() throws IllegalStateException {
65+
if (Objects.isNull(defaultConfig)) {
66+
throw new IllegalStateException("Tried to access getDefaultConfig(), but no configType was defined.");
67+
}
68+
69+
return defaultConfig;
70+
}
71+
72+
/**
73+
* Get the default verticle configuration in an untyped/dynamic format.
74+
*
75+
* @return the default verticle configuration
76+
*/
77+
public JsonObject getUntypedDefaultConfig() {
78+
return untypedDefaultConfig;
79+
}
80+
81+
/**
82+
* Get the verticle configuration in the Configuration type format.
83+
*
84+
* @return the verticle configuration
85+
*/
86+
public T getConfig() throws IllegalStateException {
87+
if (Objects.isNull(config)) {
88+
throw new IllegalStateException("Tried to access getConfig(), but no configType was defined.");
89+
}
90+
91+
return config;
92+
}
93+
94+
/**
95+
* Get the verticle configuration in an untyped/dynamic format.
96+
*
97+
* @return the verticle configuration
98+
*/
99+
public JsonObject getUntypedConfig() {
100+
return untypedConfig;
101+
}
102+
103+
private static <T extends TelestionConfiguration> T getDefaultConfigFromConfigType(Class<T> configType) {
104+
try {
105+
return configType != null ? configType.getConstructor().newInstance() : null;
106+
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
107+
return null;
108+
}
109+
}
110+
111+
/**
112+
* Get the Configuration Class type from the inheriting class.
113+
*
114+
* @return the Configuration Class type
115+
*/
116+
@SuppressWarnings("unchecked")
117+
protected static <T extends TelestionConfiguration> Class<T> getConfigType(Class<? extends TelestionVerticle<?>> clazz) {
118+
try {
119+
String className = ((ParameterizedType) clazz.getGenericSuperclass()).getActualTypeArguments()[0].getTypeName();
120+
Class<?> configClazz = Class.forName(className);
121+
return (Class<T>) configClazz;
122+
} catch (Exception e) {
123+
return null;
124+
}
125+
}
126+
}

telestion-examples/src/main/java/de/wuespace/telestion/examples/DefaultConfigVerticle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ public DefaultConfigVerticle() {
4545
@Override
4646
public void onStart() {
4747
var delay = Duration.ofSeconds(1).toMillis();
48-
vertx.setPeriodic(delay, id -> logger.info(getConfig().message()));
48+
vertx.setPeriodic(delay, id -> logger.info(verticleConfigStrategy.getConfig().message()));
4949
}
5050
}

telestion-examples/src/main/java/de/wuespace/telestion/examples/GenericSimpleCommandHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public class GenericSimpleCommandHandler extends TelestionVerticle<UntypedConfig
1616
@Override
1717
public void onStart() {
1818
// with "controller":
19-
register(getGenericConfig().getString("inAddress"), this::handleCommand, SimpleCommand.class);
19+
register(verticleConfigStrategy.getUntypedConfig().getString("inAddress"), this::handleCommand, SimpleCommand.class);
2020

2121
// inline:
22-
register(getGenericConfig().getString("pingAddress"),
22+
register(verticleConfigStrategy.getUntypedConfig().getString("pingAddress"),
2323
(SimpleCommand body, Message<Object> message) -> message.reply(
2424
new SimpleCommand("pong", new String[]{ /* Pure nothingness */ })
2525
),

telestion-examples/src/main/java/de/wuespace/telestion/examples/PingVerticle.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public static void main(String[] args) {
3030

3131
@Override
3232
public void onStart() {
33-
vertx.setPeriodic(Duration.ofSeconds(getConfig().interval).toMillis(), this::sendPing);
33+
vertx.setPeriodic(Duration.ofSeconds(verticleConfigStrategy.getConfig().interval).toMillis(), this::sendPing);
3434
}
3535

3636
private void sendPing(Long intervalId) {
3737
logger.info("Send ping");
38-
request(getConfig().address, new SimpleMessage("ping", "A simple ping message"),
38+
request(verticleConfigStrategy.getConfig().address, new SimpleMessage("ping", "A simple ping message"),
3939
SimpleMessage.class).onSuccess(container -> logger.info("Received pong"));
4040
}
4141
}

telestion-examples/src/main/java/de/wuespace/telestion/examples/PongVerticle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static void main(String[] args) {
2828

2929
@Override
3030
public void onStart() {
31-
register(getConfig().address, this::handlePing, SimpleMessage.class);
31+
register(verticleConfigStrategy.getConfig().address, this::handlePing, SimpleMessage.class);
3232
}
3333

3434
private void handlePing(SimpleMessage body, Message<Object> message) {

telestion-examples/src/main/java/de/wuespace/telestion/examples/SayHello.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ public record Configuration(
3131

3232
@Override
3333
public void onStart(Promise<Void> startPromise) {
34-
var delay = Duration.ofSeconds(getConfig().period());
35-
var duration = Duration.ofSeconds(getConfig().duration());
34+
var delay = Duration.ofSeconds(verticleConfigStrategy.getConfig().period());
35+
var duration = Duration.ofSeconds(verticleConfigStrategy.getConfig().duration());
3636
// setup interval
3737
var timing = interval(delay, id -> logger.info(
3838
"{} from {}",
39-
getConfig().message(),
39+
verticleConfigStrategy.getConfig().message(),
4040
deploymentID()
4141
));
4242

4343
// cancel after of some time
4444
timeout(duration, id -> timing.cancel());
4545

4646
startPromise.complete();
47-
logger.info("Started {} with config {}", SayHello.class.getSimpleName(), getConfig());
47+
logger.info("Started {} with config {}", SayHello.class.getSimpleName(), verticleConfigStrategy.getConfig());
4848
}
4949
}

0 commit comments

Comments
 (0)