Search in sources :

Example 41 with MuleRuntimeException

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);
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) StaticValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver) ReflectiveMethodOperationExecutor(org.mule.runtime.module.extension.internal.runtime.operation.ReflectiveMethodOperationExecutor) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 42 with MuleRuntimeException

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();
        }
    }
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Scheduler(org.mule.runtime.core.api.source.scheduler.Scheduler) PollingSource(org.mule.runtime.extension.api.runtime.source.PollingSource) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ResolverSetBasedObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.ResolverSetBasedObjectBuilder) ValueResolvingContext(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolvingContext) PollingSource(org.mule.runtime.extension.api.runtime.source.PollingSource) Source(org.mule.runtime.extension.api.runtime.source.Source) PollingSourceWrapper(org.mule.runtime.module.extension.internal.runtime.source.poll.PollingSourceWrapper) MuleException(org.mule.runtime.api.exception.MuleException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 43 with MuleRuntimeException

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);
    }
}
Also used : InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 44 with MuleRuntimeException

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);
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ExtensionDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) StreamSource(javax.xml.transform.stream.StreamSource) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Document(org.w3c.dom.Document) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException)

Example 45 with MuleRuntimeException

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);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URL(java.net.URL)

Aggregations

MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)123 IOException (java.io.IOException)22 List (java.util.List)22 MuleException (org.mule.runtime.api.exception.MuleException)22 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)22 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)22 Map (java.util.Map)20 Optional (java.util.Optional)20 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)18 ArrayList (java.util.ArrayList)17 String.format (java.lang.String.format)16 File (java.io.File)15 HashMap (java.util.HashMap)15 HashSet (java.util.HashSet)13 Set (java.util.Set)13 Collectors.toList (java.util.stream.Collectors.toList)12 ConfigurationException (org.mule.runtime.core.api.config.ConfigurationException)12 ComponentIdentifier (org.mule.runtime.api.component.ComponentIdentifier)10 Collections.emptyMap (java.util.Collections.emptyMap)9 Optional.empty (java.util.Optional.empty)9