use of org.mule.runtime.config.api.dsl.processor.AbstractAttributeDefinitionVisitor 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.config.api.dsl.processor.AbstractAttributeDefinitionVisitor 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);
}
Aggregations