Authored by Lixiaodi

增加代理serviceCall

  1 +package com.yohoufo.common.caller;
  2 +
  3 +import java.lang.reflect.InvocationTargetException;
  4 +import java.lang.reflect.Method;
  5 +import java.util.HashMap;
  6 +import java.util.Map;
  7 +
  8 +import org.apache.commons.collections.MapUtils;
  9 +import org.apache.commons.lang3.StringUtils;
  10 +import org.slf4j.Logger;
  11 +import org.slf4j.LoggerFactory;
  12 +import org.springframework.context.ApplicationContext;
  13 +import org.springframework.context.ApplicationListener;
  14 +import org.springframework.context.event.ContextRefreshedEvent;
  15 +import org.springframework.stereotype.Controller;
  16 +import org.springframework.util.ReflectionUtils;
  17 +import org.springframework.web.bind.annotation.RequestMapping;
  18 +import org.springframework.web.bind.annotation.RestController;
  19 +
  20 +import com.yoho.core.rest.exception.ServiceNotFoundException;
  21 +
  22 +public class UfoServiceCaller implements ApplicationListener<ContextRefreshedEvent> {
  23 +
  24 + private final static Logger logger = LoggerFactory.getLogger(UfoServiceCaller.class);
  25 +
  26 + private static class ServiceMethod {
  27 +
  28 + Object bean;
  29 + Method method;
  30 +
  31 + ServiceMethod(Object bean, Method method) {
  32 + this.bean = bean;
  33 + this.method = method;
  34 + }
  35 +
  36 + Object call(Object[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  37 + return method.invoke(bean, args);
  38 + }
  39 + }
  40 +
  41 + private Map<String, ServiceMethod> serviceMap = new HashMap<>();
  42 +
  43 + public Object call(String serviceMethod, Object[] param) {
  44 + logger.info("call ufo service : {}", serviceMethod);
  45 + ServiceMethod sm = serviceMap.get(serviceMethod);
  46 + if (sm == null) {
  47 + throw new ServiceNotFoundException("Not found service " + serviceMethod, serviceMethod);
  48 + }
  49 +
  50 + try {
  51 + return sm.call(param);
  52 + } catch (Exception e) {
  53 + logger.error("service error : " + serviceMethod, e);
  54 + return null;
  55 + }
  56 + }
  57 +
  58 + @Override
  59 + public void onApplicationEvent(ContextRefreshedEvent event) {
  60 +
  61 + //查找bean
  62 + logger.info("start to register ufo service info");
  63 + final Map<String /* bean name*/,Object /*bean instance*/> serviceBeanMap = new HashMap<>();
  64 + ApplicationContext applicationContext = event.getApplicationContext();
  65 + serviceBeanMap.putAll(applicationContext.getBeansWithAnnotation(Controller.class));
  66 + serviceBeanMap.putAll(applicationContext.getBeansWithAnnotation(RestController.class));
  67 +
  68 + //注册
  69 + if(MapUtils.isNotEmpty(serviceBeanMap)) {
  70 + register(applicationContext, serviceBeanMap);
  71 + }
  72 + }
  73 +
  74 + private void register(ApplicationContext applicationContext, Map<String, Object> serviceBeanMap) {
  75 + for (Object bean : serviceBeanMap.values()) {
  76 + // 获取每一个方法上的request mapping的value,可能为空
  77 + Method[] methods = ReflectionUtils.getAllDeclaredMethods(bean.getClass());
  78 + for (Method method : methods) {
  79 + RequestMapping method_requestMappingAnno = method.getAnnotation(RequestMapping.class);
  80 + String[] params = method_requestMappingAnno.params();
  81 + String tag = "method=";
  82 + if (params != null && params.length == 1 && params[1].trim().startsWith(tag)) {
  83 + String methodStr = params[1].trim().substring(tag.length());
  84 + if (StringUtils.isNotBlank(methodStr)) {
  85 + serviceMap.put(methodStr, new ServiceMethod(bean, method));
  86 + }
  87 + }
  88 + }
  89 + logger.info("Register all ufo service for controller:{} success", bean);
  90 + }
  91 + }
  92 +}
@@ -87,6 +87,7 @@ @@ -87,6 +87,7 @@
87 </bean> 87 </bean>
88 <!-- api文档配置 end --> 88 <!-- api文档配置 end -->
89 89
  90 + <bean id="ufoServiceCaller" class="com.yohoufo.common.caller.UfoServiceCaller">
90 91
91 <!--Spring mvc 拦截器--> 92 <!--Spring mvc 拦截器-->
92 <mvc:interceptors> 93 <mvc:interceptors>