Search in sources :

Example 1 with SetterAttributeDefinition

use of org.mule.runtime.dsl.api.component.SetterAttributeDefinition in project mule by mulesoft.

the class ComponentConfigurationBuilder method processConfiguration.

public void processConfiguration() {
    componentBuildingDefinition.getIgnoredConfigurationParameters().stream().forEach(simpleParameters::remove);
    for (SetterAttributeDefinition setterAttributeDefinition : componentBuildingDefinition.getSetterParameterDefinitions()) {
        AttributeDefinition attributeDefinition = setterAttributeDefinition.getAttributeDefinition();
        attributeDefinition.accept(setterVisitor(setterAttributeDefinition.getAttributeName(), attributeDefinition));
    }
    for (AttributeDefinition attributeDefinition : componentBuildingDefinition.getConstructorAttributeDefinition()) {
        attributeDefinition.accept(constructorVisitor());
    }
}
Also used : SetterAttributeDefinition(org.mule.runtime.dsl.api.component.SetterAttributeDefinition) AttributeDefinition(org.mule.runtime.dsl.api.component.AttributeDefinition) SetterAttributeDefinition(org.mule.runtime.dsl.api.component.SetterAttributeDefinition)

Example 2 with SetterAttributeDefinition

use of org.mule.runtime.dsl.api.component.SetterAttributeDefinition 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);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) ObjectProvider(org.mule.runtime.api.ioc.ObjectProvider) BeanUtils.setProperty(org.apache.commons.beanutils.BeanUtils.setProperty) ConstantFactoryBean(org.mule.runtime.config.internal.factories.ConstantFactoryBean) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) SetterAttributeDefinition(org.mule.runtime.dsl.api.component.SetterAttributeDefinition) AbstractAttributeDefinitionVisitor(org.mule.runtime.config.api.dsl.processor.AbstractAttributeDefinitionVisitor) String.format(java.lang.String.format) ComponentBuildingDefinition(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition) Map(java.util.Map) Optional(java.util.Optional) BeanDefinitionBuilder.rootBeanDefinition(org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) SetterAttributeDefinition(org.mule.runtime.dsl.api.component.SetterAttributeDefinition) AbstractAttributeDefinitionVisitor(org.mule.runtime.config.api.dsl.processor.AbstractAttributeDefinitionVisitor) ComponentBuildingDefinition(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition)

Aggregations

SetterAttributeDefinition (org.mule.runtime.dsl.api.component.SetterAttributeDefinition)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 String.format (java.lang.String.format)1 Map (java.util.Map)1 Optional (java.util.Optional)1 BeanUtils.setProperty (org.apache.commons.beanutils.BeanUtils.setProperty)1 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)1 ObjectProvider (org.mule.runtime.api.ioc.ObjectProvider)1 AbstractAttributeDefinitionVisitor (org.mule.runtime.config.api.dsl.processor.AbstractAttributeDefinitionVisitor)1 SpringComponentModel (org.mule.runtime.config.internal.dsl.model.SpringComponentModel)1 ConstantFactoryBean (org.mule.runtime.config.internal.factories.ConstantFactoryBean)1 AttributeDefinition (org.mule.runtime.dsl.api.component.AttributeDefinition)1 ComponentBuildingDefinition (org.mule.runtime.dsl.api.component.ComponentBuildingDefinition)1 BeanDefinitionBuilder.rootBeanDefinition (org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition)1