潘志宝
2024-12-24 5846c06afa6cffcd39ca72826e7a0b59631ace8f
iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java
@@ -1,5 +1,8 @@
package com.iailab.module.model.mpk.common.utils;
import cn.hutool.core.io.FileUtil;
import com.iail.model.IAILModel;
import com.iail.utils.RSAUtils;
import com.iailab.module.model.mpk.common.MdkConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@@ -181,27 +184,45 @@
                        String jarFilePath = bakPath + File.separator + projectId + MdkConstant.SPLIT + historyId + ".jar";
                        String dllFilePath = bakPath + File.separator + projectId + MdkConstant.SPLIT + historyId + ".dll";
                        URLClassLoader urlClassLoader = null;
                        try {
                            // 加载新的jar
                            urlClassLoader = DllUtils.loadJar(jarFilePath);
                        } catch (Exception e) {
                            throw new RuntimeException("加载jar异常",e);
                        }
                        if (FileUtil.exist(jarFilePath) && FileUtil.exist(dllFilePath)) {
                            URLClassLoader urlClassLoader = null;
                            try {
                                // 加载新的jar
                                urlClassLoader = loadJar(jarFilePath);
                            } catch (Exception e) {
                                throw new RuntimeException("加载jar异常",e);
                            }
                        try {
                            // 加载新的dll
                            DllUtils.loadDll(urlClassLoader.loadClass("iail.mdk.model.common.Environment"),dllFilePath);
                        } catch (Exception e) {
                            DllUtils.unloadJar(urlClassLoader);
                            throw new RuntimeException("加载dll异常",e);
                            try {
                                // 加载新的dll
                                loadDll(urlClassLoader.loadClass("iail.mdk.model.common.Environment"),dllFilePath);
                            } catch (Exception e) {
                                unloadJar(urlClassLoader);
                                throw new RuntimeException("加载dll异常",e);
                            }
                            // 都加载成功后加入缓存
                            addClassLoaderCache(projectId,urlClassLoader);
                        }
                        // 都加载成功后加入缓存
                        DllUtils.addClassLoaderCache(projectId,urlClassLoader);
                    }
                }
            }
        }
    }
    public static HashMap<String, Object> run(IAILModel model, Object[] paramsValueArray, String projectId) throws Exception {
        if (RSAUtils.checkLisenceBean().getCode() != 1) {
            throw new SecurityException("Lisence 不可用!");
        } else if (model == null) {
            throw new RuntimeException("模型文件不能为空!");
        } else {
            URLClassLoader classLoader = DllUtils.getClassLoader(projectId);
            if (null == classLoader) {
                throw new RuntimeException("dll未发布,classLoader为null");
            }
            Class<?> clazz = classLoader.loadClass(model.getClassName());
            Method method = clazz.getMethod(model.getMethodName(), model.getParamsArray());
            return (HashMap)method.invoke(clazz.newInstance(), paramsValueArray);
        }
    }
}