@param namespace module name @return HotModule @throws Exception error
(String namespace, HotTransaction txn)
| 528 | * @throws Exception error |
| 529 | */ |
| 530 | private HotModule _install(String namespace, HotTransaction txn) throws Exception { |
| 531 | var moduleSrc = Path.of(distributeDir, namespace + ".jar").toFile(); |
| 532 | var interfaceSrc = Path.of(distributeDir, namespace + ".interface.jar").toFile(); |
| 533 | if (!moduleSrc.exists() || !interfaceSrc.exists()) |
| 534 | throw new RuntimeException("distributes not ready."); |
| 535 | |
| 536 | // 安装 interface |
| 537 | var interfaceDst = Path.of(workingDir, "interfaces", namespace + ".interface.jar").toFile().getCanonicalFile(); |
| 538 | { |
| 539 | var oldI = jars.remove(interfaceDst); |
| 540 | if (null != oldI) |
| 541 | oldI.close(); |
| 542 | } |
| 543 | var interfaceDstBackup = Path.of(workingDir, "interfaces", namespace + ".interface.jar.backup").toFile(); |
| 544 | if (!interfaceDst.renameTo(interfaceDstBackup)) |
| 545 | throw new RuntimeException("backup interface.jar fail. " + interfaceDst + " -> " + interfaceDstBackup); |
| 546 | // 从备份恢复,并且重新加载旧文件。 |
| 547 | txn.whileRollback(() -> { |
| 548 | if (interfaceDstBackup.renameTo(interfaceDst)) |
| 549 | putJar(interfaceDst); |
| 550 | else |
| 551 | logger.error("restore interface backup fail {} -> {}", interfaceDstBackup, interfaceDst); |
| 552 | }); |
| 553 | // 提交的时候删除备份 |
| 554 | txn.whileCommit(() -> Files.deleteIfExists(interfaceDstBackup.toPath())); |
| 555 | throwIfMatch("install1"); |
| 556 | |
| 557 | if (!interfaceSrc.renameTo(interfaceDst)) |
| 558 | throw new RuntimeException("rename fail. " + interfaceSrc + "->" + interfaceDst); |
| 559 | putJar(interfaceDst); |
| 560 | txn.whileRollback(() -> { |
| 561 | if (!interfaceDst.renameTo(interfaceSrc)) |
| 562 | logger.error("uninstall interface {} -> {} fail", interfaceDst, interfaceSrc); |
| 563 | }); |
| 564 | throwIfMatch("install2"); |
| 565 | |
| 566 | // 安装 module |
| 567 | var moduleDst = Path.of(workingDir, "modules", namespace + ".jar"); |
| 568 | var moduleDstBackup = Path.of(workingDir, "modules", namespace + ".jar.backup").toFile(); |
| 569 | if (!moduleDst.toFile().renameTo(moduleDstBackup)) |
| 570 | throw new RuntimeException("backup module.jar fail. " + moduleDst + " -> " + moduleDstBackup); |
| 571 | |
| 572 | txn.whileRollback(() -> { |
| 573 | if (!moduleDstBackup.renameTo(moduleDst.toFile())) |
| 574 | logger.error("restore module backup fail {} -> {}", moduleDstBackup, moduleDst); |
| 575 | // module.jar 不用预先装载,在旧HotModule重启的时候会用本来的文件名重新打开。 |
| 576 | }); |
| 577 | txn.whileCommit(() -> Files.deleteIfExists(moduleDstBackup.toPath())); |
| 578 | throwIfMatch("install3"); |
| 579 | |
| 580 | var moduleDstFile = moduleDst.toFile(); |
| 581 | if (!moduleSrc.renameTo(moduleDstFile)) |
| 582 | throw new RuntimeException("rename fail. " + moduleSrc + "->" + moduleDst); |
| 583 | |
| 584 | txn.whileRollback(() -> { |
| 585 | if (!moduleDstFile.renameTo(moduleSrc)) |
| 586 | logger.error("uninstall module {} -> {} fail", moduleDstFile, moduleSrc); |
| 587 | }); |
no test coverage detected