You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2014/03/17 10:07:00 UTC

[04/12] git commit: CAMEL-3933: Get rid of synchronized code in unmarhal in protobuf dataformat.

CAMEL-3933: Get rid of synchronized code in unmarhal in protobuf dataformat.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/46191509
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/46191509
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/46191509

Branch: refs/heads/camel-2.13.x
Commit: 46191509a9306c8e5d03bacc9d53c41fd8a13fd9
Parents: 56bd1e6
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Mar 16 13:30:45 2014 +0100
Committer: Willem Jiang <wi...@gmail.com>
Committed: Mon Mar 17 17:04:16 2014 +0800

----------------------------------------------------------------------
 .../dataformat/protobuf/ProtobufDataFormat.java | 89 +++++++++++---------
 .../protobuf/generated/AddressBookProtos.java   | 19 ++++-
 2 files changed, 66 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/46191509/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java b/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java
index 15b47a2..1c240d4 100755
--- a/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java
+++ b/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java
@@ -14,7 +14,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.camel.dataformat.protobuf;
 
 import java.io.InputStream;
@@ -24,30 +23,36 @@ import com.google.protobuf.Message;
 import com.google.protobuf.Message.Builder;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
 import org.apache.camel.CamelException;
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.spi.DataFormat;
+import org.apache.camel.support.ServiceSupport;
 import org.apache.camel.util.ObjectHelper;
 
+public class ProtobufDataFormat extends ServiceSupport implements DataFormat, CamelContextAware {
 
-public class ProtobufDataFormat implements DataFormat {
-
+    private CamelContext camelContext;
     private Message defaultInstance;
     private String instanceClassName;
     
-    
-    /**
-     * @param defaultInstance
-     */
+    public ProtobufDataFormat() {
+    }
+
     public ProtobufDataFormat(Message defaultInstance) {
         this.defaultInstance = defaultInstance;
     }
-    
-    public ProtobufDataFormat() {
+
+    public CamelContext getCamelContext() {
+        return camelContext;
     }
-    
-    public void setDefaultInstace(Message instance) {
+
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    public void setDefaultInstance(Message instance) {
         this.defaultInstance = instance;
     }
     
@@ -59,38 +64,11 @@ public class ProtobufDataFormat implements DataFormat {
         }
     }
     
-    public synchronized Message getInstance(Exchange exchange) throws Exception {
-        if (defaultInstance == null) {
-            if (instanceClassName == null) {
-                throw new CamelException("There is not defaultInstance for protobuf unmarshaling");
-            } else {
-                defaultInstance = loadDefaultInstance(instanceClassName, exchange.getContext());
-            }
-        }
-        return defaultInstance;
-    }
-    
     public void setInstanceClass(String className) throws Exception {
         ObjectHelper.notNull(className, "ProtobufDataFormat instaceClass");
         instanceClassName = className;
     }
     
-    protected Message loadDefaultInstance(String className, CamelContext context) throws CamelException, ClassNotFoundException {
-        Class<?> instanceClass = context.getClassResolver().resolveMandatoryClass(className);
-        if (Message.class.isAssignableFrom(instanceClass)) {
-            try {
-                Method method = instanceClass.getMethod("getDefaultInstance", new Class[0]);
-                return (Message) method.invoke(null, new Object[0]);
-            } catch (Exception ex) {
-                throw new CamelException("Can't set the defaultInstance of ProtobufferDataFormat with " 
-                                         + className + ", caused by " + ex);
-            }
-        } else {
-            throw new CamelException("Can't set the defaultInstance of ProtobufferDataFormat with " 
-                  + className + ", as the class is not a subClass of com.google.protobuf.Message");
-        }
-    }
-
     /*
      * (non-Javadoc)
      * @see org.apache.camel.spi.DataFormat#marshal(org.apache.camel.Exchange,
@@ -106,14 +84,43 @@ public class ProtobufDataFormat implements DataFormat {
      * java.io.InputStream)
      */
     public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
-        Message instance = getInstance(exchange);
-        Builder builder = instance.newBuilderForType().mergeFrom(inputStream);
+        ObjectHelper.notNull(defaultInstance, "defaultInstance or instanceClassName must be set", this);
+
+        Builder builder = defaultInstance.newBuilderForType().mergeFrom(inputStream);
         if (!builder.isInitialized()) {
             // TODO which exception should be thrown here?
-            throw new InvalidPayloadException(exchange, instance.getClass());
+            throw new InvalidPayloadException(exchange, defaultInstance.getClass());
         }
 
         return builder.build();
     }
 
+    protected Message loadDefaultInstance(String className, CamelContext context) throws CamelException, ClassNotFoundException {
+        Class<?> instanceClass = context.getClassResolver().resolveMandatoryClass(className);
+        if (Message.class.isAssignableFrom(instanceClass)) {
+            try {
+                Method method = instanceClass.getMethod("getDefaultInstance", new Class[0]);
+                return (Message) method.invoke(null, new Object[0]);
+            } catch (Exception ex) {
+                throw new CamelException("Can't set the defaultInstance of ProtobufferDataFormat with "
+                        + className + ", caused by " + ex);
+            }
+        } else {
+            throw new CamelException("Can't set the defaultInstance of ProtobufferDataFormat with "
+                    + className + ", as the class is not a subClass of com.google.protobuf.Message");
+        }
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        if (defaultInstance == null && instanceClassName != null) {
+            defaultInstance = loadDefaultInstance(instanceClassName, getCamelContext());
+        }
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        // noop
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/46191509/components/camel-protobuf/src/test/java/org/apache/camel/dataformat/protobuf/generated/AddressBookProtos.java
----------------------------------------------------------------------
diff --git a/components/camel-protobuf/src/test/java/org/apache/camel/dataformat/protobuf/generated/AddressBookProtos.java b/components/camel-protobuf/src/test/java/org/apache/camel/dataformat/protobuf/generated/AddressBookProtos.java
index 33dff5f..f88a9c0 100644
--- a/components/camel-protobuf/src/test/java/org/apache/camel/dataformat/protobuf/generated/AddressBookProtos.java
+++ b/components/camel-protobuf/src/test/java/org/apache/camel/dataformat/protobuf/generated/AddressBookProtos.java
@@ -1,8 +1,25 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 // Generated by the protocol buffer compiler.  DO NOT EDIT!
 // source: addressbook.proto
-
 package org.apache.camel.dataformat.protobuf.generated;
 
+//CHECKSTYLE:OFF
 public final class AddressBookProtos {
   private AddressBookProtos() {}
   public static void registerAllExtensions(