use of org.mule.runtime.api.component.ComponentIdentifier in project mule by mulesoft.
the class ConfigurationBasedElementModelFactory method getComponentChildVisitor.
private MetadataTypeVisitor getComponentChildVisitor(final DslElementModel.Builder typeBuilder, final ComponentConfiguration configuration, final MetadataType model, final String name, final DslElementSyntax modelDsl, final Optional<String> defaultValue, Deque<String> typeResolvingStack) {
final Map<String, String> parameters = configuration.getParameters();
return new MetadataTypeVisitor() {
@Override
protected void defaultVisit(MetadataType metadataType) {
DslElementModel.Builder<MetadataType> elementBuilder = DslElementModel.<MetadataType>builder().withModel(model).withDsl(modelDsl);
Optional<ComponentIdentifier> identifier = getIdentifier(modelDsl);
String value = parameters.get(name);
if (isBlank(value)) {
if (identifier.isPresent()) {
ComponentConfiguration nested = getSingleComponentConfiguration(getNestedComponents(configuration), identifier);
if (nested != null && nested.getValue().isPresent() && !isBlank(nested.getValue().get())) {
value = nested.getValue().get().trim();
}
} else if (defaultValue.isPresent()) {
value = defaultValue.get();
elementBuilder.isExplicitInDsl(false);
}
}
if (!isBlank(value)) {
typeBuilder.containing(elementBuilder.withValue(value).build());
}
}
@Override
public void visitArrayType(ArrayType arrayType) {
Optional<ComponentIdentifier> identifier = getIdentifier(modelDsl);
if (identifier.isPresent()) {
ComponentConfiguration fieldComponent = getSingleComponentConfiguration(getNestedComponents(configuration), identifier);
if (fieldComponent != null) {
DslElementModel.Builder<Object> list = DslElementModel.builder().withModel(model).withDsl(modelDsl).withConfig(fieldComponent);
modelDsl.getGeneric(arrayType.getType()).ifPresent(itemdsl -> {
ComponentIdentifier itemIdentifier = getIdentifier(itemdsl).get();
fieldComponent.getNestedComponents().forEach(c -> {
if (c.getIdentifier().equals(itemIdentifier)) {
getComponentChildVisitor(list, c, arrayType.getType(), VALUE_ATTRIBUTE_NAME, itemdsl, defaultValue, typeResolvingStack);
}
});
});
typeBuilder.containing(list.build());
return;
}
}
defaultValue.ifPresent(s -> typeBuilder.containing(DslElementModel.builder().withModel(model).withDsl(modelDsl).withValue(defaultValue.get()).isExplicitInDsl(false).build()));
}
@Override
public void visitObject(ObjectType objectType) {
Optional<ComponentIdentifier> identifier = getIdentifier(modelDsl);
if (identifier.isPresent()) {
if (isMap(objectType)) {
typeBuilder.containing(createMapElement(objectType, modelDsl, configuration));
return;
}
Multimap<ComponentIdentifier, ComponentConfiguration> nestedComponents = getNestedComponents(configuration);
ComponentConfiguration fieldComponent = nestedComponents.containsKey(identifier.get()) ? nestedComponents.get(identifier.get()).iterator().next() : null;
fieldComponent = fieldComponent == null ? configuration : fieldComponent;
String value = fieldComponent.getParameters().get(modelDsl.getAttributeName());
if (!isBlank(value)) {
typeBuilder.containing(DslElementModel.builder().withModel(model).withDsl(modelDsl).withValue(value).build());
} else {
resolveBasedOnType(objectType, fieldComponent, typeResolvingStack).ifPresent(typeBuilder::containing);
}
return;
}
defaultValue.ifPresent(s -> typeBuilder.containing(DslElementModel.builder().withModel(model).withDsl(modelDsl).withValue(defaultValue.get()).isExplicitInDsl(false).build()));
}
};
}
use of org.mule.runtime.api.component.ComponentIdentifier in project mule by mulesoft.
the class ConfigurationBasedElementModelFactory method createIdentifiedElement.
private DslElementModel createIdentifiedElement(ComponentConfiguration configuration) {
final ComponentIdentifier identifier = configuration.getIdentifier();
Optional<Map.Entry<ExtensionModel, DslSyntaxResolver>> entry = resolvers.entrySet().stream().filter(e -> e.getKey().getXmlDslModel().getPrefix().equals(identifier.getNamespace())).findFirst();
if (!entry.isPresent()) {
return null;
}
currentExtension = entry.get().getKey();
dsl = entry.get().getValue();
Reference<DslElementModel> elementModel = new Reference<>();
new ExtensionWalker() {
@Override
protected void onConfiguration(ConfigurationModel model) {
final DslElementSyntax elementDsl = dsl.resolve(model);
getIdentifier(elementDsl).ifPresent(elementId -> {
if (elementId.equals(identifier)) {
DslElementModel.Builder<ConfigurationModel> element = createElementModel(model, elementDsl, configuration);
addConnectionProvider(model, dsl, element, configuration);
elementModel.set(element.build());
stop();
}
});
}
@Override
protected void onConstruct(HasConstructModels owner, ConstructModel model) {
final DslElementSyntax elementDsl = dsl.resolve(model);
getIdentifier(elementDsl).ifPresent(elementId -> {
if (elementId.equals(identifier)) {
elementModel.set(createElementModel(model, elementDsl, configuration).build());
stop();
}
});
}
@Override
protected void onOperation(HasOperationModels owner, OperationModel model) {
final DslElementSyntax elementDsl = dsl.resolve(model);
getIdentifier(elementDsl).ifPresent(elementId -> {
if (elementId.equals(identifier)) {
elementModel.set(createElementModel(model, elementDsl, configuration).build());
stop();
}
});
}
@Override
protected void onSource(HasSourceModels owner, SourceModel model) {
final DslElementSyntax elementDsl = dsl.resolve(model);
getIdentifier(elementDsl).ifPresent(elementId -> {
if (elementId.equals(identifier)) {
elementModel.set(createElementModel(model, elementDsl, configuration).build());
stop();
}
});
}
}.walk(currentExtension);
if (elementModel.get() == null) {
resolveBasedOnTypes(configuration).ifPresent(elementModel::set);
}
return elementModel.get();
}
use of org.mule.runtime.api.component.ComponentIdentifier in project mule by mulesoft.
the class BeanDefinitionFactory method resolveErrorType.
private ErrorType resolveErrorType(String representation) {
int separator = representation.indexOf(":");
String namespace;
String identifier;
if (separator > 0) {
namespace = representation.substring(0, separator).toUpperCase();
identifier = representation.substring(separator + 1).toUpperCase();
} else {
namespace = CORE_ERROR_NS;
identifier = representation.toUpperCase();
}
ComponentIdentifier errorIdentifier = ComponentIdentifier.builder().namespace(namespace).name(identifier).build();
if (CORE_ERROR_NS.equals(namespace)) {
return errorTypeRepository.lookupErrorType(errorIdentifier).orElseThrow(() -> new MuleRuntimeException(createStaticMessage(format("There's no MULE error named '%s'.", identifier))));
} else if (errorTypeRepository.getErrorNamespaces().contains(namespace) && !syntheticErrorNamespaces.contains(namespace)) {
throw new MuleRuntimeException(createStaticMessage(format("Cannot use error type '%s:%s': namespace already exists.", namespace, identifier)));
} else if (syntheticErrorNamespaces.contains(namespace)) {
Optional<ErrorType> optionalErrorType = errorTypeRepository.lookupErrorType(errorIdentifier);
if (optionalErrorType.isPresent()) {
return optionalErrorType.get();
}
} else {
syntheticErrorNamespaces.add(namespace);
}
return errorTypeRepository.addErrorType(errorIdentifier, errorTypeRepository.getAnyErrorType());
}
use of org.mule.runtime.api.component.ComponentIdentifier in project mule by mulesoft.
the class MacroExpansionModuleModel method lookForOperation.
/**
* Looks for an operation checking if it is defined within the scope of a {@link ConfigurationModel} or the
* {@link ExtensionModel}.
*
* @param operationIdentifier element to look for in the current {@link #extensionModel}
* @param prefix to check if the {@code operationIdentifier} namespace targets an operation of the <module/> (usually maps to
* the {@link ExtensionModel} prefix, or the {@link #TNS_PREFIX}.
* @return an {@link OperationModel} if found, {@link Optional#empty()} otherwise.
*/
private Optional<OperationModel> lookForOperation(ComponentIdentifier operationIdentifier, String prefix) {
Optional<OperationModel> result = Optional.empty();
final String operationName = operationIdentifier.getName();
if (operationIdentifier.getNamespace().equals(prefix)) {
// As the operation can be inside the extension or the config, it has to be looked up in both elements.
final HasOperationModels hasOperationModels = getConfigurationModel().map(configurationModel -> (HasOperationModels) configurationModel).orElse(extensionModel);
result = hasOperationModels.getOperationModel(operationName);
}
// If the operation is not present, it might be a private one and it must be looked inside of the model property
if (!result.isPresent() && extensionModel.getModelProperty(PrivateOperationsModelProperty.class).isPresent()) {
result = extensionModel.getModelProperty(PrivateOperationsModelProperty.class).get().getOperationModel(operationName);
}
return result;
}
use of org.mule.runtime.api.component.ComponentIdentifier in project mule by mulesoft.
the class ApplicationModel method validateNamedTopLevelElementsHaveName.
private void validateNamedTopLevelElementsHaveName(ComponentBuildingDefinitionRegistry componentBuildingDefinitionRegistry) throws ConfigurationException {
try {
List<ComponentModel> topLevelComponents = muleComponentModels.get(0).getInnerComponents();
topLevelComponents.stream().forEach(topLevelComponent -> {
final ComponentIdentifier identifier = topLevelComponent.getIdentifier();
componentBuildingDefinitionRegistry.getBuildingDefinition(identifier).filter(ComponentBuildingDefinition::isNamed).ifPresent(buildingDefinition -> {
if (isBlank(topLevelComponent.getNameAttribute())) {
throw new MuleRuntimeException(createStaticMessage(format("Global element %s:%s does not provide a name attribute.", identifier.getNamespace(), identifier.getName())));
}
});
});
} catch (Exception e) {
throw new ConfigurationException(e);
}
}
Aggregations