camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/3] git commit: CAMEL-7000: Avro data format should init in start|stop logic.
Date Sat, 23 Nov 2013 12:10:38 GMT
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/9795a32f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/9795a32f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/9795a32f

Branch: refs/heads/camel-2.11.x
Commit: 9795a32f17fd94716bf8248039c151b445887fda
Parents: df97a5d
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sat Nov 23 13:10:57 2013 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat Nov 23 13:11:27 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/9795a32f/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");
         }
     }
 


Mime
View raw message