Search in sources :

Example 11 with SpringComponentModel

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

the class SpringPostProcessorIocHelper method toBeanDefinitions.

@Override
public List toBeanDefinitions(List<ComponentConfiguration> components) {
    ManagedList responseMessageProcessorsBeanList = new ManagedList();
    components.forEach(responseProcessorComponentModel -> {
        SpringComponentModel springComponentModel = (SpringComponentModel) responseProcessorComponentModel.getProperty(COMPONENT_MODEL_KEY).orElseThrow(() -> new MuleRuntimeException(createStaticMessage("Component configuration is expected to have the component model at this point.")));
        BeanDefinition beanDefinition = springComponentModel.getBeanDefinition();
        responseMessageProcessorsBeanList.add(beanDefinition != null ? beanDefinition : springComponentModel.getBeanReference());
    });
    return responseMessageProcessorsBeanList;
}
Also used : SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ManagedList(org.springframework.beans.factory.support.ManagedList) BeanDefinitionBuilder.genericBeanDefinition(org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeanDefinitionBuilder.rootBeanDefinition(org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition)

Example 12 with SpringComponentModel

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

the class BeanDefinitionFactory method processComponentWrapper.

private void processComponentWrapper(SpringComponentModel componentModel) {
    ComponentBuildingDefinition<?> parentBuildingDefinition = componentBuildingDefinitionRegistry.getBuildingDefinition(componentModel.getParent().getIdentifier()).get();
    Map<String, WrapperElementType> wrapperIdentifierAndTypeMap = getWrapperIdentifierAndTypeMap(parentBuildingDefinition);
    WrapperElementType wrapperElementType = wrapperIdentifierAndTypeMap.get(componentModel.getIdentifier().getName());
    if (wrapperElementType.equals(SINGLE)) {
        if (componentModel.getInnerComponents().isEmpty()) {
            String location = componentModel.getComponentLocation() != null ? componentModel.getComponentLocation().getLocation() : "";
            throw new IllegalStateException(format("Element [%s] located at [%s] does not have any child element declared, but one is required.", componentModel.getIdentifier(), location));
        }
        final SpringComponentModel firstComponentModel = (SpringComponentModel) componentModel.getInnerComponents().get(0);
        componentModel.setType(firstComponentModel.getType());
        componentModel.setBeanDefinition(firstComponentModel.getBeanDefinition());
        componentModel.setBeanReference(firstComponentModel.getBeanReference());
    } else {
        throw new IllegalStateException(format("Element %s does not have a building definition and it should since it's of type %s", componentModel.getIdentifier(), wrapperElementType));
    }
}
Also used : SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel)

Example 13 with SpringComponentModel

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

the class BeanDefinitionFactory method resolveComponent.

private BeanDefinition resolveComponent(ComponentModel parentComponentModel, SpringComponentModel componentModel, BeanDefinitionRegistry registry, BiConsumer<ComponentModel, BeanDefinitionRegistry> componentDefinitionModelProcessor, SpringConfigurationComponentLocator componentLocator) {
    if (isComponentIgnored(componentModel.getIdentifier())) {
        return null;
    }
    if (!componentModel.isEnabled()) {
        // Just register the location, for support of lazyInit scenarios
        componentLocator.addComponentLocation(componentModel.getComponentLocation());
        return null;
    }
    resolveComponentBeanDefinition(parentComponentModel, componentModel);
    componentDefinitionModelProcessor.accept(componentModel, registry);
    // TODO MULE-9638: Once we migrate all core definitions we need to define a mechanism for customizing
    // how core constructs are processed.
    processMuleConfiguration(componentModel, registry);
    processMuleSecurityManager(componentModel, registry);
    processRaiseError(componentModel);
    componentBuildingDefinitionRegistry.getBuildingDefinition(componentModel.getIdentifier()).ifPresent(componentBuildingDefinition -> {
        if ((componentModel.getType() != null) && Component.class.isAssignableFrom(componentModel.getType())) {
            addAnnotation(ANNOTATION_NAME, componentModel.getIdentifier(), componentModel);
            // We need to use a mutable map since spring will resolve the properties placeholder present in the value if needed
            // and it will be done by mutating the same map.
            addAnnotation(ANNOTATION_PARAMETERS, new HashMap<>(componentModel.getParameters()), componentModel);
            // add any error mappings if present
            List<ComponentModel> errorMappingComponents = componentModel.getInnerComponents().stream().filter(innerComponent -> ERROR_MAPPING_IDENTIFIER.equals(innerComponent.getIdentifier())).collect(toList());
            if (!errorMappingComponents.isEmpty()) {
                addAnnotation(ANNOTATION_ERROR_MAPPINGS, errorMappingComponents.stream().map(innerComponent -> {
                    Map<String, String> parameters = innerComponent.getParameters();
                    ComponentIdentifier source = buildFromStringRepresentation(parameters.get(SOURCE_TYPE));
                    ErrorType errorType = errorTypeRepository.lookupErrorType(source).orElseThrow(() -> new MuleRuntimeException(createStaticMessage("Could not find error '%s'.", source)));
                    ErrorTypeMatcher errorTypeMatcher = new SingleErrorTypeMatcher(errorType);
                    ErrorType targetValue = resolveErrorType(parameters.get(TARGET_TYPE));
                    return new ErrorMapping(errorTypeMatcher, targetValue);
                }).collect(toList()), componentModel);
            }
            componentLocator.addComponentLocation(componentModel.getComponentLocation());
        }
    });
    addAnnotation(LOCATION_KEY, componentModel.getComponentLocation(), componentModel);
    BeanDefinition beanDefinition = componentModel.getBeanDefinition();
    return beanDefinition;
}
Also used : KeyAttributeDefinitionPair(org.mule.runtime.dsl.api.component.KeyAttributeDefinitionPair) CORE_PREFIX(org.mule.runtime.internal.dsl.DslConstants.CORE_PREFIX) BeanReference(org.springframework.beans.factory.config.BeanReference) Optional.of(java.util.Optional.of) BiFunction(java.util.function.BiFunction) ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) DESCRIPTION_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.DESCRIPTION_IDENTIFIER) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) SECURITY_MANAGER_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.SECURITY_MANAGER_IDENTIFIER) MULE_PROPERTY_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.MULE_PROPERTY_IDENTIFIER) SingleErrorTypeMatcher(org.mule.runtime.core.api.exception.SingleErrorTypeMatcher) MVELExpressionLanguage(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage) Map(java.util.Map) MULE_EE_DOMAIN_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.MULE_EE_DOMAIN_IDENTIFIER) DEFAULT_OBJECT_SERIALIZER_NAME(org.mule.runtime.api.serialization.ObjectSerializer.DEFAULT_OBJECT_SERIALIZER_NAME) RAISE_ERROR_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.RAISE_ERROR_IDENTIFIER) MULE_DOMAIN_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.MULE_DOMAIN_IDENTIFIER) ANNOTATIONS_ELEMENT_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.ANNOTATIONS_ELEMENT_IDENTIFIER) ERROR_MAPPING_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.ERROR_MAPPING_IDENTIFIER) ImmutableSet(com.google.common.collect.ImmutableSet) SpringConfigurationComponentLocator(org.mule.runtime.config.internal.SpringConfigurationComponentLocator) Set(java.util.Set) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ServiceLoader(java.util.ServiceLoader) CommonBeanDefinitionCreator.areMatchingTypes(org.mule.runtime.config.internal.dsl.spring.CommonBeanDefinitionCreator.areMatchingTypes) String.format(java.lang.String.format) ComponentBuildingDefinition(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition) List(java.util.List) ErrorType(org.mule.runtime.api.message.ErrorType) Optional(java.util.Optional) ANNOTATION_NAME(org.mule.runtime.core.internal.component.ComponentAnnotations.ANNOTATION_NAME) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) ANNOTATION_PARAMETERS(org.mule.runtime.core.internal.component.ComponentAnnotations.ANNOTATION_PARAMETERS) DOC_DESCRIPTION_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.DOC_DESCRIPTION_IDENTIFIER) RetryPolicyTemplate(org.mule.runtime.core.api.retry.policy.RetryPolicyTemplate) ComponentBuildingDefinitionRegistry(org.mule.runtime.config.api.dsl.model.ComponentBuildingDefinitionRegistry) OBJECT_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.OBJECT_IDENTIFIER) ErrorMapping(org.mule.runtime.core.internal.exception.ErrorMapping) ComponentIdentifier.buildFromStringRepresentation(org.mule.runtime.api.component.ComponentIdentifier.buildFromStringRepresentation) OBJECT_EXPRESSION_LANGUAGE(org.mule.runtime.core.api.config.MuleProperties.OBJECT_EXPRESSION_LANGUAGE) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) MAP(org.mule.runtime.config.internal.dsl.spring.WrapperElementType.MAP) MULE_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.MULE_IDENTIFIER) SINGLE(org.mule.runtime.config.internal.dsl.spring.WrapperElementType.SINGLE) COLLECTION(org.mule.runtime.config.internal.dsl.spring.WrapperElementType.COLLECTION) Component(org.mule.runtime.api.component.Component) BiConsumer(java.util.function.BiConsumer) ComponentModelHelper.addAnnotation(org.mule.runtime.config.internal.dsl.spring.ComponentModelHelper.addAnnotation) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) OBJECT_DEFAULT_RETRY_POLICY_TEMPLATE(org.mule.runtime.core.api.config.MuleProperties.OBJECT_DEFAULT_RETRY_POLICY_TEMPLATE) LOCATION_KEY(org.mule.runtime.api.component.AbstractComponent.LOCATION_KEY) MULE_PROPERTIES_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.MULE_PROPERTIES_IDENTIFIER) GLOBAL_PROPERTY_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.GLOBAL_PROPERTY_IDENTIFIER) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) AttributeDefinition(org.mule.runtime.dsl.api.component.AttributeDefinition) AbstractAttributeDefinitionVisitor(org.mule.runtime.config.api.dsl.processor.AbstractAttributeDefinitionVisitor) ErrorTypeMatcher(org.mule.runtime.core.api.exception.ErrorTypeMatcher) Consumer(java.util.function.Consumer) Either(org.mule.runtime.core.api.functional.Either) Collectors.toList(java.util.stream.Collectors.toList) ConfigurationPropertiesProviderFactory(org.mule.runtime.config.api.dsl.model.properties.ConfigurationPropertiesProviderFactory) Element(org.w3c.dom.Element) ComponentModel(org.mule.runtime.config.internal.model.ComponentModel) CONFIGURATION_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.CONFIGURATION_IDENTIFIER) ANNOTATION_ERROR_MAPPINGS(org.mule.runtime.core.internal.exception.ErrorMapping.ANNOTATION_ERROR_MAPPINGS) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) ErrorMapping(org.mule.runtime.core.internal.exception.ErrorMapping) SingleErrorTypeMatcher(org.mule.runtime.core.api.exception.SingleErrorTypeMatcher) ErrorTypeMatcher(org.mule.runtime.core.api.exception.ErrorTypeMatcher) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) SingleErrorTypeMatcher(org.mule.runtime.core.api.exception.SingleErrorTypeMatcher) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ErrorType(org.mule.runtime.api.message.ErrorType) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) ComponentModel(org.mule.runtime.config.internal.model.ComponentModel) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Component(org.mule.runtime.api.component.Component)

Example 14 with SpringComponentModel

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

the class CommonBeanDefinitionCreator method handleRequest.

@Override
public boolean handleRequest(CreateBeanDefinitionRequest request) {
    SpringComponentModel componentModel = request.getComponentModel();
    ComponentBuildingDefinition buildingDefinition = request.getComponentBuildingDefinition();
    componentModel.setType(retrieveComponentType(componentModel, buildingDefinition));
    BeanDefinitionBuilder beanDefinitionBuilder = createBeanDefinitionBuilder(componentModel, buildingDefinition);
    processAnnotations(componentModel, beanDefinitionBuilder);
    processComponentDefinitionModel(request.getParentComponentModel(), componentModel, buildingDefinition, beanDefinitionBuilder);
    return true;
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) ComponentBuildingDefinition(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition)

Example 15 with SpringComponentModel

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

the class ComponentConfigurationBuilder method collectComplexParametersWithTypes.

private List<ComponentValue> collectComplexParametersWithTypes(ComponentModel componentModel) {
    /*
     * TODO: MULE-9638 This ugly code is required since we need to get the object type from the bean definition. This code will go
     * away one we remove the old parsing method.
     */
    return componentModel.getInnerComponents().stream().filter(child -> child.isEnabled()).map(model -> {
        // When it comes from old model it does not have the type set
        Class<?> beanDefinitionType = model.getType();
        final SpringComponentModel springModel = (SpringComponentModel) model;
        if (beanDefinitionType == null) {
            if (springModel.getBeanDefinition() == null) {
                // Some component do not have a bean definition since the element parsing is ignored. i.e: annotations
                return null;
            } else {
                try {
                    String beanClassName = springModel.getBeanDefinition().getBeanClassName();
                    if (beanClassName != null) {
                        beanDefinitionType = org.apache.commons.lang3.ClassUtils.getClass(beanClassName);
                    } else {
                        // Happens in case of spring:property
                        beanDefinitionType = Object.class;
                    }
                } catch (ClassNotFoundException e) {
                    logger.debug("Exception trying to determine ComponentModel type: ", e);
                    beanDefinitionType = Object.class;
                }
            }
        }
        Object bean = springModel.getBeanDefinition() != null ? springModel.getBeanDefinition() : springModel.getBeanReference();
        return new ComponentValue(model, beanDefinitionType, bean);
    }).filter(beanDefinitionTypePair -> beanDefinitionTypePair != null).collect(toList());
}
Also used : KeyAttributeDefinitionPair(org.mule.runtime.dsl.api.component.KeyAttributeDefinitionPair) Logger(org.slf4j.Logger) ManagedMap(org.springframework.beans.factory.support.ManagedMap) Optional.ofNullable(java.util.Optional.ofNullable) Predicate(java.util.function.Predicate) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) LoggerFactory(org.slf4j.LoggerFactory) AttributeDefinition(org.mule.runtime.dsl.api.component.AttributeDefinition) HashMap(java.util.HashMap) SetterAttributeDefinition(org.mule.runtime.dsl.api.component.SetterAttributeDefinition) CommonBeanDefinitionCreator.areMatchingTypes(org.mule.runtime.config.internal.dsl.spring.CommonBeanDefinitionCreator.areMatchingTypes) AtomicReference(java.util.concurrent.atomic.AtomicReference) TypeConverter(org.mule.runtime.dsl.api.component.TypeConverter) Consumer(java.util.function.Consumer) ManagedList(org.springframework.beans.factory.support.ManagedList) Collectors.toList(java.util.stream.Collectors.toList) ComponentBuildingDefinition(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition) List(java.util.List) ComponentModel(org.mule.runtime.config.internal.model.ComponentModel) Map(java.util.Map) Optional(java.util.Optional) AttributeDefinitionVisitor(org.mule.runtime.dsl.api.component.AttributeDefinitionVisitor) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel)

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