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

Add comments to ServiceConfig inspection code and change println #1340

Merged
merged 8 commits into from
Sep 7, 2023
24 changes: 11 additions & 13 deletions src/main/java/org/myrobotlab/service/config/ServiceConfig.java
Original file line number Diff line number Diff line change
@@ -170,7 +170,7 @@ public ServiceConfig addDefaultGlobalConfig(Plan plan, String key, String global
* @param plan
* @param key
* @param globalName
* @param peer
* @param peerType
* @return
*/
public ServiceConfig addDefaultGlobalConfig(Plan plan, String key, String globalName, String peerType, boolean autoStart) {
@@ -215,19 +215,8 @@ public Peer putPeerType(String peerKey, String fullName, String peerType) {
}

public static Plan getDefault(Plan plan, String name, String inType) {
// if ("Service".equals(inType) || Service.class.getCanonicalName().equals(inType)) {
// ServiceConfig sc = new ServiceConfig();
// sc.type = inType;
// plan.put(name, sc);
// return plan;
// }
try {

// if (type == null) {
// log.error("getDefault(null)");
// return null;
// }

// FIXME - at some point setting, examining and changing
// peer keys to actual names will need to be worky
String fullType = getConfigType(inType);
@@ -240,12 +229,17 @@ public static Plan getDefault(Plan plan, String name, String inType) {
config.getDefault(plan, name);

} catch (ClassNotFoundException cnfe) {
// We could not find the config type with the simple {serviceType}Config pattern
// So now we look at its superclasses and try to find a config class in
// the generic type parameters
// FIXME should also perform the simple pattern check on superclasses
try {
@SuppressWarnings("rawtypes")
Class<? extends Service> serviceClass = Class.forName(CodecUtils.makeFullTypeName(inType)).asSubclass(Service.class);
Type superClass = serviceClass.getGenericSuperclass();
if (superClass instanceof ParameterizedType) {
ParameterizedType genericSuperClass = (ParameterizedType) superClass;
System.out.println("Got generic superclass: " + genericSuperClass + " for service class " + serviceClass);
log.debug("Got generic superclass: " + genericSuperClass + " for service class " + serviceClass);
Class<? extends ServiceConfig> configClass = ((Class<?>) genericSuperClass.getActualTypeArguments()[0]).asSubclass(ServiceConfig.class);
ServiceConfig newConfig = configClass.getConstructor().newInstance();
newConfig.type = inType;
@@ -256,6 +250,10 @@ public static Plan getDefault(Plan plan, String name, String inType) {

} catch (NoClassDefFoundError | ClassNotFoundException | NoSuchElementException | NoSuchMethodException | InstantiationException |
IllegalAccessException | InvocationTargetException | ClassCastException ignored) {
// Many ways for the generic inspection code to fail. NoClassDefFound is thrown when the service isn't installed
// We should probably only attempt to load configs for installed services
// NoSuchElementException is manually thrown when we can't find a generic superclass to inspect
// All others are checked exceptions thrown by the reflection utilities being used
log.info("could not find config class for {}, loading generalized ServiceConfig", inType);
ServiceConfig sc = new ServiceConfig();
sc.type = inType;