Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(api): Improve structure of TelestionVerticle #539

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,84 +21,6 @@
* Ludwig Richter (@fussel178)
*/
public abstract class TelestionVerticle<T extends TelestionConfiguration> extends AbstractVerticle {
/**
* The default verticle configuration in a generic format.
*/
private JsonObject defaultGenericConfig = new JsonObject();
/**
* The default verticle configuration in the Configuration type format.<p>
* Is <code>null</code> when no type via {@link #getConfigType()} is given.
*/
private T defaultConfig;

/**
* The verticle configuration in a generic format.
*/
private JsonObject genericConfig = new JsonObject();
/**
* The verticle configuration in the Configuration type format.<p>
* Is <code>null</code> when no type via {@link #getConfigType()} is given.
*/
private T config;

/**
* The default logger instance.
*/
protected final Logger logger = LoggerFactory.getLogger(getClass());

/**
* Get the Configuration Class type from the inheriting class.
*
* @return the Configuration Class type
*/
@SuppressWarnings("unchecked")
protected Class<T> getConfigType() {
try {
String className = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName();
Class<?> clazz = Class.forName(className);
//noinspection unchecked
return (Class<T>) clazz;
} catch (Exception e) {
logger.warn("Cannot get Class type from generic: {}", e.getMessage());
return null;
}
}

/**
* Creates a new Telestion verticle and tries to load the default configuration
* from the specified configuration class.
*
* @param skipDefaultConfigLoading when {@code true} the loading of the default configuration is skipped
*/
public TelestionVerticle(boolean skipDefaultConfigLoading) {
if (skipDefaultConfigLoading) {
return;
}
var configType = getConfigType();
if (Objects.isNull(configType)) {
return;
}

try {
var defaultConfig = configType.getConstructor().newInstance();
this.defaultConfig = defaultConfig;
this.defaultGenericConfig = defaultConfig.json();
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
// no default configuration on configuration class found, ignoring
logger.info("No default configuration found for {}. " +
"Expected constructor with no arguments to exist on {}. " +
"Continuing without default configuration.",
getClass().getSimpleName(), getConfigType().getSimpleName());
}
}

/**
* Same as {@link TelestionVerticle#TelestionVerticle(boolean)}
* but enables loading of default configuration if possible.
*/
public TelestionVerticle() {
this(false);
}

@Override
public final void start(Promise<Void> startPromise) throws Exception {
Expand Down Expand Up @@ -181,6 +103,52 @@ public void onStop(Promise<Void> stopPromise) throws Exception {
public void onStop() throws Exception {
}

/**
* Block the usage of <code>config()</code> in inheriting classes.
*
* @return the verticle configuration from vertx merged with the default configuration
*/
@Override
public final JsonObject config() {
return defaultGenericConfig.mergeIn(super.config());
}

/**
* Creates a new Telestion verticle and tries to load the default configuration
* from the specified configuration class.
*
* @param skipDefaultConfigLoading when {@code true} the loading of the default configuration is skipped
*/
public TelestionVerticle(boolean skipDefaultConfigLoading) {
if (skipDefaultConfigLoading) {
return;
}
var configType = getConfigType();
if (Objects.isNull(configType)) {
return;
}

try {
var defaultConfig = configType.getConstructor().newInstance();
this.defaultConfig = defaultConfig;
this.defaultGenericConfig = defaultConfig.json();
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
// no default configuration on configuration class found, ignoring
logger.info("No default configuration found for {}. " +
"Expected constructor with no arguments to exist on {}. " +
"Continuing without default configuration.",
getClass().getSimpleName(), getConfigType().getSimpleName());
}
}

/**
* Same as {@link TelestionVerticle#TelestionVerticle(boolean)}
* but enables loading of default configuration if possible.
*/
public TelestionVerticle() {
this(false);
}

/**
* Set the default verticle configuration and update the verticle configuration.
*
Expand Down Expand Up @@ -242,13 +210,26 @@ public JsonObject getGenericConfig() {
}

/**
* Block the usage of <code>config()</code> in inheriting classes.
* The default logger instance.
*/
protected final Logger logger = LoggerFactory.getLogger(getClass());

/**
* Get the Configuration Class type from the inheriting class.
*
* @return the verticle configuration from vertx merged with the default configuration
* @return the Configuration Class type
*/
@Override
public final JsonObject config() {
return defaultGenericConfig.mergeIn(super.config());
@SuppressWarnings("unchecked")
protected Class<T> getConfigType() {
try {
String className = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName();
Class<?> clazz = Class.forName(className);
//noinspection unchecked
return (Class<T>) clazz;
} catch (Exception e) {
logger.warn("Cannot get Class type from generic: {}", e.getMessage());
return null;
}
}

/**
Expand All @@ -270,4 +251,24 @@ private T mapToConfiguration(JsonObject object) {
var type = getConfigType();
return type != null ? object.mapTo(type) : null;
}

/**
* The default verticle configuration in a generic format.
*/
private JsonObject defaultGenericConfig = new JsonObject();
/**
* The default verticle configuration in the Configuration type format.<p>
* Is <code>null</code> when no type via {@link #getConfigType()} is given.
*/
private T defaultConfig;

/**
* The verticle configuration in a generic format.
*/
private JsonObject genericConfig = new JsonObject();
/**
* The verticle configuration in the Configuration type format.<p>
* Is <code>null</code> when no type via {@link #getConfigType()} is given.
*/
private T config;
}