Search in sources :

Example 71 with Message

use of org.mule.runtime.api.message.Message in project mule by mulesoft.

the class DataTypeBuilderTestCase method cglibClassProxy.

@Test
public void cglibClassProxy() {
    final Message muleMessageProxy = mock(MessageTestImpl.class);
    final DataType dataType = DataType.fromObject(muleMessageProxy);
    assertThat(dataType.getType(), is(equalTo(MessageTestImpl.class)));
}
Also used : Message(org.mule.runtime.api.message.Message) DefaultFunctionDataType(org.mule.runtime.core.internal.metadata.DefaultFunctionDataType) SimpleDataType(org.mule.runtime.core.internal.metadata.SimpleDataType) FunctionDataType(org.mule.runtime.api.metadata.FunctionDataType) DefaultMapDataType(org.mule.runtime.core.internal.metadata.DefaultMapDataType) DataType(org.mule.runtime.api.metadata.DataType) DefaultCollectionDataType(org.mule.runtime.core.internal.metadata.DefaultCollectionDataType) Test(org.junit.Test)

Example 72 with Message

use of org.mule.runtime.api.message.Message in project mule by mulesoft.

the class DataTypeBuilderTestCase method cacheClean.

@Test
public void cacheClean() throws InterruptedException, ClassNotFoundException {
    ClassLoader custom = new ClassLoader(this.getClass().getClassLoader()) {

        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            if (Message.class.getName().equals(name)) {
                byte[] classBytes;
                try {
                    classBytes = toByteArray(this.getClass().getResourceAsStream("/org/mule/runtime/api/message/Message.class"));
                    return this.defineClass(null, classBytes, 0, classBytes.length);
                } catch (Exception e) {
                    return super.loadClass(name);
                }
            } else {
                return super.loadClass(name);
            }
        }
    };
    PhantomReference<ClassLoader> clRef = new PhantomReference<>(custom, new ReferenceQueue<>());
    DataType.builder().type(custom.loadClass(Message.class.getName())).build();
    custom = null;
    new PollingProber(GC_POLLING_TIMEOUT, DEFAULT_POLLING_INTERVAL).check(new JUnitLambdaProbe(() -> {
        System.gc();
        assertThat(clRef.isEnqueued(), is(true));
        return true;
    }, "A hard reference is being mantained to the type of the DataType."));
}
Also used : Message(org.mule.runtime.api.message.Message) JUnitLambdaProbe(org.mule.tck.probe.JUnitLambdaProbe) PhantomReference(java.lang.ref.PhantomReference) PollingProber(org.mule.tck.probe.PollingProber) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Example 73 with Message

use of org.mule.runtime.api.message.Message in project mule by mulesoft.

the class DataTypeBuilderTestCase method cglibInterfaceProxy.

@Test
public void cglibInterfaceProxy() {
    final Message muleMessageProxy = mock(Message.class);
    final DataType dataType = DataType.fromObject(muleMessageProxy);
    assertThat(dataType.getType(), is(equalTo(Message.class)));
}
Also used : Message(org.mule.runtime.api.message.Message) DefaultFunctionDataType(org.mule.runtime.core.internal.metadata.DefaultFunctionDataType) SimpleDataType(org.mule.runtime.core.internal.metadata.SimpleDataType) FunctionDataType(org.mule.runtime.api.metadata.FunctionDataType) DefaultMapDataType(org.mule.runtime.core.internal.metadata.DefaultMapDataType) DataType(org.mule.runtime.api.metadata.DataType) DefaultCollectionDataType(org.mule.runtime.core.internal.metadata.DefaultCollectionDataType) Test(org.junit.Test)

Example 74 with Message

use of org.mule.runtime.api.message.Message 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 75 with Message

use of org.mule.runtime.api.message.Message in project mule by mulesoft.

the class EventToMessageSequenceSplittingStrategy method split.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public MessageSequence<?> split(CoreEvent event) {
    if (expressionSplitterStrategy.hasDefaultExpression()) {
        Message msg = event.getMessage();
        Object payload = msg.getPayload().getValue();
        if (payload instanceof MessageSequence<?>) {
            return ((MessageSequence<?>) payload);
        }
        if (payload instanceof Iterator<?>) {
            return new IteratorMessageSequence(((Iterator<Object>) payload));
        }
        if (payload instanceof Collection) {
            return new CollectionMessageSequence(copyCollection((Collection) payload));
        } else if (payload instanceof CursorIteratorProvider) {
            return new IteratorMessageSequence(((CursorIteratorProvider) payload).openCursor());
        } else if (payload instanceof Iterable<?>) {
            return new IteratorMessageSequence(((Iterable<Object>) payload).iterator());
        } else if (payload instanceof Object[]) {
            return new ArrayMessageSequence((Object[]) payload);
        } else if (payload instanceof NodeList) {
            return new NodeListMessageSequence((NodeList) payload);
        } else if (payload instanceof Map<?, ?>) {
            List<Object> list = new LinkedList<>();
            Set<Map.Entry<?, ?>> set = ((Map) payload).entrySet();
            for (Map.Entry<?, ?> entry : set) {
                list.add(new ImmutableEntry<>(entry));
            }
            return new CollectionMessageSequence(list);
        }
    }
    try {
        Iterator<TypedValue<?>> valueIterator = expressionSplitterStrategy.split(event);
        if (hasMelExpression(expressionSplitterStrategy.getExpression())) {
            List<Object> iteratorCollection = new ArrayList<>();
            valueIterator.forEachRemaining(iteratorCollection::add);
            return new CollectionMessageSequence<>(iteratorCollection);
        }
        return new IteratorMessageSequence(valueIterator);
    } catch (ExpressionRuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new IllegalArgumentException(format("Could not split result of expression %s. The provided value is not instance of %s java " + "type or it's not a collection in any other format", expressionSplitterStrategy.getExpression(), new Class[] { Iterable.class, Iterator.class, MessageSequence.class, Collection.class }), e);
    }
}
Also used : ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) NodeListMessageSequence(org.mule.runtime.core.internal.routing.outbound.NodeListMessageSequence) Set(java.util.Set) Message(org.mule.runtime.api.message.Message) ArrayList(java.util.ArrayList) CursorIteratorProvider(org.mule.runtime.api.streaming.object.CursorIteratorProvider) ImmutableEntry(org.mule.runtime.internal.util.collection.ImmutableEntry) CollectionMessageSequence(org.mule.runtime.core.internal.routing.outbound.CollectionMessageSequence) Iterator(java.util.Iterator) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) TypedValue(org.mule.runtime.api.metadata.TypedValue) ImmutableEntry(org.mule.runtime.internal.util.collection.ImmutableEntry) NodeList(org.w3c.dom.NodeList) IteratorMessageSequence(org.mule.runtime.core.internal.routing.outbound.IteratorMessageSequence) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) NodeListMessageSequence(org.mule.runtime.core.internal.routing.outbound.NodeListMessageSequence) IteratorMessageSequence(org.mule.runtime.core.internal.routing.outbound.IteratorMessageSequence) CollectionMessageSequence(org.mule.runtime.core.internal.routing.outbound.CollectionMessageSequence) Collection(java.util.Collection) Map(java.util.Map)

Aggregations

Message (org.mule.runtime.api.message.Message)226 Test (org.junit.Test)189 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)104 SmallTest (org.mule.tck.size.SmallTest)68 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)46 Transformer (org.mule.runtime.core.api.transformer.Transformer)35 DataType (org.mule.runtime.api.metadata.DataType)33 ArrayList (java.util.ArrayList)26 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)26 Processor (org.mule.runtime.core.api.processor.Processor)25 List (java.util.List)20 Assert.assertThat (org.junit.Assert.assertThat)19 ExpectedException (org.junit.rules.ExpectedException)19 MuleContextUtils.eventBuilder (org.mule.tck.util.MuleContextUtils.eventBuilder)19 Map (java.util.Map)18 Rule (org.junit.Rule)18 MockConverterBuilder (org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder)18 ExpectedException.none (org.junit.rules.ExpectedException.none)17 MockTransformerBuilder (org.mule.runtime.core.internal.transformer.builder.MockTransformerBuilder)17 TypedValue (org.mule.runtime.api.metadata.TypedValue)16