Search in sources :

Example 6 with Transformer

use of org.apache.camel.spi.Transformer in project camel by apache.

the class ContractAdvice method applyTransformerChain.

private boolean applyTransformerChain(Message message, DataType from, DataType to) throws Exception {
    CamelContext context = message.getExchange().getContext();
    Transformer fromTransformer = context.resolveTransformer(from.getModel());
    Transformer toTransformer = context.resolveTransformer(to.getModel());
    if (fromTransformer != null && toTransformer != null) {
        LOG.debug("Applying transformer 1/2: from='{}', to='{}', transformer='{}'", from, to, fromTransformer);
        fromTransformer.transform(message, from, new DataType(Object.class));
        LOG.debug("Applying transformer 2/2: from='{}', to='{}', transformer='{}'", from, to, toTransformer);
        toTransformer.transform(message, new DataType(Object.class), to);
        return true;
    }
    return false;
}
Also used : CamelContext(org.apache.camel.CamelContext) Transformer(org.apache.camel.spi.Transformer) DataType(org.apache.camel.spi.DataType)

Example 7 with Transformer

use of org.apache.camel.spi.Transformer in project camel by apache.

the class TransformerBuilderTest method testEndpointTransformer.

public void testEndpointTransformer() throws Exception {
    CamelContext ctx = new DefaultCamelContext();
    RouteBuilder builder = new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            transformer().fromType("json:foo").toType("xml:bar").withUri("direct:transformer");
            from("direct:transformer").log("test");
        }
    };
    ctx.addRoutes(builder);
    ctx.start();
    Transformer transformer = ctx.resolveTransformer(new DataType("json:foo"), new DataType("xml:bar"));
    assertNotNull(transformer);
    assertEquals(ProcessorTransformer.class, transformer.getClass());
    ProcessorTransformer pt = (ProcessorTransformer) transformer;
    Field f = ProcessorTransformer.class.getDeclaredField("processor");
    f.setAccessible(true);
    Object processor = f.get(pt);
    assertEquals(SendProcessor.class, processor.getClass());
    SendProcessor sp = (SendProcessor) processor;
    assertEquals("direct://transformer", sp.getEndpoint().getEndpointUri());
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) Field(java.lang.reflect.Field) ProcessorTransformer(org.apache.camel.impl.transformer.ProcessorTransformer) Transformer(org.apache.camel.spi.Transformer) DataFormatTransformer(org.apache.camel.impl.transformer.DataFormatTransformer) DataType(org.apache.camel.spi.DataType) SendProcessor(org.apache.camel.processor.SendProcessor) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProcessorTransformer(org.apache.camel.impl.transformer.ProcessorTransformer)

Example 8 with Transformer

use of org.apache.camel.spi.Transformer in project camel by apache.

the class DefaultTransformerRegistry method put.

@Override
public Transformer put(TransformerKey key, Transformer transformer) {
    // at first we must see if the key already exists and then replace it back, so it stays the same spot
    Transformer answer = staticMap.remove(key);
    if (answer != null) {
        // replace existing
        staticMap.put(key, transformer);
        return answer;
    }
    answer = super.remove(key);
    if (answer != null) {
        // replace existing
        super.put(key, transformer);
        return answer;
    }
    // we want transformers to be static if they are part of setting up or starting routes
    if (context.isSetupRoutes() || context.isStartingRoutes()) {
        answer = staticMap.put(key, transformer);
    } else {
        answer = super.put(key, transformer);
    }
    return answer;
}
Also used : Transformer(org.apache.camel.spi.Transformer)

Example 9 with Transformer

use of org.apache.camel.spi.Transformer in project camel by apache.

the class DefaultTransformerRegistry method resolveTransformer.

@Override
public Transformer resolveTransformer(TransformerKey key) {
    if (ObjectHelper.isEmpty(key.getScheme()) && key.getTo() == null) {
        return null;
    }
    // try exact match
    Transformer answer = get(aliasMap.containsKey(key) ? aliasMap.get(key) : key);
    if (answer != null || ObjectHelper.isNotEmpty(key.getScheme())) {
        return answer;
    }
    // try wildcard match for next - add an alias if matched
    TransformerKey alias = null;
    if (key.getFrom() != null && ObjectHelper.isNotEmpty(key.getFrom().getName())) {
        alias = new TransformerKey(new DataType(key.getFrom().getModel()), key.getTo());
        answer = get(alias);
    }
    if (answer == null && ObjectHelper.isNotEmpty(key.getTo().getName())) {
        alias = new TransformerKey(key.getFrom(), new DataType(key.getTo().getModel()));
        answer = get(alias);
    }
    if (answer == null && key.getFrom() != null && ObjectHelper.isNotEmpty(key.getFrom().getName()) && ObjectHelper.isNotEmpty(key.getTo().getName())) {
        alias = new TransformerKey(new DataType(key.getFrom().getModel()), new DataType(key.getTo().getModel()));
        answer = get(alias);
    }
    if (answer == null && key.getFrom() != null) {
        alias = new TransformerKey(key.getFrom().getModel());
        answer = get(alias);
    }
    if (answer == null) {
        alias = new TransformerKey(key.getTo().getModel());
        answer = get(alias);
    }
    if (answer != null) {
        aliasMap.put(key, alias);
    }
    return answer;
}
Also used : Transformer(org.apache.camel.spi.Transformer) TransformerKey(org.apache.camel.impl.transformer.TransformerKey) DataType(org.apache.camel.spi.DataType)

Aggregations

Transformer (org.apache.camel.spi.Transformer)9 CamelContext (org.apache.camel.CamelContext)5 DataType (org.apache.camel.spi.DataType)5 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)3 DataFormatTransformer (org.apache.camel.impl.transformer.DataFormatTransformer)3 ProcessorTransformer (org.apache.camel.impl.transformer.ProcessorTransformer)3 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 CompositeData (javax.management.openmbean.CompositeData)1 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)1 CompositeType (javax.management.openmbean.CompositeType)1 TabularData (javax.management.openmbean.TabularData)1 TabularDataSupport (javax.management.openmbean.TabularDataSupport)1 TransformerKey (org.apache.camel.impl.transformer.TransformerKey)1 StringDataFormat (org.apache.camel.model.dataformat.StringDataFormat)1 SendProcessor (org.apache.camel.processor.SendProcessor)1