chickenlj closed pull request #1258: compatible:compatible with dubbox
URL: https://github.com/apache/incubator-dubbo/pull/1258
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/DubboxUtil.java 
b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/DubboxUtil.java
new file mode 100644
index 0000000000..ef55685438
--- /dev/null
+++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/DubboxUtil.java
@@ -0,0 +1,29 @@
+/*
+ * 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.
+ */
+package com.alibaba.dubbo.common.utils;
+
+/**
+ * to judge whether is dangdang dubbox or not
+ */
+public class DubboxUtil {
+    public static boolean isDubbox(String version) {
+        if (version != null && version.startsWith("2.8")) {
+            return true;
+        }
+        return false;
+    }
+}
diff --git 
a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/DubboxUtilTest.java 
b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/DubboxUtilTest.java
new file mode 100644
index 0000000000..b4d0a2a0f6
--- /dev/null
+++ 
b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/DubboxUtilTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.
+ */
+package com.alibaba.dubbo.common.utils;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DubboxUtilTest {
+    @Test
+    public void dubboxTest() {
+        String version1 = "2.5.4";
+        org.junit.Assert.assertFalse(DubboxUtil.isDubbox(version1));
+
+        String version2 = "2.8";
+        org.junit.Assert.assertTrue(DubboxUtil.isDubbox(version2));
+
+
+        String version3 = "2.8.1";
+        org.junit.Assert.assertTrue(DubboxUtil.isDubbox(version3));
+
+        String version4 = "2.8.2-SNAPSHOT";
+        org.junit.Assert.assertTrue(DubboxUtil.isDubbox(version4));
+
+
+        String version5 = "2.8.2.1";
+        Assert.assertTrue(DubboxUtil.isDubbox(version5));
+
+
+    }
+
+}
diff --git 
a/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java
 
b/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java
index c46f39267d..5d55087759 100644
--- 
a/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java
+++ 
b/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java
@@ -21,6 +21,7 @@
 import com.alibaba.dubbo.common.logger.LoggerFactory;
 import com.alibaba.dubbo.common.serialize.ObjectInput;
 import com.alibaba.dubbo.common.utils.Assert;
+import com.alibaba.dubbo.common.utils.DubboxUtil;
 import com.alibaba.dubbo.common.utils.ReflectUtils;
 import com.alibaba.dubbo.common.utils.StringUtils;
 import com.alibaba.dubbo.remoting.Channel;
@@ -91,46 +92,97 @@ public Object decode(Channel channel, InputStream input) 
throws IOException {
         setAttachment(Constants.VERSION_KEY, in.readUTF());
 
         setMethodName(in.readUTF());
-        try {
-            Object[] args;
-            Class<?>[] pts;
-            String desc = in.readUTF();
-            if (desc.length() == 0) {
-                pts = DubboCodec.EMPTY_CLASS_ARRAY;
-                args = DubboCodec.EMPTY_OBJECT_ARRAY;
-            } else {
-                pts = ReflectUtils.desc2classArray(desc);
-                args = new Object[pts.length];
-                for (int i = 0; i < args.length; i++) {
-                    try {
-                        args[i] = in.readObject(pts[i]);
-                    } catch (Exception e) {
-                        if (log.isWarnEnabled()) {
-                            log.warn("Decode argument failed: " + 
e.getMessage(), e);
+
+        boolean isDubbox = 
DubboxUtil.isDubbox(getAttachment(Constants.DUBBO_VERSION_KEY));
+
+        if (isDubbox){
+            try {
+                Object[] args;
+                Class<?>[] pts;
+
+                //Compatible with dubbox,But so far I do not understand why 
they did it
+                in.readInt();
+
+                String desc = in.readUTF();
+                if (desc.length() == 0) {
+                    pts = DubboCodec.EMPTY_CLASS_ARRAY;
+                    args = DubboCodec.EMPTY_OBJECT_ARRAY;
+                } else {
+                    pts = ReflectUtils.desc2classArray(desc);
+                    args = new Object[pts.length];
+                    for (int i = 0; i < args.length; i++) {
+                        try {
+                            args[i] = in.readObject(pts[i]);
+                        } catch (Exception e) {
+                            if (log.isWarnEnabled()) {
+                                log.warn("Decode argument failed: " + 
e.getMessage(), e);
+                            }
                         }
                     }
                 }
-            }
-            setParameterTypes(pts);
+                setParameterTypes(pts);
 
-            Map<String, String> map = (Map<String, String>) 
in.readObject(Map.class);
-            if (map != null && map.size() > 0) {
-                Map<String, String> attachment = getAttachments();
-                if (attachment == null) {
-                    attachment = new HashMap<String, String>();
+                Map<String, String> map = (Map<String, String>) 
in.readObject(Map.class);
+                if (map != null && map.size() > 0) {
+                    Map<String, String> attachment = getAttachments();
+                    if (attachment == null) {
+                        attachment = new HashMap<String, String>();
+                    }
+                    attachment.putAll(map);
+                    setAttachments(attachment);
                 }
-                attachment.putAll(map);
-                setAttachments(attachment);
-            }
-            //decode argument ,may be callback
-            for (int i = 0; i < args.length; i++) {
-                args[i] = decodeInvocationArgument(channel, this, pts, i, 
args[i]);
+                //decode argument ,may be callback
+                for (int i = 0; i < args.length; i++) {
+                    args[i] = decodeInvocationArgument(channel, this, pts, i, 
args[i]);
+                }
+
+                setArguments(args);
+
+            } catch (ClassNotFoundException e) {
+                throw new IOException(StringUtils.toString("Read invocation 
data failed.", e));
             }
+        }else {
+            try {
+                Object[] args;
+                Class<?>[] pts;
+                String desc = in.readUTF();
+                if (desc.length() == 0) {
+                    pts = DubboCodec.EMPTY_CLASS_ARRAY;
+                    args = DubboCodec.EMPTY_OBJECT_ARRAY;
+                } else {
+                    pts = ReflectUtils.desc2classArray(desc);
+                    args = new Object[pts.length];
+                    for (int i = 0; i < args.length; i++) {
+                        try {
+                            args[i] = in.readObject(pts[i]);
+                        } catch (Exception e) {
+                            if (log.isWarnEnabled()) {
+                                log.warn("Decode argument failed: " + 
e.getMessage(), e);
+                            }
+                        }
+                    }
+                }
+                setParameterTypes(pts);
+
+                Map<String, String> map = (Map<String, String>) 
in.readObject(Map.class);
+                if (map != null && map.size() > 0) {
+                    Map<String, String> attachment = getAttachments();
+                    if (attachment == null) {
+                        attachment = new HashMap<String, String>();
+                    }
+                    attachment.putAll(map);
+                    setAttachments(attachment);
+                }
+                //decode argument ,may be callback
+                for (int i = 0; i < args.length; i++) {
+                    args[i] = decodeInvocationArgument(channel, this, pts, i, 
args[i]);
+                }
 
-            setArguments(args);
+                setArguments(args);
 
-        } catch (ClassNotFoundException e) {
-            throw new IOException(StringUtils.toString("Read invocation data 
failed.", e));
+            } catch (ClassNotFoundException e) {
+                throw new IOException(StringUtils.toString("Read invocation 
data failed.", e));
+            }
         }
         return this;
     }
diff --git 
a/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboCodec.java
 
b/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboCodec.java
index 012051cbc4..a834eeab13 100644
--- 
a/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboCodec.java
+++ 
b/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboCodec.java
@@ -26,6 +26,7 @@
 import com.alibaba.dubbo.common.serialize.ObjectInput;
 import com.alibaba.dubbo.common.serialize.ObjectOutput;
 import com.alibaba.dubbo.common.serialize.Serialization;
+import com.alibaba.dubbo.common.utils.DubboxUtil;
 import com.alibaba.dubbo.common.utils.ReflectUtils;
 import com.alibaba.dubbo.common.utils.StringUtils;
 import com.alibaba.dubbo.remoting.Channel;
@@ -168,13 +169,30 @@ protected void encodeRequestData(Channel channel, 
ObjectOutput out, Object data)
         out.writeUTF(inv.getAttachment(Constants.VERSION_KEY));
 
         out.writeUTF(inv.getMethodName());
-        out.writeUTF(ReflectUtils.getDesc(inv.getParameterTypes()));
-        Object[] args = inv.getArguments();
-        if (args != null)
-            for (int i = 0; i < args.length; i++) {
-                out.writeObject(encodeInvocationArgument(channel, inv, i));
-            }
-        out.writeObject(inv.getAttachments());
+
+        boolean isDubbox = 
DubboxUtil.isDubbox(channel.getUrl().getParameter(Constants.DUBBO_VERSION_KEY));
+
+        if (isDubbox){
+            //Compatible with dubbox
+            out.writeInt(-1);
+            out.writeUTF(ReflectUtils.getDesc(inv.getParameterTypes()));
+            Object[] args = inv.getArguments();
+            if (args != null)
+                for (int i = 0; i < args.length; i++) {
+                    out.writeObject(encodeInvocationArgument(channel, inv, i));
+                }
+            out.writeObject(inv.getAttachments());
+        }else {
+            out.writeUTF(ReflectUtils.getDesc(inv.getParameterTypes()));
+            Object[] args = inv.getArguments();
+            if (args != null)
+                for (int i = 0; i < args.length; i++) {
+                    out.writeObject(encodeInvocationArgument(channel, inv, i));
+                }
+            out.writeObject(inv.getAttachments());
+        }
+
+
     }
 
     @Override


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to