Search in sources :

Example 1 with Transformer

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

the class AbstractLocalCamelController method getTransformers.

@Override
public List<Map<String, String>> getTransformers(String camelContextName) throws Exception {
    List<Map<String, String>> answer = new ArrayList<Map<String, String>>();
    if (camelContextName != null) {
        CamelContext context = this.getLocalCamelContext(camelContextName);
        if (context != null) {
            List<Transformer> transformers = new ArrayList<Transformer>(context.getTransformerRegistry().values());
            for (Transformer transformer : transformers) {
                Map<String, String> row = new LinkedHashMap<String, String>();
                row.put("camelContextName", context.getName());
                row.put("scheme", transformer.getModel());
                row.put("from", transformer.getFrom().toString());
                row.put("to", transformer.getTo().toString());
                row.put("state", transformer.getStatus().toString());
                row.put("description", transformer.toString());
                answer.add(row);
            }
        }
    }
    return answer;
}
Also used : CamelContext(org.apache.camel.CamelContext) Transformer(org.apache.camel.spi.Transformer) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with Transformer

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

the class CustomTransformerDefinition method doCreateTransformer.

@Override
protected Transformer doCreateTransformer(CamelContext context) throws Exception {
    if (ref == null && className == null) {
        throw new IllegalArgumentException("'ref' or 'className' must be specified for customTransformer");
    }
    Transformer transformer;
    if (ref != null) {
        transformer = context.getRegistry().lookupByNameAndType(ref, Transformer.class);
        if (transformer == null) {
            throw new IllegalArgumentException("Cannot find transformer with ref:" + ref);
        }
        if (transformer.getModel() != null || transformer.getFrom() != null || transformer.getTo() != null) {
            throw new IllegalArgumentException(String.format("Transformer '%s' is already in use. Please check if duplicate transformer exists.", ref));
        }
    } else {
        Class<Transformer> transformerClass = context.getClassResolver().resolveMandatoryClass(className, Transformer.class);
        if (transformerClass == null) {
            throw new IllegalArgumentException("Cannot find transformer class: " + className);
        }
        transformer = context.getInjector().newInstance(transformerClass);
    }
    transformer.setCamelContext(context);
    return transformer.setModel(getScheme()).setFrom(getFromType()).setTo(getToType());
}
Also used : Transformer(org.apache.camel.spi.Transformer)

Example 3 with Transformer

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

the class TransformerBuilderTest method testCustomTransformer.

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

        @Override
        public void configure() throws Exception {
            transformer().scheme("other").withJava(MyTransformer.class);
            from("direct:input").log("test");
        }
    };
    ctx.addRoutes(builder);
    ctx.start();
    Transformer transformer = ctx.resolveTransformer("other");
    assertNotNull(transformer);
    assertEquals(MyTransformer.class, transformer.getClass());
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) ProcessorTransformer(org.apache.camel.impl.transformer.ProcessorTransformer) Transformer(org.apache.camel.spi.Transformer) DataFormatTransformer(org.apache.camel.impl.transformer.DataFormatTransformer) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Example 4 with Transformer

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

the class TransformerBuilderTest method testDataFormatTransformer.

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

        @Override
        public void configure() throws Exception {
            transformer().fromType("xml:foo").toType("json:bar").withDataFormat(new StringDataFormat());
            from("direct:input").log("test");
        }
    };
    ctx.addRoutes(builder);
    ctx.start();
    Transformer transformer = ctx.resolveTransformer(new DataType("xml:foo"), new DataType("json:bar"));
    assertNotNull(transformer);
    assertEquals(DataFormatTransformer.class, transformer.getClass());
    DataFormatTransformer dft = (DataFormatTransformer) transformer;
    Field f = DataFormatTransformer.class.getDeclaredField("dataFormatType");
    f.setAccessible(true);
    Object dataFormatType = f.get(dft);
    assertEquals(StringDataFormat.class, dataFormatType.getClass());
}
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) StringDataFormat(org.apache.camel.model.dataformat.StringDataFormat) DataType(org.apache.camel.spi.DataType) DataFormatTransformer(org.apache.camel.impl.transformer.DataFormatTransformer) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Example 5 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)

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