You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/01/18 03:43:23 UTC

[incubator-servicecomb-java-chassis] 02/05: SCB-251 Object codec as json: byte[], so that protobuf can transfer Object.

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

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 39b9903d4a5b2c6105845848b919194e41dcd472
Author: wujimin <wu...@huawei.com>
AuthorDate: Thu Jan 18 00:17:08 2018 +0800

    SCB-251 Object codec as json: byte[], so that protobuf can transfer Object.
---
 .../codec/protobuf/utils/ProtobufSchemaUtils.java  | 46 +++++++++++++++++++++-
 .../protobuf/utils/TestProtobufSchemaUtils.java    | 16 ++++++++
 2 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/utils/ProtobufSchemaUtils.java b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/utils/ProtobufSchemaUtils.java
index 9006354..14fb7a3 100644
--- a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/utils/ProtobufSchemaUtils.java
+++ b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/utils/ProtobufSchemaUtils.java
@@ -17,6 +17,7 @@
 
 package org.apache.servicecomb.codec.protobuf.utils;
 
+import java.io.IOException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Parameter;
 import java.lang.reflect.Type;
@@ -28,22 +29,60 @@ import java.util.concurrent.ConcurrentHashMap;
 import org.apache.servicecomb.codec.protobuf.utils.schema.WrapSchemaFactory;
 import org.apache.servicecomb.common.javassist.JavassistUtils;
 import org.apache.servicecomb.core.definition.OperationMeta;
+import org.apache.servicecomb.foundation.common.utils.JsonUtils;
 import org.springframework.util.ClassUtils;
 
 import com.fasterxml.jackson.databind.JavaType;
 import com.fasterxml.jackson.databind.type.TypeFactory;
 
+import io.protostuff.Input;
+import io.protostuff.Output;
+import io.protostuff.Pipe;
 import io.protostuff.Schema;
+import io.protostuff.WireFormat.FieldType;
+import io.protostuff.runtime.DefaultIdStrategy;
+import io.protostuff.runtime.Delegate;
 import io.protostuff.runtime.ProtobufCompatibleUtils;
+import io.protostuff.runtime.RuntimeEnv;
 import io.protostuff.runtime.RuntimeSchema;
 
 public final class ProtobufSchemaUtils {
   private static volatile Map<String, WrapSchema> schemaCache = new ConcurrentHashMap<>();
 
   static {
+    initProtobufObjectCodec();
     ProtobufCompatibleUtils.init();
   }
 
+  protected static void initProtobufObjectCodec() {
+    ((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY).registerDelegate(new Delegate<Object>() {
+      @Override
+      public FieldType getFieldType() {
+        return FieldType.BYTES;
+      }
+
+      @Override
+      public Object readFrom(Input input) throws IOException {
+        return JsonUtils.readValue(input.readByteArray(), Object.class);
+      }
+
+      @Override
+      public void writeTo(Output output, int number, Object value, boolean repeated) throws IOException {
+        output.writeByteArray(number, JsonUtils.writeValueAsBytes(value), false);
+      }
+
+      @Override
+      public void transfer(Pipe pipe, Input input, Output output, int number, boolean repeated) throws IOException {
+        throw new IllegalStateException("not support.");
+      }
+
+      @Override
+      public Class<?> typeClass() {
+        return Object.class;
+      }
+    });
+  }
+
   private interface SchemaCreator {
     WrapSchema create() throws Exception;
   }
@@ -83,12 +122,15 @@ public final class ProtobufSchemaUtils {
   }
 
   private static boolean isNeedWrap(Class<?> cls) {
-    // protobuf不支持原子类型、enum、string、数组、collection等等作为msg,只有Object类型才可以
+    // protobuf不支持原子类型、enum、string、数组、collection等等作为msg
+    // 只有pojo类型才可以
+    // java.lang.Object也不可以,因为这可以是任意类型,结果不确定
     return ClassUtils.isPrimitiveOrWrapper(cls) || cls.isArray() || cls.isEnum()
         || String.class.isAssignableFrom(cls)
         || Collection.class.isAssignableFrom(cls)
         || Map.class.isAssignableFrom(cls)
-        || Date.class.isAssignableFrom(cls);
+        || Date.class.isAssignableFrom(cls)
+        || Object.class.equals(cls);
   }
 
   // 为了支持method args的场景,全部实现ProtobufMessageWrapper接口,有的场景有点浪费,不过无关紧要
diff --git a/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/utils/TestProtobufSchemaUtils.java b/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/utils/TestProtobufSchemaUtils.java
index af9c5c7..883edb0 100644
--- a/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/utils/TestProtobufSchemaUtils.java
+++ b/common/common-protobuf/src/test/java/org/apache/servicecomb/codec/protobuf/utils/TestProtobufSchemaUtils.java
@@ -22,6 +22,7 @@ import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.servicecomb.common.javassist.FieldConfig;
+import org.hamcrest.Matchers;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -117,4 +118,19 @@ public class TestProtobufSchemaUtils {
 
     return schema.readObject(input);
   }
+
+  @Test
+  public void object() throws Exception {
+    WrapSchema schema = ProtobufSchemaUtils.getOrCreateSchema(Object.class);
+
+    LinkedBuffer linkedBuf = LinkedBuffer.allocate();
+    ProtobufOutput output = new ProtobufOutput(linkedBuf);
+    schema.writeObject(output, 1);
+
+    Input input = new ByteArrayInput(output.toByteArray(), false);
+    Object result = schema.readObject(input);
+
+    Assert.assertEquals(1, result);
+    Assert.assertThat(result, Matchers.instanceOf(Integer.class));
+  }
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.