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"));
}
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);
}
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;
}
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;
}
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;
}
Aggregations