GlueFactory.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.ruoyi.ucp.glue;
  2. import com.ruoyi.ucp.glue.impl.SpringGlueFactory;
  3. import com.ruoyi.ucp.handler.IJobHandler;
  4. import groovy.lang.GroovyClassLoader;
  5. import java.math.BigInteger;
  6. import java.security.MessageDigest;
  7. import java.util.concurrent.ConcurrentHashMap;
  8. import java.util.concurrent.ConcurrentMap;
  9. /**
  10. * glue factory, product class/object by name
  11. *
  12. * @author xuxueli 2016-1-2 20:02:27
  13. */
  14. public class GlueFactory {
  15. private static GlueFactory glueFactory = new SpringGlueFactory();
  16. public static GlueFactory getInstance() {
  17. return glueFactory;
  18. }
  19. public static String getFunctionTemplate() {
  20. String FunctionTemplate = "package com.ruoyi.ucp;\n" +
  21. "\n" +
  22. "import cn.hutool.core.date.DateUtil;\n" +
  23. "import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;\n" +
  24. "import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;\n" +
  25. "import com.ruoyi.ucp.entity.StationInfoDay;\n" +
  26. "import com.ruoyi.ucp.feign.AdapterApi;\n" +
  27. "import com.ruoyi.ucp.handler.IJobHandler;\n" +
  28. "import com.ruoyi.ucp.service.ILineInfoDayService;\n" +
  29. "import com.ruoyi.ucp.service.IPointInfoService;\n" +
  30. "import com.ruoyi.ucp.service.IStationInfoDayService;\n" +
  31. "import com.ruoyi.ucp.service.IStationInfoHourService;\n" +
  32. "\n" +
  33. "import javax.annotation.Resource;\n" +
  34. "import java.util.Date;\n" +
  35. "import java.util.List;\n" +
  36. "import java.util.Map;\n" +
  37. "import java.util.function.Function;\n" +
  38. "import java.util.stream.Collectors;\n" +
  39. "\n" +
  40. "public class JavaFunctionJobHandler extends IJobHandler {\n" +
  41. "\n" +
  42. " @Resource\n" +
  43. " private AdapterApi adapter;\n" +
  44. " @Resource\n" +
  45. " private IPointInfoService pointService;\n" +
  46. " @Resource\n" +
  47. " private IStationInfoHourService stationInfoHourService;\n" +
  48. " @Resource\n" +
  49. " private IStationInfoDayService stationInfoDayService;\n" +
  50. " @Resource\n" +
  51. " private ILineInfoDayService lineInfoDayService;\n" +
  52. "\n" +
  53. " @Override\n" +
  54. " public void execute() throws Exception {\n" +
  55. "\n" +
  56. " }\n" +
  57. "\n" +
  58. " @Override\n" +
  59. " public IJobHandler getFunctionHandler() {\n" +
  60. " return null;\n" +
  61. " }\n" +
  62. "\n" +
  63. " @Override\n" +
  64. " public void setFunctionHandler(IJobHandler jobHandler) {\n" +
  65. "\n" +
  66. " }\n" +
  67. " //此处加公式\n" +
  68. "}";
  69. return FunctionTemplate;
  70. }
  71. public static void refreshInstance(int type) {
  72. if (type == 0) {
  73. glueFactory = new GlueFactory();
  74. } else if (type == 1) {
  75. glueFactory = new SpringGlueFactory();
  76. }
  77. }
  78. /**
  79. * groovy class loader
  80. */
  81. private GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
  82. private ConcurrentMap<String, Class<?>> CLASS_CACHE = new ConcurrentHashMap<>();
  83. /**
  84. * load new instance, prototype
  85. *
  86. * @param codeSource
  87. * @return
  88. * @throws Exception
  89. */
  90. public IJobHandler loadNewInstance(String codeSource) throws Exception {
  91. if (codeSource != null && codeSource.trim().length() > 0) {
  92. Class<?> clazz = getCodeSourceClass(codeSource);
  93. if (clazz != null) {
  94. Object instance = clazz.newInstance();
  95. if (instance != null) {
  96. if (instance instanceof IJobHandler) {
  97. this.injectService(instance);
  98. return (IJobHandler) instance;
  99. } else {
  100. throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, "
  101. + "cannot convert from instance[" + instance.getClass() + "] to IJobHandler");
  102. }
  103. }
  104. }
  105. }
  106. throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, instance is null");
  107. }
  108. private Class<?> getCodeSourceClass(String codeSource) {
  109. try {
  110. // md5
  111. byte[] md5 = MessageDigest.getInstance("MD5").digest(codeSource.getBytes());
  112. String md5Str = new BigInteger(1, md5).toString(16);
  113. Class<?> clazz = CLASS_CACHE.get(md5Str);
  114. if (clazz == null) {
  115. clazz = groovyClassLoader.parseClass(codeSource);
  116. CLASS_CACHE.putIfAbsent(md5Str, clazz);
  117. }
  118. return clazz;
  119. } catch (Exception e) {
  120. return groovyClassLoader.parseClass(codeSource);
  121. }
  122. }
  123. /**
  124. * inject service of bean field
  125. *
  126. * @param instance
  127. */
  128. public void injectService(Object instance) {
  129. // do something
  130. }
  131. }