use of org.mule.runtime.dsl.api.component.ComponentBuildingDefinition in project mule by mulesoft.
the class BeanDefinitionFactory method getWrapperIdentifierAndTypeMap.
private <T> Map<String, WrapperElementType> getWrapperIdentifierAndTypeMap(ComponentBuildingDefinition<T> buildingDefinition) {
final Map<String, WrapperElementType> wrapperIdentifierAndTypeMap = new HashMap<>();
AbstractAttributeDefinitionVisitor wrapperIdentifiersCollector = new AbstractAttributeDefinitionVisitor() {
@Override
public void onComplexChildCollection(Class<?> type, Optional<String> wrapperIdentifierOptional) {
wrapperIdentifierOptional.ifPresent(wrapperIdentifier -> wrapperIdentifierAndTypeMap.put(wrapperIdentifier, COLLECTION));
}
@Override
public void onComplexChild(Class<?> type, Optional<String> wrapperIdentifierOptional, Optional<String> childIdentifier) {
wrapperIdentifierOptional.ifPresent(wrapperIdentifier -> wrapperIdentifierAndTypeMap.put(wrapperIdentifier, SINGLE));
}
@Override
public void onComplexChildMap(Class<?> keyType, Class<?> valueType, String wrapperIdentifier) {
wrapperIdentifierAndTypeMap.put(wrapperIdentifier, MAP);
}
@Override
public void onMultipleValues(KeyAttributeDefinitionPair[] definitions) {
for (KeyAttributeDefinitionPair attributeDefinition : definitions) {
attributeDefinition.getAttributeDefinition().accept(this);
}
}
};
Consumer<AttributeDefinition> collectWrappersConsumer = attributeDefinition -> attributeDefinition.accept(wrapperIdentifiersCollector);
buildingDefinition.getSetterParameterDefinitions().stream().map(setterAttributeDefinition -> setterAttributeDefinition.getAttributeDefinition()).forEach(collectWrappersConsumer);
buildingDefinition.getConstructorAttributeDefinition().stream().forEach(collectWrappersConsumer);
return wrapperIdentifierAndTypeMap;
}
use of org.mule.runtime.dsl.api.component.ComponentBuildingDefinition in project mule by mulesoft.
the class CollectionBeanDefinitionCreator method handleRequest.
@Override
boolean handleRequest(CreateBeanDefinitionRequest createBeanDefinitionRequest) {
SpringComponentModel componentModel = (SpringComponentModel) createBeanDefinitionRequest.getComponentModel();
ComponentBuildingDefinition componentBuildingDefinition = createBeanDefinitionRequest.getComponentBuildingDefinition();
ObjectTypeVisitor objectTypeVisitor = new ObjectTypeVisitor(componentModel);
componentBuildingDefinition.getTypeDefinition().visit(objectTypeVisitor);
if (Collection.class.isAssignableFrom(objectTypeVisitor.getType())) {
componentModel.setType(objectTypeVisitor.getType());
ManagedList<Object> managedList = new ManagedList<>();
for (ComponentModel innerComponent : componentModel.getInnerComponents()) {
SpringComponentModel innerSpringComp = (SpringComponentModel) innerComponent;
Object bean = innerSpringComp.getBeanDefinition() == null ? innerSpringComp.getBeanReference() : innerSpringComp.getBeanDefinition();
managedList.add(bean);
}
componentModel.setBeanDefinition(BeanDefinitionBuilder.genericBeanDefinition(objectTypeVisitor.getType()).addConstructorArgValue(managedList).getBeanDefinition());
return true;
}
return false;
}
use of org.mule.runtime.dsl.api.component.ComponentBuildingDefinition in project mule by mulesoft.
the class CommonBeanDefinitionCreator method createBeanDefinitionBuilderFromObjectFactory.
private BeanDefinitionBuilder createBeanDefinitionBuilderFromObjectFactory(final SpringComponentModel componentModel, final ComponentBuildingDefinition componentBuildingDefinition) {
ObjectTypeVisitor objectTypeVisitor = new ObjectTypeVisitor(componentModel);
componentBuildingDefinition.getTypeDefinition().visit(objectTypeVisitor);
Class<?> objectFactoryType = componentBuildingDefinition.getObjectFactoryType();
Optional<Consumer<Object>> instanceCustomizationFunctionOptional;
Map<String, Object> customProperties = getTransformerCustomProperties(componentModel);
if (customProperties.isEmpty()) {
instanceCustomizationFunctionOptional = empty();
} else {
instanceCustomizationFunctionOptional = of(object -> injectSpringProperties(customProperties, object));
}
return rootBeanDefinition(objectFactoryClassRepository.getObjectFactoryClass(componentBuildingDefinition, objectFactoryType, objectTypeVisitor.getType(), new LazyValue<>(() -> componentModel.getBeanDefinition().isLazyInit()), instanceCustomizationFunctionOptional));
}
use of org.mule.runtime.dsl.api.component.ComponentBuildingDefinition 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.dsl.api.component.ComponentBuildingDefinition in project mule by mulesoft.
the class MapBeanDefinitionCreator method handleRequest.
@Override
boolean handleRequest(CreateBeanDefinitionRequest createBeanDefinitionRequest) {
SpringComponentModel componentModel = createBeanDefinitionRequest.getComponentModel();
ObjectTypeVisitor objectTypeVisitor = new ObjectTypeVisitor(componentModel);
ComponentBuildingDefinition componentBuildingDefinition = createBeanDefinitionRequest.getComponentBuildingDefinition();
componentBuildingDefinition.getTypeDefinition().visit(objectTypeVisitor);
Class<?> type = objectTypeVisitor.getType();
if (Map.class.isAssignableFrom(type) && componentBuildingDefinition.getObjectFactoryType() == null) {
ManagedList managedList = componentModel.getInnerComponents().stream().map(c -> ((SpringComponentModel) c).getBeanDefinition()).collect(toCollection(ManagedList::new));
componentModel.setBeanDefinition(BeanDefinitionBuilder.genericBeanDefinition(MapFactoryBean.class).addConstructorArgValue(managedList).addConstructorArgValue(type).getBeanDefinition());
return true;
}
return false;
}
Aggregations