apzl_leasing/src_app_fresh/com/base/tool/URIMappingTool.java
liujiaji 6397d906b5 src_app 移除
src_app_fresh 添加
2018-06-11 13:40:56 +08:00

111 lines
3.4 KiB
Java

package com.base.tool;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import cn.config.mapping.URIMappingCache;
import cn.config.mapping.URIMappingKey;
import cn.config.mapping.URIMappingValue;
import com.amarsoft.are.ARE;
import com.base.util.ClassUtil;
public class URIMappingTool {
public static void uriMappingClassMethod(Set<Class<?>> classes) {
for (Class<?> clazz : classes) {
Path classAnno = clazz.getAnnotation(Path.class);
if (classAnno == null)
continue;
String classUriPath = classAnno.value();
Method[] methods = clazz.getMethods();
for (Method method : methods) {
Path methodAnno = method.getAnnotation(Path.class);
String methodUriPath = "";
if (methodAnno != null) {
methodUriPath = methodAnno.value();
}
String accessUri = classUriPath + methodUriPath;
// 访问URI+请求方法组合成一个KEY
URIMappingKey key = null;
if (method.getAnnotation(POST.class) != null) {
key = new URIMappingKey(accessUri, HttpMethod.POST);
} else if (method.getAnnotation(GET.class) != null) {
key = new URIMappingKey(accessUri, HttpMethod.GET);
} else if (method.getAnnotation(PUT.class) != null) {
key = new URIMappingKey(accessUri, HttpMethod.PUT);
} else if (method.getAnnotation(DELETE.class) != null) {
key = new URIMappingKey(accessUri, HttpMethod.DELETE);
} else {
continue;
}
// 类+方法组合成一个VALUE,如果已经被绑定过了,则抛出异常
URIMappingValue existValue = URIMappingCache.getInstance().get(
key);
if (existValue != null) {
throw new RuntimeException(MessageFormat.format(
"{0} {1} --> {2}.{3} error,mapped on {4}.{5}",
key.getHttpMethod(), key.getUri(), clazz.getName(),
method.getName(), existValue.getClazz().getName(),
existValue.getMethod().getName()));
}
URIMappingValue value = new URIMappingValue(clazz, method);
ARE.getLog().info(
String.format("%-6s %s --> %s.%s ",
key.getHttpMethod(), key.getUri(),
clazz.getName(), method.getName()));
URIMappingCache.getInstance().put(key, value);
}
}
}
public static void scanPackageAndInitResfSetting(String pkgExpr) {
URIMappingCache.getInstance().clear();
String[] resfPkgs = PkgsTool.parsePkgs(pkgExpr);
if (resfPkgs == null || resfPkgs.length == 0)
return;
Set<Class<?>> classes = new HashSet<Class<?>>();
ARE.getLog()
.info("---------------------RESTFULL MAPPING------------------------");
for (String pkg : resfPkgs) {
classes.addAll(findClassWithPathAnno(pkg));
}
URIMappingTool.uriMappingClassMethod(classes);
ARE.getLog().info(
"---------------------------------------------------------");
}
private static List<Class<?>> findClassWithPathAnno(String pkg) {
List<Class<?>> classList = new ArrayList<Class<?>>();
Set<Class<?>> classes = ClassUtil.getClasses(pkg);
for (Class<?> clazz : classes) {
// 从类注解上查找Path
Path classAnno = clazz.getAnnotation(Path.class);
if (classAnno == null)
continue;
// String annoPath = classAnno.value();
classList.add(clazz);
}
return classList;
}
}