Search in sources :

Example 1 with ClassTypeLoader

use of org.mule.metadata.api.ClassTypeLoader in project mule by mulesoft.

the class MuleExtensionModelDeclarer method createExtensionModel.

ExtensionDeclarer createExtensionModel() {
    final ClassTypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader(MuleExtensionModelDeclarer.class.getClassLoader());
    ExtensionDeclarer extensionDeclarer = new ExtensionDeclarer().named(MULE_NAME).describedAs("Mule Runtime and Integration Platform: Core components").onVersion(MULE_VERSION).fromVendor(MULESOFT_VENDOR).withCategory(COMMUNITY).withModelProperty(new CustomBuildingDefinitionProviderModelProperty()).withXmlDsl(XmlDslModel.builder().setPrefix(CORE_PREFIX).setNamespace(CORE_NAMESPACE).setSchemaVersion(MULE_VERSION).setXsdFileName(CORE_PREFIX + ".xsd").setSchemaLocation(CORE_SCHEMA_LOCATION).build());
    declareExportedTypes(typeLoader, extensionDeclarer);
    // constructs
    declareFlow(extensionDeclarer, typeLoader);
    declareSubflow(extensionDeclarer);
    declareChoice(extensionDeclarer, typeLoader);
    declareErrorHandler(extensionDeclarer, typeLoader);
    declareTry(extensionDeclarer, typeLoader);
    declareScatterGather(extensionDeclarer, typeLoader);
    declareSplitAggregate(extensionDeclarer, typeLoader);
    declareFirstSuccessful(extensionDeclarer);
    declareRoundRobin(extensionDeclarer);
    declareConfiguration(extensionDeclarer, typeLoader);
    declareConfigurationProperties(extensionDeclarer, typeLoader);
    declareAsync(extensionDeclarer, typeLoader);
    declareForEach(extensionDeclarer, typeLoader);
    declareUntilSuccessful(extensionDeclarer, typeLoader);
    // operations
    declareFlowRef(extensionDeclarer, typeLoader);
    declareIdempotentValidator(extensionDeclarer, typeLoader);
    declareLogger(extensionDeclarer, typeLoader);
    declareSetPayload(extensionDeclarer, typeLoader);
    declareSetVariable(extensionDeclarer, typeLoader);
    declareRemoveVariable(extensionDeclarer, typeLoader);
    declareParseTemplate(extensionDeclarer, typeLoader);
    declareRaiseError(extensionDeclarer, typeLoader);
    // sources
    declareScheduler(extensionDeclarer, typeLoader);
    // errors
    declareErrors(extensionDeclarer);
    return extensionDeclarer;
}
Also used : ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) ExtensionDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer) CustomBuildingDefinitionProviderModelProperty(org.mule.runtime.core.internal.extension.CustomBuildingDefinitionProviderModelProperty)

Example 2 with ClassTypeLoader

use of org.mule.metadata.api.ClassTypeLoader in project mule by mulesoft.

the class PollingSourceDeclarationEnricher method enrich.

@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
    ClassTypeLoader loader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
    ExtensionDeclarer extensionDeclarer = extensionLoadingContext.getExtensionDeclarer();
    Reference<Boolean> thereArePollingSources = new Reference<>(false);
    new IdempotentDeclarationWalker() {

        @Override
        protected void onSource(SourceDeclaration source) {
            extractType(source).ifPresent(type -> {
                if (type.isAssignableTo(PollingSource.class)) {
                    source.setRunsOnPrimaryNodeOnly(true);
                    ParameterDeclaration parameter = new ParameterDeclaration(SCHEDULING_STRATEGY_PARAMETER_NAME);
                    parameter.setDescription(SCHEDULING_STRATEGY_PARAMETER_DESCRIPTION);
                    parameter.setRequired(true);
                    parameter.setType(loader.load(Scheduler.class), false);
                    parameter.setExpressionSupport(NOT_SUPPORTED);
                    parameter.addModelProperty(new InfrastructureParameterModelProperty(10));
                    parameter.addModelProperty(new QNameModelProperty(new QName(CORE_NAMESPACE, SCHEDULING_STRATEGY_ELEMENT_IDENTIFIER, CORE_PREFIX)));
                    parameter.setDslConfiguration(ParameterDslConfiguration.builder().allowsInlineDefinition(true).allowsReferences(false).allowTopLevelDefinition(false).build());
                    thereArePollingSources.set(true);
                    source.getParameterGroup(DEFAULT_GROUP_NAME).addParameter(parameter);
                }
            });
        }
    }.walk(extensionDeclarer.getDeclaration());
    if (thereArePollingSources.get() && !isSchedulerAlreadyImported(extensionDeclarer.getDeclaration())) {
        ClassTypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
        extensionDeclarer.withImportedType(new ImportedTypeModel((ObjectType) typeLoader.load(Scheduler.class)));
    }
}
Also used : InfrastructureParameterModelProperty(org.mule.runtime.extension.api.property.InfrastructureParameterModelProperty) ParameterDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ParameterDeclaration) CORE_PREFIX(org.mule.runtime.internal.dsl.DslConstants.CORE_PREFIX) ImportedTypeModel(org.mule.runtime.api.meta.model.ImportedTypeModel) PollingSource(org.mule.runtime.extension.api.runtime.source.PollingSource) ExtensionsTypeLoaderFactory(org.mule.runtime.extension.api.declaration.type.ExtensionsTypeLoaderFactory) QNameModelProperty(org.mule.runtime.extension.api.property.QNameModelProperty) CORE_NAMESPACE(org.mule.runtime.internal.dsl.DslConstants.CORE_NAMESPACE) DEFAULT_GROUP_NAME(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel.DEFAULT_GROUP_NAME) MetadataTypeUtils.getTypeId(org.mule.metadata.api.utils.MetadataTypeUtils.getTypeId) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) DeclarationEnricher(org.mule.runtime.extension.api.loader.DeclarationEnricher) SourceDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclaration) ParameterDslConfiguration(org.mule.runtime.api.meta.model.ParameterDslConfiguration) SCHEDULING_STRATEGY_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.SCHEDULING_STRATEGY_PARAMETER_NAME) ObjectType(org.mule.metadata.api.model.ObjectType) DeclarationEnricherPhase(org.mule.runtime.extension.api.loader.DeclarationEnricherPhase) ExtensionDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration) ExtensionLoadingContext(org.mule.runtime.extension.api.loader.ExtensionLoadingContext) STRUCTURE(org.mule.runtime.extension.api.loader.DeclarationEnricherPhase.STRUCTURE) SCHEDULING_STRATEGY_PARAMETER_DESCRIPTION(org.mule.runtime.extension.api.ExtensionConstants.SCHEDULING_STRATEGY_PARAMETER_DESCRIPTION) Scheduler(org.mule.runtime.core.api.source.scheduler.Scheduler) Reference(org.mule.runtime.api.util.Reference) MetadataType(org.mule.metadata.api.model.MetadataType) ExtensionDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer) QName(javax.xml.namespace.QName) NOT_SUPPORTED(org.mule.runtime.api.meta.ExpressionSupport.NOT_SUPPORTED) SCHEDULING_STRATEGY_ELEMENT_IDENTIFIER(org.mule.runtime.internal.dsl.DslConstants.SCHEDULING_STRATEGY_ELEMENT_IDENTIFIER) IdempotentDeclarationWalker(org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker) QNameModelProperty(org.mule.runtime.extension.api.property.QNameModelProperty) Reference(org.mule.runtime.api.util.Reference) QName(javax.xml.namespace.QName) Scheduler(org.mule.runtime.core.api.source.scheduler.Scheduler) PollingSource(org.mule.runtime.extension.api.runtime.source.PollingSource) InfrastructureParameterModelProperty(org.mule.runtime.extension.api.property.InfrastructureParameterModelProperty) ObjectType(org.mule.metadata.api.model.ObjectType) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) ImportedTypeModel(org.mule.runtime.api.meta.model.ImportedTypeModel) ExtensionDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer) IdempotentDeclarationWalker(org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker) ParameterDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ParameterDeclaration) SourceDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclaration)

Example 3 with ClassTypeLoader

use of org.mule.metadata.api.ClassTypeLoader in project mule by mulesoft.

the class RedeliveryPolicyDeclarationEnricher method enrich.

@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
    final Reference<Boolean> hasObjectStoreParams = new Reference<>(false);
    ExtensionDeclaration extension = extensionLoadingContext.getExtensionDeclarer().getDeclaration();
    new IdempotentDeclarationWalker() {

        @Override
        protected void onSource(SourceDeclaration declaration) {
            addRedeliveryPolicy(declaration);
            hasObjectStoreParams.set(true);
        }
    }.walk(extension);
    if (hasObjectStoreParams.get() && !isObjectStoreAlreadyImported(extension)) {
        ClassTypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
        extension.getImportedTypes().add(new ImportedTypeModel((ObjectType) typeLoader.load(ObjectStore.class)));
    }
}
Also used : ObjectType(org.mule.metadata.api.model.ObjectType) ObjectStore(org.mule.runtime.api.store.ObjectStore) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) ImportedTypeModel(org.mule.runtime.api.meta.model.ImportedTypeModel) Reference(org.mule.runtime.api.util.Reference) ExtensionDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration) IdempotentDeclarationWalker(org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker) SourceDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclaration)

Example 4 with ClassTypeLoader

use of org.mule.metadata.api.ClassTypeLoader in project mule by mulesoft.

the class NotificationsDeclarationEnricher method enrich.

@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
    ExtensionDeclaration declaration = extensionLoadingContext.getExtensionDeclarer().getDeclaration();
    Optional<ExtensionTypeDescriptorModelProperty> extensionType = declaration.getModelProperty(ExtensionTypeDescriptorModelProperty.class);
    String extensionNamespace = getExtensionsNamespace(declaration);
    ClassTypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
    if (extensionType.isPresent() && extensionType.get().getType().getDeclaringClass().isPresent()) {
        Type extensionElement = extensionType.get().getType();
        Optional<NotificationActions> annotation = extensionElement.getAnnotation(NotificationActions.class);
        annotation.ifPresent(actionsAnnotation -> {
            NotificationActionDefinition<?>[] actions = (NotificationActionDefinition<?>[]) actionsAnnotation.value().getEnumConstants();
            Map<NotificationActionDefinition, NotificationModel> notificationModels = new HashMap<>();
            stream(actions).forEach(action -> {
                NotificationModel model = new ImmutableNotificationModel(extensionNamespace, ((Enum) action).name(), typeLoader.load(action.getDataType().getType()));
                declaration.addNotificationModel(model);
                notificationModels.put(action, model);
            });
            new IdempotentDeclarationWalker() {

                @Override
                public void onOperation(WithOperationsDeclaration owner, OperationDeclaration declaration) {
                    Optional<ExtensionOperationDescriptorModelProperty> modelProperty = declaration.getModelProperty(ExtensionOperationDescriptorModelProperty.class);
                    if (modelProperty.isPresent()) {
                        MethodElement method = modelProperty.get().getOperationMethod();
                        Optional<Fires> emitsNotifications = getOperationNotificationDeclaration(method, extensionElement);
                        includeNotificationDeclarationIfNeeded(declaration, emitsNotifications);
                    }
                }

                @Override
                public void onSource(SourceDeclaration declaration) {
                    Optional<ExtensionTypeDescriptorModelProperty> modelProperty = declaration.getModelProperty(ExtensionTypeDescriptorModelProperty.class);
                    if (modelProperty.isPresent()) {
                        Type sourceContainer = modelProperty.get().getType();
                        Optional<Fires> emitsNotifications = getNotificationDeclaration(sourceContainer, extensionElement);
                        includeNotificationDeclarationIfNeeded(declaration, emitsNotifications);
                    }
                }

                private Optional<Fires> getOperationNotificationDeclaration(MethodElement operationMethod, Type extensionElement) {
                    Type operationContainer = operationMethod.getEnclosingType();
                    return ofNullable(operationMethod.getAnnotation(Fires.class)).orElse(getNotificationDeclaration(operationContainer, extensionElement));
                }

                private Optional<Fires> getNotificationDeclaration(Type container, Type extensionElement) {
                    return ofNullable(container.getAnnotation(Fires.class).orElseGet(() -> extensionElement.getAnnotation(Fires.class).orElse(null)));
                }

                private void includeNotificationDeclarationIfNeeded(ExecutableComponentDeclaration declaration, Optional<Fires> emitsNotifications) {
                    emitsNotifications.ifPresent(emits -> {
                        Class<? extends NotificationActionProvider>[] providers = emits.value();
                        stream(providers).forEach(provider -> {
                            try {
                                NotificationActionProvider notificationActionProvider = provider.newInstance();
                                notificationActionProvider.getNotificationActions().stream().map(action -> validateEmits(actions, action)).forEach(action -> declaration.addNotificationModel(notificationModels.get(action)));
                            } catch (InstantiationException | IllegalAccessException e) {
                                throw new MuleRuntimeException(createStaticMessage("Could not create NotificationActionProvider of type " + provider.getName()), e);
                            }
                        });
                    });
                }

                private NotificationActionDefinition validateEmits(NotificationActionDefinition[] actions, NotificationActionDefinition action) {
                    Class<?> extensionAction = actions.getClass().getComponentType();
                    if (!action.getClass().equals(extensionAction) && !action.getClass().getSuperclass().equals(extensionAction)) {
                        throw new IllegalModelDefinitionException(format("Invalid EmitsNotification detected, the extension declared" + " firing notifications of %s type, but a notification of %s type has been detected", extensionAction, action.getClass()));
                    } else {
                        return action;
                    }
                }
            }.walk(declaration);
        });
    }
}
Also used : ImmutableNotificationModel(org.mule.runtime.extension.api.model.notification.ImmutableNotificationModel) MuleExtensionUtils.getExtensionsNamespace(org.mule.runtime.module.extension.internal.util.MuleExtensionUtils.getExtensionsNamespace) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) NotificationActionProvider(org.mule.runtime.extension.api.annotation.notification.NotificationActionProvider) ExtensionsTypeLoaderFactory(org.mule.runtime.extension.api.declaration.type.ExtensionsTypeLoaderFactory) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) OperationDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration) NotificationActions(org.mule.runtime.extension.api.annotation.notification.NotificationActions) HashMap(java.util.HashMap) NotificationActionDefinition(org.mule.runtime.extension.api.notification.NotificationActionDefinition) Type(org.mule.runtime.module.extension.api.loader.java.type.Type) SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) WithOperationsDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.WithOperationsDeclaration) MethodElement(org.mule.runtime.module.extension.api.loader.java.type.MethodElement) Map(java.util.Map) NotificationModel(org.mule.runtime.api.meta.model.notification.NotificationModel) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) DeclarationEnricher(org.mule.runtime.extension.api.loader.DeclarationEnricher) SourceDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclaration) Optional.ofNullable(java.util.Optional.ofNullable) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) DeclarationEnricherPhase(org.mule.runtime.extension.api.loader.DeclarationEnricherPhase) ExtensionDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration) ExtensionLoadingContext(org.mule.runtime.extension.api.loader.ExtensionLoadingContext) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) String.format(java.lang.String.format) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) Fires(org.mule.runtime.extension.api.annotation.notification.Fires) Optional(java.util.Optional) ExecutableComponentDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExecutableComponentDeclaration) Arrays.stream(java.util.Arrays.stream) IdempotentDeclarationWalker(org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker) ExtensionOperationDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionOperationDescriptorModelProperty) INITIALIZE(org.mule.runtime.extension.api.loader.DeclarationEnricherPhase.INITIALIZE) IllegalModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalModelDefinitionException) NotificationActionProvider(org.mule.runtime.extension.api.annotation.notification.NotificationActionProvider) ExtensionOperationDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionOperationDescriptorModelProperty) HashMap(java.util.HashMap) MethodElement(org.mule.runtime.module.extension.api.loader.java.type.MethodElement) ImmutableNotificationModel(org.mule.runtime.extension.api.model.notification.ImmutableNotificationModel) NotificationModel(org.mule.runtime.api.meta.model.notification.NotificationModel) OperationDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration) NotificationActions(org.mule.runtime.extension.api.annotation.notification.NotificationActions) IllegalModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalModelDefinitionException) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) Fires(org.mule.runtime.extension.api.annotation.notification.Fires) ExecutableComponentDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExecutableComponentDeclaration) ImmutableNotificationModel(org.mule.runtime.extension.api.model.notification.ImmutableNotificationModel) SourceDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclaration) Optional(java.util.Optional) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) NotificationActionDefinition(org.mule.runtime.extension.api.notification.NotificationActionDefinition) ExtensionDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration) WithOperationsDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.WithOperationsDeclaration) Type(org.mule.runtime.module.extension.api.loader.java.type.Type) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) IdempotentDeclarationWalker(org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker)

Example 5 with ClassTypeLoader

use of org.mule.metadata.api.ClassTypeLoader in project mule by mulesoft.

the class ObjectStoreParameterDeclarationEnricher method enrich.

@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
    final ExtensionDeclaration extension = extensionLoadingContext.getExtensionDeclarer().getDeclaration();
    final Reference<Boolean> hasObjectStoreParams = new Reference<>(false);
    new IdempotentDeclarationWalker() {

        @Override
        protected void onParameter(ParameterGroupDeclaration parameterGroup, ParameterDeclaration parameter) {
            if (isObjectStore(parameter.getType())) {
                List<StereotypeModel> stereotypes = parameter.getAllowedStereotypeModels();
                if (!stereotypes.contains(OBJECT_STORE)) {
                    stereotypes.add(OBJECT_STORE);
                }
                hasObjectStoreParams.set(true);
            }
        }
    }.walk(extension);
    if (hasObjectStoreParams.get() && !isObjectStoreAlreadyImported(extension)) {
        ClassTypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
        extension.getImportedTypes().add(new ImportedTypeModel((ObjectType) typeLoader.load(ObjectStore.class)));
    }
}
Also used : ObjectStore(org.mule.runtime.api.store.ObjectStore) Reference(org.mule.runtime.api.util.Reference) ParameterGroupDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ParameterGroupDeclaration) ExtensionDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration) ObjectType(org.mule.metadata.api.model.ObjectType) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) ImportedTypeModel(org.mule.runtime.api.meta.model.ImportedTypeModel) List(java.util.List) IdempotentDeclarationWalker(org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker) ParameterDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ParameterDeclaration)

Aggregations

ClassTypeLoader (org.mule.metadata.api.ClassTypeLoader)5 ExtensionDeclaration (org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration)4 IdempotentDeclarationWalker (org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker)4 ObjectType (org.mule.metadata.api.model.ObjectType)3 ImportedTypeModel (org.mule.runtime.api.meta.model.ImportedTypeModel)3 SourceDeclaration (org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclaration)3 Reference (org.mule.runtime.api.util.Reference)3 ExtensionDeclarer (org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer)2 ParameterDeclaration (org.mule.runtime.api.meta.model.declaration.fluent.ParameterDeclaration)2 ObjectStore (org.mule.runtime.api.store.ObjectStore)2 ExtensionsTypeLoaderFactory (org.mule.runtime.extension.api.declaration.type.ExtensionsTypeLoaderFactory)2 DeclarationEnricher (org.mule.runtime.extension.api.loader.DeclarationEnricher)2 DeclarationEnricherPhase (org.mule.runtime.extension.api.loader.DeclarationEnricherPhase)2 ExtensionLoadingContext (org.mule.runtime.extension.api.loader.ExtensionLoadingContext)2 String.format (java.lang.String.format)1 Arrays.stream (java.util.Arrays.stream)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1