CAMEL-7000: Avro data format should init in start|stop logic.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c388c53e Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c388c53e Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c388c53e Branch: refs/heads/camel-2.12.x Commit: c388c53e87e629295c08374a41dd29e7750daafb Parents: fc5dc86 Author: Claus Ibsen <davscl...@apache.org> Authored: Sat Nov 23 13:10:57 2013 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sat Nov 23 13:12:21 2013 +0100 ---------------------------------------------------------------------- .../camel/dataformat/avro/AvroDataFormat.java | 43 ++++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/c388c53e/components/camel-avro/src/main/java/org/apache/camel/dataformat/avro/AvroDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-avro/src/main/java/org/apache/camel/dataformat/avro/AvroDataFormat.java b/components/camel-avro/src/main/java/org/apache/camel/dataformat/avro/AvroDataFormat.java index 3297bec..3e1ed05 100644 --- a/components/camel-avro/src/main/java/org/apache/camel/dataformat/avro/AvroDataFormat.java +++ b/components/camel-avro/src/main/java/org/apache/camel/dataformat/avro/AvroDataFormat.java @@ -31,15 +31,16 @@ import org.apache.avro.io.Encoder; import org.apache.avro.io.EncoderFactory; import org.apache.avro.specific.SpecificDatumReader; import org.apache.avro.specific.SpecificDatumWriter; - import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; import org.apache.camel.CamelException; import org.apache.camel.Exchange; import org.apache.camel.spi.DataFormat; -import org.apache.camel.util.ObjectHelper; +import org.apache.camel.support.ServiceSupport; -public class AvroDataFormat implements DataFormat { +public class AvroDataFormat extends ServiceSupport implements DataFormat, CamelContextAware { + private CamelContext camelContext; private Schema schema; private String instanceClassName; @@ -50,11 +51,28 @@ public class AvroDataFormat implements DataFormat { this.schema = schema; } - public synchronized Schema getSchema(Exchange exchange, Object graph) throws Exception { + public CamelContext getCamelContext() { + return camelContext; + } + + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + @Override + protected void doStart() throws Exception { + if (instanceClassName != null) { + schema = loadDefaultSchema(instanceClassName, camelContext); + } + } + + @Override + protected void doStop() throws Exception { + // noop + } + + public Schema getSchema(Exchange exchange, Object graph) throws Exception { if (schema == null) { - if (instanceClassName != null) { - return loadDefaultSchema(instanceClassName, exchange.getContext()); - } if (graph != null && graph instanceof GenericContainer) { return loadDefaultSchema(graph.getClass().getName(), exchange.getContext()); } else { @@ -73,10 +91,13 @@ public class AvroDataFormat implements DataFormat { } public void setInstanceClass(String className) throws Exception { - ObjectHelper.notNull(className, "AvroDataFormat messageClass"); instanceClassName = className; } + public String getInstanceClassName() { + return instanceClassName; + } + protected Schema loadDefaultSchema(String className, CamelContext context) throws CamelException, ClassNotFoundException { Class<?> instanceClass = context.getClassResolver().resolveMandatoryClass(className); if (GenericContainer.class.isAssignableFrom(instanceClass)) { @@ -84,12 +105,10 @@ public class AvroDataFormat implements DataFormat { Method method = instanceClass.getMethod("getSchema", new Class[0]); return (Schema) method.invoke(instanceClass.newInstance(), new Object[0]); } catch (Exception ex) { - throw new CamelException("Can't set the defaultInstance of AvroDataFormat with " - + className + ", caused by " + ex); + throw new CamelException("Error calling getSchema on " + instanceClass, ex); } } else { - throw new CamelException("Can't set the shcema of AvroDataFormat with " - + className + ", as the class is not a subClass of SpecificData"); + throw new CamelException("Class " + instanceClass + " must be instanceof org.apache.avro.generic.GenericContainer"); } }