Search in sources :

Example 31 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class MuleRegistryHelperTestCase method closestToTypesTransformerIsFoundEvenIfWeightIsLess.

@Test
public void closestToTypesTransformerIsFoundEvenIfWeightIsLess() throws Exception {
    Transformer bananaToBloodOrange = new MockConverterBuilder().named("bananaToBloodOrange").from(BANANA_DATA_TYPE).to(BLOOD_ORANGE_DATA_TYPE).weighting(10).build();
    Transformer bananaToOrange = new MockConverterBuilder().named("bananaToOrange").from(BANANA_DATA_TYPE).to(ORANGE_DATA_TYPE).weighting(1).build();
    ((MuleContextWithRegistries) muleContext).getRegistry().registerTransformer(bananaToBloodOrange);
    ((MuleContextWithRegistries) muleContext).getRegistry().registerTransformer(bananaToOrange);
    Transformer trans = ((MuleContextWithRegistries) muleContext).getRegistry().lookupTransformer(BANANA_DATA_TYPE, ORANGE_DATA_TYPE);
    assertThat(trans, is(notNullValue()));
    assertThat(trans.getName(), is("bananaToOrange"));
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) Test(org.junit.Test)

Example 32 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class MuleRegistryHelperTransformerLookupTestCase method cachesTransformerResolvers.

@Test
public void cachesTransformerResolvers() throws Exception {
    Transformer transformer1 = muleRegistryHelper.lookupTransformer(DataType.STRING, ORANGE_DATA_TYPE);
    Transformer transformer2 = muleRegistryHelper.lookupTransformer(ORANGE_DATA_TYPE, DataType.STRING);
    Mockito.verify(registry, times(0)).lookupObjects(TransformerResolver.class);
    assertEquals(stringToOrange, transformer1);
    assertEquals(orangeToString, transformer2);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 33 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class TypeBasedTransformerResolver method calculateTransformerWeightings.

private List<TransformerWeighting> calculateTransformerWeightings(List<Transformer> transformers, Class input, Class output) {
    List<TransformerWeighting> weightings = new ArrayList<>(transformers.size());
    for (Transformer transformer : transformers) {
        TransformerWeighting transformerWeighting = new TransformerWeighting(input, output, transformer);
        weightings.add(transformerWeighting);
    }
    Collections.sort(weightings);
    return weightings;
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) ArrayList(java.util.ArrayList)

Example 34 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class TypeBasedTransformerResolver method resolve.

public Transformer resolve(DataType source, DataType result) throws ResolverException {
    Transformer transformer = exactTransformerCache.get(source.toString() + result.toString());
    if (transformer != null) {
        return transformer;
    }
    MuleRegistry registry = ((MuleContextWithRegistries) muleContext).getRegistry();
    List<Transformer> trans = registry.lookupTransformers(source, result);
    Transformer compositeTransformer = graphTransformerResolver.resolve(source, result);
    if (compositeTransformer != null) {
        // Needs to create a new list because the lookup returns a cached instance
        trans = new LinkedList<>(trans);
        trans.add(compositeTransformer);
    }
    transformer = getNearestTransformerMatch(trans, source.getType(), result.getType());
    // If an exact mach is not found, we have a 'second pass' transformer that can be used to converting to String or
    // byte[]
    Transformer secondPass;
    if (transformer == null) {
        // using Object.class and then convert to String or byte[] using the second pass transformer
        if (String.class.equals(result.getType())) {
            secondPass = objectToString;
        } else if (byte[].class.equals(result.getType())) {
            secondPass = objectToByteArray;
        } else {
            return null;
        }
        // Perform a more general search
        trans = registry.lookupTransformers(source, DataType.OBJECT);
        transformer = getNearestTransformerMatch(trans, source.getType(), result.getType());
        if (transformer != null) {
            transformer = new TransformerChain(transformer, secondPass);
            try {
                registry.registerTransformer(transformer);
            } catch (MuleException e) {
                throw new ResolverException(e.getI18nMessage(), e);
            }
        }
    }
    if (transformer != null) {
        exactTransformerCache.put(source.toString() + result.toString(), transformer);
    }
    return transformer;
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) ResolverException(org.mule.runtime.core.internal.transformer.ResolverException) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) TransformerChain(org.mule.runtime.core.privileged.transformer.TransformerChain) MuleException(org.mule.runtime.api.exception.MuleException)

Example 35 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class ExtendedTransformationService method applyAllTransformers.

private Message applyAllTransformers(final Message message, final CoreEvent event, final List<? extends Transformer> transformers) throws MuleException {
    Message result = message;
    if (!transformers.isEmpty()) {
        for (int index = 0; index < transformers.size(); index++) {
            Transformer transformer = transformers.get(index);
            Class<?> srcCls = result.getPayload().getDataType().getType();
            DataType originalSourceType = DataType.fromType(srcCls);
            if (transformer.isSourceDataTypeSupported(originalSourceType)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Using " + transformer + " to transform payload.");
                }
                result = transformMessage(result, event, transformer);
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("Transformer " + transformer + " doesn't support the source payload: " + srcCls);
                }
                if (canSkipTransformer(result, transformers, index)) {
                    continue;
                }
                // Resolves implicit conversion if possible
                Transformer implicitTransformer = muleContext.getDataTypeConverterResolver().resolve(originalSourceType, transformer.getSourceDataTypes());
                if (implicitTransformer != null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Performing implicit transformation with: " + transformer);
                    }
                    result = transformMessage(result, event, implicitTransformer);
                    result = transformMessage(result, event, transformer);
                } else {
                    throw new IllegalArgumentException("Cannot apply transformer " + transformer + " on source payload: " + srcCls);
                }
            }
        }
        Transformer lastTransformer = transformers.get(transformers.size() - 1);
        DataType returnDataType = lastTransformer.getReturnDataType();
        checkResultDataType(message, returnDataType, result.getPayload().getValue());
    }
    return result;
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MessageTransformer(org.mule.runtime.core.api.transformer.MessageTransformer) Message(org.mule.runtime.api.message.Message) DataType(org.mule.runtime.api.metadata.DataType)

Aggregations

Transformer (org.mule.runtime.core.api.transformer.Transformer)87 Test (org.junit.Test)65 SmallTest (org.mule.tck.size.SmallTest)54 MockConverterBuilder (org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder)37 Message (org.mule.runtime.api.message.Message)35 MockTransformerBuilder (org.mule.runtime.core.internal.transformer.builder.MockTransformerBuilder)19 DataType (org.mule.runtime.api.metadata.DataType)14 Converter (org.mule.runtime.core.api.transformer.Converter)12 ArrayList (java.util.ArrayList)10 TransformerException (org.mule.runtime.core.api.transformer.TransformerException)10 AbstractTransformer (org.mule.runtime.core.api.transformer.AbstractTransformer)9 CompositeConverter (org.mule.runtime.core.privileged.transformer.CompositeConverter)9 Serializable (java.io.Serializable)6 MuleContextWithRegistries (org.mule.runtime.core.internal.context.MuleContextWithRegistries)6 TransformerWeighting (org.mule.runtime.core.internal.registry.TransformerWeighting)6 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)4 MessageTransformerException (org.mule.runtime.core.api.transformer.MessageTransformerException)4 IOException (java.io.IOException)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3