use of org.mule.runtime.extension.api.exception.IllegalModelDefinitionException in project mule by mulesoft.
the class BaseExtensionResourcesGeneratorAnnotationProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
log("Starting Resources generator for Extensions");
ResourcesGenerator generator = new AnnotationProcessorResourceGenerator(fetchResourceFactories(), processingEnv);
try {
getExtension(roundEnv).ifPresent(extensionElement -> {
if (!shouldProcess(extensionElement, processingEnv)) {
return;
}
Optional<Class<Object>> annotatedClass = processor.classFor(extensionElement, processingEnv);
ExtensionElement extension = toExtensionElement(extensionElement, processingEnv);
ClassLoader classLoader = annotatedClass.map(Class::getClassLoader).orElseGet(ExtensionModel.class::getClassLoader);
withContextClassLoader(classLoader, () -> {
ExtensionModel extensionModel = parseExtension(extensionElement, extension, roundEnv, classLoader);
generator.generateFor(extensionModel);
});
});
return false;
} catch (MuleRuntimeException e) {
Optional<IllegalModelDefinitionException> exception = extractOfType(e, IllegalModelDefinitionException.class);
if (exception.isPresent()) {
throw exception.get();
}
processingEnv.getMessager().printMessage(ERROR, format("%s\n%s", e.getMessage(), getStackTrace(e)));
throw e;
}
}
use of org.mule.runtime.extension.api.exception.IllegalModelDefinitionException in project mule by mulesoft.
the class FunctionModelLoaderDelegate method declareFunctions.
void declareFunctions(ExtensionDeclarer extensionDeclarer, HasFunctionDeclarer declarer, FunctionContainerElement methodOwnerClass, List<FunctionElement> functions) {
for (FunctionElement function : functions) {
FunctionContainerElement functionOwner = methodOwnerClass != null ? methodOwnerClass : function.getEnclosingType();
checkIsNotAnExtension(functionOwner);
final Optional<ExtensionParameter> configParameter = loader.getConfigParameter(function);
if (configParameter.isPresent()) {
throw new IllegalModelDefinitionException(format("Function '%s' requires a config parameter, but that is not allowed. " + "Remove such parameter.", function.getName()));
}
HasFunctionDeclarer actualDeclarer = (HasFunctionDeclarer) loader.selectDeclarerBasedOnConfig(extensionDeclarer, (Declarer) declarer, configParameter, empty());
if (functionDeclarers.containsKey(function)) {
actualDeclarer.withFunction(functionDeclarers.get(function));
continue;
}
final FunctionDeclarer functionDeclarer = actualDeclarer.withFunction(function.getAlias());
function.getMethod().ifPresent(method -> {
functionDeclarer.withModelProperty(new ImplementingMethodModelProperty(method));
function.getDeclaringClass().ifPresent(clazz -> functionDeclarer.withModelProperty(new FunctionExecutorModelProperty(new ReflectiveFunctionExecutorFactory<>(clazz, method))));
});
functionDeclarer.withOutput().ofType(function.getReturnMetadataType());
ParameterDeclarationContext declarationContext = new ParameterDeclarationContext(FUNCTION, functionDeclarer.getDeclaration());
loader.getMethodParametersLoader().declare(functionDeclarer, function.getParameters(), declarationContext);
functionDeclarers.put(function, functionDeclarer);
}
}
use of org.mule.runtime.extension.api.exception.IllegalModelDefinitionException 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);
}
use of org.mule.runtime.extension.api.exception.IllegalModelDefinitionException 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.extension.api.exception.IllegalModelDefinitionException in project mule by mulesoft.
the class XmlExtensionLoaderDelegate method declareErrorModels.
private void declareErrorModels(OperationDeclarer operationDeclarer, XmlDslModel xmlDslModel, String operationName, ComponentModel operationModel) {
Optional<ComponentModel> optionalParametersComponentModel = operationModel.getInnerComponents().stream().filter(child -> child.getIdentifier().equals(OPERATION_ERRORS_IDENTIFIER)).findAny();
optionalParametersComponentModel.ifPresent(componentModel -> componentModel.getInnerComponents().stream().filter(child -> child.getIdentifier().equals(OPERATION_ERROR_IDENTIFIER)).forEach(param -> {
final String namespace = xmlDslModel.getPrefix().toUpperCase();
final String typeName = param.getParameters().get(ERROR_TYPE_ATTRIBUTE);
if (StringUtils.isBlank(typeName)) {
throw new IllegalModelDefinitionException(format("The operation [%s] cannot have an <error> with an empty 'type' attribute", operationName));
}
if (typeName.contains(NAMESPACE_SEPARATOR)) {
throw new IllegalModelDefinitionException(format("The operation [%s] cannot have an <error> [%s] that contains a reserved character [%s]", operationName, typeName, NAMESPACE_SEPARATOR));
}
operationDeclarer.withErrorModel(ErrorModelBuilder.newError(typeName, namespace).withParent(ErrorModelBuilder.newError(ANY).build()).build());
}));
}
Aggregations