use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class MuleApplicationClassLoader method resolveContextArtifactPluginClassLoadersForCurrentClassLoader.
/**
* Resolves the plugin classloader of the thread context classloader artifact.
* <p>
* If that classloader doesn't contain plugins, the current context classloader is returned.
*
* @return the plugin classloader of the current thread context artifact.
*/
private static List<ClassLoader> resolveContextArtifactPluginClassLoadersForCurrentClassLoader() {
// TODO MULE-12254
// When running the tests, the classloader hierarchy is build with the launcher, but when executing here we are with the
// container.
// This is why this reflection bloat is required.
final Method getArtifactPluginClassLoaders;
try {
getArtifactPluginClassLoaders = currentThread().getContextClassLoader().getClass().getMethod("getArtifactPluginClassLoaders");
} catch (NoSuchMethodException | SecurityException e) {
return singletonList(currentThread().getContextClassLoader());
}
final List artifactPluginClassLoaders;
try {
artifactPluginClassLoaders = (List) getArtifactPluginClassLoaders.invoke(currentThread().getContextClassLoader());
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new MuleRuntimeException(e);
}
final List<ClassLoader> classLoaders = new ArrayList<ClassLoader>((List<ClassLoader>) artifactPluginClassLoaders.stream().map(acl -> {
try {
return acl.getClass().getMethod("getClassLoader").invoke(acl);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new MuleRuntimeException(e);
}
}).collect(toList()));
return classLoaders;
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class ExtensionDefinitionParser method parseRoute.
private void parseRoute(NestedRouteModel routeModel) {
DslElementSyntax routeDsl = dslResolver.resolve(routeModel);
Class<?> type = routeModel.getModelProperty(ImplementingTypeModelProperty.class).map(ImplementingTypeModelProperty::getType).orElseThrow(() -> new IllegalStateException("Missing route information"));
MetadataType metadataType = typeLoader.load(type);
addParameter(getChildKey(routeModel.getName()), new DefaultObjectParsingDelegate().parse(routeModel.getName(), (ObjectType) metadataType, routeDsl));
try {
new RouteComponentParser(baseDefinitionBuilder, routeModel, metadataType, getContextClassLoader(), routeDsl, dslResolver, parsingContext).parse().forEach(this::addDefinition);
} catch (Exception e) {
throw new MuleRuntimeException(new ConfigurationException(e));
}
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class ExtensionDefinitionParser method parseObjectParameter.
/**
* Registers a definition for a {@link ParameterModel} which represents an {@link ObjectType}
*
* @param key the key that the parsed value should have on the parsed parameter's map
* @param name the parameter's name
* @param type an {@link ObjectType}
* @param defaultValue the parameter's default value
* @param expressionSupport the parameter's {@link ExpressionSupport}
* @param required whether the parameter is required or not
* @param modelProperties parameter's {@link ModelProperty}s
*/
protected void parseObjectParameter(String key, String name, ObjectType type, Object defaultValue, ExpressionSupport expressionSupport, boolean required, boolean acceptsReferences, DslElementSyntax elementDsl, Set<ModelProperty> modelProperties) {
parseObject(key, name, type, defaultValue, expressionSupport, required, acceptsReferences, elementDsl, modelProperties);
final String elementNamespace = elementDsl.getPrefix();
final String elementName = elementDsl.getElementName();
if (elementDsl.supportsChildDeclaration() && !elementDsl.isWrapped() && modelProperties.stream().noneMatch(m -> m.getName().equals(InfrastructureParameterModelProperty.NAME))) {
try {
new ObjectTypeParameterParser(baseDefinitionBuilder, elementName, elementNamespace, type, getContextClassLoader(), dslResolver, parsingContext).parse().forEach(this::addDefinition);
} catch (Exception e) {
throw new MuleRuntimeException(new ConfigurationException(e));
}
}
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class ErrorsDeclarationEnricher method registerOperationErrorTypes.
private void registerOperationErrorTypes(MethodElement operationMethod, ComponentDeclaration operation, ErrorsModelFactory errorModelDescriber, ErrorTypeDefinition<?>[] extensionErrorTypes, Type extensionElement) {
if (extensionElement.getDeclaringClass().isPresent()) {
getOperationThrowsDeclaration(operationMethod, extensionElement).ifPresent(throwsAnnotation -> {
Class<? extends ErrorTypeProvider>[] providers = throwsAnnotation.value();
Stream.of(providers).forEach(provider -> {
try {
ErrorTypeProvider errorTypeProvider = provider.newInstance();
errorTypeProvider.getErrorTypes().stream().map(error -> validateOperationThrows(extensionErrorTypes, error)).map(errorModelDescriber::getErrorModel).forEach(operation::addErrorModel);
} catch (InstantiationException | IllegalAccessException e) {
throw new MuleRuntimeException(createStaticMessage("Could not create ErrorTypeProvider of type " + provider.getName()), e);
}
});
});
}
}
use of org.mule.runtime.api.exception.MuleRuntimeException 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