Search in sources :

Example 6 with ObjectTypeVisitor

use of org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor in project mule by mulesoft.

the class ApplicationModel method resolveComponentTypes.

/**
 * Resolves the types of each component model when possible.
 */
public void resolveComponentTypes() {
    // TODO MULE-13894 enable this once changes are completed and no componentBuildingDefinition is needed
    // checkState(componentBuildingDefinitionRegistry.isPresent(),
    // "ApplicationModel was created without a " + ComponentBuildingDefinitionProvider.class.getName());
    componentBuildingDefinitionRegistry.ifPresent(buildingDefinitionRegistry -> {
        executeOnEveryComponentTree(componentModel -> {
            Optional<ComponentBuildingDefinition<?>> buildingDefinition = buildingDefinitionRegistry.getBuildingDefinition(componentModel.getIdentifier());
            buildingDefinition.map(definition -> {
                ObjectTypeVisitor typeDefinitionVisitor = new ObjectTypeVisitor(componentModel);
                definition.getTypeDefinition().visit(typeDefinitionVisitor);
                componentModel.setType(typeDefinitionVisitor.getType());
                return definition;
            }).orElseGet(() -> {
                String classParameter = componentModel.getParameters().get(CLASS_ATTRIBUTE);
                if (classParameter != null) {
                    try {
                        componentModel.setType(ClassUtils.getClass(classParameter));
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeConfigurationException(I18nMessageFactory.createStaticMessage(String.format("Could not resolve class '%s' for component '%s'", classParameter, componentModel.getComponentLocation())));
                    }
                }
                return null;
            });
        });
    });
}
Also used : ResourceProvider(org.mule.runtime.config.api.dsl.model.ResourceProvider) MacroExpansionModulesModel(org.mule.runtime.config.internal.dsl.model.extension.xml.MacroExpansionModulesModel) CORE_PREFIX(org.mule.runtime.internal.dsl.DslConstants.CORE_PREFIX) MULE_ROOT_ELEMENT(org.mule.runtime.config.api.dsl.CoreDslConstants.MULE_ROOT_ELEMENT) Optional.of(java.util.Optional.of) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ConfigurationProperty(org.mule.runtime.config.api.dsl.model.properties.ConfigurationProperty) Collections.singletonList(java.util.Collections.singletonList) ClassUtils(org.apache.commons.lang3.ClassUtils) DefaultConfigurationParameters(org.mule.runtime.config.internal.dsl.model.DefaultConfigurationParameters) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) MULE_EE_DOMAIN_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.MULE_EE_DOMAIN_IDENTIFIER) DslResolvingContext(org.mule.runtime.api.dsl.DslResolvingContext) DslElementModelFactory(org.mule.runtime.config.api.dsl.model.DslElementModelFactory) ERROR_HANDLER_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.ERROR_HANDLER_IDENTIFIER) ComponentModelReader(org.mule.runtime.config.internal.dsl.model.ComponentModelReader) CompositeConfigurationPropertiesProvider(org.mule.runtime.config.internal.dsl.model.config.CompositeConfigurationPropertiesProvider) PropertiesResolverConfigurationProperties(org.mule.runtime.config.internal.dsl.model.config.PropertiesResolverConfigurationProperties) EnvironmentPropertiesConfigurationProvider(org.mule.runtime.config.internal.dsl.model.config.EnvironmentPropertiesConfigurationProvider) MULE_DOMAIN_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.MULE_DOMAIN_IDENTIFIER) DEFAULT_EXPRESSION_PREFIX(org.mule.runtime.core.api.el.ExpressionManager.DEFAULT_EXPRESSION_PREFIX) ImmutableSet(com.google.common.collect.ImmutableSet) ComponentModelHelper.resolveComponentType(org.mule.runtime.config.internal.dsl.spring.ComponentModelHelper.resolveComponentType) MapConfigurationPropertiesProvider(org.mule.runtime.config.internal.dsl.model.config.MapConfigurationPropertiesProvider) TypedComponentIdentifier(org.mule.runtime.api.component.TypedComponentIdentifier) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) UNKNOWN(org.mule.runtime.api.component.TypedComponentIdentifier.ComponentType.UNKNOWN) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ServiceLoader(java.util.ServiceLoader) SOURCE_TYPE(org.mule.runtime.config.internal.dsl.spring.BeanDefinitionFactory.SOURCE_TYPE) NameValidationUtil.verifyStringDoesNotContainsReservedCharacters(org.mule.runtime.internal.util.NameValidationUtil.verifyStringDoesNotContainsReservedCharacters) String.format(java.lang.String.format) FileConfigurationPropertiesProvider(org.mule.runtime.config.internal.dsl.model.config.FileConfigurationPropertiesProvider) ERROR_HANDLER(org.mule.runtime.config.api.dsl.CoreDslConstants.ERROR_HANDLER) I18nMessageFactory(org.mule.runtime.api.i18n.I18nMessageFactory) XmlCustomAttributeHandler(org.mule.runtime.config.internal.dsl.processor.xml.XmlCustomAttributeHandler) ComponentBuildingDefinition(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition) List(java.util.List) ElementDeclaration(org.mule.runtime.app.declaration.api.ElementDeclaration) GlobalPropertyConfigurationPropertiesProvider(org.mule.runtime.config.internal.dsl.model.config.GlobalPropertyConfigurationPropertiesProvider) Optional(java.util.Optional) QName(javax.xml.namespace.QName) MuleExtensionModelProvider(org.mule.runtime.core.api.extension.MuleExtensionModelProvider) ComponentIdentifier.builder(org.mule.runtime.api.component.ComponentIdentifier.builder) Optional.empty(java.util.Optional.empty) ComponentBuildingDefinitionRegistry(org.mule.runtime.config.api.dsl.model.ComponentBuildingDefinitionRegistry) DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) LifecycleUtils.initialiseIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.initialiseIfNeeded) ConfigurationProperties(org.mule.runtime.api.component.ConfigurationProperties) ComponentConfiguration(org.mule.runtime.dsl.api.component.config.ComponentConfiguration) ComponentLocationVisitor(org.mule.runtime.config.internal.dsl.model.ComponentLocationVisitor) NameUtils.pluralize(org.mule.runtime.extension.api.util.NameUtils.pluralize) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MULE_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.MULE_IDENTIFIER) ConfigurationPropertiesProvider(org.mule.runtime.config.api.dsl.model.properties.ConfigurationPropertiesProvider) ArtifactDeclaration(org.mule.runtime.app.declaration.api.ArtifactDeclaration) Component(org.mule.runtime.api.component.Component) Node(org.w3c.dom.Node) BiConsumer(java.util.function.BiConsumer) FLOW_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.FLOW_IDENTIFIER) RuntimeConfigurationException(org.mule.runtime.config.internal.dsl.model.config.RuntimeConfigurationException) ConfigLine(org.mule.runtime.config.api.dsl.processor.ConfigLine) ConfigFile(org.mule.runtime.config.api.dsl.processor.ConfigFile) DefaultConfigurationProperty(org.mule.runtime.config.internal.dsl.model.config.DefaultConfigurationProperty) LinkedList(java.util.LinkedList) Collections.emptyMap(java.util.Collections.emptyMap) Collections.emptySet(java.util.Collections.emptySet) ConfigurationException(org.mule.runtime.core.api.config.ConfigurationException) Optional.ofNullable(java.util.Optional.ofNullable) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) NameUtils.hyphenize(org.mule.runtime.extension.api.util.NameUtils.hyphenize) ConfigurationParameters(org.mule.runtime.config.api.dsl.model.ConfigurationParameters) ExtensionModelHelper(org.mule.runtime.config.internal.dsl.model.ExtensionModelHelper) ArtifactConfig(org.mule.runtime.config.api.dsl.processor.ArtifactConfig) DefaultConfigurationPropertiesResolver(org.mule.runtime.config.internal.dsl.model.config.DefaultConfigurationPropertiesResolver) Joiner.on(com.google.common.base.Joiner.on) ObjectTypeVisitor(org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) ConfigurationPropertiesResolver(org.mule.runtime.config.internal.dsl.model.config.ConfigurationPropertiesResolver) FLOW_REF_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.FLOW_REF_IDENTIFIER) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) ConfigurationPropertiesProviderFactory(org.mule.runtime.config.api.dsl.model.properties.ConfigurationPropertiesProviderFactory) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) CollectionUtils.disjunction(org.apache.commons.collections.CollectionUtils.disjunction) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) ANY_IDENTIFIER(org.mule.runtime.core.api.exception.Errors.Identifiers.ANY_IDENTIFIER) RuntimeConfigurationException(org.mule.runtime.config.internal.dsl.model.config.RuntimeConfigurationException) ComponentBuildingDefinition(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition) ObjectTypeVisitor(org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor)

Example 7 with ObjectTypeVisitor

use of org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor 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 8 with ObjectTypeVisitor

use of org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor 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 9 with ObjectTypeVisitor

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

Example 10 with ObjectTypeVisitor

use of org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor in project mule by mulesoft.

the class SimpleTypeBeanDefinitionCreator method handleRequest.

@Override
boolean handleRequest(CreateBeanDefinitionRequest createBeanDefinitionRequest) {
    ObjectTypeVisitor objectTypeVisitor = new ObjectTypeVisitor(createBeanDefinitionRequest.getComponentModel());
    createBeanDefinitionRequest.getComponentBuildingDefinition().getTypeDefinition().visit(objectTypeVisitor);
    Class<?> type = objectTypeVisitor.getType();
    if (isSimpleType(type)) {
        SpringComponentModel componentModel = createBeanDefinitionRequest.getComponentModel();
        componentModel.setType(type);
        Map<String, String> parameters = componentModel.getParameters();
        if (parameters.size() >= 2) {
            // Component model has more than one parameter when it's supposed to have at most one parameter
            return false;
        }
        if (componentModel.getTextContent() != null && !componentModel.getParameters().isEmpty()) {
            // Component model has both a parameter and an inner content
            return false;
        }
        final String value = componentModel.getTextContent() != null ? componentModel.getTextContent() : parameters.values().iterator().next();
        Optional<TypeConverter> typeConverterOptional = createBeanDefinitionRequest.getComponentBuildingDefinition().getTypeConverter();
        componentModel.setBeanDefinition(getConvertibleBeanDefinition(type, value, typeConverterOptional));
        return true;
    }
    return false;
}
Also used : TypeConverter(org.mule.runtime.dsl.api.component.TypeConverter) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) ObjectTypeVisitor(org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor)

Aggregations

ObjectTypeVisitor (org.mule.runtime.config.internal.dsl.processor.ObjectTypeVisitor)12 SpringComponentModel (org.mule.runtime.config.internal.dsl.model.SpringComponentModel)10 ComponentModel (org.mule.runtime.config.internal.model.ComponentModel)6 Test (org.junit.Test)5 ComponentBuildingDefinition (org.mule.runtime.dsl.api.component.ComponentBuildingDefinition)5 TypeDefinition (org.mule.runtime.dsl.api.component.TypeDefinition)5 Map (java.util.Map)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 ArrayList (java.util.ArrayList)2 Collections.emptyMap (java.util.Collections.emptyMap)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Optional (java.util.Optional)2 Optional.empty (java.util.Optional.empty)2 Optional.of (java.util.Optional.of)2 Set (java.util.Set)2 Consumer (java.util.function.Consumer)2 QName (javax.xml.namespace.QName)2 AbstractComponent (org.mule.runtime.api.component.AbstractComponent)2 Component (org.mule.runtime.api.component.Component)2