public T detectConfiguration()

in streams-config/src/main/java/org/apache/streams/config/ComponentConfigurator.java [86:185]


  public T detectConfiguration(Config typesafe, String path) {

    T pojoConfig = null;

    Config rootConfig = typesafe;

    Config cascadeConfig = null;

    String[] canonicalNameParts = StringUtils.split(configClass.getCanonicalName(), '.');

    /*
     * Resolve from any of the parent packages of the configured class's package
     */
    for( int partIndex = 1; partIndex < canonicalNameParts.length; partIndex++) {
      String[] partialPathParts = ArrayUtils.subarray(canonicalNameParts, 0, partIndex);
      String partialPath = StringUtils.join(partialPathParts, '.');

      LOGGER.debug("partialPath: ", partialPath);

      if( rootConfig.hasPath(partialPath) ) {
        Config partialPathConfig = rootConfig.getConfig(partialPath);
        partialPathConfig = valuesOnly(partialPathConfig);
        if (!partialPathConfig.isEmpty() && !partialPathConfig.root().isEmpty()) {
          if (cascadeConfig == null) {
            cascadeConfig = partialPathConfig;
          } else {
            cascadeConfig = partialPathConfig.withFallback(cascadeConfig);
          }
        }
      }
    }

    /*
     * Resolve from any of the superclasses of the configured class
     */
    List<Class> superclasses = getSuperClasses(configClass);

    for( Class superclass : superclasses) {
      String superclassCanonicalName = superclass.getCanonicalName();
      if( rootConfig.hasPath(superclassCanonicalName)) {
        Config superclassConfig = rootConfig.getConfig(superclassCanonicalName);
        if (cascadeConfig == null) {
          cascadeConfig = superclassConfig;
        } else {
          cascadeConfig = superclassConfig.withFallback(cascadeConfig);
        }
      }
    }

    /*
     * Resolve from any of the configured class simple name
     */
    if( rootConfig.hasPath(configClass.getSimpleName()) ) {
      Config simpleNameConfig = rootConfig.getConfig(configClass.getSimpleName());
      if( cascadeConfig == null ) {
        cascadeConfig = simpleNameConfig;
      } else {
        cascadeConfig = simpleNameConfig.withFallback(cascadeConfig);
      }
    }

    /*
     * Resolve from any of the configured class canonical name
     */
    if( rootConfig.hasPath(configClass.getCanonicalName()) ) {
      Config canonicalNameConfig = rootConfig.getConfig(configClass.getCanonicalName());
      if( cascadeConfig == null ) {
        cascadeConfig = canonicalNameConfig;
      } else {
        cascadeConfig = canonicalNameConfig.withFallback(cascadeConfig);
      }
    }

    if( StringUtils.isNotBlank(path) && !StringUtils.startsWith(path, "$") && rootConfig.hasPath(path) ) {
      Config pathConfig = rootConfig.getConfig(path);
      if( cascadeConfig == null ) {
        cascadeConfig = pathConfig;
      } else {
        cascadeConfig = pathConfig.withFallback(cascadeConfig);
      }
    }
    else if( path == null || StringUtils.isBlank(path) ) {
      if( cascadeConfig == null ) {
        cascadeConfig = rootConfig;
      } else {
        cascadeConfig = valuesOnly(rootConfig).withFallback(cascadeConfig);
      }
    }

    if( cascadeConfig != null && cascadeConfig.root() != null) {
      try {
        pojoConfig = mapper.readValue(cascadeConfig.root().render(ConfigRenderOptions.concise()), configClass);
      } catch (Exception ex) {
        ex.printStackTrace();
        LOGGER.warn("Could not parse:", cascadeConfig);
      }
    }

    return pojoConfig;
  }