Skip to content

Commit

Permalink
Add comments to ServiceConfig inspection code and change println (#1340)
Browse files Browse the repository at this point in the history
* Fix incorrect duplicate deps checking, regen pom

* Add StringUtil docstring and test

* Fix incorrect full type creation

* Add additional fallback lookup for service configs based on generic superclasses

* Fix broken installation

* Fix Sphinx, sort of

* Add comments to superclass generic introspection code and change sout to log.info
  • Loading branch information
AutonomicPerfectionist authored Sep 7, 2023
1 parent 9ff1d96 commit 9a2f382
Showing 1 changed file with 11 additions and 13 deletions.
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
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 9a2f382

Please sign in to comment.