Search in sources :

Example 6 with MotanFrameworkException

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);
            }
        }
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Example 7 with MotanFrameworkException

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);
        }
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ConfigDesc(com.weibo.api.motan.config.annotation.ConfigDesc) Method(java.lang.reflect.Method) Map(java.util.Map) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 8 with MotanFrameworkException

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;
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ObjectInput(java.io.ObjectInput) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 9 with MotanFrameworkException

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);
        }
    }
}
Also used : Serialization(com.weibo.api.motan.codec.Serialization) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) IOException(java.io.IOException)

Example 10 with MotanFrameworkException

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;
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) IOException(java.io.IOException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Aggregations

MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)41 URL (com.weibo.api.motan.rpc.URL)7 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)6 ArrayList (java.util.ArrayList)6 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)5 IOException (java.io.IOException)5 Method (java.lang.reflect.Method)4 Registry (com.weibo.api.motan.registry.Registry)3 Response (com.weibo.api.motan.rpc.Response)3 HeartbeatFactory (com.weibo.api.motan.transport.HeartbeatFactory)3 ObjectInput (java.io.ObjectInput)3 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Serialization (com.weibo.api.motan.codec.Serialization)2 ConfigHandler (com.weibo.api.motan.config.handler.ConfigHandler)2 CommandListener (com.weibo.api.motan.registry.support.command.CommandListener)2 ServiceListener (com.weibo.api.motan.registry.support.command.ServiceListener)2 TransportException (com.weibo.api.motan.transport.TransportException)2 Map (java.util.Map)2 IZkChildListener (org.I0Itec.zkclient.IZkChildListener)2