Search in sources :

Example 6 with SpringComponentModel

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

the class MapEntryBeanDefinitionCreator method handleRequest.

@Override
boolean handleRequest(CreateBeanDefinitionRequest createBeanDefinitionRequest) {
    ObjectTypeVisitor objectTypeVisitor = new ObjectTypeVisitor(createBeanDefinitionRequest.getComponentModel());
    createBeanDefinitionRequest.getComponentBuildingDefinition().getTypeDefinition().visit(objectTypeVisitor);
    Class<?> type = objectTypeVisitor.getType();
    if (!(MapEntryType.class.isAssignableFrom(type))) {
        return false;
    }
    SpringComponentModel componentModel = createBeanDefinitionRequest.getComponentModel();
    ComponentBuildingDefinition componentBuildingDefinition = createBeanDefinitionRequest.getComponentBuildingDefinition();
    componentModel.setType(type);
    final Object key = componentModel.getParameters().get(ENTRY_TYPE_KEY_PARAMETER_NAME);
    Object keyBeanDefinition = getConvertibleBeanDefinition(objectTypeVisitor.getMapEntryType().get().getKeyType(), key, componentBuildingDefinition.getKeyTypeConverter());
    Object value = null;
    // MULE-11984: Check that generated map entries are not empty
    if (componentModel.getParameters().get(ENTRY_TYPE_VALUE_REF_PARAMETER_NAME) != null) {
        value = new RuntimeBeanReference(componentModel.getParameters().get(ENTRY_TYPE_VALUE_REF_PARAMETER_NAME));
    } else {
        value = getValue(objectTypeVisitor, componentModel, componentBuildingDefinition);
    }
    AbstractBeanDefinition beanDefinition = genericBeanDefinition(MapEntry.class).addConstructorArgValue(keyBeanDefinition).addConstructorArgValue(value).getBeanDefinition();
    componentModel.setBeanDefinition(beanDefinition);
    return true;
}
Also used : MapEntry(org.mule.runtime.dsl.api.component.MapEntry) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) ComponentBuildingDefinition(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ObjectTypeVisitor(org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor)

Example 7 with SpringComponentModel

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

the class ObjectBeanDefinitionCreator method handleRequest.

@Override
boolean handleRequest(CreateBeanDefinitionRequest createBeanDefinitionRequest) {
    SpringComponentModel componentModel = createBeanDefinitionRequest.getComponentModel();
    if (!componentModel.getIdentifier().equals(buildFromStringRepresentation("mule:object"))) {
        return false;
    }
    String refParameterValue = componentModel.getParameters().get(REF_PARAMETER);
    String classParameterValue = componentModel.getParameters().get(CLASS_PARAMETER);
    if (refParameterValue != null && classParameterValue != null) {
        throw new RuntimeConfigurationException(createStaticMessage(format("Object cannot contain both '%s' and '%s' parameters. Offending resource is '%s'", REF_PARAMETER, CLASS_PARAMETER, componentModel.getComponentLocation())));
    }
    if (refParameterValue == null && classParameterValue == null) {
        throw new RuntimeConfigurationException(createStaticMessage(format("Object must contain '%s' or '%s' parameter. Offending resource is '%s'", REF_PARAMETER, CLASS_PARAMETER, componentModel.getComponentLocation())));
    }
    if (refParameterValue != null) {
        componentModel.setBeanReference(new RuntimeBeanReference(refParameterValue));
    }
    if (classParameterValue != null) {
        BeanDefinitionBuilder beanDefinitionBuilder;
        final Class<?> classParameter;
        try {
            classParameter = ClassUtils.getClass(classParameterValue);
        } catch (ClassNotFoundException e) {
            throw new RuntimeConfigurationException(createStaticMessage(format("Could not resolve class '%s' for component '%s'", classParameterValue, componentModel.getComponentLocation())));
        }
        beanDefinitionBuilder = rootBeanDefinition(addAnnotationsToClass(classParameter));
        processMuleProperties(componentModel, beanDefinitionBuilder, null);
        componentModel.setBeanDefinition(beanDefinitionBuilder.getBeanDefinition());
    }
    return true;
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) RuntimeConfigurationException(org.mule.runtime.config.internal.dsl.model.config.RuntimeConfigurationException) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 8 with SpringComponentModel

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

the class ObjectTypeVisitorTestCase method typeIsInstanceOfCheckedClassFromAttribute.

@Test
public void typeIsInstanceOfCheckedClassFromAttribute() throws ClassNotFoundException {
    ComponentModel componentModel = new SpringComponentModel();
    componentModel.setParameter("type", "org.mule.runtime.core.internal.processor.ReferenceProcessor");
    ObjectTypeVisitor visitor = new ObjectTypeVisitor(componentModel);
    TypeDefinition typeDefinition = fromConfigurationAttribute("type").checkingThatIsClassOrInheritsFrom(ReferenceProcessor.class);
    typeDefinition.visit(visitor);
    assertTrue(ReferenceProcessor.class.isAssignableFrom(visitor.getType()));
}
Also used : ReferenceProcessor(org.mule.runtime.core.internal.processor.ReferenceProcessor) 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) ObjectTypeVisitor(org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor) TypeDefinition(org.mule.runtime.dsl.api.component.TypeDefinition) Test(org.junit.Test)

Example 9 with SpringComponentModel

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

the class ObjectTypeVisitorTestCase method typeIsInstanceOfClassInheritedFromCheckedClassFromAttribute.

@Test
public void typeIsInstanceOfClassInheritedFromCheckedClassFromAttribute() throws ClassNotFoundException {
    ComponentModel componentModel = new SpringComponentModel();
    componentModel.setParameter("type", "org.mule.runtime.core.internal.processor.ReferenceProcessor");
    ObjectTypeVisitor visitor = new ObjectTypeVisitor(componentModel);
    // Check that ReferenceProcessor inherits from AbstractProcessor
    TypeDefinition typeDefinition = fromConfigurationAttribute("type").checkingThatIsClassOrInheritsFrom(AbstractProcessor.class);
    typeDefinition.visit(visitor);
    assertTrue(AbstractProcessor.class.isAssignableFrom(visitor.getType()));
}
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) AbstractProcessor(org.mule.runtime.core.internal.processor.AbstractProcessor) ObjectTypeVisitor(org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor) TypeDefinition(org.mule.runtime.dsl.api.component.TypeDefinition) Test(org.junit.Test)

Example 10 with SpringComponentModel

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

the class ObjectTypeVisitorTestCase method typeIsInstanceOfGivenClassFromAttribute.

@Test
public void typeIsInstanceOfGivenClassFromAttribute() throws ClassNotFoundException {
    ComponentModel componentModel = new SpringComponentModel();
    componentModel.setParameter("type", "org.mule.runtime.core.internal.processor.ReferenceProcessor");
    ObjectTypeVisitor visitor = new ObjectTypeVisitor(componentModel);
    TypeDefinition typeDefinition = fromConfigurationAttribute("type");
    typeDefinition.visit(visitor);
    assertTrue(ReferenceProcessor.class.isAssignableFrom(visitor.getType()));
}
Also used : ReferenceProcessor(org.mule.runtime.core.internal.processor.ReferenceProcessor) 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) ObjectTypeVisitor(org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor) TypeDefinition(org.mule.runtime.dsl.api.component.TypeDefinition) Test(org.junit.Test)

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