Search in sources :

Example 1 with I18nMessage

use of org.mule.runtime.api.i18n.I18nMessage in project mule by mulesoft.

the class LifecycleUtils method initialiseIfNeeded.

/**
 * The same as {@link #initialiseIfNeeded(Object)}, only that before checking for {@code object} being {@link Initialisable}, it
 * uses the given {@code muleContext} to perform further initialization.
 * <p>
 * It checks if the {@code object} implements {@link MuleContextAware}, in which case it will invoke
 * {@link MuleContextAware#setMuleContext(MuleContext)} with the given {@code muleContext}.
 * <p>
 * Also depending on the value of the {@code inject} argument, it will perform dependency injection on the {@code object}
 *
 * @param object the object you're trying to initialise
 * @param inject whether it should perform dependency injection on the {@code object} before actually initialising it
 * @param muleContext a {@link MuleContext}
 * @throws InitialisationException
 * @throws IllegalArgumentException if {@code MuleContext} is {@code null}
 */
public static void initialiseIfNeeded(Object object, boolean inject, MuleContext muleContext) throws InitialisationException {
    checkArgument(muleContext != null, "muleContext cannot be null");
    object = unwrap(object);
    if (object == null) {
        return;
    }
    if (object instanceof MuleContextAware) {
        ((MuleContextAware) object).setMuleContext(muleContext);
    }
    if (inject) {
        try {
            muleContext.getInjector().inject(object);
        } catch (MuleException e) {
            I18nMessage message = createStaticMessage(format("Found exception trying to inject object of type '%s' on initialising phase", object.getClass().getName()));
            if (object instanceof Initialisable) {
                throw new InitialisationException(message, e, (Initialisable) object);
            }
            throw new MuleRuntimeException(message, e);
        }
    }
    initialiseIfNeeded(object);
}
Also used : MuleContextAware(org.mule.runtime.core.api.context.MuleContextAware) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) I18nMessage(org.mule.runtime.api.i18n.I18nMessage) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleException(org.mule.runtime.api.exception.MuleException)

Example 2 with I18nMessage

use of org.mule.runtime.api.i18n.I18nMessage in project mule by mulesoft.

the class AbstractTransformer method transform.

@Override
public Object transform(Object src, Charset enc) throws TransformerException {
    Object payload = src;
    DataType sourceType;
    if (src instanceof Message) {
        Message message = (Message) src;
        if ((!isSourceDataTypeSupported(DataType.MULE_MESSAGE, true) && !(this instanceof AbstractMessageTransformer))) {
            payload = message.getPayload().getValue();
            sourceType = message.getPayload().getDataType();
        } else {
            sourceType = DataType.fromObject(payload);
        }
    } else {
        sourceType = DataType.fromObject(payload);
    }
    if (!isSourceDataTypeSupported(sourceType)) {
        I18nMessage msg = transformOnObjectUnsupportedTypeOfEndpoint(getName(), payload.getClass());
        // / FIXME
        throw new TransformerException(msg, this);
    }
    if (logger.isDebugEnabled()) {
        logger.debug(format("Applying transformer %s (%s)", getName(), getClass().getName()));
        logger.debug(format("Object before transform: %s", StringMessageUtils.toString(payload)));
    }
    Object result = doTransform(payload, enc);
    if (logger.isDebugEnabled()) {
        logger.debug(format("Object after transform: %s", StringMessageUtils.toString(result)));
    }
    checkTransformerReturnClass(this, result);
    return result;
}
Also used : Message(org.mule.runtime.api.message.Message) I18nMessage(org.mule.runtime.api.i18n.I18nMessage) DataType(org.mule.runtime.api.metadata.DataType) I18nMessage(org.mule.runtime.api.i18n.I18nMessage)

Example 3 with I18nMessage

use of org.mule.runtime.api.i18n.I18nMessage in project mule by mulesoft.

the class AbstractMessageTransformer method transform.

@Override
public final Object transform(Object src, Charset enc, CoreEvent event) throws MessageTransformerException {
    DataType sourceType = DataType.fromType(src.getClass());
    if (!isSourceDataTypeSupported(sourceType)) {
        if (isIgnoreBadInput()) {
            logger.debug("Source type is incompatible with this transformer and property 'ignoreBadInput' is set to true, so the transformer chain will continue.");
            return src;
        } else {
            I18nMessage msg = CoreMessages.transformOnObjectUnsupportedTypeOfEndpoint(getName(), src.getClass());
            throw new MessageTransformerException(msg, this, event.getMessage());
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug(format("Applying transformer %s (%s)", getName(), getClass().getName()));
        logger.debug(format("Object before transform: %s", StringMessageUtils.toString(src)));
    }
    Message message;
    if (src instanceof Message) {
        message = (Message) src;
    } else // TODO MULE-9342 Clean up transformer vs message transformer confusion
    if (src instanceof CoreEvent) {
        event = (CoreEvent) src;
        message = event.getMessage();
    } else if (muleContext.getConfiguration().isAutoWrapMessageAwareTransform()) {
        message = of(src);
    } else {
        if (event == null) {
            throw new MessageTransformerException(CoreMessages.noCurrentEventForTransformer(), this, null);
        }
        message = event.getMessage();
    }
    Object result;
    // TODO MULE-9342 Clean up transformer vs message transformer confusion
    if (event == null) {
        MuleClientFlowConstruct flowConstruct = new MuleClientFlowConstruct(muleContext);
        ComponentLocation location = getLocation() != null ? getLocation() : fromSingleComponent("AbstractMessageTransformer");
        event = InternalEvent.builder(create(flowConstruct, location)).message(message).build();
    }
    result = transformMessage(event, enc);
    if (logger.isDebugEnabled()) {
        logger.debug(format("Object after transform: %s", StringMessageUtils.toString(result)));
    }
    result = checkReturnClass(result, event);
    return result;
}
Also used : ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) MuleClientFlowConstruct(org.mule.runtime.core.privileged.client.MuleClientFlowConstruct) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) I18nMessage(org.mule.runtime.api.i18n.I18nMessage) Message(org.mule.runtime.api.message.Message) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DataType(org.mule.runtime.api.metadata.DataType) I18nMessage(org.mule.runtime.api.i18n.I18nMessage)

Example 4 with I18nMessage

use of org.mule.runtime.api.i18n.I18nMessage in project mule by mulesoft.

the class PropertiesUtils method loadProperties.

public static Properties loadProperties(InputStream is) throws IOException {
    if (is == null) {
        I18nMessage error = CoreMessages.objectIsNull("input stream");
        throw new IOException(error.toString());
    }
    try {
        Properties props = new Properties();
        props.load(is);
        return props;
    } finally {
        is.close();
    }
}
Also used : I18nMessage(org.mule.runtime.api.i18n.I18nMessage) IOException(java.io.IOException) Properties(java.util.Properties) OrderedProperties(org.mule.runtime.core.internal.util.OrderedProperties)

Example 5 with I18nMessage

use of org.mule.runtime.api.i18n.I18nMessage in project mule by mulesoft.

the class MuleContainer method shutdown.

/**
 * Will shut down the server displaying the cause and time of the shutdown
 *
 * @param e the exception that caused the shutdown
 */
public void shutdown(Throwable e) throws MuleException {
    I18nMessage msg = fatalErrorWhileRunning();
    MuleException muleException = getRootMuleException(e);
    if (muleException != null) {
        logger.error(muleException.getDetailedMessage());
    } else {
        logger.error(msg.toString() + " " + e.getMessage(), e);
    }
    List<String> msgs = new ArrayList<>();
    msgs.add(msg.getMessage());
    Throwable root = getRootException(e);
    msgs.add(root.getMessage() + " (" + root.getClass().getName() + ")");
    msgs.add(" ");
    msgs.add(fatalErrorInShutdown().getMessage());
    String shutdownMessage = getBoilerPlate(msgs, '*', 80);
    logger.error(shutdownMessage);
    doShutdown();
    System.exit(1);
}
Also used : ArrayList(java.util.ArrayList) I18nMessage(org.mule.runtime.api.i18n.I18nMessage) MuleException(org.mule.runtime.api.exception.MuleException) ExceptionHelper.getRootMuleException(org.mule.runtime.api.exception.ExceptionHelper.getRootMuleException)

Aggregations

I18nMessage (org.mule.runtime.api.i18n.I18nMessage)5 MuleException (org.mule.runtime.api.exception.MuleException)2 Message (org.mule.runtime.api.message.Message)2 DataType (org.mule.runtime.api.metadata.DataType)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 ComponentLocation (org.mule.runtime.api.component.location.ComponentLocation)1 ExceptionHelper.getRootMuleException (org.mule.runtime.api.exception.ExceptionHelper.getRootMuleException)1 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)1 Initialisable (org.mule.runtime.api.lifecycle.Initialisable)1 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)1 MuleContextAware (org.mule.runtime.core.api.context.MuleContextAware)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)1 OrderedProperties (org.mule.runtime.core.internal.util.OrderedProperties)1 MuleClientFlowConstruct (org.mule.runtime.core.privileged.client.MuleClientFlowConstruct)1