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

Use MapUtils instead of AttachmentsAdapter #8772

Merged
merged 8 commits into from Sep 14, 2021
Expand Up @@ -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
Expand Down Expand Up @@ -75,7 +73,7 @@ public Object[] getArguments() {
}

public Map<String, String> getAttachments() {
return new AttachmentsAdapter.ObjectToStringMap(attachments);
return MapUtils.objectToStringMap(attachments);
}

@Override
Expand All @@ -90,7 +88,7 @@ public void setAttachment(String key, String value) {

@Override
public void setAttachment(String key, Object value) {
setObjectAttachment(key, value);
setObjectAttachment(key, value);
}

@Override
Expand Down
@@ -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();
}
}
}
Expand Up @@ -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;

Expand Down Expand Up @@ -79,7 +79,7 @@ public Object[] getArguments() {
}

public Map<String, String> getAttachments() {
return new AttachmentsAdapter.ObjectToStringMap(attachments);
return MapUtils.objectToStringMap(attachments);
}

@Override
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -121,7 +122,7 @@ public boolean hasException() {
@Override
@Deprecated
public Map<String, String> getAttachments() {
return new AttachmentsAdapter.ObjectToStringMap(attachments);
return MapUtils.objectToStringMap(attachments);
}

@Override
Expand Down
Expand Up @@ -22,12 +22,18 @@
/**
* 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
Copy link
Member

Choose a reason for hiding this comment

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

this file method convert has problem, if object type is not string, it will return null, cound you fix it conveniently

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, the new method takes this into consideration, calling Object#toString() directly。Using JSON here may not be the best choice.

/**
 * 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();
    }
}

public ObjectToStringMap(Map<String, Object> attachments) {
for (Entry<String, Object> entry : attachments.entrySet()) {
String convertResult = convert(entry.getValue());
Expand All @@ -51,10 +57,11 @@ public String remove(Object key) {
}

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
Expand Down
Expand Up @@ -20,6 +20,7 @@
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;

Expand Down Expand Up @@ -546,13 +547,15 @@ public RpcContext removeAttachment(String key) {
}

/**
* 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());
}

/**
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -277,7 +278,7 @@ public void setAttachment(String key, String value) {
@Deprecated
@Override
public Map<String, String> getAttachments() {
return new AttachmentsAdapter.ObjectToStringMap(attachments);
return MapUtils.objectToStringMap(attachments);
}

@Deprecated
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -75,7 +75,7 @@ public Object[] getArguments() {
}

public Map<String, String> getAttachments() {
return new AttachmentsAdapter.ObjectToStringMap(attachments);
return MapUtils.objectToStringMap(attachments);
}

@Override
Expand Down