use of org.mule.runtime.extension.api.loader.ExtensionLoadingContext in project mule by mulesoft.
the class ExtensionDescriptionDocumenterTestCase method loadDocumentationFromFile.
@Test
public void loadDocumentationFromFile() {
ClassLoader cl = currentThread().getContextClassLoader();
ExtensionLoadingContext ctx = new DefaultExtensionLoadingContext(cl, getDefault(emptySet()));
DefaultJavaModelLoaderDelegate loader = new DefaultJavaModelLoaderDelegate(extensionClass, "1.0.0-dev");
loader.declare(ctx);
ExtensionDescriptionsEnricher enricher = new ExtensionDescriptionsEnricher();
enricher.enrich(ctx);
ExtensionModelFactory factory = new ExtensionModelFactory();
ExtensionModel extensionModel = factory.create(ctx);
assertDescribedExtensionModel(extensionModel);
}
use of org.mule.runtime.extension.api.loader.ExtensionLoadingContext in project mule by mulesoft.
the class PollingSourceDeclarationEnricher method enrich.
@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
ClassTypeLoader loader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
ExtensionDeclarer extensionDeclarer = extensionLoadingContext.getExtensionDeclarer();
Reference<Boolean> thereArePollingSources = new Reference<>(false);
new IdempotentDeclarationWalker() {
@Override
protected void onSource(SourceDeclaration source) {
extractType(source).ifPresent(type -> {
if (type.isAssignableTo(PollingSource.class)) {
source.setRunsOnPrimaryNodeOnly(true);
ParameterDeclaration parameter = new ParameterDeclaration(SCHEDULING_STRATEGY_PARAMETER_NAME);
parameter.setDescription(SCHEDULING_STRATEGY_PARAMETER_DESCRIPTION);
parameter.setRequired(true);
parameter.setType(loader.load(Scheduler.class), false);
parameter.setExpressionSupport(NOT_SUPPORTED);
parameter.addModelProperty(new InfrastructureParameterModelProperty(10));
parameter.addModelProperty(new QNameModelProperty(new QName(CORE_NAMESPACE, SCHEDULING_STRATEGY_ELEMENT_IDENTIFIER, CORE_PREFIX)));
parameter.setDslConfiguration(ParameterDslConfiguration.builder().allowsInlineDefinition(true).allowsReferences(false).allowTopLevelDefinition(false).build());
thereArePollingSources.set(true);
source.getParameterGroup(DEFAULT_GROUP_NAME).addParameter(parameter);
}
});
}
}.walk(extensionDeclarer.getDeclaration());
if (thereArePollingSources.get() && !isSchedulerAlreadyImported(extensionDeclarer.getDeclaration())) {
ClassTypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
extensionDeclarer.withImportedType(new ImportedTypeModel((ObjectType) typeLoader.load(Scheduler.class)));
}
}
use of org.mule.runtime.extension.api.loader.ExtensionLoadingContext 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.extension.api.loader.ExtensionLoadingContext 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.extension.api.loader.ExtensionLoadingContext 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