Search in sources :

Example 66 with Converter

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

the class SimpleRegistryBootstrap method registerTransformers.

@Override
protected void registerTransformers() throws MuleException {
    MuleRegistryHelper registry = (MuleRegistryHelper) ((MuleContextWithRegistries) muleContext).getRegistry();
    Map<String, Converter> converters = registry.lookupByType(Converter.class);
    for (Converter converter : converters.values()) {
        registry.notifyTransformerResolvers(converter, TransformerResolver.RegistryAction.ADDED);
    }
}
Also used : MuleRegistryHelper(org.mule.runtime.core.internal.registry.MuleRegistryHelper) Converter(org.mule.runtime.core.api.transformer.Converter)

Example 67 with Converter

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

the class MuleRegistryHelper method lookupTransformers.

/**
 * {@inheritDoc}
 */
@Override
public List<Transformer> lookupTransformers(DataType source, DataType result) {
    // To maintain the previous behaviour, we don't want to consider the result mimeType when resolving a transformer
    // and only find transformers with a targetType the same as or a super class of the expected one.
    // The same could be done for the source but since if the source expected by the transformer is more generic that
    // the provided, it will be found.
    result = builder(result).mediaType(ANY).charset((Charset) null).build();
    final String dataTypePairHash = getDataTypeSourceResultPairHash(source, result);
    List<Transformer> results = (List<Transformer>) transformerListCache.get(dataTypePairHash);
    if (results != null) {
        return results;
    }
    results = new ArrayList<>(2);
    Lock readLock = transformersLock.readLock();
    readLock.lock();
    try {
        for (Transformer transformer : transformers) {
            // going to find it here
            if (!(transformer instanceof Converter)) {
                continue;
            }
            if (result.isCompatibleWith(transformer.getReturnDataType()) && transformer.isSourceDataTypeSupported(source)) {
                results.add(transformer);
            }
        }
    } finally {
        readLock.unlock();
    }
    List<Transformer> concurrentlyAddedTransformers = (List<Transformer>) transformerListCache.putIfAbsent(dataTypePairHash, results);
    if (concurrentlyAddedTransformers != null) {
        return concurrentlyAddedTransformers;
    } else {
        return results;
    }
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) Converter(org.mule.runtime.core.api.transformer.Converter) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 68 with Converter

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

the class PriorityWeightingConverterFilter method getTransformerWeightings.

private List<TransformerWeighting> getTransformerWeightings(List<Converter> converters, Class input, Class output) {
    List<TransformerWeighting> weightings = new LinkedList<>();
    for (Converter converter : converters) {
        TransformerWeighting current = new TransformerWeighting(input, output, converter);
        weightings.add(current);
    }
    Collections.sort(weightings);
    return weightings;
}
Also used : TransformerWeighting(org.mule.runtime.core.internal.registry.TransformerWeighting) Converter(org.mule.runtime.core.api.transformer.Converter) LinkedList(java.util.LinkedList)

Example 69 with Converter

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

the class ExtendedTransformationService method canSkipTransformer.

private boolean canSkipTransformer(Message message, List<? extends Transformer> transformers, int index) {
    Transformer transformer = transformers.get(index);
    boolean skipConverter = false;
    if (transformer instanceof Converter) {
        if (index == transformers.size() - 1) {
            try {
                checkTransformerReturnClass(transformer, message.getPayload().getValue());
                skipConverter = true;
            } catch (TransformerException e) {
            // Converter cannot be skipped
            }
        } else {
            skipConverter = true;
        }
    }
    if (skipConverter) {
        logger.debug("Skipping converter: " + transformer);
    }
    return skipConverter;
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MessageTransformer(org.mule.runtime.core.api.transformer.MessageTransformer) Converter(org.mule.runtime.core.api.transformer.Converter) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Aggregations

Converter (org.mule.runtime.core.api.transformer.Converter)69 Test (org.junit.Test)56 SmallTest (org.mule.tck.size.SmallTest)55 MockConverterBuilder (org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder)48 CompositeConverter (org.mule.runtime.core.privileged.transformer.CompositeConverter)26 ArrayList (java.util.ArrayList)16 Transformer (org.mule.runtime.core.api.transformer.Transformer)12 List (java.util.List)5 DataType (org.mule.runtime.api.metadata.DataType)5 LinkedList (java.util.LinkedList)3 HashSet (java.util.HashSet)2 Lock (java.util.concurrent.locks.Lock)2 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)2 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)2 TransformerWeighting (org.mule.runtime.core.internal.registry.TransformerWeighting)2 MockTransformerBuilder (org.mule.runtime.core.internal.transformer.builder.MockTransformerBuilder)2 ImmutableList (com.google.common.collect.ImmutableList)1 Charset (java.nio.charset.Charset)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 MessageTransformer (org.mule.runtime.core.api.transformer.MessageTransformer)1