Search in sources :

Example 6 with RpcException

use of com.duangframework.core.exceptions.RpcException in project duangframework by tcrct.

the class AutoBuildServiceInterface method createInterface.

/**
 * 根据参数,创建接口类文件
 * @param clazz		要创建接口文件的类
 * @param interFaceDirPath  接口文件路径,不能包括文件名
 * @param packagePath 包路径名
 * @return
 */
public static boolean createInterface(Class<?> clazz, String interFaceDirPath, String packagePath) throws Exception {
    if (ToolsKit.isEmpty(interFaceDirPath)) {
        throw new RpcException("interFaceFilePath is null");
    }
    if (interFaceDirPath.contains(".")) {
        throw new RpcException("interFaceFilePath only dir path");
    }
    Set<String> excludedMethodName = ObjectKit.buildExcludedMethodName();
    // 只取public的方法
    Method[] methods = clazz.getMethods();
    String classPath = clazz.getName() + ".";
    StringBuilder sb = new StringBuilder();
    try {
        ClassPool pool = ClassPool.getDefault();
        CtClass cc = pool.get(clazz.getName());
        for (Method method : methods) {
            // 过滤掉Object.class里的公用方法及静态方法
            if (excludedMethodName.contains(method.getName()) || Modifier.isStatic(method.getModifiers())) {
                continue;
            }
            // 反射取出方法里的参数名
            List<String> variableNames = getLocalVariableAttributeName(cc, method);
            sb.append("\t").append(toGenericString(method, variableNames).replace(classPath, "")).append(";").append("\n\n");
        }
        // Service接口名
        String fileName = "I" + clazz.getSimpleName();
        // 创建接口类内容
        String fileContext = createInterfaceContextString(clazz.getName(), fileName, packagePath, sb.toString());
        File interFaceFileDir = new File(interFaceDirPath);
        // 文件夹不存在则创建
        if (!interFaceFileDir.exists() && interFaceFileDir.isDirectory()) {
            logger.warn("dir is not exists, create it...");
            interFaceFileDir.mkdirs();
        }
        File interFaceFile = createInterFaceFileOnDisk(interFaceDirPath, fileName, fileContext);
        if (interFaceFile.isFile()) {
            logger.warn("create " + interFaceDirPath + "/" + fileName + ".java is success!");
        }
        return true;
    } catch (Exception e) {
        throw new RpcException(e.getMessage(), e);
    }
}
Also used : CtClass(javassist.CtClass) RpcException(com.duangframework.core.exceptions.RpcException) ClassPool(javassist.ClassPool) CtMethod(javassist.CtMethod) Method(java.lang.reflect.Method) File(java.io.File) RpcException(com.duangframework.core.exceptions.RpcException) NotFoundException(javassist.NotFoundException) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException)

Example 7 with RpcException

use of com.duangframework.core.exceptions.RpcException in project duangframework by tcrct.

the class AutoBuildServiceInterface method getLocalVariableAttributeName.

/**
 * 反射取出方法里的参数名
 * @param cc                类对象
 * @param method        方法名
 * @return      方法名集合
 * @throws Exception
 */
private static List<String> getLocalVariableAttributeName(CtClass cc, Method method) throws Exception {
    List<String> paramNames = null;
    try {
        CtMethod cm = cc.getDeclaredMethod(method.getName());
        MethodInfo methodInfo = cm.getMethodInfo();
        CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
        LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
        if (attr != null) {
            int size = cm.getParameterTypes().length;
            paramNames = new ArrayList<>(size);
            int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
            for (int i = 0; i < size; i++) {
                paramNames.add(attr.variableName(i + pos));
            }
        }
    } catch (NotFoundException e) {
        throw new RpcException(e.getMessage(), e);
    }
    return paramNames;
}
Also used : CodeAttribute(javassist.bytecode.CodeAttribute) RpcException(com.duangframework.core.exceptions.RpcException) NotFoundException(javassist.NotFoundException) MethodInfo(javassist.bytecode.MethodInfo) LocalVariableAttribute(javassist.bytecode.LocalVariableAttribute) CtMethod(javassist.CtMethod)

Example 8 with RpcException

use of com.duangframework.core.exceptions.RpcException in project duangframework by tcrct.

the class AutoBuildServiceInterface method getTypeName.

/**
 * 根据参数类型取出参数类型名称字符串
 * @param type      参数类型
 * @return
 * @throws Exception
 */
private static String getTypeName(Class<?> type) throws Exception {
    if (type.isArray()) {
        try {
            Class<?> cl = type;
            int dimensions = 0;
            while (cl.isArray()) {
                dimensions++;
                cl = cl.getComponentType();
            }
            StringBuffer sb = new StringBuffer();
            sb.append(cl.getName());
            for (int i = 0; i < dimensions; i++) {
                sb.append("[]");
            }
            return sb.toString();
        } catch (Throwable e) {
            throw new RpcException(e.getMessage(), e);
        }
    }
    return type.getName();
}
Also used : RpcException(com.duangframework.core.exceptions.RpcException)

Example 9 with RpcException

use of com.duangframework.core.exceptions.RpcException in project duangframework by tcrct.

the class AutoBuildServiceInterface method createBatchInterface.

/**
 *  批量创建Service类接口文件到指定目录下
 * @param interFaceDirPath      存放RPC接口文件的目录
 * @param  customizeDir           自定义目录
 * @return
 * @throws Exception
 */
public static boolean createBatchInterface(String interFaceDirPath, String customizeDir) throws Exception {
    Map<Class<?>, Object> serviceMap = BeanUtils.getAllBeanMaps().get(Service.class.getSimpleName());
    if (ToolsKit.isEmpty(serviceMap)) {
        throw new EmptyNullException("serviceMap is null");
    }
    try {
        for (Iterator<Class<?>> iterator = serviceMap.keySet().iterator(); iterator.hasNext(); ) {
            Class<?> clazz = iterator.next();
            String packagePath = RpcUtils.getRpcPackagePath(customizeDir);
            createInterface(clazz, interFaceDirPath, packagePath);
        }
        return true;
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
        throw new RpcException("batch create service interface is fail: " + e.getMessage(), e);
    }
}
Also used : EmptyNullException(com.duangframework.core.exceptions.EmptyNullException) RpcException(com.duangframework.core.exceptions.RpcException) Service(com.duangframework.core.annotation.mvc.Service) CtClass(javassist.CtClass) RpcException(com.duangframework.core.exceptions.RpcException) NotFoundException(javassist.NotFoundException) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException)

Example 10 with RpcException

use of com.duangframework.core.exceptions.RpcException in project duangframework by tcrct.

the class RpcFactory method watchNode.

public static void watchNode() {
    for (Iterator<String> iterator = watchNodePath.iterator(); iterator.hasNext(); ) {
        String path = iterator.next();
        if (ToolsKit.isEmpty(path)) {
            continue;
        }
        boolean isExists = ZooKit.duang().path(path).exists();
        if (!isExists) {
            throw new RpcException("zookeeper note[" + path + "]  is not exists");
        }
        List<String> nodeList = ZooKit.duang().path(path).children();
        if (ToolsKit.isEmpty(nodeList)) {
            throw new RpcException("zookeeper children note[" + path + "]  is empty");
        }
        for (String nodePath : nodeList) {
            String subPath = path + "/" + nodePath;
            String jsonText = ZooKit.duang().path(subPath).get();
            Map<String, Object> map = ToolsKit.jsonParseObject(jsonText, Map.class);
            if (ToolsKit.isNotEmpty(map)) {
                for (Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator(); it.hasNext(); ) {
                    Map.Entry<String, Object> entry = it.next();
                    String key = entry.getKey();
                    String value = entry.getValue() + "";
                    RpcAction rpcAction = ToolsKit.jsonParseObject(value, RpcAction.class);
                    addRpcAction2List(key, rpcAction);
                }
            }
        }
    }
    // 如果有指定的调用服务器时 2017-7-13
    Map<String, List<RpcAction>> actionMapNew = RpcUtils.getAssignRpcActionMap(actionMap);
    if (ToolsKit.isNotEmpty(actionMapNew)) {
        actionMap.clear();
        actionMap.putAll(actionMapNew);
        logger.warn("AssignRpcActionEndport:  " + ToolsKit.toJsonString(actionMap));
    }
    // 监听该节点下的所有目录
    if (ToolsKit.isEmpty(zooKeeperListener)) {
        zooKeeperListener = new ZooKeeperListener(watchNodePath);
        zooKeeperListener.startListener();
    }
}
Also used : RpcException(com.duangframework.core.exceptions.RpcException) RpcAction(com.duangframework.rpc.common.RpcAction) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

RpcException (com.duangframework.core.exceptions.RpcException)11 EmptyNullException (com.duangframework.core.exceptions.EmptyNullException)5 NotFoundException (javassist.NotFoundException)4 CtClass (javassist.CtClass)3 RpcAction (com.duangframework.rpc.common.RpcAction)2 Method (java.lang.reflect.Method)2 CtMethod (javassist.CtMethod)2 Service (com.duangframework.core.annotation.mvc.Service)1 Rpc (com.duangframework.core.annotation.rpc.Rpc)1 IProxy (com.duangframework.core.interfaces.IProxy)1 RpcRequest (com.duangframework.rpc.common.RpcRequest)1 RpcResponse (com.duangframework.rpc.common.RpcResponse)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 File (java.io.File)1 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ClassPool (javassist.ClassPool)1