use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class ReflectiveOperationExecutorFactory method createExecutor.
@Override
public ComponentExecutor<M> createExecutor(M operationModel, Map<String, Object> parameters) {
DefaultObjectBuilder objectBuilder = new DefaultObjectBuilder(implementationClass);
parameters.forEach((k, v) -> objectBuilder.addPropertyResolver(k, new StaticValueResolver<>(v)));
Object delegate;
CoreEvent initialiserEvent = null;
try {
initialiserEvent = getInitialiserEvent();
delegate = objectBuilder.build(from(initialiserEvent));
} catch (Exception e) {
throw new MuleRuntimeException(createStaticMessage("Could not create instance of operation class " + implementationClass.getName()), e);
} finally {
if (initialiserEvent != null) {
((BaseEventContext) initialiserEvent.getContext()).success();
}
}
return new ReflectiveMethodOperationExecutor(operationModel, operationMethod, delegate);
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class SourceConfigurer method configure.
/**
* Performs the configuration of the given {@code source} and returns the result
*
* @param source a {@link Source}
* @param config the {@link ConfigurationInstance config instance} associated to {@code this} source object.
* @return the configured instance
* @throws MuleException
*/
public Source configure(Source source, Optional<ConfigurationInstance> config) throws MuleException {
ResolverSetBasedObjectBuilder<Source> builder = new ResolverSetBasedObjectBuilder<Source>(source.getClass(), model, resolverSet) {
@Override
protected Source instantiateObject() {
return source;
}
@Override
public Source build(ValueResolvingContext context) throws MuleException {
Source source = build(resolverSet.resolve(context));
injectDefaultEncoding(model, source, muleContext.getConfiguration().getDefaultEncoding());
injectComponentLocation(source, componentLocation);
config.ifPresent(c -> injectRefName(source, c.getName(), getReflectionCache()));
return source;
}
};
CoreEvent initialiserEvent = null;
try {
initialiserEvent = getInitialiserEvent(muleContext);
Source configuredSource = builder.build(from(initialiserEvent, config));
if (configuredSource instanceof PollingSource) {
Scheduler scheduler = (Scheduler) resolverSet.getResolvers().get(SCHEDULING_STRATEGY_PARAMETER_NAME).resolve(ValueResolvingContext.from(initialiserEvent));
configuredSource = new PollingSourceWrapper((PollingSource) configuredSource, scheduler);
}
return configuredSource;
} catch (Exception e) {
throw new MuleRuntimeException(createStaticMessage("Exception was found trying to configure source of type " + source.getClass().getName()), e);
} finally {
if (initialiserEvent != null) {
((BaseEventContext) initialiserEvent.getContext()).success();
}
}
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class ResolverUtils method getExpressionBasedValueResolver.
private static ValueResolver<?> getExpressionBasedValueResolver(String expression, BooleanSupplier isTypedValue, BooleanSupplier isParameterResolver, Optional<StackedTypesModelProperty> stackedTypesModelProperty, MetadataType type, MuleContext muleContext) {
try {
ValueResolver resolver;
if (stackedTypesModelProperty.isPresent()) {
resolver = stackedTypesModelProperty.get().getValueResolverFactory().getExpressionBasedValueResolver(expression, getType(type));
// TODO MULE-13518: Add support for stacked value resolvers for @Parameter inside pojos // The following "IFs" should be removed once implemented
} else if (isTypedValue.getAsBoolean()) {
ExpressionTypedValueValueResolver<Object> valueResolver = new ExpressionTypedValueValueResolver<>(expression, getType(type));
valueResolver.setTransformationService(muleContext.getTransformationService());
valueResolver.setExtendedExpressionManager(muleContext.getExpressionManager());
resolver = valueResolver;
} else if (isParameterResolver.getAsBoolean()) {
ExpressionBasedParameterResolverValueResolver<Object> valueResolver = new ExpressionBasedParameterResolverValueResolver<>(expression, getType(type), type);
valueResolver.setTransformationService(muleContext.getTransformationService());
valueResolver.setExtendedExpressionManager(muleContext.getExpressionManager());
resolver = valueResolver;
} else if (muleContext.getExpressionManager().isExpression(expression)) {
TypeSafeExpressionValueResolver<Object> valueResolver = new TypeSafeExpressionValueResolver<>(expression, getType(type), type);
valueResolver.setTransformationService(muleContext.getTransformationService());
valueResolver.setExtendedExpressionManager(muleContext.getExpressionManager());
resolver = valueResolver;
} else {
TypeSafeValueResolverWrapper typeSafeValueResolverWrapper = new TypeSafeValueResolverWrapper<>(new StaticValueResolver<>(expression), getType(type));
typeSafeValueResolverWrapper.setTransformationService(muleContext.getTransformationService());
resolver = typeSafeValueResolverWrapper;
}
initialiseIfNeeded(resolver, muleContext);
return resolver;
} catch (InitialisationException e) {
throw new MuleRuntimeException(e);
}
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class XmlExtensionLoaderDelegate method createTnsExtensionModel.
/**
* Transforms the current <module/> by stripping out the <body/>'s content, so that there are not parsing errors, to generate
* a simpler {@link ExtensionModel} if there are references to the TNS prefix defined by the {@link #XMLNS_TNS}.
*
* @param resource <module/>'s resource
* @param extensions complete list of extensions the current module depends on
* @return an {@link ExtensionModel} if there's a {@link #XMLNS_TNS} defined, {@link Optional#empty()} otherwise
* @throws IOException if it fails reading the resource
*/
private Optional<ExtensionModel> createTnsExtensionModel(URL resource, Set<ExtensionModel> extensions) throws IOException {
ExtensionModel result = null;
final ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
try {
final Source xslt = new StreamSource(getClass().getClassLoader().getResourceAsStream(TRANSFORMATION_FOR_TNS_RESOURCE));
final Source moduleToTransform = new StreamSource(resource.openStream());
TransformerFactory.newInstance().newTransformer(xslt).transform(moduleToTransform, new StreamResult(resultStream));
} catch (TransformerException e) {
throw new MuleRuntimeException(createStaticMessage(format("There was an issue transforming the stream for the resource %s while trying to remove the content of the <body> element to generate an XSD", resource.getFile())), e);
}
final Document transformedModuleDocument = schemaValidatingDocumentLoader(NoOpXmlErrorHandler::new).loadDocument(extensions, resource.getFile(), new ByteArrayInputStream(resultStream.toByteArray()));
if (StringUtils.isNotBlank(transformedModuleDocument.getDocumentElement().getAttribute(XMLNS_TNS))) {
final ExtensionDeclarer extensionDeclarer = new ExtensionDeclarer();
loadModuleExtension(extensionDeclarer, resource, transformedModuleDocument, extensions, true);
result = createExtensionModel(extensionDeclarer);
}
return Optional.ofNullable(result);
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class MuleLoggerContextFactory method getLogConfig.
/**
* Checks if there's an app-specific logging configuration available, scope the lookup to this classloader only, as
* getResource() will delegate to parents locate xml config first, fallback to properties format if not found
*
* @param localResourceLocator
* @return
*/
private URI getLogConfig(LocalResourceLocator localResourceLocator) {
URL appLogConfig = localResourceLocator.findLocalResource("log4j2-test.xml");
if (appLogConfig == null) {
appLogConfig = localResourceLocator.findLocalResource("log4j2.xml");
}
if (appLogConfig == null) {
File defaultConfigFile = new File(getMuleBase(), "conf");
defaultConfigFile = new File(defaultConfigFile, "log4j2.xml");
try {
appLogConfig = defaultConfigFile.toURI().toURL();
} catch (MalformedURLException e) {
throw new MuleRuntimeException(createStaticMessage("Could not locate log config in MULE_HOME"), e);
}
}
try {
return appLogConfig.toURI();
} catch (URISyntaxException e) {
throw new MuleRuntimeException(createStaticMessage("Could not read log file " + appLogConfig), e);
}
}
Aggregations