This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch 3.3.0-beta.1-release
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.3.0-beta.1-release by this 
push:
     new 99ba6e3037 Fix hard dependency on protobuf on pojo mode (#13012)
99ba6e3037 is described below

commit 99ba6e30379f89ad6c294767fbeef8afb9b9e02c
Author: icodening <[email protected]>
AuthorDate: Wed Sep 6 21:28:14 2023 +0800

    Fix hard dependency on protobuf on pojo mode (#13012)
---
 .../dubbo/remoting/http12/message/JsonCodec.java   |  3 --
 .../dubbo/remoting/http12/message/JsonPbCodec.java | 19 +++++++-
 .../protocol/tri/h12/grpc/GrpcCompositeCodec.java  | 54 ++++++++++++++++------
 3 files changed, 59 insertions(+), 17 deletions(-)

diff --git 
a/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodec.java
 
b/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodec.java
index 5ee8dfb13e..852e1d0ceb 100644
--- 
a/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodec.java
+++ 
b/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodec.java
@@ -93,9 +93,6 @@ public class JsonCodec implements HttpMessageCodec {
         List<Object> result = new ArrayList<>();
         try {
             try {
-                if (targetTypes.length == 1) {
-                    return new Object[]{this.decode(dataInputStream, 
targetTypes[0])};
-                }
                 int len;
                 byte[] data = new byte[4096];
                 StringBuilder builder = new StringBuilder(4096);
diff --git 
a/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonPbCodec.java
 
b/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonPbCodec.java
index 4ade704e97..6d7a760b7e 100644
--- 
a/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonPbCodec.java
+++ 
b/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonPbCodec.java
@@ -89,13 +89,30 @@ public class JsonPbCodec implements HttpMessageCodec {
 
     @Override
     public Object[] decode(InputStream dataInputStream, Class<?>[] 
targetTypes) throws DecodeException {
+        try {
+            if (hasProtobuf(targetTypes)) {
+                //protobuf only support one parameter
+                return new Object[]{decode(dataInputStream, targetTypes[0])};
+            }
+        } catch (Throwable e) {
+            throw new DecodeException(e);
+        }
         return jsonCodec.decode(dataInputStream, targetTypes);
     }
 
-    private boolean isProtobuf(Class<?> targetType) {
+    private static boolean isProtobuf(Class<?> targetType) {
         if (targetType == null) {
             return false;
         }
         return Message.class.isAssignableFrom(targetType);
     }
+
+    private static boolean hasProtobuf(Class<?>[] classes){
+        for (Class<?> clazz : classes) {
+            if (isProtobuf(clazz)) {
+                return true;
+            }
+        }
+        return false;
+    }
 }
diff --git 
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/h12/grpc/GrpcCompositeCodec.java
 
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/h12/grpc/GrpcCompositeCodec.java
index 5ff1b1da21..48171cf234 100644
--- 
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/h12/grpc/GrpcCompositeCodec.java
+++ 
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/h12/grpc/GrpcCompositeCodec.java
@@ -27,6 +27,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
+import static 
org.apache.dubbo.common.constants.CommonConstants.PROTOBUF_MESSAGE_CLASS_NAME;
+
 /**
  * compatible low version.
  * version < 3.3
@@ -64,11 +66,8 @@ public class GrpcCompositeCodec implements HttpMessageCodec {
         try {
             int compressed = 0;
             outputStream.write(compressed);
-            if (data instanceof Message) {
-                int serializedSize = ((Message) data).getSerializedSize();
-                //write length
-                writeLength(outputStream, serializedSize);
-                protobufHttpMessageCodec.encode(outputStream, data);
+            if (isProtobuf(data)) {
+                ProtobufWriter.write(protobufHttpMessageCodec, outputStream, 
data);
                 return;
             }
             //wrapper
@@ -80,7 +79,7 @@ public class GrpcCompositeCodec implements HttpMessageCodec {
 
     @Override
     public Object decode(InputStream inputStream, Class<?> targetType) throws 
DecodeException {
-        if (isProtobuf(targetType)) {
+        if (isProtoClass(targetType)) {
             return protobufHttpMessageCodec.decode(inputStream, targetType);
         }
         return wrapperHttpMessageCodec.decode(inputStream, targetType);
@@ -94,13 +93,6 @@ public class GrpcCompositeCodec implements HttpMessageCodec {
         return HttpMessageCodec.super.decode(inputStream, targetTypes);
     }
 
-    private boolean isProtobuf(Class<?> targetType) {
-        if (targetType == null) {
-            return false;
-        }
-        return Message.class.isAssignableFrom(targetType);
-    }
-
     private static void writeLength(OutputStream outputStream, int length) {
         try {
             outputStream.write(((length >> 24) & 0xFF));
@@ -121,4 +113,40 @@ public class GrpcCompositeCodec implements 
HttpMessageCodec {
     public boolean support(String contentType) {
         return contentType.startsWith(MEDIA_TYPE.getName());
     }
+
+    private static boolean isProtobuf(Object data) {
+        if (data == null) {
+            return false;
+        }
+        return isProtoClass(data.getClass());
+    }
+
+    private static boolean isProtoClass(Class<?> clazz) {
+        while (clazz != Object.class && clazz != null) {
+            Class<?>[] interfaces = clazz.getInterfaces();
+            if (interfaces.length > 0) {
+                for (Class<?> clazzInterface : interfaces) {
+                    if 
(PROTOBUF_MESSAGE_CLASS_NAME.equalsIgnoreCase(clazzInterface.getName())) {
+                        return true;
+                    }
+                }
+            }
+            clazz = clazz.getSuperclass();
+        }
+        return false;
+    }
+
+    /**
+     * lazy init protobuf class
+     */
+    private static class ProtobufWriter {
+
+        private static void write(HttpMessageCodec codec, OutputStream 
outputStream, Object data) {
+            int serializedSize = ((Message) data).getSerializedSize();
+            //write length
+            writeLength(outputStream, serializedSize);
+            codec.encode(outputStream, data);
+        }
+
+    }
 }

Reply via email to