This is an automated email from the ASF dual-hosted git repository.
horizonzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new f8f6995 Use MapUtils instead of AttachmentsAdapter (#8772)
f8f6995 is described below
commit f8f6995b565250557eed548995ae1a6f12b8c1d5
Author: yizhenqiang <[email protected]>
AuthorDate: Tue Sep 14 12:40:39 2021 +0800
Use MapUtils instead of AttachmentsAdapter (#8772)
* Use MapUtils instead of AttachmentsAdapter
---
.../rpc/cluster/directory/MockDirInvocation.java | 12 ++--
.../org/apache/dubbo/common/utils/MapUtils.java | 65 ++++++++++++++++++++++
.../org/apache/dubbo/service/MockInvocation.java | 4 +-
.../java/org/apache/dubbo/rpc/AppResponse.java | 3 +-
.../org/apache/dubbo/rpc/AttachmentsAdapter.java | 13 ++++-
.../main/java/org/apache/dubbo/rpc/RpcContext.java | 7 ++-
.../java/org/apache/dubbo/rpc/RpcInvocation.java | 3 +-
.../apache/dubbo/rpc/support/MockInvocation.java | 4 +-
8 files changed, 93 insertions(+), 18 deletions(-)
diff --git
a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/MockDirInvocation.java
b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/MockDirInvocation.java
index bc237b9..de4235e 100644
---
a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/MockDirInvocation.java
+++
b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/MockDirInvocation.java
@@ -16,19 +16,17 @@
*/
package org.apache.dubbo.rpc.cluster.directory;
-import org.apache.dubbo.rpc.AttachmentsAdapter;
-import org.apache.dubbo.rpc.Invocation;
-import org.apache.dubbo.rpc.Invoker;
-
import java.util.HashMap;
import java.util.Map;
-
import static
org.apache.dubbo.common.constants.CommonConstants.DUBBO_VERSION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
+import org.apache.dubbo.common.utils.MapUtils;
import static org.apache.dubbo.rpc.Constants.TOKEN_KEY;
+import org.apache.dubbo.rpc.Invocation;
+import org.apache.dubbo.rpc.Invoker;
/**
* MockInvocation.java
@@ -75,7 +73,7 @@ public class MockDirInvocation implements Invocation {
}
public Map<String, String> getAttachments() {
- return new AttachmentsAdapter.ObjectToStringMap(attachments);
+ return MapUtils.objectToStringMap(attachments);
}
@Override
@@ -90,7 +88,7 @@ public class MockDirInvocation implements Invocation {
@Override
public void setAttachment(String key, Object value) {
- setObjectAttachment(key, value);
+ setObjectAttachment(key, value);
}
@Override
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/MapUtils.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/MapUtils.java
new file mode 100644
index 0000000..00454a5
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/MapUtils.java
@@ -0,0 +1,65 @@
+/*
+ * 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.util.HashMap;
+import java.util.Map;
+
+/**
+ * Map tools
+ */
+public class MapUtils {
+
+ /**
+ * switch Map<String, Object> to Map<String, String>
+ *
+ * If the value of the original Map is not of type String, then toString()
of value will be called
+ *
+ * @param originMap
+ * @return
+ */
+ public static Map<String, String> objectToStringMap(Map<String, Object>
originMap) {
+ Map<String, String> newStrMap = new HashMap<>();
+
+ if (originMap == null) {
+ return newStrMap;
+ }
+
+ for (Map.Entry<String, Object> entry : originMap.entrySet()) {
+ String stringValue = convertToString(entry.getValue());
+ if (stringValue != null) {
+ newStrMap.put(entry.getKey(), stringValue);
+ }
+ }
+
+ return newStrMap;
+ }
+
+ /**
+ * use {@link Object#toString()} switch Obj to String
+ *
+ * @param obj
+ * @return
+ */
+ private static String convertToString(Object obj) {
+ if (obj == null) {
+ return null;
+ } else {
+ return obj.toString();
+ }
+ }
+}
diff --git
a/dubbo-compatible/src/test/java/org/apache/dubbo/service/MockInvocation.java
b/dubbo-compatible/src/test/java/org/apache/dubbo/service/MockInvocation.java
index a2cb52e..3a1b76e 100644
---
a/dubbo-compatible/src/test/java/org/apache/dubbo/service/MockInvocation.java
+++
b/dubbo-compatible/src/test/java/org/apache/dubbo/service/MockInvocation.java
@@ -16,7 +16,7 @@
*/
package org.apache.dubbo.service;
-import org.apache.dubbo.rpc.AttachmentsAdapter;
+import org.apache.dubbo.common.utils.MapUtils;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
@@ -79,7 +79,7 @@ public class MockInvocation implements Invocation {
}
public Map<String, String> getAttachments() {
- return new AttachmentsAdapter.ObjectToStringMap(attachments);
+ return MapUtils.objectToStringMap(attachments);
}
@Override
diff --git
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java
index 27df08a..65d859a 100644
---
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java
+++
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java
@@ -17,6 +17,7 @@
package org.apache.dubbo.rpc;
+import org.apache.dubbo.common.utils.MapUtils;
import org.apache.dubbo.rpc.proxy.InvokerInvocationHandler;
import java.util.HashMap;
@@ -121,7 +122,7 @@ public class AppResponse implements Result {
@Override
@Deprecated
public Map<String, String> getAttachments() {
- return new AttachmentsAdapter.ObjectToStringMap(attachments);
+ return MapUtils.objectToStringMap(attachments);
}
@Override
diff --git
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AttachmentsAdapter.java
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AttachmentsAdapter.java
index def349a..e6cb643 100644
---
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AttachmentsAdapter.java
+++
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AttachmentsAdapter.java
@@ -22,12 +22,18 @@ import java.util.Map;
/**
* This class provides map adapters to support attachments in RpcContext,
Invocation and Result switch from
* <String, String> to <String, Object>
+ *
+ * please use {@link org.apache.dubbo.common.utils.MapUtils}
+ *
*/
+@Deprecated
public class AttachmentsAdapter {
+ @Deprecated
public static class ObjectToStringMap extends HashMap<String, String> {
private Map<String, Object> attachments;
+ @Deprecated
public ObjectToStringMap(Map<String, Object> attachments) {
for (Entry<String, Object> entry : attachments.entrySet()) {
String convertResult = convert(entry.getValue());
@@ -51,10 +57,11 @@ public class AttachmentsAdapter {
}
private String convert(Object obj) {
- if (obj instanceof String) {
- return (String) obj;
+ if (obj == null) {
+ return null;
+ } else {
+ return obj.toString();
}
- return null; // or JSON.toString(obj);
}
@Override
diff --git
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java
index 8077067..0a48f7c 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java
@@ -20,6 +20,7 @@ import org.apache.dubbo.common.Experimental;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.threadlocal.InternalThreadLocal;
import org.apache.dubbo.common.utils.CollectionUtils;
+import org.apache.dubbo.common.utils.MapUtils;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.common.utils.StringUtils;
@@ -546,13 +547,15 @@ public class RpcContext {
}
/**
- * get attachments.
+ * get String type attachments.
+ *
+ * Best to use {{@link #getObjectAttachments()}}
*
* @return attachments
*/
@Deprecated
public Map<String, String> getAttachments() {
- return new
AttachmentsAdapter.ObjectToStringMap(this.getObjectAttachments());
+ return MapUtils.objectToStringMap(this.getObjectAttachments());
}
/**
diff --git
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java
index f94c001..116f3e6 100644
---
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java
+++
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java
@@ -17,6 +17,7 @@
package org.apache.dubbo.rpc;
import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.utils.MapUtils;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.model.ApplicationModel;
@@ -277,7 +278,7 @@ public class RpcInvocation implements Invocation,
Serializable {
@Deprecated
@Override
public Map<String, String> getAttachments() {
- return new AttachmentsAdapter.ObjectToStringMap(attachments);
+ return MapUtils.objectToStringMap(attachments);
}
@Deprecated
diff --git
a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MockInvocation.java
b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MockInvocation.java
index 586c990..2eb2ddd 100644
---
a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MockInvocation.java
+++
b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MockInvocation.java
@@ -16,7 +16,7 @@
*/
package org.apache.dubbo.rpc.support;
-import org.apache.dubbo.rpc.AttachmentsAdapter;
+import org.apache.dubbo.common.utils.MapUtils;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
@@ -75,7 +75,7 @@ public class MockInvocation implements Invocation {
}
public Map<String, String> getAttachments() {
- return new AttachmentsAdapter.ObjectToStringMap(attachments);
+ return MapUtils.objectToStringMap(attachments);
}
@Override