use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class YarExporter method validateInterface.
protected void validateInterface(Class<?> interfaceClazz) {
HashMap<String, List<Integer>> tempMap = new HashMap<String, List<Integer>>();
for (Method m : interfaceClazz.getDeclaredMethods()) {
if (!tempMap.containsKey(m.getName())) {
List<Integer> templist = new ArrayList<Integer>();
templist.add(m.getParameterTypes().length);
tempMap.put(m.getName(), templist);
} else {
List<Integer> templist = tempMap.get(m.getName());
if (templist.contains(m.getParameterTypes().length)) {
throw new MotanFrameworkException("in yar protocol, methods with same name must have different params size !");
} else {
templist.add(m.getParameterTypes().length);
}
}
}
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class AbstractConfig method appendConfigParams.
/**
* 将config 参数录入Map中
*
* @param parameters
*/
@SuppressWarnings("unchecked")
protected void appendConfigParams(Map<String, String> parameters, String prefix) {
Method[] methods = this.getClass().getMethods();
for (Method method : methods) {
try {
String name = method.getName();
if (isConfigMethod(method)) {
int idx = name.startsWith("get") ? 3 : 2;
String prop = name.substring(idx, idx + 1).toLowerCase() + name.substring(idx + 1);
String key = prop;
ConfigDesc configDesc = method.getAnnotation(ConfigDesc.class);
if (configDesc != null && !StringUtils.isBlank(configDesc.key())) {
key = configDesc.key();
}
Object value = method.invoke(this);
if (value == null || StringUtils.isBlank(String.valueOf(value))) {
if (configDesc != null && configDesc.required()) {
throw new MotanFrameworkException(String.format("%s.%s should not be null or empty", this.getClass().getSimpleName(), key), MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
continue;
}
if (prefix != null && prefix.length() > 0) {
key = prefix + "." + key;
}
parameters.put(key, String.valueOf(value).trim());
} else if ("getParameters".equals(name) && Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 0 && method.getReturnType() == Map.class) {
Map<String, String> map = (Map<String, String>) method.invoke(this);
if (map != null && map.size() > 0) {
String pre = prefix != null && prefix.length() > 0 ? prefix + "." : "";
for (Map.Entry<String, String> entry : map.entrySet()) {
parameters.put(pre + entry.getKey(), entry.getValue());
}
}
}
} catch (Exception e) {
throw new MotanFrameworkException(String.format("Error when append params for config: %s.%s", this.getClass().getSimpleName(), method.getName()), e, MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
}
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class CompressRpcCodec method decodeResponse.
/**
*
* @param body
* @param dataType
* @param requestId
* @param rpcProtocolVersion rpc协议的版本号,不同版本可能有不同的序列化方式
* @param serialization
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
private Object decodeResponse(byte[] body, byte dataType, long requestId, byte rpcProtocolVersion, Serialization serialization) throws IOException, ClassNotFoundException {
ObjectInput input = createInput(getInputStream(body));
long processTime = input.readLong();
DefaultResponse response = new DefaultResponse();
response.setRequestId(requestId);
response.setProcessTime(processTime);
if (dataType == MotanConstants.FLAG_RESPONSE_VOID) {
return response;
}
String className = input.readUTF();
Class<?> clz = ReflectUtil.forName(className);
Object result = deserialize((byte[]) input.readObject(), clz, serialization);
if (dataType == MotanConstants.FLAG_RESPONSE) {
response.setValue(result);
} else if (dataType == MotanConstants.FLAG_RESPONSE_ATTACHMENT) {
response.setValue(result);
Map<String, String> attachment = decodeRequestAttachments(input);
checkAttachment(attachment);
} else if (dataType == MotanConstants.FLAG_RESPONSE_EXCEPTION) {
response.setException((Exception) result);
} else {
throw new MotanFrameworkException("decode error: response dataType not support " + dataType, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
response.setRequestId(requestId);
input.close();
return response;
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class CompressRpcCodec method decodeV2.
/**
* decode data
*
* <pre>
* 对于client端:主要是来自server端的response or exception
* 对于server端: 主要是来自client端的request
* </pre>
*
* @param data
* @return
* @throws IOException
*/
public Object decodeV2(Channel channel, String remoteIp, byte[] data) throws IOException {
if (data.length <= RpcProtocolVersion.VERSION_2.getHeaderLength()) {
throw new MotanFrameworkException("decode error: format problem", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
short type = ByteUtil.bytes2short(data, 0);
if (type != MAGIC) {
throw new MotanFrameworkException("decode error: magic error", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
int bodyLength = ByteUtil.bytes2int(data, 12);
if (RpcProtocolVersion.VERSION_2.getHeaderLength() + bodyLength != data.length) {
throw new MotanFrameworkException("decode error: content length error", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
byte flag = data[3];
byte dataType = (byte) (flag & MASK);
boolean isResponse = (dataType != MotanConstants.FLAG_REQUEST);
byte[] body = new byte[bodyLength];
System.arraycopy(data, RpcProtocolVersion.VERSION_1.getHeaderLength(), body, 0, bodyLength);
long requestId = ByteUtil.bytes2long(data, 4);
Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(channel.getUrl().getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue()));
try {
if (isResponse) {
return decodeResponse(body, dataType, requestId, data[2], serialization);
} else {
return decodeRequest(body, requestId, remoteIp, serialization);
}
} catch (ClassNotFoundException e) {
throw new MotanFrameworkException("decode " + (isResponse ? "response" : "request") + " error: class not found", e, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
} catch (Exception e) {
if (ExceptionUtil.isMotanException(e)) {
throw (RuntimeException) e;
} else {
throw new MotanFrameworkException("decode error: isResponse=" + isResponse, e, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
}
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class DefaultRpcCodec method decodeResponse.
private Object decodeResponse(byte[] body, byte dataType, long requestId, Serialization serialization) throws IOException, ClassNotFoundException {
ByteArrayInputStream inputStream = new ByteArrayInputStream(body);
ObjectInput input = createInput(inputStream);
long processTime = input.readLong();
DefaultResponse response = new DefaultResponse();
response.setRequestId(requestId);
response.setProcessTime(processTime);
if (dataType == MotanConstants.FLAG_RESPONSE_VOID) {
return response;
}
String className = input.readUTF();
Class<?> clz = ReflectUtil.forName(className);
Object result = deserialize((byte[]) input.readObject(), clz, serialization);
if (dataType == MotanConstants.FLAG_RESPONSE) {
response.setValue(result);
} else if (dataType == MotanConstants.FLAG_RESPONSE_EXCEPTION) {
response.setException((Exception) result);
} else {
throw new MotanFrameworkException("decode error: response dataType not support " + dataType, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
response.setRequestId(requestId);
input.close();
return response;
}
Aggregations