Search in sources :

Example 31 with Converter

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

the class GraphTransformerResolver method resolve.

@Override
public Transformer resolve(DataType source, DataType result) throws ResolverException {
    String cacheKey = getDataTypeSourceResultPairHash(source, result);
    readWriteLock.readLock().lock();
    try {
        if (cache.containsKey(cacheKey)) {
            return (Converter) cache.get(cacheKey);
        }
    } finally {
        readWriteLock.readLock().unlock();
    }
    List<Converter> converters = converterFilter.filter(lookupStrategyTransformation.lookupConverters(source, result), source, result);
    if (converters.size() > 1) {
        throw new ResolverException(CoreMessages.transformHasMultipleMatches(source.getType(), result.getType(), converters));
    }
    Transformer converter = (converters.size() == 0) ? null : converters.get(0);
    readWriteLock.writeLock().lock();
    try {
        cache.put(cacheKey, converter);
    } finally {
        readWriteLock.writeLock().unlock();
    }
    return converter;
}
Also used : ResolverException(org.mule.runtime.core.internal.transformer.ResolverException) Transformer(org.mule.runtime.core.api.transformer.Transformer) Converter(org.mule.runtime.core.api.transformer.Converter)

Example 32 with Converter

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

the class PriorityWeightingConverterFilter method filter.

@Override
public List<Converter> filter(List<Converter> converters, DataType source, DataType result) {
    if (converters.size() == 0) {
        return Collections.emptyList();
    }
    List<TransformerWeighting> weightings = getTransformerWeightings(converters, source.getType(), result.getType());
    TransformerWeighting transformerWeighting = weightings.get(weightings.size() - 1);
    int index = weightings.size() - 2;
    List<Converter> heaviestConverter = new LinkedList<>();
    heaviestConverter.add((Converter) transformerWeighting.getTransformer());
    for (; index > -1; --index) {
        if (weightings.get(index).compareTo(transformerWeighting) < 0) {
            break;
        } else {
            heaviestConverter.add((Converter) weightings.get(index).getTransformer());
        }
    }
    return heaviestConverter;
}
Also used : TransformerWeighting(org.mule.runtime.core.internal.registry.TransformerWeighting) Converter(org.mule.runtime.core.api.transformer.Converter) LinkedList(java.util.LinkedList)

Example 33 with Converter

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

the class TransformationGraphLookupStrategy method lookupConverters.

/**
 * Looks for {@link Converter} to convert from the source to the target data types.
 * All {@link Converter}s found will have a source that is compatible with {@param source}
 * (since if a converter can convert a super type, it should be able to convert any type that extends it)
 * and a target such that {@param target} isCompatibleWith() the {@link Converter}'s one
 * (since if we want a converter that returns an specific type, it should return exactly that type or any type that extends it.)
 *
 * @param source data type to be converted
 * @param target data type to be converted to
 * @return a list of {@link Converter} that are able to convert from the source to the target data types.
 */
public List<Converter> lookupConverters(DataType source, DataType target) {
    List<Converter> converters = new LinkedList<>();
    if (!graph.containsVertexOrSuper(source)) {
        return converters;
    }
    if (!graph.containsVertexOrSub(target)) {
        return converters;
    }
    // Since we should have all possible transformations we should check for them all.
    List<DataType> compatibleSourceVertexes = graph.getSuperVertexes(source);
    List<DataType> compatibleTargetVertexes = graph.getSubVertexes(target);
    List<List<TransformationEdge>> transformationPaths = new LinkedList<>();
    for (DataType sourceVertex : compatibleSourceVertexes) {
        for (DataType targetVertex : compatibleTargetVertexes) {
            transformationPaths.addAll(findTransformationPaths(sourceVertex, targetVertex, new HashSet<>()));
        }
    }
    converters = createConverters(transformationPaths);
    return converters;
}
Also used : Converter(org.mule.runtime.core.api.transformer.Converter) CompositeConverter(org.mule.runtime.core.privileged.transformer.CompositeConverter) DataType(org.mule.runtime.api.metadata.DataType) List(java.util.List) LinkedList(java.util.LinkedList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 34 with Converter

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

the class TransformationGraphLookupStrategy method createConverters.

private List<Converter> createConverters(List<List<TransformationEdge>> transformationPaths) {
    // Using a set instead of a list for when a path of just one converter is found multiple times.
    Set<Converter> converters = new HashSet<>();
    for (List<TransformationEdge> transformationPath : transformationPaths) {
        Converter[] pathConverters = new Converter[transformationPath.size()];
        int index = 0;
        for (TransformationEdge edge : transformationPath) {
            pathConverters[index++] = edge.getConverter();
        }
        Converter converter;
        if (transformationPath.size() == 1) {
            converter = transformationPath.get(0).getConverter();
        } else {
            converter = new CompositeConverter(pathConverters);
        }
        converters.add(converter);
    }
    return new ArrayList<>(converters);
}
Also used : ArrayList(java.util.ArrayList) Converter(org.mule.runtime.core.api.transformer.Converter) CompositeConverter(org.mule.runtime.core.privileged.transformer.CompositeConverter) HashSet(java.util.HashSet) CompositeConverter(org.mule.runtime.core.privileged.transformer.CompositeConverter)

Example 35 with Converter

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

the class CompositeConverterTestCase method isSourceDataTypeSupported.

@Test
public void isSourceDataTypeSupported() {
    Converter converter = mock(Converter.class);
    when(converter.isSourceDataTypeSupported(DataType.STRING)).thenReturn(true);
    CompositeConverter chain = new CompositeConverter(converter);
    assertTrue(chain.isSourceDataTypeSupported(DataType.STRING));
}
Also used : Converter(org.mule.runtime.core.api.transformer.Converter) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

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