Search in sources :

Example 1 with ImportPhase

use of cn.taketoday.framework.context.config.ConfigDataEnvironmentContributor.ImportPhase in project today-infrastructure by TAKETODAY.

the class ConfigDataEnvironmentContributors method withProcessedImports.

/**
 * Processes imports from all active contributors and return a new
 * {@link ConfigDataEnvironmentContributors} instance.
 *
 * @param importer the importer used to import {@link ConfigData}
 * @param activationContext the current activation context or {@code null} if the
 * context has not yet been created
 * @return a {@link ConfigDataEnvironmentContributors} instance with all relevant
 * imports have been processed
 */
ConfigDataEnvironmentContributors withProcessedImports(ConfigDataImporter importer, @Nullable ConfigDataActivationContext activationContext) {
    ImportPhase importPhase = ImportPhase.get(activationContext);
    boolean traceEnabled = log.isTraceEnabled();
    if (traceEnabled) {
        log.trace("Processing imports for phase {}. {}", importPhase, activationContext != null ? activationContext : "no activation context");
    }
    ConfigDataEnvironmentContributors result = this;
    int processed = 0;
    while (true) {
        ConfigDataEnvironmentContributor contributor = getNextToProcess(result, activationContext, importPhase);
        if (contributor == null) {
            if (traceEnabled) {
                log.trace("Processed imports for of {} contributors", processed);
            }
            return result;
        }
        if (contributor.getKind() == Kind.UNBOUND_IMPORT) {
            ConfigDataEnvironmentContributor bound = contributor.withBoundProperties(result, activationContext);
            result = new ConfigDataEnvironmentContributors(bootstrapContext, result.getRoot().withReplacement(contributor, bound));
            continue;
        }
        var locationResolverContext = new ContributorConfigDataLocationResolverContext(result, contributor, activationContext);
        ConfigDataLoaderContext loaderContext = new ContributorDataLoaderContext(this);
        List<ConfigDataLocation> imports = contributor.getImports();
        if (traceEnabled) {
            log.trace("Processing imports {}", imports);
        }
        var imported = importer.resolveAndLoad(activationContext, locationResolverContext, loaderContext, imports);
        if (traceEnabled) {
            log.trace(getImportedMessage(imported.keySet()));
        }
        ConfigDataEnvironmentContributor contributorAndChildren = contributor.withChildren(importPhase, asContributors(imported));
        result = new ConfigDataEnvironmentContributors(bootstrapContext, result.getRoot().withReplacement(contributor, contributorAndChildren));
        processed++;
    }
}
Also used : ImportPhase(cn.taketoday.framework.context.config.ConfigDataEnvironmentContributor.ImportPhase)

Example 2 with ImportPhase

use of cn.taketoday.framework.context.config.ConfigDataEnvironmentContributor.ImportPhase in project today-framework by TAKETODAY.

the class ConfigDataEnvironmentContributors method withProcessedImports.

/**
 * Processes imports from all active contributors and return a new
 * {@link ConfigDataEnvironmentContributors} instance.
 *
 * @param importer the importer used to import {@link ConfigData}
 * @param activationContext the current activation context or {@code null} if the
 * context has not yet been created
 * @return a {@link ConfigDataEnvironmentContributors} instance with all relevant
 * imports have been processed
 */
ConfigDataEnvironmentContributors withProcessedImports(ConfigDataImporter importer, @Nullable ConfigDataActivationContext activationContext) {
    ImportPhase importPhase = ImportPhase.get(activationContext);
    boolean traceEnabled = log.isTraceEnabled();
    if (traceEnabled) {
        log.trace("Processing imports for phase {}. {}", importPhase, activationContext != null ? activationContext : "no activation context");
    }
    ConfigDataEnvironmentContributors result = this;
    int processed = 0;
    while (true) {
        ConfigDataEnvironmentContributor contributor = getNextToProcess(result, activationContext, importPhase);
        if (contributor == null) {
            if (traceEnabled) {
                log.trace("Processed imports for of {} contributors", processed);
            }
            return result;
        }
        if (contributor.getKind() == Kind.UNBOUND_IMPORT) {
            ConfigDataEnvironmentContributor bound = contributor.withBoundProperties(result, activationContext);
            result = new ConfigDataEnvironmentContributors(bootstrapContext, result.getRoot().withReplacement(contributor, bound));
            continue;
        }
        var locationResolverContext = new ContributorConfigDataLocationResolverContext(result, contributor, activationContext);
        ConfigDataLoaderContext loaderContext = new ContributorDataLoaderContext(this);
        List<ConfigDataLocation> imports = contributor.getImports();
        if (traceEnabled) {
            log.trace("Processing imports {}", imports);
        }
        var imported = importer.resolveAndLoad(activationContext, locationResolverContext, loaderContext, imports);
        if (traceEnabled) {
            log.trace(getImportedMessage(imported.keySet()));
        }
        ConfigDataEnvironmentContributor contributorAndChildren = contributor.withChildren(importPhase, asContributors(imported));
        result = new ConfigDataEnvironmentContributors(bootstrapContext, result.getRoot().withReplacement(contributor, contributorAndChildren));
        processed++;
    }
}
Also used : ImportPhase(cn.taketoday.framework.context.config.ConfigDataEnvironmentContributor.ImportPhase)

Aggregations

ImportPhase (cn.taketoday.framework.context.config.ConfigDataEnvironmentContributor.ImportPhase)2