Author: ningjiang
Date: Fri Aug 27 07:46:36 2010
New Revision: 990064

URL: http://svn.apache.org/viewvc?rev=990064&view=rev
Log:
Refactor the ProtobufDataFormat for the double check locking issue

Modified:
    
camel/trunk/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java

Modified: 
camel/trunk/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java?rev=990064&r1=990063&r2=990064&view=diff
==============================================================================
--- 
camel/trunk/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java
 (original)
+++ 
camel/trunk/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java
 Fri Aug 27 07:46:36 2010
@@ -61,6 +61,19 @@ public class ProtobufDataFormat implemen
         }
     }
     
+    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 {
+                if (defaultInstance == null) {
+                    defaultInstance = loadDefaultInstance(instanceClassName, 
exchange.getContext());
+                }
+            }
+        }
+        return defaultInstance;
+    }
+    
     public void setInstanceClass(String className) throws Exception {
         ObjectHelper.notNull(className, "ProtobufDataFormat instaceClass");
         instanceClassName = className;
@@ -97,22 +110,11 @@ public class ProtobufDataFormat implemen
      * java.io.InputStream)
      */
     public Object unmarshal(Exchange exchange, InputStream inputStream) throws 
Exception {
-               
-        if (defaultInstance == null) {
-            if (instanceClassName == null) {
-                throw new CamelException("There is not defaultInstance for 
protobuf unmarshaling");
-            } else {
-                synchronized (this) {
-                    if (defaultInstance == null) {
-                        defaultInstance = 
loadDefaultInstance(instanceClassName, exchange.getContext());
-                    }
-                }
-            }
-        }
-        Builder builder = 
this.defaultInstance.newBuilderForType().mergeFrom(inputStream);
+        Message instance = getInstance(exchange);
+        Builder builder = instance.newBuilderForType().mergeFrom(inputStream);
         if (!builder.isInitialized()) {
             // TODO which exception should be thrown here?
-            throw new InvalidPayloadException(exchange, 
this.defaultInstance.getClass());
+            throw new InvalidPayloadException(exchange, instance.getClass());
         }
 
         return builder.build();


Reply via email to