5. 协议可能需要完善的专题讨论
5.1. 深度分析
首先声明:在未与Michael充分沟通交流前,一切都只是个人的想法,未必正确。
本人在“服务的匹配与调用”章节,曾经提出过服务方法匹配的问题,Michael在buffalo.js的310行提到过“Guess the type of an javascript object”,但光完善客户端,只怕于事无补。
就如我做的实验那样,客户端可以提供divide(double a, double b)的请求,此时的xml为:
<buffalo-call>
<method>divide</method>
<double>1.1</double>
<double>2.1</double>
</buffalo-call>
此时调用的是divide(Double a, Double b)而不是divide(double a, double b)。
虽然已经相差无几,但依然无法到达准确制导,主要还是primitive及其Wrapper之间容易造成误会。
首先我们还是仔细来看看匹配权重的代码,在相同服务下,同名称同参数长度下,下一步就考虑到了参数的匹配权重问题(这里我加了注释):
private int parameterAssignable(Class targetType, Class sourceType) {
if (targetType.equals(sourceType)) return 6;//如果类型相等,匹配度为6
if (targetType.isAssignableFrom(sourceType)) return 5;//同源,为5
if (targetType.isPrimitive()) {//如果是primitive,则获取Wrapper类
targetType = ClassUtil.getWrapperClass(targetType);
}
if (targetType.equals(sourceType)) {
return 4; //此时肯定是经过处理的匹配,只能为4
} else if (Number.class.isAssignableFrom(targetType) &&
Number.class.isAssignableFrom(sourceType)) {
return 3; //如果两边都是Number型的,马虎匹配了,为3
}
return 0; //0就是没啥匹配度了
}
题外话:注意下后面的if/else代码稍微有些逻辑交待不清。
同时,需要结合所涉及到的Converter,此时为DoubleConverter。同样我们看看代码:
public class DoubleConverter extends AbstractBasicConverter implements Converter{
public boolean canConvert(Class value) {
if (value == null) return false;
return value.equals(double.class) ||
value.equals(Double.class) ||
value.equals(float.class) ||
value.equals(Float.class);
}
protected String getType() {
return ProtocalTag.TAG_DOUBLE;
}
public Object fromString(String string) {
return Double.valueOf(string);
}
}
显然,在这个例子上,是这个转换器模糊了double和Double,统统变成了Double。
5.2. 可能的解决方案
当然,这里的解决方案是以“需要这么干”为前提假设的。在很大程度上来说,这种“精确制导”没有必要。
我仅以解决上述例子为目标,做出修缮。
5.2.1. 在ProtocalTag中加入新协议标签
位置:net.buffalo.protocal.ProtocalTag
需要加入新的标签定义:
package net.buffalo.protocal;
public interface ProtocalTag {
public static final String TAG_BOOLEAN = "boolean";
public static final String TAG_DATE = "date";
public static final String TAG_INT = "int";
public static final String TAG_LONG = "long";
public static final String TAG_NULL = "null";
public static final String TAG_STRING = "string";
public static final String TAG_LENGTH = "length";
public static final String TAG_TYPE = "type";
public static final String TAG_LIST = "list";
public static final String TAG_MAP = "map";
public static final String TAG_DOUBLE = "double";
public static final String TAG_REPLY = "buffalo-reply";
public static final String TAG_REF = "ref";
public static final String TAG_CALL = "buffalo-call";
public static final String TAG_FAULT = "fault";
public static final String TAG_METHOD = "method";
//加入新的协议标签 by zhengxq
//java.lang.Double
public static final String TAG_DOUBLE_W = "java.lang.Double";
//其他}
5.2.2. 定义两个工具类
5.2.2.1. PrimitiveObjectWrapper
Primitive对象的包装类:
package net.buffalo.protocal.util;
public class PrimitiveObjectWrapper {
private Class clazz;
private Object value;
public PrimitiveObjectWrapper(Object value){
this.clazz = PrimitiveObjectUtil.getPrivitiveType(value);
this.value = value;
}
public Class getClazz() {
return clazz;
}
public void setClazz(Class clazz) {
this.clazz = clazz;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
5.2.2.2. PrimitiveObjectUtil
一个获取primitive类型的工具:
package net.buffalo.protocal.util;
public class PrimitiveObjectUtil {
public static Class getPrivitiveType(Object v){
Class clazz = v instanceof Integer ? int.class :
v instanceof Long ? long.class :
v instanceof Short ? short.class :
v instanceof Byte ? byte.class :
v instanceof Float ? float.class :
v instanceof Double ? double.class :
v instanceof Boolean ? boolean.class :
v.getClass();
return clazz;
}
}
5.2.3. 新增转换器PrimitiveDoubleConverter
位置:net.buffalo.protocal.converters.primitive
新增primitive的package,并建立新的转换器PrimitiveDoubleConverter.java:
package net.buffalo.protocal.converters.primitive;
import net.buffalo.protocal.ProtocalTag;
import net.buffalo.protocal.converters.Converter;
import net.buffalo.protocal.converters.basic.AbstractBasicConverter;
import net.buffalo.protocal.util.PrimitiveObjectWrapper;
public class PrimitiveDoubleConverter extends AbstractBasicConverter implements Converter{
public boolean canConvert(Class value) {
if (value == null) return false;
return value.equals(double.class);
}
protected String getType() {
return ProtocalTag.TAG_DOUBLE;
}
public Object fromString(String string) {
//get the custom Wrapper
return new PrimitiveObjectWrapper(Double.valueOf(string));
}
}
5.2.4. 修改DoubleConverter的协议标签类型
位置:net.buffalo.protocal.converters.basic.DoubleConverter.java
不再是ProtocalTag.TAG_DOUBLE了:
protected String getType() {
return ProtocalTag.TAG_DOUBLE_W;
}
5.2.5. 注册转换器到DefaultConverterLookup
位置:net.buffalo.protocal.converters.DefaultConverterLookup.java
protected void registerDefaultConverters() {
BooleanConverter booleanConverter = new BooleanConverter();
DoubleConverter doubleConverter = new DoubleConverter();
IntegerConverter integerConverter = new IntegerConverter();
LongConverter longConverter = new LongConverter();
StringConverter stringConverter = new StringConverter();
DateConverter dateConverter = new DateConverter();
CollectionConverter collectionConverter = new CollectionConverter();
MapConverter mapConverter = new MapConverter();
ArrayConverter arrayConverter = new ArrayConverter();
SqlDateConverter sqlDateConverter = new SqlDateConverter();
BigNumberConverter bigNumberConverter = new BigNumberConverter();
ExceptionConverter exceptionConverter = new ExceptionConverter();
ObjectConverter objectConverter = new ObjectConverter();
//新增新的转换器定义
PrimitiveDoubleConverter primitiveDoubleConverter = new PrimitiveDoubleConverter();
converters.add(nullConverter);
converters.add(booleanConverter);
converters.add(doubleConverter);
converters.add(integerConverter);
converters.add(longConverter);
converters.add(stringConverter);
converters.add(dateConverter);
converters.add(collectionConverter);
converters.add(mapConverter);
converters.add(arrayConverter);
converters.add(sqlDateConverter);
converters.add(bigNumberConverter);
converters.add(exceptionConverter);
// register new primitiveDoubleConverter
converters.add(primitiveDoubleConverter);
// Should be last one
converters.add(objectConverter);
tagNameConverterCache.put(ProtocalTag.TAG_BOOLEAN, booleanConverter);
tagNameConverterCache.put(ProtocalTag.TAG_STRING, stringConverter);
tagNameConverterCache.put(ProtocalTag.TAG_INT, integerConverter);
tagNameConverterCache.put(ProtocalTag.TAG_LONG, longConverter);
tagNameConverterCache.put(ProtocalTag.TAG_NULL, nullConverter);
tagNameConverterCache.put(ProtocalTag.TAG_DATE, dateConverter);
tagNameConverterCache.put(ProtocalTag.TAG_LIST, collectionConverter);
tagNameConverterCache.put(ProtocalTag.TAG_MAP, mapConverter);
tagNameConverterCache.put(ProtocalTag.TAG_REF, new ReferenceConverter());
// edit the doubleConverter
tagNameConverterCache.put(ProtocalTag.TAG_DOUBLE_W, doubleConverter);
// register new primitiveDoubleConverter
tagNameConverterCache.put(ProtocalTag.TAG_DOUBLE, primitiveDoubleConverter);
converterCache.put(Boolean.class, booleanConverter);
converterCache.put(boolean.class, booleanConverter);
converterCache.put(String.class, stringConverter);
converterCache.put(Integer.class, integerConverter);
converterCache.put(int.class, integerConverter);
converterCache.put(Long.class, longConverter);
converterCache.put(long.class, longConverter);
converterCache.put(Double.class, doubleConverter);
// edit the double converter mapping
//converterCache.put(double.class, doubleConverter);
converterCache.put(double.class, primitiveDoubleConverter);
converterCache.put(Date.class, dateConverter);
converterCache.put(ArrayList.class, collectionConverter);
converterCache.put(LinkedList.class, collectionConverter);
converterCache.put(HashSet.class, collectionConverter);
converterCache.put(Vector.class, collectionConverter);
converterCache.put(TreeSet.class, collectionConverter);
converterCache.put(HashMap.class, mapConverter);
converterCache.put(TreeMap.class, mapConverter);
converterCache.put(java.sql.Date.class, sqlDateConverter);
converterCache.put(java.math.BigDecimal.class, bigNumberConverter);
converterCache.put(java.math.BigInteger.class, bigNumberConverter);
}
5.2.6. 修订FastStreamReader
位置:net.buffalo.protocal.io.FastStreamReader
/* 判断是否有效的标签字符,必须是字母、数字和两个特定字符:':'、'-'
* 又新增了字符'.'
*/
private boolean isTagChar(int ch) {
return (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0'
&& ch <= '9' || ch == ':' || ch == '-' || ch == '.');
}
5.2.7. 完善BuffaloCall
位置:net.buffalo.protocal.BuffaloCall
public BuffaloCall(String methodName, Object[] arguments) {
this.argumentTypes = new Class[arguments.length];
this.arguments = new Object[arguments.length];
for (int i = 0; i < arguments.length; i++) {
// from code
//types[i] = arguments[i].getClass();
// to - begin
if(arguments[i] instanceof PrimitiveObjectWrapper){
PrimitiveObjectWrapper p = (PrimitiveObjectWrapper)arguments[i];
//the type
this.argumentTypes[i] = PrimitiveObjectUtil.getPrivitiveType(p.getValue());
//the value
this.arguments[i] = p.getValue();
}else{
this.argumentTypes[i] = arguments[i].getClass();
this.arguments[i] = arguments[i];
}
//end
}
this.methodName = methodName;
}
5.2.8. 客户端buffalo.js
需要处理客户端对于java.lang.Double标签的处理,在Buffalo.Reply中(注意红色的部分):
deserialize: function(dataNode) {
var ret;
var type = this._getType(dataNode);
switch (type) {
case "boolean": ret = this.doBoolean(dataNode); break;
case "date": ret = this.doDate(dataNode); break;
case "java.lang.double":;//new add
case "double": ret = this.doDouble(dataNode); break;
case "int":
case "long":
ret = this.doInt(dataNode);
break;
case "list": ret = this.doList(dataNode); break;
case "map": ret = this.doMap(dataNode); break;
case "null": ret = this.doNull(dataNode); break;
case "ref": ret = this.doRef(dataNode); break;
case "string": ret = this.doString(dataNode);break;
case "xml": ret = this.doXML(dataNode); break;
case "fault": ret = this.doFault(dataNode); break;
default: ;
}
return ret;
}