Search in sources :

Example 36 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class JacksonHelper method loadClassToCache.

/**
 * load method paramters and return types to cache, will not pass through to next
 *
 * @param key        key
 * @param clazz      interface name
 * @param methodName method name
 */
private void loadClassToCache(String key, Class clazz, String methodName) {
    Method jsonMethod = null;
    Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        if (methodName.equals(method.getName())) {
            jsonMethod = method;
            break;
        }
    }
    if (jsonMethod == null) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_METHOD_NOT_FOUND, clazz.getName(), methodName));
    }
    // parse request types
    Type[] parameterTypes = jsonMethod.getGenericParameterTypes();
    JavaType[] javaTypes = new JavaType[parameterTypes.length];
    for (int i = 0; i < parameterTypes.length; i++) {
        JavaType javaType = mapper.getTypeFactory().constructType(parameterTypes[i]);
        javaTypes[i] = javaType;
    }
    requestClassCache.put(key, javaTypes);
    // parse response types
    Type resType = jsonMethod.getGenericReturnType();
    if (resType == void.class) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_VOID_RETURN, "jackson", clazz.getName()));
    }
    JavaType resJavaType = mapper.getTypeFactory().constructType(resType);
    responseClassCache.put(key, resJavaType);
}
Also used : Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) JavaType(com.fasterxml.jackson.databind.JavaType) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Method(java.lang.reflect.Method)

Example 37 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class ProtobufHelper method loadProtoClassToCache.

/**
 * 加载protobuf接口里方法的参数和返回值类型到缓存,不需要传递
 *
 * @param key        缓存的key
 * @param clazz      接口名
 * @param methodName 方法名
 */
private void loadProtoClassToCache(String key, Class clazz, String methodName) {
    Method pbMethod = null;
    Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        if (methodName.equals(method.getName())) {
            pbMethod = method;
            break;
        }
    }
    if (pbMethod == null) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_METHOD_NOT_FOUND, clazz.getName(), methodName));
    }
    Class[] parameterTypes = pbMethod.getParameterTypes();
    if (parameterTypes == null || parameterTypes.length != 1 || isProtoBufMessageObject(parameterTypes[0])) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_ONLY_ONE_PARAM, "protobuf", clazz.getName()));
    }
    Class reqClass = parameterTypes[0];
    requestClassCache.put(key, reqClass);
    Class resClass = pbMethod.getReturnType();
    if (resClass == void.class || !isProtoBufMessageClass(resClass)) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_PROTOBUF_RETURN, clazz.getName()));
    }
    responseClassCache.put(key, resClass);
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Method(java.lang.reflect.Method)

Example 38 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class ClassUtils method newInstanceWithArgs.

/**
 * 实例化一个对象(根据参数自动检测构造方法)
 *
 * @param clazz    对象类
 * @param argTypes 构造函数需要的参数
 * @param args     构造函数需要的参数
 * @param <T>      对象具体类
 * @return 对象实例
 * @throws SofaRpcRuntimeException 没有找到方法,或者无法处理,或者初始化方法异常等
 */
public static <T> T newInstanceWithArgs(Class<T> clazz, Class<?>[] argTypes, Object[] args) throws SofaRpcRuntimeException {
    if (CommonUtils.isEmpty(argTypes)) {
        return newInstance(clazz);
    }
    try {
        if (!(clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers()))) {
            Constructor<T> constructor = clazz.getDeclaredConstructor(argTypes);
            constructor.setAccessible(true);
            return constructor.newInstance(args);
        } else {
            Constructor<T>[] constructors = (Constructor<T>[]) clazz.getDeclaredConstructors();
            if (constructors == null || constructors.length == 0) {
                throw new SofaRpcRuntimeException("The " + clazz.getCanonicalName() + " has no constructor with argTypes :" + Arrays.toString(argTypes));
            }
            Constructor<T> constructor = null;
            for (Constructor<T> c : constructors) {
                Class[] ps = c.getParameterTypes();
                if (ps.length == argTypes.length + 1) {
                    // 长度多一
                    boolean allMath = true;
                    for (int i = 1; i < ps.length; i++) {
                        // 而且第二个开始的参数类型匹配
                        if (ps[i] != argTypes[i - 1]) {
                            allMath = false;
                            break;
                        }
                    }
                    if (allMath) {
                        constructor = c;
                        break;
                    }
                }
            }
            if (constructor == null) {
                throw new SofaRpcRuntimeException("The " + clazz.getCanonicalName() + " has no constructor with argTypes :" + Arrays.toString(argTypes));
            } else {
                constructor.setAccessible(true);
                Object[] newArgs = new Object[args.length + 1];
                System.arraycopy(args, 0, newArgs, 1, args.length);
                return constructor.newInstance(newArgs);
            }
        }
    } catch (SofaRpcRuntimeException e) {
        throw e;
    } catch (Throwable e) {
        throw new SofaRpcRuntimeException(e.getMessage(), e);
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Constructor(java.lang.reflect.Constructor)

Example 39 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class HashUtils method messageDigest.

/**
 * 换算法? MD5  SHA-1 MurMurHash???
 *
 * @param value the value
 * @return the byte []
 */
public static byte[] messageDigest(String value) {
    MessageDigest md5;
    try {
        md5 = MessageDigest.getInstance("MD5");
        md5.update(value.getBytes("UTF-8"));
        return md5.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new SofaRpcRuntimeException("No such algorithm named md5", e);
    } catch (UnsupportedEncodingException e) {
        throw new SofaRpcRuntimeException("Unsupported encoding of" + value, e);
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 40 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class BeanUtils method copyPropertiesToMap.

/**
 * 复制属性到map,可以自定义前缀
 *
 * @param bean   对象
 * @param prefix 放入key的前缀
 * @param map    要写入的map
 */
public static void copyPropertiesToMap(Object bean, String prefix, Map<String, Object> map) {
    Class clazz = bean.getClass();
    Method[] methods = bean.getClass().getMethods();
    for (Method method : methods) {
        // 复制属性
        Class returnc = method.getReturnType();
        if (ReflectUtils.isBeanPropertyReadMethod(method)) {
            String propertyName = ReflectUtils.getPropertyNameFromBeanReadMethod(method);
            try {
                if (ReflectUtils.getPropertySetterMethod(clazz, propertyName, returnc) == null) {
                    // 还需要有set方法
                    continue;
                }
            } catch (Exception e) {
                continue;
            }
            Object val;
            try {
                val = method.invoke(bean);
            } catch (InvocationTargetException e) {
                throw new SofaRpcRuntimeException("Can't access copy " + propertyName, e.getCause());
            } catch (IllegalAccessException e) {
                throw new SofaRpcRuntimeException("Can't access copy " + propertyName, e);
            }
            if (val != null) {
                // 值不为空,放入缓存
                map.put(prefix + propertyName, val);
            }
        }
    }
    Field[] fields = bean.getClass().getFields();
    for (Field field : fields) {
        String fieldName = field.getName();
        if (map.containsKey(prefix + fieldName)) {
            continue;
        }
        int m = field.getModifiers();
        if (!Modifier.isStatic(m) && !Modifier.isTransient(m)) {
            Object val = null;
            try {
                if (field.isAccessible()) {
                    val = field.get(bean);
                } else {
                    try {
                        field.setAccessible(true);
                        val = field.get(bean);
                    } finally {
                        field.setAccessible(false);
                    }
                }
            } catch (IllegalAccessException e) {
            // LOGGER.warn("Can't access field" + fieldName + "when copy value to context", e);
            }
            if (val != null) {
                map.put(prefix + fieldName, val);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Method(java.lang.reflect.Method) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)64 Method (java.lang.reflect.Method)10 KeeperException (org.apache.zookeeper.KeeperException)7 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)6 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)6 NacosException (com.alibaba.nacos.api.exception.NacosException)5 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)4 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)4 Instance (com.alibaba.nacos.api.naming.pojo.Instance)3 ConsumerSubEvent (com.alipay.sofa.rpc.event.ConsumerSubEvent)3 ProviderPubEvent (com.alipay.sofa.rpc.event.ProviderPubEvent)3 ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)3 SslContext (io.netty.handler.ssl.SslContext)3 File (java.io.File)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 Test (org.junit.Test)3 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)2 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)2 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)2