use of org.mule.runtime.config.api.dsl.processor.ConfigFile in project mule by mulesoft.
the class ApplicationModel method convertConfigFileToComponentModel.
private void convertConfigFileToComponentModel(ArtifactConfig artifactConfig) {
List<ConfigFile> configFiles = artifactConfig.getConfigFiles();
ComponentModelReader componentModelReader = new ComponentModelReader(configurationProperties.getConfigurationPropertiesResolver());
configFiles.stream().forEach(configFile -> {
ComponentModel componentModel = componentModelReader.extractComponentDefinitionModel(configFile.getConfigLines().get(0), configFile.getFilename());
if (muleComponentModels.isEmpty()) {
muleComponentModels.add(componentModel);
} else {
// Only one componentModel as Root should be set, therefore componentModel is merged
final ComponentModel rootComponentModel = muleComponentModels.get(0);
muleComponentModels.set(0, new ComponentModel.Builder(rootComponentModel).merge(componentModel).build());
}
});
}
use of org.mule.runtime.config.api.dsl.processor.ConfigFile in project mule by mulesoft.
the class ApplicationModel method getConfigurationPropertiesProvidersFromComponents.
private List<ConfigurationPropertiesProvider> getConfigurationPropertiesProvidersFromComponents(ArtifactConfig artifactConfig, ConfigurationPropertiesResolver localResolver) {
Map<ComponentIdentifier, ConfigurationPropertiesProviderFactory> providerFactoriesMap = new HashMap<>();
ServiceLoader<ConfigurationPropertiesProviderFactory> providerFactories = java.util.ServiceLoader.load(ConfigurationPropertiesProviderFactory.class);
providerFactories.forEach(service -> {
ComponentIdentifier componentIdentifier = service.getSupportedComponentIdentifier();
if (providerFactoriesMap.containsKey(componentIdentifier)) {
throw new MuleRuntimeException(createStaticMessage("Multiple configuration providers for component: " + componentIdentifier));
}
providerFactoriesMap.put(componentIdentifier, service);
});
List<ConfigurationPropertiesProvider> configConfigurationPropertiesProviders = new ArrayList<>();
artifactConfig.getConfigFiles().stream().forEach(configFile -> configFile.getConfigLines().stream().forEach(configLine -> {
for (ConfigLine componentConfigLine : configLine.getChildren()) {
if (componentConfigLine.getNamespace() == null) {
continue;
}
ComponentIdentifier componentIdentifier = ComponentIdentifier.builder().namespace(componentConfigLine.getNamespace()).name(componentConfigLine.getIdentifier()).build();
if (!providerFactoriesMap.containsKey(componentIdentifier)) {
continue;
}
DefaultConfigurationParameters.Builder configurationParametersBuilder = DefaultConfigurationParameters.builder();
ConfigurationParameters configurationParameters = resolveConfigurationParameters(configurationParametersBuilder, componentConfigLine, localResolver);
ConfigurationPropertiesProvider provider = providerFactoriesMap.get(componentIdentifier).createProvider(configurationParameters, externalResourceProvider);
if (provider instanceof Component) {
Component providerComponent = (Component) provider;
TypedComponentIdentifier typedComponentIdentifier = TypedComponentIdentifier.builder().type(UNKNOWN).identifier(componentIdentifier).build();
DefaultComponentLocation.DefaultLocationPart locationPart = new DefaultComponentLocation.DefaultLocationPart(componentIdentifier.getName(), of(typedComponentIdentifier), of(configFile.getFilename()), of(configLine.getLineNumber()));
providerComponent.setAnnotations(ImmutableMap.<QName, Object>builder().put(AbstractComponent.LOCATION_KEY, new DefaultComponentLocation(of(componentIdentifier.getName()), singletonList(locationPart))).build());
}
configConfigurationPropertiesProviders.add(provider);
try {
initialiseIfNeeded(provider);
} catch (InitialisationException e) {
throw new MuleRuntimeException(e);
}
}
}));
return configConfigurationPropertiesProviders;
}
use of org.mule.runtime.config.api.dsl.processor.ConfigFile in project mule by mulesoft.
the class MuleArtifactContext method recursivelyResolveConfigFiles.
private List<ConfigFile> recursivelyResolveConfigFiles(List<Pair<String, InputStream>> configFilesToResolve, List<ConfigFile> alreadyResolvedConfigFiles) {
DefaultConfigurationPropertiesResolver propertyResolver = new DefaultConfigurationPropertiesResolver(empty(), new EnvironmentPropertiesConfigurationProvider());
ImmutableList.Builder<ConfigFile> resolvedConfigFilesBuilder = ImmutableList.<ConfigFile>builder().addAll(alreadyResolvedConfigFiles);
configFilesToResolve.stream().filter(fileNameInputStreamPair -> !alreadyResolvedConfigFiles.stream().anyMatch(configFile -> configFile.getFilename().equals(fileNameInputStreamPair.getFirst()))).forEach(fileNameInputStreamPair -> {
Document document = xmlConfigurationDocumentLoader.loadDocument(muleContext.getExtensionManager() == null ? emptySet() : muleContext.getExtensionManager().getExtensions(), fileNameInputStreamPair.getFirst(), fileNameInputStreamPair.getSecond());
ConfigLine mainConfigLine = xmlApplicationParser.parse(document.getDocumentElement()).get();
ConfigFile configFile = new ConfigFile(fileNameInputStreamPair.getFirst(), asList(mainConfigLine));
resolvedConfigFilesBuilder.add(configFile);
try {
fileNameInputStreamPair.getSecond().close();
} catch (IOException e) {
throw new MuleRuntimeException(e);
}
});
ImmutableSet.Builder<String> importedFiles = ImmutableSet.builder();
for (ConfigFile configFile : resolvedConfigFilesBuilder.build()) {
List<ConfigLine> rootConfigLines = configFile.getConfigLines();
ConfigLine muleRootElementConfigLine = rootConfigLines.get(0);
importedFiles.addAll(muleRootElementConfigLine.getChildren().stream().filter(configLine -> configLine.getNamespace().equals(CORE_PREFIX) && configLine.getIdentifier().equals(IMPORT_ELEMENT)).map(configLine -> {
SimpleConfigAttribute fileConfigAttribute = configLine.getConfigAttributes().get("file");
if (fileConfigAttribute == null) {
throw new RuntimeConfigurationException(createStaticMessage(format("<import> does not have a file attribute defined. At file '%s', at line %s", configFile.getFilename(), configLine.getLineNumber())));
}
return fileConfigAttribute.getValue();
}).map(value -> (String) propertyResolver.resolveValue(value)).filter(fileName -> !alreadyResolvedConfigFiles.stream().anyMatch(solvedConfigFile -> solvedConfigFile.getFilename().equals(fileName))).collect(toList()));
}
Set<String> importedConfigurationFiles = importedFiles.build();
if (importedConfigurationFiles.isEmpty()) {
return resolvedConfigFilesBuilder.build();
}
List<Pair<String, InputStream>> newConfigFilesToResolved = importedConfigurationFiles.stream().map(importedFileName -> {
InputStream resourceAsStream = muleContext.getExecutionClassLoader().getResourceAsStream(importedFileName);
if (resourceAsStream == null) {
throw new RuntimeConfigurationException(createStaticMessage(format("Could not find imported resource '%s'", importedFileName)));
}
return (Pair<String, InputStream>) new Pair(importedFileName, resourceAsStream);
}).collect(toList());
return recursivelyResolveConfigFiles(newConfigFilesToResolved, resolvedConfigFilesBuilder.build());
}
use of org.mule.runtime.config.api.dsl.processor.ConfigFile in project mule by mulesoft.
the class MuleArtifactContext method resolveArtifactConfig.
private ArtifactConfig resolveArtifactConfig() throws IOException {
ArtifactConfig.Builder applicationConfigBuilder = new ArtifactConfig.Builder();
applicationConfigBuilder.setArtifactProperties(this.artifactProperties);
List<Pair<String, InputStream>> initialConfigFiles = new ArrayList<>();
for (ConfigResource artifactConfigResource : artifactConfigResources) {
initialConfigFiles.add(new Pair<>(artifactConfigResource.getResourceName(), artifactConfigResource.getInputStream()));
}
List<ConfigFile> configFiles = new ArrayList<>();
recursivelyResolveConfigFiles(initialConfigFiles, configFiles).forEach(applicationConfigBuilder::addConfigFile);
applicationConfigBuilder.setApplicationName(muleContext.getConfiguration().getId());
return applicationConfigBuilder.build();
}
Aggregations