|
|
@ -1,8 +1,6 @@ |
|
|
|
package com.fanruan.proxy.interceptor; |
|
|
|
package com.fanruan.proxy.interceptor; |
|
|
|
|
|
|
|
|
|
|
|
import com.fanruan.annotation.LocalMethod; |
|
|
|
import com.fanruan.annotation.*; |
|
|
|
import com.fanruan.annotation.NotImplemented; |
|
|
|
|
|
|
|
import com.fanruan.annotation.RemoteClass; |
|
|
|
|
|
|
|
import com.fanruan.pojo.message.RpcRequest; |
|
|
|
import com.fanruan.pojo.message.RpcRequest; |
|
|
|
import com.fanruan.service.jdbc.AbstractBind; |
|
|
|
import com.fanruan.service.jdbc.AbstractBind; |
|
|
|
import com.fanruan.service.jdbc.ServiceInputStream; |
|
|
|
import com.fanruan.service.jdbc.ServiceInputStream; |
|
|
@ -10,34 +8,38 @@ import com.fanruan.service.jdbc.ServiceReader; |
|
|
|
import com.fanruan.utils.Commons; |
|
|
|
import com.fanruan.utils.Commons; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.Reader; |
|
|
|
import java.io.Reader; |
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
|
|
|
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
import java.lang.reflect.Parameter; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @author Yichen Dai |
|
|
|
* @author Yichen Dai |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class InterceptorUtils { |
|
|
|
public class InterceptorUtils { |
|
|
|
|
|
|
|
|
|
|
|
private static final String[] WRAPPER_CLASS_LIST = new String[]{ |
|
|
|
private static final Map<String, Class<?>> WRAPPER_CLASS_MAP = new HashMap<String, Class<?>>(){ |
|
|
|
"Boolean", |
|
|
|
{ |
|
|
|
"Integer", |
|
|
|
put("Integer", Integer.TYPE); |
|
|
|
"Double", |
|
|
|
put("Short", Short.TYPE); |
|
|
|
"Long", |
|
|
|
put("Long", Long.TYPE); |
|
|
|
"Character", |
|
|
|
put("Double", Double.TYPE); |
|
|
|
"Byte", |
|
|
|
put("Float", Float.TYPE); |
|
|
|
"Short", |
|
|
|
put("Byte", Byte.TYPE); |
|
|
|
"Float" |
|
|
|
put("Character", Character.TYPE); |
|
|
|
|
|
|
|
put("Boolean", Boolean.TYPE); |
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean isWraps(Object o){ |
|
|
|
public static boolean isWraps(Object o){ |
|
|
|
for(String ex : WRAPPER_CLASS_LIST){ |
|
|
|
Class<?> clz = o.getClass(); |
|
|
|
if(ex.equals(getClassName(o.getClass().getName()))){ |
|
|
|
if(clz == null) { |
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return WRAPPER_CLASS_MAP.containsKey(getClassName(clz.getName())); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String getClassName(String fullyQualifiedClassName){ |
|
|
|
public static String getClassName(String fullyQualifiedClassName){ |
|
|
|
String[] arr = fullyQualifiedClassName.split("\\."); |
|
|
|
String[] arr = fullyQualifiedClassName.split("\\."); |
|
|
@ -53,10 +55,17 @@ public class InterceptorUtils { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
Class<?> clazz = o.getClass(); |
|
|
|
Class<?> clazz = o.getClass(); |
|
|
|
if(clazz.isAnnotationPresent(RemoteClass.class)){ |
|
|
|
return clazz.isAnnotationPresent(RemoteClass.class); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void replaceBindingParameter(Method method, Object[] args){ |
|
|
|
|
|
|
|
Parameter[] pars = method.getParameters(); |
|
|
|
|
|
|
|
for(int i=0; i<pars.length; i++){ |
|
|
|
|
|
|
|
Parameter par = pars[i]; |
|
|
|
|
|
|
|
if(par.isAnnotationPresent(BindingParamter.class) && isInBindList(args[i])){ |
|
|
|
|
|
|
|
args[i] = getBindID(args[i]); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void setBindID(Object returnObj, String ID){ |
|
|
|
public static void setBindID(Object returnObj, String ID){ |
|
|
@ -88,6 +97,10 @@ public class InterceptorUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static RpcRequest generateRequest(Class<?> clazz, Object o, Method method, Object[] objects){ |
|
|
|
public static RpcRequest generateRequest(Class<?> clazz, Object o, Method method, Object[] objects){ |
|
|
|
|
|
|
|
if(isWithBindingParameter(method)){ |
|
|
|
|
|
|
|
replaceBindingParameter(method, objects); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
RpcRequest rpcRequest = new RpcRequest(); |
|
|
|
RpcRequest rpcRequest = new RpcRequest(); |
|
|
|
rpcRequest |
|
|
|
rpcRequest |
|
|
|
.setID(Commons.getID()) |
|
|
|
.setID(Commons.getID()) |
|
|
@ -114,17 +127,24 @@ public class InterceptorUtils { |
|
|
|
return rpcRequest; |
|
|
|
return rpcRequest; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static boolean isNotImplemented(Method method) { |
|
|
|
public static Class<?>[] getArgTypes(Object[] objects){ |
|
|
|
if(method.isAnnotationPresent(NotImplemented.class)){ |
|
|
|
int n = objects.length; |
|
|
|
return true; |
|
|
|
Class<?>[] argTypes = new Class<?>[n]; |
|
|
|
|
|
|
|
for(int i=0; i<n; i++){ |
|
|
|
|
|
|
|
argTypes[i] = objects[i].getClass(); |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return argTypes; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean isNotImplemented(Method method) { |
|
|
|
|
|
|
|
return method.isAnnotationPresent(NotImplemented.class); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static boolean isLocalMethod(Method method) { |
|
|
|
public static boolean isLocalMethod(Method method) { |
|
|
|
if(method.isAnnotationPresent(LocalMethod.class)){ |
|
|
|
return method.isAnnotationPresent(LocalMethod.class); |
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
public static boolean isWithBindingParameter(Method method){ |
|
|
|
|
|
|
|
return method.isAnnotationPresent(WithBindingParameter.class); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|