Search in sources :

Example 1 with SpringComponentModel

use of org.mule.runtime.config.internal.dsl.model.SpringComponentModel 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;
}
Also used : SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) ComponentModel(org.mule.runtime.config.internal.model.ComponentModel) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) ManagedList(org.springframework.beans.factory.support.ManagedList) ComponentBuildingDefinition(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition) ObjectTypeVisitor(org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor)

Example 2 with SpringComponentModel

use of org.mule.runtime.config.internal.dsl.model.SpringComponentModel 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));
}
Also used : BeanDefinitionPostProcessor(org.mule.runtime.config.privileged.dsl.BeanDefinitionPostProcessor) Optional.of(java.util.Optional.of) CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) SecurityFilter(org.mule.runtime.core.api.security.SecurityFilter) MuleApplicationClassLoader.resolveContextArtifactPluginClassLoaders(org.mule.runtime.deployment.model.internal.application.MuleApplicationClassLoader.resolveContextArtifactPluginClassLoaders) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) ROOT_MACRO_EXPANDED_FLOW_CONTAINER_NAME(org.mule.runtime.config.internal.dsl.model.extension.xml.MacroExpansionModuleModel.ROOT_MACRO_EXPANDED_FLOW_CONTAINER_NAME) SpiServiceRegistry(org.mule.runtime.core.api.registry.SpiServiceRegistry) MULE_PROPERTY_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.MULE_PROPERTY_IDENTIFIER) Collectors.toMap(java.util.stream.Collectors.toMap) SecurityFilterMessageProcessor(org.mule.runtime.core.privileged.processor.SecurityFilterMessageProcessor) Map(java.util.Map) CUSTOM_TRANSFORMER_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.CUSTOM_TRANSFORMER_IDENTIFIER) Pair(org.mule.runtime.api.util.Pair) ANNOTATIONS_ELEMENT_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.ANNOTATIONS_ELEMENT_IDENTIFIER) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) SPRING_PROTOTYPE_OBJECT(org.mule.runtime.config.internal.dsl.spring.BeanDefinitionFactory.SPRING_PROTOTYPE_OBJECT) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ProcessingStrategy(org.mule.runtime.core.api.processor.strategy.ProcessingStrategy) XmlCustomAttributeHandler(org.mule.runtime.config.internal.dsl.processor.xml.XmlCustomAttributeHandler) ComponentBuildingDefinition(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition) List(java.util.List) Optional(java.util.Optional) BeanUtils.copyProperty(org.apache.commons.beanutils.BeanUtils.copyProperty) QName(javax.xml.namespace.QName) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Optional.empty(java.util.Optional.empty) XmlCustomAttributeHandler.from(org.mule.runtime.config.internal.dsl.processor.xml.XmlCustomAttributeHandler.from) DefaultMessageProcessorChainBuilder(org.mule.runtime.core.privileged.processor.chain.DefaultMessageProcessorChainBuilder) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanDefinitionBuilder.genericBeanDefinition(org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition) HashMap(java.util.HashMap) ANNOTATIONS_PROPERTY_NAME(org.mule.runtime.api.component.Component.ANNOTATIONS_PROPERTY_NAME) XmlMetadataAnnotations(org.mule.runtime.config.internal.parsers.XmlMetadataAnnotations) ArrayList(java.util.ArrayList) Component(org.mule.runtime.api.component.Component) Node(org.w3c.dom.Node) ServiceConfigurationError(java.util.ServiceConfigurationError) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ModuleOperationMessageProcessorChainFactoryBean(org.mule.runtime.config.internal.factories.ModuleOperationMessageProcessorChainFactoryBean) Collections.emptyMap(java.util.Collections.emptyMap) MULE_PROPERTIES_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.MULE_PROPERTIES_IDENTIFIER) PropertyComponentUtils.getPropertyValueFromPropertyComponent(org.mule.runtime.config.internal.dsl.spring.PropertyComponentUtils.getPropertyValueFromPropertyComponent) LocationExecutionContextProvider.addMetadataAnnotationsFromXml(org.mule.runtime.core.privileged.execution.LocationExecutionContextProvider.addMetadataAnnotationsFromXml) ObjectTypeVisitor(org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Consumer(java.util.function.Consumer) LazyValue(org.mule.runtime.api.util.LazyValue) Element(org.w3c.dom.Element) ComponentModel(org.mule.runtime.config.internal.model.ComponentModel) BeanDefinitionBuilder.rootBeanDefinition(org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) LazyValue(org.mule.runtime.api.util.LazyValue) Consumer(java.util.function.Consumer) ObjectTypeVisitor(org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor)

Example 3 with SpringComponentModel

use of org.mule.runtime.config.internal.dsl.model.SpringComponentModel 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)

Example 4 with SpringComponentModel

use of org.mule.runtime.config.internal.dsl.model.SpringComponentModel in project mule by mulesoft.

the class ExceptionStrategyRefBeanDefinitionCreator method handleRequest.

@Override
boolean handleRequest(CreateBeanDefinitionRequest createBeanDefinitionRequest) {
    ComponentModel componentModel = createBeanDefinitionRequest.getComponentModel();
    if (componentModel.getIdentifier().equals(EXCEPTION_STRATEGY_REFERENCE_IDENTIFIER)) {
        componentModel.setType(FlowExceptionHandler.class);
        ((SpringComponentModel) componentModel).setBeanReference(new RuntimeBeanReference(componentModel.getParameters().get(REFERENCE_ATTRIBUTE)));
        return true;
    }
    return false;
}
Also used : SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) ComponentModel(org.mule.runtime.config.internal.model.ComponentModel) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 5 with SpringComponentModel

use of org.mule.runtime.config.internal.dsl.model.SpringComponentModel 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;
}
Also used : Collectors.toCollection(java.util.stream.Collectors.toCollection) ManagedList(org.springframework.beans.factory.support.ManagedList) ComponentBuildingDefinition(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) Map(java.util.Map) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) ObjectTypeVisitor(org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) ManagedList(org.springframework.beans.factory.support.ManagedList) ComponentBuildingDefinition(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition) Map(java.util.Map) ObjectTypeVisitor(org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor)

Aggregations

SpringComponentModel (org.mule.runtime.config.internal.dsl.model.SpringComponentModel)20 ComponentModel (org.mule.runtime.config.internal.model.ComponentModel)11 ObjectTypeVisitor (org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor)10 ComponentBuildingDefinition (org.mule.runtime.dsl.api.component.ComponentBuildingDefinition)8 Map (java.util.Map)6 ManagedList (org.springframework.beans.factory.support.ManagedList)6 Optional (java.util.Optional)5 Test (org.junit.Test)5 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)5 TypeDefinition (org.mule.runtime.dsl.api.component.TypeDefinition)5 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 HashMap (java.util.HashMap)4 List (java.util.List)4 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)4 String.format (java.lang.String.format)3 Optional.of (java.util.Optional.of)3 Set (java.util.Set)3 Consumer (java.util.function.Consumer)3 Collectors.toList (java.util.stream.Collectors.toList)3