Authored by Lixiaodi

增加代理serviceCall

package com.yohoufo.common.caller;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Controller;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yoho.core.rest.exception.ServiceNotFoundException;
public class UfoServiceCaller implements ApplicationListener<ContextRefreshedEvent> {
private final static Logger logger = LoggerFactory.getLogger(UfoServiceCaller.class);
private static class ServiceMethod {
Object bean;
Method method;
ServiceMethod(Object bean, Method method) {
this.bean = bean;
this.method = method;
}
Object call(Object[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return method.invoke(bean, args);
}
}
private Map<String, ServiceMethod> serviceMap = new HashMap<>();
public Object call(String serviceMethod, Object[] param) {
logger.info("call ufo service : {}", serviceMethod);
ServiceMethod sm = serviceMap.get(serviceMethod);
if (sm == null) {
throw new ServiceNotFoundException("Not found service " + serviceMethod, serviceMethod);
}
try {
return sm.call(param);
} catch (Exception e) {
logger.error("service error : " + serviceMethod, e);
return null;
}
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
//查找bean
logger.info("start to register ufo service info");
final Map<String /* bean name*/,Object /*bean instance*/> serviceBeanMap = new HashMap<>();
ApplicationContext applicationContext = event.getApplicationContext();
serviceBeanMap.putAll(applicationContext.getBeansWithAnnotation(Controller.class));
serviceBeanMap.putAll(applicationContext.getBeansWithAnnotation(RestController.class));
//注册
if(MapUtils.isNotEmpty(serviceBeanMap)) {
register(applicationContext, serviceBeanMap);
}
}
private void register(ApplicationContext applicationContext, Map<String, Object> serviceBeanMap) {
for (Object bean : serviceBeanMap.values()) {
// 获取每一个方法上的request mapping的value,可能为空
Method[] methods = ReflectionUtils.getAllDeclaredMethods(bean.getClass());
for (Method method : methods) {
RequestMapping method_requestMappingAnno = method.getAnnotation(RequestMapping.class);
String[] params = method_requestMappingAnno.params();
String tag = "method=";
if (params != null && params.length == 1 && params[1].trim().startsWith(tag)) {
String methodStr = params[1].trim().substring(tag.length());
if (StringUtils.isNotBlank(methodStr)) {
serviceMap.put(methodStr, new ServiceMethod(bean, method));
}
}
}
logger.info("Register all ufo service for controller:{} success", bean);
}
}
}
... ...
... ... @@ -87,6 +87,7 @@
</bean>
<!-- api文档配置 end -->
<bean id="ufoServiceCaller" class="com.yohoufo.common.caller.UfoServiceCaller">
<!--Spring mvc 拦截器-->
<mvc:interceptors>
... ...