use of org.mule.runtime.dsl.api.component.ComponentBuildingDefinition in project mule by mulesoft.
the class MapEntryBeanDefinitionCreator method handleRequest.
@Override
boolean handleRequest(CreateBeanDefinitionRequest createBeanDefinitionRequest) {
ObjectTypeVisitor objectTypeVisitor = new ObjectTypeVisitor(createBeanDefinitionRequest.getComponentModel());
createBeanDefinitionRequest.getComponentBuildingDefinition().getTypeDefinition().visit(objectTypeVisitor);
Class<?> type = objectTypeVisitor.getType();
if (!(MapEntryType.class.isAssignableFrom(type))) {
return false;
}
SpringComponentModel componentModel = createBeanDefinitionRequest.getComponentModel();
ComponentBuildingDefinition componentBuildingDefinition = createBeanDefinitionRequest.getComponentBuildingDefinition();
componentModel.setType(type);
final Object key = componentModel.getParameters().get(ENTRY_TYPE_KEY_PARAMETER_NAME);
Object keyBeanDefinition = getConvertibleBeanDefinition(objectTypeVisitor.getMapEntryType().get().getKeyType(), key, componentBuildingDefinition.getKeyTypeConverter());
Object value = null;
// MULE-11984: Check that generated map entries are not empty
if (componentModel.getParameters().get(ENTRY_TYPE_VALUE_REF_PARAMETER_NAME) != null) {
value = new RuntimeBeanReference(componentModel.getParameters().get(ENTRY_TYPE_VALUE_REF_PARAMETER_NAME));
} else {
value = getValue(objectTypeVisitor, componentModel, componentBuildingDefinition);
}
AbstractBeanDefinition beanDefinition = genericBeanDefinition(MapEntry.class).addConstructorArgValue(keyBeanDefinition).addConstructorArgValue(value).getBeanDefinition();
componentModel.setBeanDefinition(beanDefinition);
return true;
}
use of org.mule.runtime.dsl.api.component.ComponentBuildingDefinition 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());
}
}));
}
use of org.mule.runtime.dsl.api.component.ComponentBuildingDefinition in project mule by mulesoft.
the class ApplicationModel method executeOnEveryRootElementWithBuildingDefinition.
private void executeOnEveryRootElementWithBuildingDefinition(BiConsumer<ComponentModel, ComponentBuildingDefinition> action) {
if (componentBuildingDefinitionRegistry.isPresent()) {
ComponentBuildingDefinitionRegistry definitionRegistry = componentBuildingDefinitionRegistry.get();
this.executeOnEveryRootElement(componentModel -> {
Optional<ComponentBuildingDefinition<?>> buildingDefinition = definitionRegistry.getBuildingDefinition(componentModel.getIdentifier());
buildingDefinition.ifPresent(componentBuildingDefinition -> {
action.accept(componentModel, componentBuildingDefinition);
});
});
}
}
use of org.mule.runtime.dsl.api.component.ComponentBuildingDefinition in project mule by mulesoft.
the class ApplicationModel method resolveComponentTypes.
/**
* Resolves the types of each component model when possible.
*/
public void resolveComponentTypes() {
// TODO MULE-13894 enable this once changes are completed and no componentBuildingDefinition is needed
// checkState(componentBuildingDefinitionRegistry.isPresent(),
// "ApplicationModel was created without a " + ComponentBuildingDefinitionProvider.class.getName());
componentBuildingDefinitionRegistry.ifPresent(buildingDefinitionRegistry -> {
executeOnEveryComponentTree(componentModel -> {
Optional<ComponentBuildingDefinition<?>> buildingDefinition = buildingDefinitionRegistry.getBuildingDefinition(componentModel.getIdentifier());
buildingDefinition.map(definition -> {
ObjectTypeVisitor typeDefinitionVisitor = new ObjectTypeVisitor(componentModel);
definition.getTypeDefinition().visit(typeDefinitionVisitor);
componentModel.setType(typeDefinitionVisitor.getType());
return definition;
}).orElseGet(() -> {
String classParameter = componentModel.getParameters().get(CLASS_ATTRIBUTE);
if (classParameter != null) {
try {
componentModel.setType(ClassUtils.getClass(classParameter));
} catch (ClassNotFoundException e) {
throw new RuntimeConfigurationException(I18nMessageFactory.createStaticMessage(String.format("Could not resolve class '%s' for component '%s'", classParameter, componentModel.getComponentLocation())));
}
}
return null;
});
});
});
}
use of org.mule.runtime.dsl.api.component.ComponentBuildingDefinition in project mule by mulesoft.
the class SpringPostProcessorIocHelper method addDefinitionPropertyValue.
@Override
public void addDefinitionPropertyValue(String propertyName, Class beanClass, Map<String, Object> properties, boolean addContext, Object... constructorArgs) {
BeanDefinitionBuilder builder;
if (ObjectFactory.class.isAssignableFrom(beanClass)) {
final ComponentBuildingDefinition definition = new ComponentBuildingDefinition.Builder().withTypeDefinition(fromType(Object.class)).withObjectFactoryType(beanClass).withNamespace("helper").withIdentifier(propertyName).build();
builder = genericBeanDefinition(objectFactoryClassRepository.getObjectFactoryClass(definition, beanClass, Object.class, () -> true, empty()));
} else {
builder = genericBeanDefinition(beanClass);
}
for (Object constructorArg : constructorArgs) {
builder = builder.addConstructorArgValue(constructorArg);
}
if (addContext) {
builder = builder.addConstructorArgReference(OBJECT_MULE_CONTEXT);
}
for (Entry<String, Object> propertyEntry : properties.entrySet()) {
builder = builder.addPropertyValue(propertyEntry.getKey(), propertyEntry.getValue());
}
beanDefinition.getPropertyValues().addPropertyValue(propertyName, builder.getBeanDefinition());
}
Aggregations