use of org.mule.runtime.module.extension.api.loader.java.type.Type 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);
}
use of org.mule.runtime.module.extension.api.loader.java.type.Type in project mule by mulesoft.
the class ParameterModelsLoaderDelegate method parseNullSafe.
private void parseNullSafe(ExtensionParameter extensionParameter, ParameterDeclarer parameter) {
if (extensionParameter.isAnnotatedWith(NullSafe.class)) {
if (extensionParameter.isAnnotatedWith(ConfigOverride.class)) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' and also marked as a config override, which is redundant. " + "The default value for this parameter will come from the configuration parameter", extensionParameter.getName(), NullSafe.class.getSimpleName()));
}
if (extensionParameter.isRequired() && !extensionParameter.isAnnotatedWith(ParameterGroup.class)) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is required but annotated with '@%s', which is redundant", extensionParameter.getName(), NullSafe.class.getSimpleName()));
}
Type nullSafeAnnotationType = extensionParameter.getValueFromAnnotation(NullSafe.class).get().getClassValue(NullSafe::defaultImplementingType);
final boolean hasDefaultOverride = !nullSafeAnnotationType.isSameType(Object.class);
MetadataType nullSafeType = hasDefaultOverride ? nullSafeAnnotationType.asMetadataType() : parameter.getDeclaration().getType();
boolean isInstantiable = hasDefaultOverride ? nullSafeAnnotationType.isInstantiable() : extensionParameter.getType().isInstantiable();
parameter.getDeclaration().getType().accept(new BasicTypeMetadataVisitor() {
@Override
protected void visitBasicType(MetadataType metadataType) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' but is of type '%s'. That annotation can only be " + "used with complex types (Pojos, Lists, Maps)", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
}
@Override
public void visitArrayType(ArrayType arrayType) {
if (hasDefaultOverride) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' is of type '%s'" + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for Collections", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
}
}
@Override
public void visitObject(ObjectType objectType) {
if (hasDefaultOverride && isMap(objectType)) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' is of type '%s'" + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for Maps", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
}
if (hasDefaultOverride && extensionParameter.getType().isInstantiable()) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' is of concrete type '%s'," + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for concrete types", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
}
if (!isInstantiable && !isMap(nullSafeType)) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' but is of type '%s'. That annotation can only be " + "used with complex instantiable types (Pojos, Lists, Maps)", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
}
if (hasDefaultOverride && !extensionParameter.getType().isAssignableFrom(nullSafeAnnotationType)) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' of type '%s', but provided type '%s" + " is not a subtype of the parameter's type", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName(), getType(nullSafeType).getName()));
}
}
});
parameter.withModelProperty(new NullSafeModelProperty(nullSafeType));
if (hasDefaultOverride) {
parameter.withModelProperty(new DefaultImplementingTypeModelProperty(nullSafeType));
}
}
}
use of org.mule.runtime.module.extension.api.loader.java.type.Type 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.module.extension.api.loader.java.type.Type in project mule by mulesoft.
the class JavaExportedTypesDeclarationEnricher method enrich.
@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
Optional<ExtensionTypeDescriptorModelProperty> modelProperty = extensionLoadingContext.getExtensionDeclarer().getDeclaration().getModelProperty(ExtensionTypeDescriptorModelProperty.class);
modelProperty.map(ExtensionTypeDescriptorModelProperty::getType).flatMap(type -> type.getValueFromAnnotation(Export.class)).ifPresent(exportAnnotation -> {
ExtensionDeclarer declarer = extensionLoadingContext.getExtensionDeclarer();
exportAnnotation.getClassArrayValue(Export::classes).stream().map(Type::asMetadataType).forEach(type -> registerType(declarer, type));
exportAnnotation.getArrayValue(Export::resources).forEach(declarer::withResource);
});
}
use of org.mule.runtime.module.extension.api.loader.java.type.Type 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);
});
}
}
Aggregations