package org.springframework.boot.context.config;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.logging.Log;
import org.springframework.boot.BootstrapContext;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.boot.util.Instantiator;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.log.LogMessage;
import org.springframework.util.Assert;
class ConfigDataLoaders {
private final Log logger;
private final List<ConfigDataLoader<?>> loaders;
private final List<Class<?>> resourceTypes;
ConfigDataLoaders(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext) {
this(logFactory, bootstrapContext, SpringFactoriesLoader.loadFactoryNames(ConfigDataLoader.class, null));
}
ConfigDataLoaders(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext,
List<String> names) {
this.logger = logFactory.getLog(getClass());
Instantiator<ConfigDataLoader<?>> instantiator = new Instantiator<>(ConfigDataLoader.class,
(availableParameters) -> {
availableParameters.add(Log.class, logFactory::getLog);
availableParameters.add(ConfigurableBootstrapContext.class, bootstrapContext);
availableParameters.add(BootstrapContext.class, bootstrapContext);
availableParameters.add(BootstrapRegistry.class, bootstrapContext);
});
this.loaders = instantiator.instantiate(names);
this.resourceTypes = getResourceTypes(this.loaders);
}
private List<Class<?>> getResourceTypes(List<ConfigDataLoader<?>> loaders) {
List<Class<?>> resourceTypes = new ArrayList<>(loaders.size());
for (ConfigDataLoader<?> loader : loaders) {
resourceTypes.add(getResourceType(loader));
}
return Collections.unmodifiableList(resourceTypes);
}
private Class<?> getResourceType(ConfigDataLoader<?> loader) {
return ResolvableType.forClass(loader.getClass()).as(ConfigDataLoader.class).resolveGeneric();
}
<R extends ConfigDataResource> ConfigData load(ConfigDataLoaderContext context, R resource) throws IOException {
ConfigDataLoader<R> loader = getLoader(context, resource);
this.logger.trace(LogMessage.of(() -> "Loading " + resource + " using loader " + loader.getClass().getName()));
return loader.load(context, resource);
}
@SuppressWarnings("unchecked")
private <R extends ConfigDataResource> ConfigDataLoader<R> getLoader(ConfigDataLoaderContext context, R resource) {
ConfigDataLoader<R> result = null;
for (int i = 0; i < this.loaders.size(); i++) {
ConfigDataLoader<?> candidate = this.loaders.get(i);
if (this.resourceTypes.get(i).isInstance(resource)) {
ConfigDataLoader<R> loader = (ConfigDataLoader<R>) candidate;
if (loader.isLoadable(context, resource)) {
if (result != null) {
throw new IllegalStateException("Multiple loaders found for resource '" + resource + "' ["
+ candidate.getClass().getName() + "," + result.getClass().getName() + "]");
}
result = loader;
}
}
}
Assert.state(result != null, () -> "No loader found for resource '" + resource + "'");
return result;
}
}