|
@@ -0,0 +1,151 @@
|
|
|
+package com.ruoyi.ucp.glue;
|
|
|
+
|
|
|
+import com.ruoyi.ucp.glue.impl.SpringGlueFactory;
|
|
|
+import com.ruoyi.ucp.handler.IJobHandler;
|
|
|
+import groovy.lang.GroovyClassLoader;
|
|
|
+
|
|
|
+import java.math.BigInteger;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.concurrent.ConcurrentMap;
|
|
|
+
|
|
|
+
|
|
|
+ * glue factory, product class/object by name
|
|
|
+ *
|
|
|
+ * @author xuxueli 2016-1-2 20:02:27
|
|
|
+ */
|
|
|
+public class GlueFactory {
|
|
|
+
|
|
|
+ private static GlueFactory glueFactory = new SpringGlueFactory();
|
|
|
+
|
|
|
+ public static GlueFactory getInstance(){
|
|
|
+ return glueFactory;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getJavaGlue(){
|
|
|
+ String JavaGlue = "package com.ruoyi.ucp;\n" +
|
|
|
+ "\n" +
|
|
|
+ "import com.ruoyi.ucp.feign.AdapterApi;\n" +
|
|
|
+ "import com.ruoyi.ucp.handler.IJobHandler;\n" +
|
|
|
+ "import com.ruoyi.ucp.service.ILineInfoDayService;\n" +
|
|
|
+ "import com.ruoyi.ucp.service.IPointInfoService;\n" +
|
|
|
+ "import com.ruoyi.ucp.service.IStationInfoDayService;\n" +
|
|
|
+ "import com.ruoyi.ucp.service.IStationInfoHourService;\n" +
|
|
|
+ "\n" +
|
|
|
+ "import javax.annotation.Resource;\n" +
|
|
|
+ "\n" +
|
|
|
+ "public class JavaGlueJobHandler extends IJobHandler{\n" +
|
|
|
+ "\n" +
|
|
|
+ " @Resource\n" +
|
|
|
+ " private AdapterApi adapter;\n" +
|
|
|
+ " @Resource\n" +
|
|
|
+ " private IPointInfoService pointService;\n" +
|
|
|
+ " @Resource\n" +
|
|
|
+ " private IStationInfoHourService stationInfoHourService;\n" +
|
|
|
+ " @Resource\n" +
|
|
|
+ " private IStationInfoDayService stationInfoDayService;\n" +
|
|
|
+ " @Resource\n" +
|
|
|
+ " private ILineInfoDayService lineInfoDayService;\n" +
|
|
|
+ "\n" +
|
|
|
+ " private IJobHandler jobHandler;\n" +
|
|
|
+ "\n" +
|
|
|
+ " @Override\n" +
|
|
|
+ " public void execute() throws Exception {\n" +
|
|
|
+ " //此处运行方法\n" +
|
|
|
+ " }\n" +
|
|
|
+ "\n" +
|
|
|
+ " @Override\n" +
|
|
|
+ " public IJobHandler getFunctionHandler() {\n" +
|
|
|
+ " return jobHandler;\n" +
|
|
|
+ " }\n" +
|
|
|
+ "\n" +
|
|
|
+ " @Override\n" +
|
|
|
+ " public void setFunctionHandler(IJobHandler jobHandler) {\n" +
|
|
|
+ " this.jobHandler = jobHandler;\n" +
|
|
|
+ " }\n" +
|
|
|
+ "}\n";
|
|
|
+ return JavaGlue;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getFunctionTemplate(){
|
|
|
+ String FunctionTemplate = "package com.ruoyi.ucp;\n" +
|
|
|
+ "\n" +
|
|
|
+ "import com.ruoyi.ucp.handler.IJobHandler;\n" +
|
|
|
+ "\n" +
|
|
|
+ "public class JavaFunctionJobHandler extends IJobHandler {\n" +
|
|
|
+ " @Override\n" +
|
|
|
+ " public void execute() throws Exception {\n" +
|
|
|
+ "\n" +
|
|
|
+ " }\n" +
|
|
|
+ " //此处加公式\n" +
|
|
|
+ "}\n";
|
|
|
+ return FunctionTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void refreshInstance(int type){
|
|
|
+ if (type == 0) {
|
|
|
+ glueFactory = new GlueFactory();
|
|
|
+ } else if (type == 1) {
|
|
|
+ glueFactory = new SpringGlueFactory();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * groovy class loader
|
|
|
+ */
|
|
|
+ private GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
|
|
|
+ private ConcurrentMap<String, Class<?>> CLASS_CACHE = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+
|
|
|
+ * load new instance, prototype
|
|
|
+ *
|
|
|
+ * @param codeSource
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public IJobHandler loadNewInstance(String codeSource) throws Exception{
|
|
|
+ if (codeSource!=null && codeSource.trim().length()>0) {
|
|
|
+ Class<?> clazz = getCodeSourceClass(codeSource);
|
|
|
+ if (clazz != null) {
|
|
|
+ Object instance = clazz.newInstance();
|
|
|
+ if (instance!=null) {
|
|
|
+ if (instance instanceof IJobHandler) {
|
|
|
+ this.injectService(instance);
|
|
|
+ return (IJobHandler) instance;
|
|
|
+ } else {
|
|
|
+ throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, "
|
|
|
+ + "cannot convert from instance["+ instance.getClass() +"] to IJobHandler");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, instance is null");
|
|
|
+ }
|
|
|
+ private Class<?> getCodeSourceClass(String codeSource){
|
|
|
+ try {
|
|
|
+
|
|
|
+ byte[] md5 = MessageDigest.getInstance("MD5").digest(codeSource.getBytes());
|
|
|
+ String md5Str = new BigInteger(1, md5).toString(16);
|
|
|
+
|
|
|
+ Class<?> clazz = CLASS_CACHE.get(md5Str);
|
|
|
+ if(clazz == null){
|
|
|
+ clazz = groovyClassLoader.parseClass(codeSource);
|
|
|
+ CLASS_CACHE.putIfAbsent(md5Str, clazz);
|
|
|
+ }
|
|
|
+ return clazz;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return groovyClassLoader.parseClass(codeSource);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * inject service of bean field
|
|
|
+ *
|
|
|
+ * @param instance
|
|
|
+ */
|
|
|
+ public void injectService(Object instance) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|