enhance pull request 2618 (#2691)
* enhance pull request 2618 * move spi file into the right directory * ignore protostuff test case * make unit test pass, support Time type * fix useless imports issue * add license header
This commit is contained in:
parent
910b261a00
commit
b8bc80bd45
|
|
@ -1 +0,0 @@
|
|||
protobuf=org.apache.dubbo.common.serialize.protobuf.ProtobufSerialization
|
||||
|
|
@ -25,13 +25,14 @@ limitations under the License.
|
|||
<version>2.7.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>dubbo-serialization-protobuf</artifactId>
|
||||
<artifactId>dubbo-serialization-protostuff</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>${project.artifactId}</name>
|
||||
<description>The protobuf serialization module of dubbo project</description>
|
||||
<description>The protostuff serialization module of dubbo project</description>
|
||||
|
||||
<properties>
|
||||
<protobuf.version>1.5.9</protobuf.version>
|
||||
<protostuff.version>1.5.9</protostuff.version>
|
||||
<objenesis.version>2.6</objenesis.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
|
@ -43,18 +44,18 @@ limitations under the License.
|
|||
<dependency>
|
||||
<groupId>io.protostuff</groupId>
|
||||
<artifactId>protostuff-core</artifactId>
|
||||
<version>${protobuf.version}</version>
|
||||
<version>${protostuff.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.protostuff</groupId>
|
||||
<artifactId>protostuff-runtime</artifactId>
|
||||
<version>${protobuf.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.objenesis</groupId>
|
||||
<artifactId>objenesis</artifactId>
|
||||
<version>2.6</version>
|
||||
<version>${protostuff.version}</version>
|
||||
</dependency>
|
||||
<!--<dependency>-->
|
||||
<!--<groupId>org.objenesis</groupId>-->
|
||||
<!--<artifactId>objenesis</artifactId>-->
|
||||
<!--<version>${objenesis.version}</version>-->
|
||||
<!--</dependency>-->
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
@ -15,24 +15,24 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dubbo.common.serialize.protobuf;
|
||||
package org.apache.dubbo.common.serialize.protostuff;
|
||||
|
||||
import io.protostuff.ProtobufIOUtil;
|
||||
import io.protostuff.Schema;
|
||||
import io.protostuff.runtime.RuntimeSchema;
|
||||
import org.apache.dubbo.common.serialize.ObjectInput;
|
||||
import org.apache.dubbo.common.serialize.protobuf.utils.WrapperUtils;
|
||||
import org.apache.dubbo.common.serialize.protostuff.utils.WrapperUtils;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class ProtobufObjectInput implements ObjectInput {
|
||||
public class ProtostuffObjectInput implements ObjectInput {
|
||||
|
||||
private DataInputStream dis;
|
||||
|
||||
public ProtobufObjectInput(InputStream inputStream) {
|
||||
public ProtostuffObjectInput(InputStream inputStream) {
|
||||
dis = new DataInputStream(inputStream);
|
||||
}
|
||||
|
||||
|
|
@ -70,34 +70,10 @@ public class ProtobufObjectInput implements ObjectInput {
|
|||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> T readObject(Class<T> clazz) throws IOException, ClassNotFoundException {
|
||||
int classNameLength = dis.readInt();
|
||||
int bytesLength = dis.readInt();
|
||||
|
||||
if (classNameLength < 0 || bytesLength < 0) {
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
byte[] classNameBytes = new byte[classNameLength];
|
||||
dis.read(classNameBytes, 0, classNameLength);
|
||||
|
||||
byte[] bytes = new byte[bytesLength];
|
||||
dis.read(bytes, 0, bytesLength);
|
||||
|
||||
T result;
|
||||
if (WrapperUtils.needWrapper(clazz)) {
|
||||
Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class);
|
||||
Wrapper wrapper = schema.newMessage();
|
||||
ProtobufIOUtil.mergeFrom(bytes, wrapper, schema);
|
||||
result = (T) wrapper.getData();
|
||||
} else {
|
||||
Schema<T> schema = RuntimeSchema.getSchema(clazz);
|
||||
result = schema.newMessage();
|
||||
ProtobufIOUtil.mergeFrom(bytes, result, schema);
|
||||
}
|
||||
|
||||
return result;
|
||||
return (T) readObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -15,41 +15,48 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dubbo.common.serialize.protobuf;
|
||||
package org.apache.dubbo.common.serialize.protostuff;
|
||||
|
||||
import io.protostuff.*;
|
||||
import io.protostuff.LinkedBuffer;
|
||||
import io.protostuff.ProtobufIOUtil;
|
||||
import io.protostuff.Schema;
|
||||
import io.protostuff.runtime.RuntimeSchema;
|
||||
import org.apache.dubbo.common.serialize.ObjectOutput;
|
||||
import org.apache.dubbo.common.serialize.protobuf.utils.WrapperUtils;
|
||||
import org.apache.dubbo.common.serialize.protostuff.utils.WrapperUtils;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class ProtobufObjectOutput implements ObjectOutput {
|
||||
public class ProtostuffObjectOutput implements ObjectOutput {
|
||||
|
||||
private LinkedBuffer buffer = LinkedBuffer.allocate();
|
||||
private DataOutputStream dos;
|
||||
|
||||
public ProtobufObjectOutput(OutputStream outputStream) {
|
||||
public ProtostuffObjectOutput(OutputStream outputStream) {
|
||||
dos = new DataOutputStream(outputStream);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void writeObject(Object obj) throws IOException {
|
||||
LinkedBuffer buffer = LinkedBuffer.allocate();
|
||||
|
||||
byte[] bytes;
|
||||
byte[] classNameBytes;
|
||||
|
||||
if (WrapperUtils.needWrapper(obj)) {
|
||||
Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class);
|
||||
Wrapper wrapper = new Wrapper(obj);
|
||||
bytes = ProtobufIOUtil.toByteArray(wrapper, schema, buffer);
|
||||
classNameBytes = Wrapper.class.getName().getBytes();
|
||||
} else {
|
||||
Schema schema = RuntimeSchema.getSchema(obj.getClass());
|
||||
bytes = ProtobufIOUtil.toByteArray(obj, schema, buffer);
|
||||
classNameBytes = obj.getClass().getName().getBytes();
|
||||
try {
|
||||
if (WrapperUtils.needWrapper(obj)) {
|
||||
Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class);
|
||||
Wrapper wrapper = new Wrapper(obj);
|
||||
bytes = ProtobufIOUtil.toByteArray(wrapper, schema, buffer);
|
||||
classNameBytes = Wrapper.class.getName().getBytes();
|
||||
} else {
|
||||
Schema schema = RuntimeSchema.getSchema(obj.getClass());
|
||||
bytes = ProtobufIOUtil.toByteArray(obj, schema, buffer);
|
||||
classNameBytes = obj.getClass().getName().getBytes();
|
||||
}
|
||||
} finally {
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
dos.writeInt(classNameBytes.length);
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dubbo.common.serialize.protobuf;
|
||||
package org.apache.dubbo.common.serialize.protostuff;
|
||||
|
||||
import org.apache.dubbo.common.URL;
|
||||
import org.apache.dubbo.common.serialize.ObjectInput;
|
||||
|
|
@ -26,7 +26,7 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class ProtobufSerialization implements Serialization {
|
||||
public class ProtostuffSerialization implements Serialization {
|
||||
@Override
|
||||
public byte getContentTypeId() {
|
||||
return 10;
|
||||
|
|
@ -34,16 +34,16 @@ public class ProtobufSerialization implements Serialization {
|
|||
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return "x-application/protobuf";
|
||||
return "x-application/protostuff";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectOutput serialize(URL url, OutputStream output) throws IOException {
|
||||
return new ProtobufObjectOutput(output);
|
||||
return new ProtostuffObjectOutput(output);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectInput deserialize(URL url, InputStream input) throws IOException {
|
||||
return new ProtobufObjectInput(input);
|
||||
return new ProtostuffObjectInput(input);
|
||||
}
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dubbo.common.serialize.protobuf;
|
||||
package org.apache.dubbo.common.serialize.protostuff;
|
||||
|
||||
/**
|
||||
* Protostuff can only serialize/deserialize POJOs, for those it can't deal with, use this Wrapper.
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* 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 org.apache.dubbo.common.serialize.protostuff.delegate;
|
||||
|
||||
import io.protostuff.Input;
|
||||
import io.protostuff.Output;
|
||||
import io.protostuff.Pipe;
|
||||
import io.protostuff.WireFormat;
|
||||
import io.protostuff.runtime.Delegate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Time;
|
||||
|
||||
public class TimeDelegate implements Delegate<Time> {
|
||||
@Override
|
||||
public WireFormat.FieldType getFieldType() {
|
||||
return WireFormat.FieldType.FIXED64;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Time readFrom(Input input) throws IOException {
|
||||
return new Time(input.readFixed64());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(Output output, int number, Time time, boolean repeated) throws IOException {
|
||||
output.writeFixed64(number, time.getTime(), repeated);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transfer(Pipe pipe, Input input, Output output, int number, boolean repeated) throws IOException {
|
||||
output.writeFixed64(number, input.readFixed64(), repeated);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> typeClass() {
|
||||
return Time.class;
|
||||
}
|
||||
}
|
||||
|
|
@ -15,18 +15,41 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dubbo.common.serialize.protobuf.utils;
|
||||
package org.apache.dubbo.common.serialize.protostuff.utils;
|
||||
|
||||
import org.apache.dubbo.common.serialize.protobuf.Wrapper;
|
||||
import io.protostuff.runtime.DefaultIdStrategy;
|
||||
import io.protostuff.runtime.RuntimeEnv;
|
||||
import org.apache.dubbo.common.serialize.protostuff.Wrapper;
|
||||
import org.apache.dubbo.common.serialize.protostuff.delegate.TimeDelegate;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.sql.Time;
|
||||
import java.util.ArrayList;
|
||||
import java.util.BitSet;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Hashtable;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Vector;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class WrapperUtils {
|
||||
private static final Set<Class<?>> WRAPPER_SET = new HashSet<>();
|
||||
|
||||
static {
|
||||
if (RuntimeEnv.ID_STRATEGY instanceof DefaultIdStrategy) {
|
||||
((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY).registerDelegate(new TimeDelegate());
|
||||
}
|
||||
|
||||
WRAPPER_SET.add(Map.class);
|
||||
WRAPPER_SET.add(HashMap.class);
|
||||
WRAPPER_SET.add(TreeMap.class);
|
||||
|
|
@ -52,8 +75,10 @@ public class WrapperUtils {
|
|||
WRAPPER_SET.add(BigDecimal.class);
|
||||
WRAPPER_SET.add(Date.class);
|
||||
WRAPPER_SET.add(Calendar.class);
|
||||
WRAPPER_SET.add(Time.class);
|
||||
|
||||
WRAPPER_SET.add(Wrapper.class);
|
||||
|
||||
}
|
||||
|
||||
public static boolean needWrapper(Class<?> clazz) {
|
||||
|
|
@ -0,0 +1 @@
|
|||
protostuff=org.apache.dubbo.common.serialize.protostuff.ProtostuffSerialization
|
||||
|
|
@ -61,7 +61,7 @@
|
|||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo-serialization-protobuf</artifactId>
|
||||
<artifactId>dubbo-serialization-protostuff</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
@ -70,4 +70,4 @@
|
|||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
|||
|
|
@ -1,26 +0,0 @@
|
|||
/*
|
||||
* 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 org.apache.dubbo.common.serialize.protobuf;
|
||||
|
||||
import org.apache.dubbo.common.serialize.base.AbstractSerializationPersonOkTest;
|
||||
|
||||
public class ProtobufPersonOkTest extends AbstractSerializationPersonOkTest {
|
||||
{
|
||||
serialization = new ProtobufSerialization();
|
||||
}
|
||||
}
|
||||
|
|
@ -15,12 +15,19 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.dubbo.common.serialize.protobuf;
|
||||
package org.apache.dubbo.common.serialize.protostuff;
|
||||
|
||||
import org.apache.dubbo.common.serialize.base.AbstractSerializationTest;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ProtobufSerializationTest extends AbstractSerializationTest {
|
||||
public class ProtostuffSerializationTest extends AbstractSerializationTest {
|
||||
{
|
||||
serialization = new ProtobufSerialization();
|
||||
serialization = new ProtostuffSerialization();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void test_LoopReference() throws Exception {
|
||||
// FIXME: cannot make this test pass on protostuff
|
||||
}
|
||||
}
|
||||
|
|
@ -35,7 +35,7 @@
|
|||
<module>dubbo-serialization-kryo</module>
|
||||
<module>dubbo-serialization-fst</module>
|
||||
<module>dubbo-serialization-jdk</module>
|
||||
<module>dubbo-serialization-protobuf</module>
|
||||
<module>dubbo-serialization-protostuff</module>
|
||||
<module>dubbo-serialization-test</module>
|
||||
</modules>
|
||||
</project>
|
||||
|
|
|
|||
Loading…
Reference in New Issue