From 303715d81f0c1cd8b32cd659b7734c01565369a6 Mon Sep 17 00:00:00 2001
From: 潘志宝 <979469083@qq.com>
Date: 星期一, 30 九月 2024 17:46:55 +0800
Subject: [PATCH] Merge branch 'master' of ssh://172.16.8.100:29418/iailab-plat

---
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java |   42 +++++++++++++++++-------------------------
 1 files changed, 17 insertions(+), 25 deletions(-)

diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java
index 0c77b58..6141714 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java
@@ -5,17 +5,17 @@
 import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants;
 import com.iailab.framework.common.pojo.CommonResult;
 import com.iailab.module.model.mpk.common.MdkConstant;
+import com.iailab.module.model.mpk.common.utils.DllUtils;
 import com.iailab.module.model.mpk.common.utils.Readtxt;
 import com.iailab.module.model.mpk.dto.MdkDTO;
 import io.swagger.v3.oas.annotations.Operation;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.util.CollectionUtils;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
-import java.lang.reflect.Method;
-import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -30,6 +30,7 @@
  * @createTime 2024年08月08日
  */
 @RestController
+@Slf4j
 @RequestMapping("/model/mpk/api")
 public class MdkController {
     @Value("${mpk.bak-file-path}")
@@ -38,7 +39,8 @@
 
     @PostMapping("run")
     public CommonResult<String> run(@RequestBody MdkDTO dto) {
-
+        Class<?> clazz;
+        URLClassLoader classLoader;
         try {
             File jarFile = new File(mpkBakFilePath + File.separator + MdkConstant.JAR + File.separator + dto.getPyName() + ".jar");
             if (!jarFile.exists()) {
@@ -46,13 +48,16 @@
             }
             File dllFile = new File(mpkBakFilePath + File.separator + MdkConstant.DLL + File.separator + dto.getPyName() + ".dll");
             if (!dllFile.exists()) {
-                throw new RuntimeException("dllw文件不存在,请先生成代码。dllPath:" + dllFile.getAbsolutePath());
+                throw new RuntimeException("dll文件不存在,请先生成代码。dllPath:" + dllFile.getAbsolutePath());
             }
             // 加载jar包
-            loadJar(jarFile.getAbsolutePath());
-            // 加载dll
-            System.load(dllFile.getAbsolutePath());
+            classLoader = DllUtils.loadJar(jarFile.getAbsolutePath());
+            // 实现类
+            clazz = classLoader.loadClass(dto.getClassName());
+            // 加载dll到实现类
+            DllUtils.loadDll(clazz,dllFile.getAbsolutePath());
         } catch (Exception e) {
+            e.printStackTrace();
             throw new RuntimeException("加载运行环境失败。");
         }
 
@@ -92,14 +97,16 @@
                 paramsArray[datas.size()] = HashMap.class;
             }
 
-            Class<?> clazz = Class.forName(dto.getClassName());
-            Method method = clazz.getMethod(dto.getMethodName(), paramsArray);
-            HashMap result = (HashMap)method.invoke(clazz.newInstance(), paramsValueArray);
+            HashMap result = (HashMap) clazz.getDeclaredMethod(dto.getMethodName(), paramsArray).invoke(clazz.newInstance(), paramsValueArray);
             return success(JSON.toJSONString(result));
         } catch (Exception ex) {
             ex.printStackTrace();
             return error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"运行异常");
         } finally {
+            if (classLoader != null) {
+                DllUtils.uploadDll(classLoader);
+                DllUtils.uploadJar(classLoader);
+            }
             System.gc();
         }
     }
@@ -117,20 +124,5 @@
             }
         }
         return success(result);
-    }
-
-    private void loadJar(String jarPath) throws Exception {
-        File jarFile = new File(jarPath);
-        if (!jarFile.exists()) {
-            throw new Exception("jar沒有找到!");
-        } else {
-            Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
-            boolean accessible = method.isAccessible();
-            method.setAccessible(true);
-            URLClassLoader classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
-            URL url = jarFile.toURI().toURL();
-            method.invoke(classLoader, url);
-            method.setAccessible(accessible);
-        }
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3