Search in sources :

Example 1 with ImplementingTypeModelProperty

use of org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty in project mule by mulesoft.

the class ConfigModelLoaderDelegate method declareConfiguration.

private void declareConfiguration(ExtensionDeclarer declarer, ExtensionElement extensionType, ComponentElement configType) {
    checkConfigurationIsNotAnOperation(extensionType, configType);
    ConfigurationDeclarer configurationDeclarer;
    Optional<Configuration> configurationAnnotation = configType.getAnnotation(Configuration.class);
    if (configurationAnnotation.isPresent()) {
        final Configuration configuration = configurationAnnotation.get();
        String configName = isBlank(configuration.name()) ? DEFAULT_CONFIG_NAME : configuration.name();
        configurationDeclarer = declarer.withConfig(configName);
    } else {
        configurationDeclarer = declarer.withConfig(DEFAULT_CONFIG_NAME).describedAs(DEFAULT_CONFIG_DESCRIPTION);
    }
    Class<?> extensionClass = extensionType.getDeclaringClass().orElse(Object.class);
    Class<?> configClass = configType.getDeclaringClass().orElse(Object.class);
    ClassLoader classLoader = extensionClass.getClassLoader() != null ? extensionClass.getClassLoader() : Thread.currentThread().getContextClassLoader();
    TypeAwareConfigurationFactory typeAwareConfigurationFactory = new TypeAwareConfigurationFactory(configClass, classLoader);
    configurationDeclarer.withModelProperty(new ConfigurationFactoryModelProperty(typeAwareConfigurationFactory)).withModelProperty(new ImplementingTypeModelProperty(configClass));
    configurationDeclarer.withModelProperty(new ExtensionTypeDescriptorModelProperty(configType));
    loader.parseExternalLibs(configType, configurationDeclarer);
    ParameterDeclarationContext context = new ParameterDeclarationContext(CONFIGURATION, configurationDeclarer.getDeclaration());
    loader.getFieldParametersLoader().declare(configurationDeclarer, configType.getParameters(), context);
    getOperationLoaderDelegate().declareOperations(declarer, configurationDeclarer, configType);
    getSourceModelLoaderDelegate().declareMessageSources(declarer, configurationDeclarer, configType);
    getFunctionModelLoaderDelegate().declareFunctions(declarer, configurationDeclarer, configType);
    getConnectionProviderModelLoaderDelegate().declareConnectionProviders(configurationDeclarer, configType);
}
Also used : ParameterDeclarationContext(org.mule.runtime.module.extension.internal.loader.utils.ParameterDeclarationContext) Configuration(org.mule.runtime.extension.api.annotation.Configuration) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) ConfigurationDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ConfigurationDeclarer) ConfigurationFactoryModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ConfigurationFactoryModelProperty) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)

Example 2 with ImplementingTypeModelProperty

use of org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty in project mule by mulesoft.

the class ConnectionProviderModelLoaderDelegate method declareConnectionProvider.

private void declareConnectionProvider(HasConnectionProviderDeclarer declarer, ConnectionProviderElement providerType) {
    ConnectionProviderDeclarer providerDeclarer = connectionProviderDeclarers.get(providerType);
    if (providerDeclarer != null) {
        declarer.withConnectionProvider(providerDeclarer);
        return;
    }
    String name = providerType.getAlias();
    String description = providerType.getDescription();
    if (providerType.getName().equals(providerType.getAlias())) {
        name = DEFAULT_CONNECTION_PROVIDER_NAME;
    }
    List<Type> providerGenerics = providerType.getInterfaceGenerics(ConnectionProvider.class);
    if (providerGenerics.size() != 1) {
        // TODO: MULE-9220: Add a syntax validator for this
        throw new IllegalConnectionProviderModelDefinitionException(format("Connection provider class '%s' was expected to have 1 generic type " + "(for the connection type) but %d were found", providerType.getName(), providerGenerics.size()));
    }
    providerDeclarer = declarer.withConnectionProvider(name).describedAs(description);
    ConnectionProviderDeclarer finalProviderDeclarer = providerDeclarer;
    providerType.getDeclaringClass().ifPresent(clazz -> finalProviderDeclarer.withModelProperty(new ConnectionProviderFactoryModelProperty(new DefaultConnectionProviderFactory<>(clazz, getExtensionClassLoader()))).withModelProperty(new ImplementingTypeModelProperty(clazz)));
    providerDeclarer.withModelProperty(new ConnectionTypeModelProperty(providerGenerics.get(0))).withModelProperty(new ExtensionTypeDescriptorModelProperty(providerType));
    loader.parseExternalLibs(providerType, providerDeclarer);
    ConnectionManagementType managementType = NONE;
    if (providerType.isAssignableTo(PoolingConnectionProvider.class)) {
        managementType = POOLING;
    } else if (providerType.isAssignableTo(CachedConnectionProvider.class)) {
        managementType = CACHED;
    }
    parseOAuthGrantType(providerType, providerDeclarer);
    providerDeclarer.withConnectionManagementType(managementType);
    providerDeclarer.supportsConnectivityTesting(!providerType.isAssignableTo(NoConnectivityTest.class));
    connectionProviderDeclarers.put(providerType, providerDeclarer);
    ParameterDeclarationContext context = new ParameterDeclarationContext(CONNECTION_PROVIDER, providerDeclarer.getDeclaration());
    loader.getFieldParametersLoader().declare(providerDeclarer, providerType.getParameters(), context);
}
Also used : ParameterDeclarationContext(org.mule.runtime.module.extension.internal.loader.utils.ParameterDeclarationContext) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) ConnectionManagementType(org.mule.runtime.api.meta.model.connection.ConnectionManagementType) CachedConnectionProvider(org.mule.runtime.api.connection.CachedConnectionProvider) IllegalConnectionProviderModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalConnectionProviderModelDefinitionException) Type(org.mule.runtime.module.extension.api.loader.java.type.Type) ConnectionManagementType(org.mule.runtime.api.meta.model.connection.ConnectionManagementType) AuthorizationCodeGrantType(org.mule.runtime.extension.api.connectivity.oauth.AuthorizationCodeGrantType) ConnectionProviderDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ConnectionProviderDeclarer) HasConnectionProviderDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.HasConnectionProviderDeclarer) ConnectionProviderFactoryModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ConnectionProviderFactoryModelProperty) ConnectionTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ConnectionTypeModelProperty) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)

Example 3 with ImplementingTypeModelProperty

use of org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty in project mule by mulesoft.

the class RouterModelLoaderDelegate method declareRoutes.

private void declareRoutes(ConstructDeclarer router, List<ExtensionParameter> routes) {
    routes.forEach(route -> {
        NestedRouteDeclarer routeDeclarer = router.withRoute(route.getAlias()).describedAs(route.getDescription()).withMinOccurs(route.isRequired() ? 1 : 0);
        route.getType().getDeclaringClass().ifPresent(clazz -> routeDeclarer.withModelProperty(new ImplementingTypeModelProperty(clazz)));
        final List<FieldElement> parameters = route.getType().getAnnotatedFields(Parameter.class);
        loader.getFieldParametersLoader().declare(routeDeclarer, parameters, new ParameterDeclarationContext(CONSTRUCT, router.getDeclaration()));
    });
}
Also used : ParameterDeclarationContext(org.mule.runtime.module.extension.internal.loader.utils.ParameterDeclarationContext) FieldElement(org.mule.runtime.module.extension.api.loader.java.type.FieldElement) NestedRouteDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.NestedRouteDeclarer) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)

Example 4 with ImplementingTypeModelProperty

use of org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty in project mule by mulesoft.

the class SourceModelLoaderDelegate method declareMessageSource.

void declareMessageSource(ExtensionDeclarer extensionDeclarer, HasSourceDeclarer declarer, SourceElement sourceType, boolean supportsConfig) {
    // TODO: MULE-9220 - Add a syntax validator which checks that the sourceType doesn't implement
    if (isLifecycle(sourceType)) {
        throw new IllegalSourceModelDefinitionException(format("Source class '%s' implements a lifecycle interface. Sources are not allowed to", sourceType.getName()));
    }
    final Optional<ExtensionParameter> configParameter = loader.getConfigParameter(sourceType);
    final Optional<ExtensionParameter> connectionParameter = loader.getConnectionParameter(sourceType);
    if (loader.isInvalidConfigSupport(supportsConfig, configParameter, connectionParameter)) {
        throw new IllegalSourceModelDefinitionException(format("Source '%s' is defined at the extension level but it requires a config parameter. " + "Remove such parameter or move the source to the proper config", sourceType.getName()));
    }
    HasSourceDeclarer actualDeclarer = (HasSourceDeclarer) loader.selectDeclarerBasedOnConfig(extensionDeclarer, (Declarer) declarer, configParameter, connectionParameter);
    SourceDeclarer existingDeclarer = sourceDeclarers.get(sourceType);
    if (existingDeclarer != null) {
        actualDeclarer.withMessageSource(existingDeclarer);
        return;
    }
    SourceDeclarer sourceDeclarer = actualDeclarer.withMessageSource(sourceType.getAlias());
    sourceDeclarer.withModelProperty(new ExtensionTypeDescriptorModelProperty(sourceType));
    List<Type> sourceGenerics = sourceType.getSuperClassGenerics();
    if (sourceGenerics.size() != 2) {
        // TODO: MULE-9220: Add a syntax validator for this
        throw new IllegalModelDefinitionException(format("Message source class '%s' was expected to have 2 generic types " + "(one for the Payload type and another for the Attributes type) but %d were found", sourceType.getName(), sourceGenerics.size()));
    }
    sourceDeclarer.hasResponse(sourceType.isAnnotatedWith(EmitsResponse.class)).requiresConnection(connectionParameter.isPresent());
    sourceType.getDeclaringClass().ifPresent(clazz -> sourceDeclarer.withModelProperty(new SourceFactoryModelProperty(new DefaultSourceFactory((Class<? extends Source>) clazz))).withModelProperty(new ImplementingTypeModelProperty(clazz)));
    processMimeType(sourceDeclarer, sourceType);
    processComponentConnectivity(sourceDeclarer, sourceType, sourceType);
    resolveOutputTypes(sourceDeclarer, sourceType);
    loader.addExceptionEnricher(sourceType, sourceDeclarer);
    declareSourceParameters(sourceType, sourceDeclarer);
    declareSourceCallback(sourceType, sourceDeclarer);
    sourceDeclarers.put(sourceType, sourceDeclarer);
}
Also used : ExtensionParameter(org.mule.runtime.module.extension.api.loader.java.type.ExtensionParameter) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) IllegalSourceModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalSourceModelDefinitionException) HasSourceDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.HasSourceDeclarer) SourceFactoryModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.SourceFactoryModelProperty) IllegalModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalModelDefinitionException) Type(org.mule.runtime.module.extension.api.loader.java.type.Type) MetadataType(org.mule.metadata.api.model.MetadataType) DefaultSourceFactory(org.mule.runtime.module.extension.internal.runtime.source.DefaultSourceFactory) SourceDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclarer) HasSourceDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.HasSourceDeclarer) SourceDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclarer) HasSourceDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.HasSourceDeclarer) ParameterizedDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ParameterizedDeclarer) ParameterDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ParameterDeclarer) ExtensionDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer) Declarer(org.mule.runtime.api.meta.model.declaration.fluent.Declarer) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)

Example 5 with ImplementingTypeModelProperty

use of org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty in project mule by mulesoft.

the class DefaultEncodingDeclarationEnricher method doEnrich.

private void doEnrich(BaseDeclaration declaration) {
    declaration.getModelProperty(ImplementingTypeModelProperty.class).ifPresent(p -> {
        ImplementingTypeModelProperty typeProperty = (ImplementingTypeModelProperty) p;
        Collection<Field> fields = getAllFields(typeProperty.getType(), withAnnotation(DefaultEncoding.class));
        if (isEmpty(fields)) {
            return;
        }
        // TODO: MULE-9220 - Add a syntax validator which checks that the annotated parameter is a String
        if (fields.size() > 1) {
            throw new IllegalConfigurationModelDefinitionException(String.format("Only one field is allowed to be annotated with @%s, but class '%s' has %d fields " + "with such annotation. Offending fields are: [%s]", DefaultEncoding.class.getSimpleName(), typeProperty.getType().getName(), fields.size(), Joiner.on(", ").join(fields.stream().map(Field::getName).collect(toList()))));
        }
        final Field defaultEncodingField = fields.iterator().next();
        if (!String.class.equals(defaultEncodingField.getType())) {
            throw new IllegalConfigurationModelDefinitionException(String.format("Class '%s' declares the field '%s' which is annotated with @%s and is of type '%s'. Only " + "fields of type String are allowed to carry such annotation", typeProperty.getType().getName(), defaultEncodingField.getName(), DefaultEncoding.class.getSimpleName(), defaultEncodingField.getType().getName()));
        }
        declaration.addModelProperty(new DefaultEncodingModelProperty(defaultEncodingField));
    });
}
Also used : Field(java.lang.reflect.Field) IllegalConfigurationModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalConfigurationModelDefinitionException) DefaultEncodingModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.DefaultEncodingModelProperty) DefaultEncoding(org.mule.runtime.extension.api.annotation.param.DefaultEncoding) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)

Aggregations

ImplementingTypeModelProperty (org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)15 ExtensionTypeDescriptorModelProperty (org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty)5 ParameterDeclarationContext (org.mule.runtime.module.extension.internal.loader.utils.ParameterDeclarationContext)4 Field (java.lang.reflect.Field)2 ConfigurationDeclarer (org.mule.runtime.api.meta.model.declaration.fluent.ConfigurationDeclarer)2 ConnectionProviderDeclarer (org.mule.runtime.api.meta.model.declaration.fluent.ConnectionProviderDeclarer)2 ExtensionDeclarer (org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer)2 ParameterDeclaration (org.mule.runtime.api.meta.model.declaration.fluent.ParameterDeclaration)2 DefaultEncoding (org.mule.runtime.extension.api.annotation.param.DefaultEncoding)2 ExtensionParameter (org.mule.runtime.module.extension.api.loader.java.type.ExtensionParameter)2 Type (org.mule.runtime.module.extension.api.loader.java.type.Type)2 String.format (java.lang.String.format)1 Annotation (java.lang.annotation.Annotation)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 JAVA (org.mule.metadata.api.model.MetadataFormat.JAVA)1 MetadataType (org.mule.metadata.api.model.MetadataType)1 ObjectType (org.mule.metadata.api.model.ObjectType)1