use of org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration in project mule by mulesoft.
the class ParameterLayoutOrderDeclarationEnricher method enrich.
@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
ExtensionDeclaration declaration = extensionLoadingContext.getExtensionDeclarer().getDeclaration();
new IdempotentDeclarationWalker() {
@Override
public void onOperation(OperationDeclaration declaration) {
establishOrder(declaration);
}
@Override
public void onConfiguration(ConfigurationDeclaration declaration) {
establishOrder(declaration);
}
@Override
public void onSource(SourceDeclaration declaration) {
establishOrder(declaration);
}
@Override
protected void onConnectionProvider(ConnectionProviderDeclaration declaration) {
establishOrder(declaration);
}
}.walk(declaration);
}
use of org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration 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);
});
}
}
use of org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration in project mule by mulesoft.
the class DynamicMetadataDeclarationEnricherTestCase method parseMetadataAnnotationsOnParameter.
@Test
public void parseMetadataAnnotationsOnParameter() {
final OperationDeclaration operationDeclaration = getDeclaration(declaration.getConfigurations().get(0).getOperations(), CONTENT_METADATA_WITH_KEY_ID);
final List<ParameterDeclaration> parameters = operationDeclaration.getAllParameters();
assertParameterIsMetadataKeyPart(getDeclaration(parameters, "type"));
assertParameterIsMetadataContent(getDeclaration(parameters, "content"));
}
use of org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration in project mule by mulesoft.
the class DynamicMetadataDeclarationEnricherTestCase method declareStaticAndDynamicTypesInOperation.
@Test
public void declareStaticAndDynamicTypesInOperation() {
List<ParameterDeclaration> params;
List<OperationDeclaration> operations = declaration.getConfigurations().get(0).getOperations();
OperationDeclaration dynamicContent = getDeclaration(operations, "contentMetadataWithKeyId");
assertOutputType(dynamicContent.getOutput(), toMetadataType(Object.class), true);
assertOutputType(dynamicContent.getOutputAttributes(), toMetadataType(void.class), false);
params = dynamicContent.getAllParameters();
assertParameterType(getDeclaration(params, "type"), toMetadataType(String.class));
assertParameterType(getDeclaration(params, "content"), toMetadataType(Object.class));
OperationDeclaration dynamicOutput = getDeclaration(operations, "outputMetadataWithKeyId");
assertOutputType(dynamicOutput.getOutput(), toMetadataType(Object.class), true);
assertOutputType(dynamicOutput.getOutputAttributes(), toMetadataType(void.class), false);
params = dynamicOutput.getAllParameters();
assertParameterType(getDeclaration(params, "type"), toMetadataType(String.class));
assertParameterType(getDeclaration(params, "content"), toMetadataType(Object.class));
OperationDeclaration dynaimcContentAndOutput = getDeclaration(operations, "contentAndOutputMetadataWithKeyId");
assertOutputType(dynaimcContentAndOutput.getOutput(), toMetadataType(Object.class), true);
assertOutputType(dynaimcContentAndOutput.getOutputAttributes(), toMetadataType(void.class), false);
params = dynaimcContentAndOutput.getAllParameters();
assertParameterType(getDeclaration(params, "type"), toMetadataType(String.class));
assertParameterType(getDeclaration(params, "content"), toMetadataType(Object.class));
operations = declaration.getOperations();
OperationDeclaration dynamicOutputAndAttributes = getDeclaration(operations, "outputAttributesWithDynamicMetadata");
assertOutputType(dynamicOutputAndAttributes.getOutput(), toMetadataType(Object.class), true);
assertOutputType(dynamicOutputAndAttributes.getOutputAttributes(), toMetadataType(AbstractOutputAttributes.class), true);
params = dynamicOutputAndAttributes.getAllParameters();
assertParameterType(getDeclaration(params, "type"), toMetadataType(String.class));
OperationDeclaration staticOutputOnly = getDeclaration(operations, "typeWithDeclaredSubtypesMetadata");
assertOutputType(staticOutputOnly.getOutput(), toMetadataType(boolean.class), false);
assertOutputType(staticOutputOnly.getOutputAttributes(), toMetadataType(void.class), false);
OperationDeclaration staticOutputAndAttributes = getDeclaration(operations, "outputAttributesWithDeclaredSubtypesMetadata");
assertOutputType(staticOutputAndAttributes.getOutput(), toMetadataType(Shape.class), false);
assertOutputType(staticOutputAndAttributes.getOutputAttributes(), toMetadataType(AbstractOutputAttributes.class), false);
}
use of org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration in project mule by mulesoft.
the class JavaConfigurationDeclarationEnricherTestCase method verifyConfigurationModelPropertyOnOperation.
@Test
public void verifyConfigurationModelPropertyOnOperation() {
OperationDeclaration operationDeclaration = getDeclaration(declaration.getConfigurations().get(0).getOperations(), GET_ENEMY);
final ConfigTypeModelProperty configTypeModelProperty = checkIsPresent(operationDeclaration, ConfigTypeModelProperty.class);
assertType(configTypeModelProperty);
}
Aggregations