Search in sources :

Example 1 with ResolverException

use of org.mule.runtime.core.internal.transformer.ResolverException 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 2 with ResolverException

use of org.mule.runtime.core.internal.transformer.ResolverException in project mule by mulesoft.

the class ExceptionHelperTestCase method getNonMuleExceptionCause.

@Test
public void getNonMuleExceptionCause() {
    assertThat(getNonMuleException(new ResolverException(failedToBuildMessage(), null)), nullValue());
    assertThat(getNonMuleException(new ResolverException(failedToBuildMessage(), new ConfigurationException(failedToBuildMessage(), null))), nullValue());
    assertThat(getNonMuleException(new ResolverException(failedToBuildMessage(), new ConfigurationException(failedToBuildMessage(), new IllegalArgumentException()))), instanceOf(IllegalArgumentException.class));
    assertThat(getNonMuleException(new ResolverException(failedToBuildMessage(), new ConfigurationException(failedToBuildMessage(), new IllegalArgumentException(new NullPointerException())))), instanceOf(IllegalArgumentException.class));
    assertThat(getNonMuleException(new IllegalArgumentException()), instanceOf(IllegalArgumentException.class));
}
Also used : ResolverException(org.mule.runtime.core.internal.transformer.ResolverException) ConfigurationException(org.mule.runtime.core.api.config.ConfigurationException) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 3 with ResolverException

use of org.mule.runtime.core.internal.transformer.ResolverException 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 4 with ResolverException

use of org.mule.runtime.core.internal.transformer.ResolverException in project mule by mulesoft.

the class MuleRegistryHelper method resolveTransformer.

protected Transformer resolveTransformer(DataType source, DataType result) throws TransformerException {
    Lock readLock = transformerResolversLock.readLock();
    readLock.lock();
    try {
        for (TransformerResolver resolver : transformerResolvers) {
            try {
                Transformer trans = resolver.resolve(source, result);
                if (trans != null) {
                    return trans;
                }
            } catch (ResolverException e) {
                throw new TransformerException(CoreMessages.noTransformerFoundForMessage(source, result), e);
            }
        }
    } finally {
        readLock.unlock();
    }
    return null;
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) ResolverException(org.mule.runtime.core.internal.transformer.ResolverException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Aggregations

ResolverException (org.mule.runtime.core.internal.transformer.ResolverException)4 Transformer (org.mule.runtime.core.api.transformer.Transformer)3 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 Test (org.junit.Test)1 MuleException (org.mule.runtime.api.exception.MuleException)1 ConfigurationException (org.mule.runtime.core.api.config.ConfigurationException)1 Converter (org.mule.runtime.core.api.transformer.Converter)1 TransformerException (org.mule.runtime.core.api.transformer.TransformerException)1 MuleContextWithRegistries (org.mule.runtime.core.internal.context.MuleContextWithRegistries)1 TransformerChain (org.mule.runtime.core.privileged.transformer.TransformerChain)1 SmallTest (org.mule.tck.size.SmallTest)1