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