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);
}
}
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;
}
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();
}
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);
}
}
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();
}
}
Aggregations