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

fix telnet trace times is always 1 #3038

Merged
merged 6 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.dubbo.rpc.protocol.dubbo.telnet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;
Expand All @@ -27,14 +29,10 @@
import org.apache.dubbo.rpc.model.ProviderMethodModel;
import org.apache.dubbo.rpc.model.ProviderModel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static org.apache.dubbo.common.utils.PojoUtils.realize;
import static org.apache.dubbo.rpc.RpcContext.getContext;
Expand All @@ -50,19 +48,19 @@ private static Method findMethod(List<ProviderMethodModel> methods, String metho
Class<?>[] paramTypes) {
for (ProviderMethodModel model : methods) {
Method m = model.getMethod();
if (isMatch(m, args, paramTypes,method)) {
if (isMatch(m, args, paramTypes, method)) {
return m;
}
}
return null;
}

private static boolean isMatch(Method method,List<Object> args, Class<?>[] paramClasses,String lookupMethodName) {
if(!method.getName().equals(lookupMethodName)) {
private static boolean isMatch(Method method, List<Object> args, Class<?>[] paramClasses, String lookupMethodName) {
if (!method.getName().equals(lookupMethodName)) {
return false;
}

Class<?> types[]=method.getParameterTypes();
Class<?> types[] = method.getParameterTypes();
if (types.length != args.size()) {
return false;
}
Expand Down Expand Up @@ -99,13 +97,10 @@ private static boolean isMatch(Method method,List<Object> args, Class<?>[] param
if (!ReflectUtils.isCompatible(type, arg)) {
return false;
}
} else if (arg instanceof Map) {
String name = (String) ((Map<?, ?>) arg).get("class");
Class<?> cls = arg.getClass();
if (name != null && name.length() > 0) {
cls = ReflectUtils.forName(name);
}
if (!type.isAssignableFrom(cls)) {
} else if (arg instanceof JSONObject) {
try {
((JSONObject) arg).toJavaObject(type);
Copy link
Contributor

Choose a reason for hiding this comment

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

@Data
public class User1 implements Serializable {
    private static final long serialVersionUID = -4729079781904156102L;
    private String name;
}
@Data
public class User2 implements Serializable {
    private static final long serialVersionUID = -2112637973209566052L;
    private String name;
}
public interface XXXService{
String hello(User1 user);
String hello(User2 user);

}

May find not the correct overload method; I think must class param when the method is overloaded.
telnet: invoke xxxService.hello({"name":"xxxx"})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have fix it

} catch (Exception ex) {
return false;
}
} else if (arg instanceof Collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,18 @@ public String telnet(Channel channel, String message) {
String[] parts = message.split("\\s+");
String method;
String times;
if (service == null || service.length() == 0) {
// message like : XxxService , XxxService 10 , XxxService xxxMethod , XxxService xxxMethod 10
if (StringUtils.isEmpty(service)) {
service = parts.length > 0 ? parts[0] : null;
method = parts.length > 1 ? parts[1] : null;
} else {
times = parts.length > 2 ? parts[2] : "1";
LiZhenNet marked this conversation as resolved.
Show resolved Hide resolved
} else { //message like : xxxMethod, xxxMethod 10
method = parts.length > 0 ? parts[0] : null;
times = parts.length > 1 ? parts[1] : "1";
}
if (StringUtils.isInteger(method)) {
times = method;
method = null;
} else {
times = parts.length > 2 ? parts[2] : "1";
}
if (!StringUtils.isInteger(times)) {
return "Illegal times " + times + ", must be integer.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Type enumlength(Type... types) {
return Type.Lower;
return types[0];
}

public Type getType(Type type) {
return type;
}
Expand Down Expand Up @@ -109,12 +109,12 @@ public long add(int a, long b) {

@Override
public int getPerson(Person person) {
return 1;
return person.getAge();
}

@Override
public int getPerson(Person person1, Person perso2) {
return 2;
return person1.getAge() + perso2.getAge();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService;
import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl;
import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -198,6 +197,34 @@ public void testInvokeAutoFindMethod() throws RemotingException {
assertTrue(result.contains("ok"));
}

@Test
public void testInvokeJsonParamMethod() throws RemotingException {
mockChannel = mock(Channel.class);
given(mockChannel.getAttribute("telnet.service")).willReturn(null);
given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));

ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), DemoService.class);
ApplicationModel.initProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", providerModel);
String param = "{\"name\":\"Dubbo\",\"age\":8}";
String result = invoke.telnet(mockChannel, "getPerson(" + param + ")");
assertTrue(result.contains("8"));
}

@Test
public void testInvokeMultiJsonParamMethod() throws RemotingException {
mockChannel = mock(Channel.class);
given(mockChannel.getAttribute("telnet.service")).willReturn(null);
given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));

ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), DemoService.class);
ApplicationModel.initProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", providerModel);
String param = "{\"name\":\"Dubbo\",\"age\":8},{\"name\":\"Apache\",\"age\":20}";
String result = invoke.telnet(mockChannel, "getPerson(" + param + ")");
assertTrue(result.contains("28"));
}

@Test
public void testMessageNull() throws RemotingException {
mockChannel = mock(Channel.class);
Expand Down