use of org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty in project mule by mulesoft.
the class JavaPrivilegedExportedTypesDeclarationEnricher method enrich.
@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
ExtensionDeclarer extensionDeclarer = extensionLoadingContext.getExtensionDeclarer();
extensionDeclarer.getDeclaration().getModelProperty(ExtensionTypeDescriptorModelProperty.class).map(ExtensionTypeDescriptorModelProperty::getType).flatMap(type -> type.getValueFromAnnotation(PrivilegedExport.class)).ifPresent(valueFetcher -> {
valueFetcher.getArrayValue(PrivilegedExport::artifacts).forEach(extensionDeclarer::withPrivilegedArtifact);
valueFetcher.getArrayValue(PrivilegedExport::packages).forEach(extensionDeclarer::withPrivilegedPackage);
});
}
use of org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty 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.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty in project mule by mulesoft.
the class MetadataComponentModelValidatorTestCase method before.
@Before
public void before() {
when(extensionModel.getOperationModels()).thenReturn(asList(operationModel));
when(extensionModel.getSourceModels()).thenReturn(asList(sourceModel));
ExtensionTypeDescriptorModelProperty descriptorModelProperty = mock(ExtensionTypeDescriptorModelProperty.class);
when(extensionModel.getModelProperty(ExtensionTypeDescriptorModelProperty.class)).thenReturn(of(descriptorModelProperty));
Type extensionType = mock(Type.class);
when(descriptorModelProperty.getType()).thenReturn(extensionType);
when(extensionType.getDeclaringClass()).thenReturn(of(this.getClass()));
when(operationModel.getOutput()).thenReturn(new ImmutableOutputModel(EMPTY, toMetadataType(String.class), false, emptySet()));
when(operationModel.getOutputAttributes()).thenReturn(new ImmutableOutputModel(StringUtils.EMPTY, create(JAVA).voidType().build(), false, emptySet()));
when(operationModel.getName()).thenReturn("operation");
mockMetadataResolverFactory(operationModel, null);
when(sourceModel.getOutput()).thenReturn(new ImmutableOutputModel(EMPTY, toMetadataType(String.class), false, emptySet()));
when(sourceModel.getOutputAttributes()).thenReturn(new ImmutableOutputModel(StringUtils.EMPTY, create(JAVA).voidType().build(), false, emptySet()));
when(sourceModel.getName()).thenReturn("source");
when(sourceModel.getSuccessCallback()).thenReturn(of(sourceCallbackModel));
when(sourceModel.getErrorCallback()).thenReturn(of(sourceCallbackModel));
mockMetadataResolverFactory(sourceModel, null);
MetadataKeyIdModelProperty keyIdModelProperty = new MetadataKeyIdModelProperty(loader.load(InvalidMetadataKeyIdPojo.class), EMPTY);
when(sourceModel.getModelProperty(MetadataKeyIdModelProperty.class)).thenReturn(of(keyIdModelProperty));
when(operationModel.getModelProperty(MetadataKeyIdModelProperty.class)).thenReturn(empty());
when(sourceModel.getModelProperty(ExtensionOperationDescriptorModelProperty.class)).thenReturn(empty());
when(operationModel.getModelProperty(ExtensionOperationDescriptorModelProperty.class)).thenReturn(empty());
visitableMock(operationModel, sourceModel);
dictionaryType = typeBuilder.objectType().openWith(toMetadataType(Object.class)).build();
}
use of org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty in project mule by mulesoft.
the class ClusterSupportEnricherTestCase method setSourceClass.
private void setSourceClass(Class<? extends Source> sourceClass) {
ExtensionTypeDescriptorModelProperty property = new ExtensionTypeDescriptorModelProperty(new TypeWrapper(sourceClass, typeLoader));
when(sourceDeclaration.getModelProperty(ExtensionTypeDescriptorModelProperty.class)).thenReturn(Optional.of(property));
}
use of org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty in project mule by mulesoft.
the class MetadataComponentModelValidator method validate.
@Override
public void validate(ExtensionModel extensionModel, ProblemsReporter problemsReporter) {
// TODO - MULE-14397 - Improve Dynamic Metadata Enricher to enrich without requiring Classes
// This is skipped if the extension is loaded with java, but it doesn't have classes which means AST Mode
Optional<ExtensionTypeDescriptorModelProperty> property = extensionModel.getModelProperty(ExtensionTypeDescriptorModelProperty.class);
if (property.isPresent()) {
if (!property.get().getType().getDeclaringClass().isPresent()) {
return;
}
}
final Table<String, String, Class<?>> names = HashBasedTable.create();
new ExtensionWalker() {
@Override
public void onOperation(HasOperationModels owner, OperationModel model) {
validateComponent(model);
}
@Override
public void onSource(HasSourceModels owner, SourceModel model) {
validateComponent(model);
}
private void validateComponent(ConnectableComponentModel model) {
validateMetadataReturnType(extensionModel, model, problemsReporter);
MetadataResolverFactory resolverFactory = MuleExtensionUtils.getMetadataResolverFactory(model);
validateMetadataOutputAttributes(model, resolverFactory, problemsReporter);
validateMetadataKeyId(model, resolverFactory, problemsReporter);
validateCategoriesInScope(model, resolverFactory, problemsReporter);
validateResolversName(model, resolverFactory, names, problemsReporter);
}
}.walk(extensionModel);
}
Aggregations