Skip to content

Commit

Permalink
add google pb generic service implement
Browse files Browse the repository at this point in the history
  • Loading branch information
vio-lin committed Apr 9, 2019
1 parent fb5d96d commit 70245a2
Show file tree
Hide file tree
Showing 25 changed files with 1,723 additions and 6 deletions.
4 changes: 4 additions & 0 deletions dubbo-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,9 @@
<groupId>de.ruedigermoeller</groupId>
<artifactId>fst</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ public class Constants {

public static final String GENERIC_SERIALIZATION_BEAN = "bean";

public static final String GENERIC_SERIALIZATION_PROTO = "proto";

public static final String DUBBO_IP_TO_REGISTRY = "DUBBO_IP_TO_REGISTRY";

public static final String DUBBO_PORT_TO_REGISTRY = "DUBBO_PORT_TO_REGISTRY";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.utils;

import java.lang.reflect.Method;

import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.GeneratedMessageV3.Builder;
import com.google.protobuf.MessageOrBuilder;
import com.google.protobuf.util.JsonFormat;
import com.google.protobuf.util.JsonFormat.Parser;
import com.google.protobuf.util.JsonFormat.Printer;

/**
* 2019/4/4
*/
public class ProtobufUtils {

public static boolean isSupported(Class<?> clazz) {
if (clazz == null) {
return false;
}

if (GeneratedMessageV3.class.isAssignableFrom(clazz)) {
return true;
}
return false;
}

public static <T> T deserialize(String json, Class<T> requestClass) {
if (!isSupported(requestClass)) {
throw new UnsupportedOperationException(requestClass.getName() + "can not be deserialize");
}
Builder builder;
try{
builder = getMessageBuilder(requestClass);
Parser parser = JsonFormat.parser();
parser.merge(json, builder);
}catch (Exception e){
throw new RuntimeException("Failed to deserialize "+requestClass,e );
}

return (T) builder.build();
}

public static String serialize(Object value) {
String result;
try {
Printer printer = JsonFormat.printer();
result = printer.print((MessageOrBuilder) value);
} catch (Exception e) {
result = e.getMessage();
}
return result;
}

private static Builder getMessageBuilder(Class<?> requestType) throws Exception {
Method method = requestType.getMethod("newBuilder");

return (Builder) method.invoke(null, null);
}
}
28 changes: 28 additions & 0 deletions dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-demo-protobuf</artifactId>
<groupId>org.apache.dubbo</groupId>
<version>2.7.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dubbo-demo-protobuf-api</artifactId>
</project>
Loading

0 comments on commit 70245a2

Please sign in to comment.