use of org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty in project mule by mulesoft.
the class ConfigModelLoaderDelegate method declareConfiguration.
private void declareConfiguration(ExtensionDeclarer declarer, ExtensionElement extensionType, ComponentElement configType) {
checkConfigurationIsNotAnOperation(extensionType, configType);
ConfigurationDeclarer configurationDeclarer;
Optional<Configuration> configurationAnnotation = configType.getAnnotation(Configuration.class);
if (configurationAnnotation.isPresent()) {
final Configuration configuration = configurationAnnotation.get();
String configName = isBlank(configuration.name()) ? DEFAULT_CONFIG_NAME : configuration.name();
configurationDeclarer = declarer.withConfig(configName);
} else {
configurationDeclarer = declarer.withConfig(DEFAULT_CONFIG_NAME).describedAs(DEFAULT_CONFIG_DESCRIPTION);
}
Class<?> extensionClass = extensionType.getDeclaringClass().orElse(Object.class);
Class<?> configClass = configType.getDeclaringClass().orElse(Object.class);
ClassLoader classLoader = extensionClass.getClassLoader() != null ? extensionClass.getClassLoader() : Thread.currentThread().getContextClassLoader();
TypeAwareConfigurationFactory typeAwareConfigurationFactory = new TypeAwareConfigurationFactory(configClass, classLoader);
configurationDeclarer.withModelProperty(new ConfigurationFactoryModelProperty(typeAwareConfigurationFactory)).withModelProperty(new ImplementingTypeModelProperty(configClass));
configurationDeclarer.withModelProperty(new ExtensionTypeDescriptorModelProperty(configType));
loader.parseExternalLibs(configType, configurationDeclarer);
ParameterDeclarationContext context = new ParameterDeclarationContext(CONFIGURATION, configurationDeclarer.getDeclaration());
loader.getFieldParametersLoader().declare(configurationDeclarer, configType.getParameters(), context);
getOperationLoaderDelegate().declareOperations(declarer, configurationDeclarer, configType);
getSourceModelLoaderDelegate().declareMessageSources(declarer, configurationDeclarer, configType);
getFunctionModelLoaderDelegate().declareFunctions(declarer, configurationDeclarer, configType);
getConnectionProviderModelLoaderDelegate().declareConnectionProviders(configurationDeclarer, configType);
}
use of org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty 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.internal.loader.java.property.ImplementingTypeModelProperty in project mule by mulesoft.
the class RouterModelLoaderDelegate method declareRoutes.
private void declareRoutes(ConstructDeclarer router, List<ExtensionParameter> routes) {
routes.forEach(route -> {
NestedRouteDeclarer routeDeclarer = router.withRoute(route.getAlias()).describedAs(route.getDescription()).withMinOccurs(route.isRequired() ? 1 : 0);
route.getType().getDeclaringClass().ifPresent(clazz -> routeDeclarer.withModelProperty(new ImplementingTypeModelProperty(clazz)));
final List<FieldElement> parameters = route.getType().getAnnotatedFields(Parameter.class);
loader.getFieldParametersLoader().declare(routeDeclarer, parameters, new ParameterDeclarationContext(CONSTRUCT, router.getDeclaration()));
});
}
use of org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty 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.internal.loader.java.property.ImplementingTypeModelProperty in project mule by mulesoft.
the class DefaultEncodingDeclarationEnricher method doEnrich.
private void doEnrich(BaseDeclaration declaration) {
declaration.getModelProperty(ImplementingTypeModelProperty.class).ifPresent(p -> {
ImplementingTypeModelProperty typeProperty = (ImplementingTypeModelProperty) p;
Collection<Field> fields = getAllFields(typeProperty.getType(), withAnnotation(DefaultEncoding.class));
if (isEmpty(fields)) {
return;
}
// TODO: MULE-9220 - Add a syntax validator which checks that the annotated parameter is a String
if (fields.size() > 1) {
throw new IllegalConfigurationModelDefinitionException(String.format("Only one field is allowed to be annotated with @%s, but class '%s' has %d fields " + "with such annotation. Offending fields are: [%s]", DefaultEncoding.class.getSimpleName(), typeProperty.getType().getName(), fields.size(), Joiner.on(", ").join(fields.stream().map(Field::getName).collect(toList()))));
}
final Field defaultEncodingField = fields.iterator().next();
if (!String.class.equals(defaultEncodingField.getType())) {
throw new IllegalConfigurationModelDefinitionException(String.format("Class '%s' declares the field '%s' which is annotated with @%s and is of type '%s'. Only " + "fields of type String are allowed to carry such annotation", typeProperty.getType().getName(), defaultEncodingField.getName(), DefaultEncoding.class.getSimpleName(), defaultEncodingField.getType().getName()));
}
declaration.addModelProperty(new DefaultEncodingModelProperty(defaultEncodingField));
});
}
Aggregations