From 6434fc445f48cffd49df9738576d5e340163ed34 Mon Sep 17 00:00:00 2001 From: houzhongjian <houzhongyi@126.com> Date: 星期五, 11 十月 2024 09:42:54 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java | 151 +++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 123 insertions(+), 28 deletions(-) diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java index 492056a..c46b3df 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java @@ -8,12 +8,14 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.iailab.framework.common.page.PageData; +import com.iailab.framework.common.pojo.CommonResult; import com.iailab.framework.common.service.impl.BaseServiceImpl; import com.iailab.framework.common.util.date.DateUtils; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.framework.security.core.util.SecurityFrameworkUtils; import com.iailab.module.infra.api.config.ConfigApi; 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.GenUtils; import com.iailab.module.model.mpk.dao.MpkFileDao; import com.iailab.module.model.mpk.dto.GeneratorCodeHistoryDTO; @@ -36,6 +38,8 @@ import org.springframework.web.multipart.MultipartFile; import java.io.*; +import java.net.URLClassLoader; +import java.nio.file.Files; import java.util.*; import java.util.stream.Collectors; @@ -123,6 +127,8 @@ public void save(MpkFileDTO dto) { MpkFileEntity entity = ConvertUtils.sourceToTarget(dto, MpkFileEntity.class); entity.setId(UUID.randomUUID().toString()); + entity.setPkgName(dto.getPkgName().trim()); + entity.setFilePath(dto.getFilePath().trim()); entity.setCreator(SecurityFrameworkUtils.getLoginUserId()); entity.setCreateDate(new Date()); insert(entity); @@ -189,9 +195,13 @@ VelocityContext dataContext = new VelocityContext(map); //临时文件夹 - File dirPath = new File("D:/DLUT/tmp/"); - dirPath.deleteOnExit(); - dirPath.mkdirs(); + File dirPath = null; + try { + dirPath = Files.createTempDirectory("generatorCodeTmp").toFile(); + log.info("生成临时文件夹," + dirPath.getAbsolutePath()); + } catch (IOException e) { + throw new RuntimeException("创建临时文件夹异常",e); + } List<String> javaFilePaths = new ArrayList<>(); List<String> cppFilePaths = new ArrayList<>(); @@ -270,7 +280,11 @@ generatorCodeHistoryService.insert(historyEntity); // 删除临时文件 - FileUtils.deleteDirectory(dirPath); + try { + FileUtils.deleteDirectory(dirPath); + } catch (IOException e) { + throw new RuntimeException("删除临时文件异常",e); + } return bytes; } @@ -285,9 +299,13 @@ // VelocityContext context = new VelocityContext(map); //临时文件夹 - File dirPath = new File("D:/DLUT/tmp/"); - dirPath.deleteOnExit(); - dirPath.mkdirs(); + File dirPath = null; + try { + dirPath = Files.createTempDirectory("packageModelTmp").toFile(); + log.info("生成临时文件夹," + dirPath.getAbsolutePath()); + } catch (IOException e) { + throw new RuntimeException("创建临时文件夹异常",e); + } //设置velocity资源加载器 Properties prop = new Properties(); @@ -353,6 +371,16 @@ //生成dll文件 String dllSavePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "IAIL.MDK.Mid.Jni.dll"; createDllFile(dirPath.getAbsolutePath(),cppFilePaths,dllSavePath); + // 打包历史id + String historyId = UUID.randomUUID().toString(); + //备份dll,发布时使用 + File dllFile = new File(dllSavePath); + if (dllFile.exists()) { + File dllBakFile = new File(mpkBakFilePath + File.separator + projectId + MdkConstant.SPLIT + historyId + ".dll"); + FileUtil.copy(dllFile, dllBakFile, true); + }else { + log.error("dll文件不存在,无法备份。" + dllSavePath); + } //utils + env java文件 File utilsJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "utils" + File.separator + "AlgsUtils.java"); @@ -368,10 +396,18 @@ // 删除java源文件 deleteJavaFile(javaFilePaths); // 打jar包 - pkgJar(dirPath.getAbsolutePath()); + String jarSavePath = pkgJar(dirPath.getAbsolutePath()); + //备份jar包,发布时使用 + File jarFile = new File(jarSavePath); + if (jarFile.exists()) { + File jarBakFile = new File(mpkBakFilePath + File.separator + projectId + MdkConstant.SPLIT + historyId + ".jar"); + FileUtil.copy(jarFile, jarBakFile, true); + }else { + log.error("jar文件不存在,无法备份。" + jarSavePath); + } + // 本次更新日志 ProjectPackageHistoryDTO dto = new ProjectPackageHistoryDTO(); - String historyId = UUID.randomUUID().toString(); dto.setId(historyId); dto.setProjectId(projectId); dto.setFileName(zipFileName); @@ -430,7 +466,7 @@ // 等待命令执行完成 process.waitFor(); } catch (Exception e) { - throw new RuntimeException("执行cmd命令生成dll异常"); + throw new RuntimeException("执行cmd命令生成dll异常",e); } } @@ -455,6 +491,57 @@ return result; } + @Override + public CommonResult<String> publish(Map<String, Object> params) { + String historyId = (String) params.get("historyId"); + String projectId = (String) params.get("projectId"); + + // 判断新的dll和jar文件是否存在 + String jarFileBakPath = mpkBakFilePath + File.separator + projectId + MdkConstant.SPLIT + historyId + ".jar"; + String dllFileBakPath = mpkBakFilePath + File.separator + projectId + MdkConstant.SPLIT + historyId + ".dll"; + if (!FileUtil.exist(jarFileBakPath)) { + throw new RuntimeException("jar文件不存在," + jarFileBakPath); + } + if (!FileUtil.exist(dllFileBakPath)) { + throw new RuntimeException("dll文件不存在" + dllFileBakPath); + } + + try { + // 卸载之前的dll和jar + DllUtils.removeClassLoaderCache(projectId); + // 删除之前的dll和jar备份文件 + DllUtils.removeOldFile(mpkBakFilePath + File.separator + MdkConstant.PROJECT_PUBLISH,projectId); + } catch (Exception e) { + throw new RuntimeException("卸载之前的dll和jar异常",e); + } + + // 备份新的dll和jar + String jarFilePath = mpkBakFilePath + File.separator + MdkConstant.PROJECT_PUBLISH + File.separator + projectId + MdkConstant.SPLIT + historyId + ".jar"; + String dllFilePath = mpkBakFilePath + File.separator + MdkConstant.PROJECT_PUBLISH + File.separator + projectId + MdkConstant.SPLIT + historyId + ".dll"; + FileUtil.copy(jarFileBakPath,jarFilePath,true); + FileUtil.copy(dllFileBakPath,dllFilePath,true); + + URLClassLoader urlClassLoader = null; + try { + // 加载新的jar + urlClassLoader = DllUtils.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); + } + // 都加载成功后加入缓存 + DllUtils.addClassLoaderCache(projectId,urlClassLoader); + + return CommonResult.success(); + } + private void createLog(String projectId, String projectName, String dirPath, ProjectPackageHistoryDTO dto, String version) throws IOException { Map<String, Object> map = new HashMap<>(); map.put("projectId", projectId); @@ -476,16 +563,20 @@ GenUtils.drawTemplate("log.txt.vm", data, logFile); } - private String pkgJar(String dirPath) throws InterruptedException { - String jarSavePath = dirPath + File.separator + MdkConstant.LIBS + File.separator + "IAILMDK.jar"; - StringBuilder sb = new StringBuilder(); - sb.append("jar -cvf"); - sb.append(" ").append(jarSavePath); - sb.append(" -C ").append(dirPath).append(File.separator).append("IAILMDK").append(File.separator).append(" ."); - log.info("执行cmd命令打jar包:" + sb); - Process process = RuntimeUtil.exec(sb.toString()); - process.waitFor(); - return jarSavePath; + private String pkgJar(String dirPath) { + try { + String jarSavePath = dirPath + File.separator + MdkConstant.LIBS + File.separator + "IAILMDK.jar"; + StringBuilder sb = new StringBuilder(); + sb.append("jar -cvf"); + sb.append(" ").append(jarSavePath); + sb.append(" -C ").append(dirPath).append(File.separator).append("IAILMDK").append(File.separator).append(" ."); + log.info("执行cmd命令打jar包:" + sb); + Process process = RuntimeUtil.exec(sb.toString()); + process.waitFor(); + return jarSavePath; + } catch (InterruptedException e) { + throw new RuntimeException("执行cmd命令打jar包异常",e); + } } private void deleteJavaFile(List<String> javaFilePaths) { @@ -494,14 +585,18 @@ } } - private void createClassFile(List<String> javaFilePaths) throws InterruptedException { - StringBuilder sb = new StringBuilder(); - sb.append("javac -encoding utf-8"); - for (String path : javaFilePaths) { - sb.append(" ").append(path); + private void createClassFile(List<String> javaFilePaths){ + try { + StringBuilder sb = new StringBuilder(); + sb.append("javac -encoding utf-8"); + for (String path : javaFilePaths) { + sb.append(" ").append(path); + } + log.info("执行cmd命令生成class:" + sb); + Process process = RuntimeUtil.exec(sb.toString()); + process.waitFor(); + } catch (InterruptedException e) { + throw new RuntimeException("执行cmd命令生成class异常",e); } - log.info("执行cmd命令生成class:" + sb); - Process process = RuntimeUtil.exec(sb.toString()); - process.waitFor(); } } \ No newline at end of file -- Gitblit v1.9.3