use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class ExtensionModelHelper method findComponentModel.
/**
* Finds a {@link org.mule.runtime.api.meta.model.ComponentModel} within the provided set of {@link ExtensionModel}s by a
* {@link ComponentIdentifier}.
*
* @param componentIdentifier the identifier to use for the search.
* @return the found {@link org.mule.runtime.api.meta.model.ComponentModel} or {@link Optional#empty()} if it couldn't be found.
*/
public Optional<? extends org.mule.runtime.api.meta.model.ComponentModel> findComponentModel(ComponentIdentifier componentIdentifier) {
try {
return extensionComponentModelByComponentIdentifier.get(componentIdentifier, () -> {
String componentName = toCamelCase(componentIdentifier.getName(), COMPONENT_NAME_SEPARATOR);
for (ExtensionModel extensionModel : extensionsModels) {
if (extensionModel.getXmlDslModel().getPrefix().equals(componentIdentifier.getNamespace())) {
List<HasOperationModels> operationModelsProviders = ImmutableList.<HasOperationModels>builder().add(extensionModel).addAll(extensionModel.getConfigurationModels()).build();
List<HasSourceModels> sourceModelsProviders = ImmutableList.<HasSourceModels>builder().add(extensionModel).addAll(extensionModel.getConfigurationModels()).build();
List<HasConstructModels> constructModelsProviders = singletonList(extensionModel);
Optional<? extends org.mule.runtime.api.meta.model.ComponentModel> componentModel = resolveModel(operationModelsProviders, sourceModelsProviders, constructModelsProviders, componentName);
// TODO MULE-13894 remove this once unified extensionModel names to use camelCase (see smart connectors and crafted declared extesion models)
if (!componentModel.isPresent()) {
componentModel = resolveModel(operationModelsProviders, sourceModelsProviders, constructModelsProviders, componentIdentifier.getName());
}
return componentModel;
}
}
return empty();
});
} catch (ExecutionException e) {
throw new MuleRuntimeException(e);
}
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class EagerObjectCreator method handleRequest.
@Override
boolean handleRequest(CreateBeanDefinitionRequest createBeanDefinitionRequest) {
SpringComponentModel componentModel = createBeanDefinitionRequest.getComponentModel();
Class<?> type = componentModel.getType();
if (type == null) {
return false;
}
Optional<Class<?>> foundClass = earlyCreationObjectTypes.stream().filter(clazz -> clazz.isAssignableFrom(type)).findAny();
return foundClass.map(clazz -> {
ComponentBuildingDefinition componentBuildingDefinition = createBeanDefinitionRequest.getComponentBuildingDefinition();
Object instance;
try {
instance = type.newInstance();
} catch (Exception e) {
throw new MuleRuntimeException(createStaticMessage("Could not create an instance of '%s' using default constructor. Early created object must have a default constructor", type.getName()));
}
componentBuildingDefinition.getSetterParameterDefinitions().forEach(attributeDefinition -> {
SetterAttributeDefinition setterAttributeDefinition = (SetterAttributeDefinition) attributeDefinition;
setterAttributeDefinition.getAttributeDefinition().accept(new AbstractAttributeDefinitionVisitor() {
@Override
public void onUndefinedSimpleParameters() {
Map<String, String> parameters = componentModel.getParameters();
String attributeName = setterAttributeDefinition.getAttributeName();
try {
setProperty(instance, attributeName, parameters);
} catch (Exception e) {
throw new MuleRuntimeException(e);
}
}
@Override
protected void doOnOperation(String operation) {
throw new MuleRuntimeException(createStaticMessage(format("Attribute definition with operation '%s' is not supported for earlyCreationObjects", operation)));
}
});
});
componentModel.setObjectInstance(instance);
componentModel.setBeanDefinition(rootBeanDefinition(ConstantFactoryBean.class).addConstructorArgValue(instance).getBeanDefinition());
return true;
}).orElse(false);
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class PropertyComponentUtils method getPropertyValueFromPropertyComponent.
/**
* Creates a {@link Pair} from a generic property/ies component in the configuration.
*
* @param propertyComponentModel the component model for spring:property, spring:properties or property.
* @return a {@code PropertyValue} with the parsed content of the component.
*/
public static Pair<String, Object> getPropertyValueFromPropertyComponent(ComponentModel propertyComponentModel) {
Pair<String, Object> propertyValue;
String refKey = getReferenceAttributeName(propertyComponentModel.getIdentifier());
String nameKey = getNameAttributeName(propertyComponentModel.getIdentifier());
if (propertyComponentModel.getInnerComponents().isEmpty()) {
Object value;
if (propertyComponentModel.getParameters().containsKey(refKey)) {
value = new RuntimeBeanReference(propertyComponentModel.getParameters().get(refKey));
} else {
value = propertyComponentModel.getParameters().get(VALUE_PARAMETER_NAME);
}
if (!propertyComponentModel.getParameters().containsKey(nameKey)) {
propertyValue = new Pair<>(PROPERTY_NAME_PROPERTY_ATTRIBUTE, new RuntimeBeanReference(propertyComponentModel.getParameters().get("ref")));
} else {
propertyValue = new Pair<>(propertyComponentModel.getParameters().get(nameKey), value);
}
} else if (propertyComponentModel.getInnerComponents().get(0).getIdentifier().getName().equals("map")) {
ComponentModel springMap = propertyComponentModel.getInnerComponents().get(0);
ManagedMap<String, Object> propertiesMap = new ManagedMap<>();
springMap.getInnerComponents().stream().forEach(mapEntry -> {
Object value;
if (mapEntry.getParameters().containsKey(VALUE_PARAMETER_NAME)) {
value = mapEntry.getParameters().get(VALUE_PARAMETER_NAME);
} else {
value = new RuntimeBeanReference(mapEntry.getParameters().get(REFERENCE_MULE_PROPERTY_ATTRIBUTE));
}
propertiesMap.put(mapEntry.getParameters().get(PROPERTY_NAME_MULE_PROPERTY_ATTRIBUTE), value);
});
propertyValue = new Pair<>(propertyComponentModel.getNameAttribute(), propertiesMap);
} else {
throw new MuleRuntimeException(createStaticMessage("Unrecognized property model identifier: " + propertyComponentModel.getInnerComponents().get(0).getIdentifier()));
}
return propertyValue;
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class ApplicationModel method validateErrorMappings.
private void validateErrorMappings() {
executeOnEveryComponentTree(componentModel -> {
List<ComponentModel> errorMappings = componentModel.getInnerComponents().stream().filter(c -> c.getIdentifier().equals(ERROR_MAPPING_IDENTIFIER)).collect(toList());
if (!errorMappings.isEmpty()) {
List<ComponentModel> anyMappings = errorMappings.stream().filter(this::isErrorMappingWithSourceAny).collect(toList());
if (anyMappings.size() > 1) {
throw new MuleRuntimeException(createStaticMessage("Only one mapping for 'ANY' or an empty source type is allowed."));
} else if (anyMappings.size() == 1 && !isErrorMappingWithSourceAny(errorMappings.get(errorMappings.size() - 1))) {
throw new MuleRuntimeException(createStaticMessage("Only the last error mapping can have 'ANY' or an empty source type."));
}
List<String> sources = errorMappings.stream().map(model -> model.getParameters().get(SOURCE_TYPE)).collect(toList());
List<String> distinctSources = sources.stream().distinct().collect(toList());
if (sources.size() != distinctSources.size()) {
throw new MuleRuntimeException(createStaticMessage(format("Repeated source types are not allowed. Offending types are '%s'.", on("', '").join(disjunction(sources, distinctSources)))));
}
}
});
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class ApplicationModel method validateSingletonsAreNotRepeated.
private void validateSingletonsAreNotRepeated() {
Map<String, ComponentModel> existingObjectsWithName = new HashMap<>();
executeOnEveryRootElementWithBuildingDefinition(((componentModel, componentBuildingDefinition) -> {
if (componentBuildingDefinition.getRegistrationName() != null) {
String nameAttributeValue = componentModel.getNameAttribute();
if (existingObjectsWithName.containsKey(nameAttributeValue)) {
ComponentModel otherComponentModel = existingObjectsWithName.get(nameAttributeValue);
if (componentModel.getConfigFileName().isPresent() && componentModel.getLineNumber().isPresent() && otherComponentModel.getConfigFileName().isPresent() && otherComponentModel.getLineNumber().isPresent()) {
throw new MuleRuntimeException(createStaticMessage("The configuration element [%s] can only appear once, but was present in both [%s:%d] and [%s:%d]", componentModel.getIdentifier(), otherComponentModel.getConfigFileName().get(), otherComponentModel.getLineNumber().get(), componentModel.getConfigFileName().get(), componentModel.getLineNumber().get()));
} else {
throw new MuleRuntimeException(createStaticMessage("The configuration element [%s] can only appear once, but was present multiple times", componentModel.getIdentifier()));
}
}
existingObjectsWithName.put(nameAttributeValue, componentModel);
componentModel.setParameter(ApplicationModel.NAME_ATTRIBUTE, componentBuildingDefinition.getRegistrationName());
}
}));
}
Aggregations