Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance pull request 2618 #2691

Merged
merged 6 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -70,34 +70,10 @@ public Object readObject() throws IOException, ClassNotFoundException {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a bug in a multi-threaded environment - change it to thread-local?

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,24 +26,24 @@
import java.io.InputStream;
import java.io.OutputStream;

public class ProtobufSerialization implements Serialization {
public class ProtostuffSerialization implements Serialization {
@Override
public byte getContentTypeId() {
return 10;
}

@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
protostuff=org.apache.dubbo.common.serialize.protostuff.ProtostuffSerialization
4 changes: 2 additions & 2 deletions dubbo-serialization/dubbo-serialization-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -70,4 +70,4 @@
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</project>
</project>

This file was deleted.

Loading