use of org.mule.runtime.config.internal.model.ComponentModel in project mule by mulesoft.
the class MacroExpansionModuleModel method createModuleOperationChain.
/**
* Takes a one liner call to any given message processor, expand it to creating a "module-operation-chain" scope which has the
* set of properties, the set of parameters and the list of message processors to execute.
*
* @param operationRefModel message processor that will be replaced by a scope element named "module-operation-chain".
* @param operationModel operation that provides both the <parameter/>s and content of the <body/>
* @param moduleGlobalElementsNames collection with the global components names (such as <http:config name="a"../>, <file:config
* name="b"../>, <file:matcher name="c"../> and so on) that are contained within the <module/> that will be macro
* expanded
* @param configRefParentTnsName parent reference to the global element if exists (it might not be global elements in the
* current module). Useful when replacing {@link #TNS_PREFIX} operations, as the references to the global elements will
* be those of the rootest element of the operations consumed by the app.
* @param containerName name of the container that contains the operation to be macro expanded. Not null nor empty.
* @return a new component model that represents the old placeholder but expanded with the content of the <body/>
*/
private ComponentModel createModuleOperationChain(ComponentModel operationRefModel, OperationModel operationModel, Set<String> moduleGlobalElementsNames, Optional<String> configRefParentTnsName, String containerName) {
final OperationComponentModelModelProperty operationComponentModelModelProperty = operationModel.getModelProperty(OperationComponentModelModelProperty.class).get();
final ComponentModel operationModuleComponentModel = operationComponentModelModelProperty.getBodyComponentModel();
List<ComponentModel> bodyProcessors = operationModuleComponentModel.getInnerComponents();
Optional<String> configRefName = referencesOperationsWithinModule(operationRefModel) ? configRefParentTnsName : Optional.ofNullable(operationRefModel.getParameters().get(MODULE_OPERATION_CONFIG_REF));
ComponentModel.Builder processorChainBuilder = new ComponentModel.Builder();
processorChainBuilder.setIdentifier(builder().namespace(CORE_PREFIX).name("module-operation-chain").build());
processorChainBuilder.addParameter("moduleName", extensionModel.getXmlDslModel().getPrefix(), false);
processorChainBuilder.addParameter("moduleOperation", operationModel.getName(), false);
Map<String, String> propertiesMap = extractProperties(configRefName);
Map<String, String> parametersMap = extractParameters(operationRefModel, operationModel.getAllParameterModels());
ComponentModel propertiesComponentModel = getParameterChild(propertiesMap, "module-operation-properties", "module-operation-property-entry");
ComponentModel parametersComponentModel = getParameterChild(parametersMap, "module-operation-parameters", "module-operation-parameter-entry");
processorChainBuilder.addChildComponentModel(propertiesComponentModel);
processorChainBuilder.addChildComponentModel(parametersComponentModel);
for (ComponentModel bodyProcessor : bodyProcessors) {
ComponentModel childMPcomponentModel = lookForTNSOperation(bodyProcessor).map(tnsOperation -> createModuleOperationChain(bodyProcessor, tnsOperation, moduleGlobalElementsNames, configRefName, containerName)).orElseGet(() -> copyOperationComponentModel(bodyProcessor, configRefName, moduleGlobalElementsNames, getLiteralParameters(propertiesMap, parametersMap), containerName));
processorChainBuilder.addChildComponentModel(childMPcomponentModel);
}
copyErrorMappings(operationRefModel, processorChainBuilder);
for (Map.Entry<String, Object> customAttributeEntry : operationRefModel.getCustomAttributes().entrySet()) {
processorChainBuilder.addCustomAttribute(customAttributeEntry.getKey(), customAttributeEntry.getValue());
}
processorChainBuilder.addCustomAttribute(ROOT_MACRO_EXPANDED_FLOW_CONTAINER_NAME, containerName);
ComponentModel processorChainModel = processorChainBuilder.build();
for (ComponentModel processorChainModelChild : processorChainModel.getInnerComponents()) {
processorChainModelChild.setParent(processorChainModel);
}
operationRefModel.getConfigFileName().ifPresent(processorChainBuilder::setConfigFileName);
operationRefModel.getLineNumber().ifPresent(processorChainBuilder::setLineNumber);
processorChainBuilder.addCustomAttribute(ORIGINAL_IDENTIFIER, operationRefModel.getIdentifier());
return processorChainModel;
}
use of org.mule.runtime.config.internal.model.ComponentModel in project mule by mulesoft.
the class MacroExpansionModuleModel method copyOperationComponentModel.
/**
* Goes over the {@code modelToCopy} by consuming the attributes as they are, unless some of them are actually targeting a
* global component (such as a configuration), in which it will append the {@code configRefName} to that reference, which will
* be the definitive name once the Mule application has been completely macro expanded in the final XML configuration.
*
* @param modelToCopy original <operation/> source of truth that comes from the <module/>
* @param configRefName name of the configuration being used by the current <operation/>. If the operation is a TNS one, then it
* has the value of the rootest <operation/> being called from the application.
* @param moduleGlobalElementsNames names of the <module/>s global component that will be macro expanded in the Mule application
* @param literalsParameters {@link Map} with all he <property>s and <parameter>s that were feed with a literal value in the
* Mule application's code.
* @param containerName name of the container that contains the operation to be macro expanded. Not null nor empty.
* @return a transformed {@link ComponentModel} from the {@code modelToCopy}, where the global element's attributes has been
* updated accordingly (both global components updates plus the line number, and so on). If the value for some parameter
* can be optimized by replacing it for the literal's value, it will be done as well using the
* {@code literalsParameters}
*/
private ComponentModel copyOperationComponentModel(ComponentModel modelToCopy, Optional<String> configRefName, Set<String> moduleGlobalElementsNames, Map<String, String> literalsParameters, String containerName) {
ComponentModel.Builder operationReplacementModel = getComponentModelBuilderFrom(modelToCopy);
for (Map.Entry<String, String> entry : modelToCopy.getParameters().entrySet()) {
String value = configRefName.map(s -> calculateAttributeValue(s, moduleGlobalElementsNames, entry.getValue())).orElseGet(entry::getValue);
final String optimizedValue = literalsParameters.getOrDefault(value, value);
operationReplacementModel.addParameter(entry.getKey(), optimizedValue, false);
}
for (ComponentModel operationChildModel : modelToCopy.getInnerComponents()) {
ComponentModel childMPcomponentModel = lookForTNSOperation(operationChildModel).map(tnsOperation -> createModuleOperationChain(operationChildModel, tnsOperation, moduleGlobalElementsNames, configRefName, containerName)).orElseGet(() -> copyOperationComponentModel(operationChildModel, configRefName, moduleGlobalElementsNames, literalsParameters, containerName));
operationReplacementModel.addChildComponentModel(childMPcomponentModel);
}
return buildFrom(modelToCopy, operationReplacementModel);
}
use of org.mule.runtime.config.internal.model.ComponentModel in project mule by mulesoft.
the class MacroExpansionModuleModel method buildFrom.
private ComponentModel buildFrom(ComponentModel componentModelOrigin, ComponentModel.Builder operationReplacementModel) {
componentModelOrigin.getConfigFileName().ifPresent(operationReplacementModel::setConfigFileName);
componentModelOrigin.getLineNumber().ifPresent(operationReplacementModel::setLineNumber);
ComponentModel componentModel = operationReplacementModel.build();
for (ComponentModel child : componentModel.getInnerComponents()) {
child.setParent(componentModel);
}
return componentModel;
}
use of org.mule.runtime.config.internal.model.ComponentModel in project mule by mulesoft.
the class MacroExpansionModuleModel method expandGlobalElements.
private void expandGlobalElements(List<ComponentModel> moduleComponentModels, Set<String> moduleGlobalElementsNames) {
applicationModel.executeOnEveryMuleComponentTree(muleRootComponentModel -> {
HashMap<ComponentModel, List<ComponentModel>> componentModelsToReplaceByIndex = new HashMap<>();
for (ComponentModel configRefModel : muleRootComponentModel.getInnerComponents()) {
looForConfiguration(configRefModel).ifPresent(configurationModel -> {
Map<String, String> propertiesMap = extractParameters(configRefModel, configurationModel.getAllParameterModels());
final Map<String, String> literalsParameters = getLiteralParameters(propertiesMap, emptyMap());
List<ComponentModel> replacementGlobalElements = createGlobalElementsInstance(configRefModel, moduleComponentModels, moduleGlobalElementsNames, literalsParameters);
componentModelsToReplaceByIndex.put(configRefModel, replacementGlobalElements);
});
}
for (Map.Entry<ComponentModel, List<ComponentModel>> entry : componentModelsToReplaceByIndex.entrySet()) {
final int componentModelIndex = muleRootComponentModel.getInnerComponents().indexOf(entry.getKey());
muleRootComponentModel.getInnerComponents().addAll(componentModelIndex, entry.getValue());
muleRootComponentModel.getInnerComponents().remove(componentModelIndex + entry.getValue().size());
}
});
}
use of org.mule.runtime.config.internal.model.ComponentModel in project mule by mulesoft.
the class ComponentLocationVisitor method accept.
/**
* For every {@link ComponentModel} in the configuration, sets the {@link DefaultComponentLocation} associated within an
* annotation under the key {@link AbstractComponent#LOCATION_KEY}.
*
* @param componentModel the component model that will be assign it's {@link DefaultComponentLocation}.
*/
@Override
public void accept(ComponentModel componentModel) {
if (componentModel.getParent() == null) {
// do not process root element
return;
}
DefaultComponentLocation componentLocation;
Optional<TypedComponentIdentifier> typedComponentIdentifier = of(builder().identifier(componentModel.getIdentifier()).type(componentModel.getComponentType().orElse(UNKNOWN)).build());
if (componentModel.isRoot()) {
String componentModelNameAttribute = componentModel.getNameAttribute();
ImmutableList<DefaultLocationPart> parts = ImmutableList.<DefaultLocationPart>builder().add(new DefaultLocationPart(componentModelNameAttribute, typedComponentIdentifier, componentModel.getConfigFileName(), componentModel.getLineNumber())).build();
componentLocation = new DefaultComponentLocation(ofNullable(componentModelNameAttribute), parts);
} else if (existsWithinRootContainer(componentModel)) {
ComponentModel parentComponentModel = componentModel.getParent();
DefaultComponentLocation parentComponentLocation = parentComponentModel.getComponentLocation();
if (isHttpProxyPart(componentModel)) {
componentLocation = parentComponentLocation.appendLocationPart(componentModel.getIdentifier().getName(), typedComponentIdentifier, componentModel.getConfigFileName(), componentModel.getLineNumber());
} else if (isRootProcessorScope(parentComponentModel)) {
componentLocation = processFlowDirectChild(componentModel, parentComponentLocation, typedComponentIdentifier);
} else if (isMunitFlowIdentifier(parentComponentModel)) {
componentLocation = parentComponentLocation.appendRoutePart().appendLocationPart(findNonProcessorPath(componentModel), typedComponentIdentifier, componentModel.getConfigFileName(), componentModel.getLineNumber());
} else if (isErrorHandler(componentModel)) {
componentLocation = processErrorHandlerComponent(componentModel, parentComponentLocation, typedComponentIdentifier);
} else if (isTemplateOnErrorHandler(componentModel)) {
componentLocation = processOnErrorModel(componentModel, parentComponentLocation, typedComponentIdentifier);
} else if (parentComponentIsRouter(componentModel)) {
if (isRoute(componentModel)) {
componentLocation = parentComponentLocation.appendRoutePart().appendLocationPart(findNonProcessorPath(componentModel), of(TypedComponentIdentifier.builder().type(SCOPE).identifier(ROUTE_COMPONENT_IDENTIFIER).build()), componentModel.getConfigFileName(), componentModel.getLineNumber());
} else if (isProcessor(componentModel)) {
// this is the case of the routes directly inside the router as with scatter-gather
componentLocation = parentComponentLocation.appendRoutePart().appendLocationPart(findProcessorPath(componentModel), empty(), empty(), empty()).appendProcessorsPart().appendLocationPart("0", typedComponentIdentifier, componentModel.getConfigFileName(), componentModel.getLineNumber());
} else {
// this is the case of the when element inside the choice
componentLocation = parentComponentLocation.appendRoutePart().appendLocationPart(findNonProcessorPath(componentModel), of(TypedComponentIdentifier.builder().type(UNKNOWN).identifier(ROUTE_COMPONENT_IDENTIFIER).build()), empty(), empty());
}
} else if (isProcessor(componentModel)) {
if (isModuleOperation(componentModel.getParent())) {
final Optional<TypedComponentIdentifier> operationTypedIdentifier = ApplicationModel.MODULE_OPERATION_CHAIN.equals(typedComponentIdentifier.get().getIdentifier()) ? getModuleOperationTypeComponentIdentifier(componentModel) : typedComponentIdentifier;
componentLocation = processModuleOperationChildren(componentModel, operationTypedIdentifier);
} else {
componentLocation = parentComponentLocation.appendProcessorsPart().appendLocationPart(findProcessorPath(componentModel), typedComponentIdentifier, componentModel.getConfigFileName(), componentModel.getLineNumber());
}
} else {
if (isBatchAggregator(componentModel)) {
componentLocation = parentComponentLocation.appendLocationPart(BATCH_AGGREGATOR_COMPONENT_IDENTIFIER.getName(), of(TypedComponentIdentifier.builder().type(UNKNOWN).identifier(BATCH_AGGREGATOR_COMPONENT_IDENTIFIER).build()), componentModel.getConfigFileName(), componentModel.getLineNumber());
} else {
componentLocation = parentComponentLocation.appendLocationPart(findNonProcessorPath(componentModel), typedComponentIdentifier, componentModel.getConfigFileName(), componentModel.getLineNumber());
}
}
} else {
DefaultComponentLocation parentComponentLocation = componentModel.getParent().getComponentLocation();
componentLocation = parentComponentLocation.appendLocationPart(findNonProcessorPath(componentModel), typedComponentIdentifier, componentModel.getConfigFileName(), componentModel.getLineNumber());
}
componentModel.setComponentLocation(componentLocation);
}
Aggregations