PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/xjt/ws/invoker/LocalServiceInvoker.java

https://bitbucket.org/xiejuntao/xdesktop
Java | 120 lines | 105 code | 6 blank | 9 comment | 18 complexity | eb2f7c2a7405e44101361b30011b60f8 MD5 | raw file
  1. package xjt.ws.invoker;
  2. import java.io.File;
  3. import java.io.InputStream;
  4. import java.lang.reflect.Method;
  5. import java.lang.reflect.Type;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.BeansException;
  9. import org.springframework.context.ApplicationContext;
  10. import org.springframework.context.ApplicationContextAware;
  11. import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
  12. import org.springframework.core.ParameterNameDiscoverer;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import xjt.ws.cache.LRULinkedHashMap;
  16. import xjt.ws.core.ResponseFile;
  17. import xjt.ws.core.ServiceInvokeServlet;
  18. import xjt.ws.core.ServiceRequest;
  19. import xjt.ws.core.ServiceResponse;
  20. import com.alibaba.fastjson.JSON;
  21. import com.alibaba.fastjson.JSONArray;
  22. import com.alibaba.fastjson.JSONObject;
  23. /**
  24. * 将服务请求转换成本地Spring Bean调用的实现
  25. *
  26. * @author huzq
  27. * @author xjt
  28. */
  29. @Component("serviceInvoker")
  30. public class LocalServiceInvoker implements ApplicationContextAware,
  31. ServiceInvoker {
  32. /**
  33. * Logger for this class
  34. */
  35. private static final Logger logger = LoggerFactory.getLogger(ServiceInvokeServlet.class);
  36. private ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
  37. private LRULinkedHashMap<String,Method> hashMap = new LRULinkedHashMap<String, Method>(100);
  38. private ApplicationContext context;
  39. public void invoke(ServiceRequest serviceRequest,
  40. ServiceResponse serviceResponse) throws Exception {
  41. Object serviceToCall = null;
  42. try {
  43. serviceToCall = context.getBean(
  44. serviceRequest.getService());
  45. } catch (BeansException e) {
  46. logger.error("没有找到名为" + serviceRequest.getService() + "的服务", e);
  47. serviceResponse.setErrors("没有找到名为" + serviceRequest.getService() + "的服务");
  48. return;
  49. }
  50. Method methodToCall = null;
  51. String methodName = serviceRequest.getMethod();
  52. if(hashMap.containsKey(methodName)){
  53. methodToCall = hashMap.get(methodName);
  54. }else{
  55. Method[] methods = serviceToCall.getClass().getMethods();
  56. for (int i = 0; i < methods.length; i++) {
  57. if (methods[i].getName().equals(methodName)) {
  58. methodToCall = methods[i];
  59. break;
  60. }
  61. }
  62. if (methodToCall == null) {
  63. serviceResponse.setErrors(serviceToCall.getClass().getName()
  64. + "没有找到名为" + methodName + "的方法");
  65. return;
  66. }else{
  67. hashMap.put(methodName, methodToCall);
  68. }
  69. }
  70. logger.debug("serviceToCall:" + serviceToCall.getClass().getName());
  71. logger.debug("methodToCall:" + methodToCall.getName());
  72. String[] paramNames = parameterNameDiscoverer
  73. .getParameterNames(methodToCall);
  74. JSONObject jsonParamsValues = serviceRequest.getParams();
  75. Class<?>[] parameterTypes = methodToCall.getParameterTypes();
  76. Type[] types = methodToCall.getGenericParameterTypes();
  77. Object[] paramValues = new Object[parameterTypes.length];
  78. logger.debug("jsonParamValues:" + jsonParamsValues);
  79. for (int i = 0; i < parameterTypes.length; i++) {
  80. Class<?> clazz = parameterTypes[i];
  81. String paramName = paramNames[i];
  82. Type type = types[i];
  83. paramValues[i] = ObjectUtil.initValue(clazz, type, paramName, jsonParamsValues, serviceRequest);
  84. }
  85. Object result = methodToCall.invoke(serviceToCall, paramValues);
  86. if (methodToCall.getReturnType().isAssignableFrom(InputStream.class)
  87. || methodToCall.getReturnType().isAssignableFrom(File.class)
  88. || methodToCall.getReturnType().isAssignableFrom(
  89. ResponseFile.class)) {
  90. serviceResponse.setBinary(true);
  91. } else {
  92. serviceResponse.setBinary(false);
  93. }
  94. if (result != null) {
  95. ResponseFile responseFile = null;
  96. if (result instanceof File) {
  97. responseFile = new ResponseFile((File) result);
  98. } else if (result instanceof InputStream) {
  99. responseFile = new ResponseFile((InputStream) result);
  100. } else if (result instanceof ResponseFile) {
  101. responseFile = (ResponseFile) result;
  102. } else {
  103. serviceResponse.setResults(result);
  104. }
  105. serviceResponse.setResponseFile(responseFile);
  106. }
  107. serviceResponse.setSuccess(true);
  108. }
  109. @Override
  110. public void setApplicationContext(ApplicationContext applicationContext)
  111. throws BeansException {
  112. this.context = applicationContext;
  113. }
  114. }