forked from apache/dubbo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add google pb generic service implement
- Loading branch information
Showing
25 changed files
with
1,723 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
dubbo-common/src/main/java/org/apache/dubbo/common/utils/ProtobufUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.