ExtParamFactory.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.jd.platform.jlog.client.cache;
  2. import org.reflections.Reflections;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import javax.servlet.ServletRequest;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import java.util.concurrent.atomic.AtomicBoolean;
  10. /**
  11. * @author tangbohu
  12. * @version 1.0.0
  13. * @ClassName ExtParamFactory.java
  14. * @Description TODO
  15. * @createTime 2022年03月21日 20:10:00
  16. */
  17. public class ExtParamFactory {
  18. private static Logger LOGGER = LoggerFactory.getLogger(ExtParamFactory.class);
  19. private static volatile ReqMap reqMap = null;
  20. private static volatile RespMap respMap = null;
  21. private static AtomicBoolean reqHasLoad = new AtomicBoolean(false);
  22. private static AtomicBoolean respHasLoad = new AtomicBoolean(false);
  23. public static Map<String, Object> getReqMap(ServletRequest request) {
  24. try {
  25. if(!reqHasLoad.get() && reqMap == null){
  26. synchronized (ExtParamFactory.class){
  27. if(reqMap == null){
  28. Reflections reflections = new Reflections();
  29. Set<Class<? extends ReqMap>> subTypes = reflections.getSubTypesOf(ReqMap.class);
  30. for (Class<? extends ReqMap> subType : subTypes) {
  31. reqMap = subType.newInstance();
  32. break;
  33. }
  34. }
  35. }
  36. reqHasLoad.set(true);
  37. }
  38. return reqMap == null ? new HashMap<>(0) : reqMap.appendResMap(request);
  39. }catch (Exception e){
  40. LOGGER.error("反射获取入参异常",e);
  41. }
  42. return new HashMap<>(0);
  43. }
  44. public static Map<String, Object> getRespMap(String cnt) {
  45. try {
  46. if(!respHasLoad.get() && respMap == null){
  47. synchronized (ExtParamFactory.class){
  48. if(respMap == null){
  49. Reflections reflections = new Reflections();
  50. Set<Class<? extends RespMap>> subTypes = reflections.getSubTypesOf(RespMap.class);
  51. for (Class<? extends RespMap> subType : subTypes) {
  52. respMap = subType.newInstance();
  53. break;
  54. }
  55. }
  56. }
  57. respHasLoad.set(true);
  58. }
  59. return respMap == null ? new HashMap<>(0) : respMap.appendRespMap(cnt);
  60. }catch (Exception e){
  61. LOGGER.error("反射获取出参异常",e);
  62. }
  63. return new HashMap<>(0);
  64. }
  65. }