健壮性优化

This commit is contained in:
张家训
2026-06-24 19:14:57 +08:00
parent 6f78990815
commit 9215896cea
3 changed files with 50 additions and 21 deletions
+14
View File
@@ -280,4 +280,18 @@
</plugins> </plugins>
</build> </build>
<profiles>
<profile>
<id>jdk17-home-env</id>
<activation>
<property>
<name>env.JDK17_HOME</name>
</property>
</activation>
<properties>
<jdk17.home>${env.JDK17_HOME}</jdk17.home>
</properties>
</profile>
</profiles>
</project> </project>
@@ -5,17 +5,15 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.io.*; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.ArrayList;
import java.util.List;
/** /**
* 多数据源配置管理器 * Loads datasource definitions from data/datasources.json.
* <p>
* 从 data/datasources.json 加载数据源列表,供 MyBatis 逆向工程插件选择使用。
* 配置文件的路径和首次复制由 WorkUtilsProperties 统一管理。
*/ */
@Slf4j @Slf4j
@Component @Component
@@ -30,31 +28,36 @@ public class DatasourceConfigManager {
@PostConstruct @PostConstruct
void init() { void init() {
// data/ 目录下的文件已由 WorkUtilsProperties 统一确保存在
reload(); reload();
} }
/** 重新加载数据源配置 */
public synchronized void reload() { public synchronized void reload() {
datasources.clear(); datasources.clear();
Path configFile = getConfigFile(); Path configFile = getConfigFile();
if (!Files.exists(configFile)) { if (!Files.exists(configFile)) {
log.warn("数据源配置文件不存在: {}", configFile.toAbsolutePath()); log.warn("Datasource config file does not exist: {}", configFile.toAbsolutePath());
return; return;
} }
try { try {
String json = new String(Files.readAllBytes(configFile), StandardCharsets.UTF_8); String json = new String(Files.readAllBytes(configFile), StandardCharsets.UTF_8);
List<DatasourceConfig> loaded = JSON.parseArray(json, DatasourceConfig.class); List<DatasourceConfig> loaded = JSON.parseArray(json, DatasourceConfig.class);
if (loaded != null) { if (loaded != null) {
datasources.addAll(loaded); for (DatasourceConfig datasource : loaded) {
if (datasource != null && datasource.getName() != null
&& !datasource.getName().trim().isEmpty()) {
datasources.add(datasource);
} }
log.info("已加载 {} 套数据源配置", datasources.size()); }
}
log.info("Loaded {} datasource configs", datasources.size());
} catch (IOException e) { } catch (IOException e) {
log.error("解析数据源配置失败: {}", e.getMessage()); log.error("Failed to read datasource config: {}", e.getMessage());
} catch (RuntimeException e) {
log.error("Failed to parse datasource config: {}", e.getMessage());
} }
} }
/** 获取所有数据源名称列表 */
public List<String> getDatasourceNames() { public List<String> getDatasourceNames() {
List<String> names = new ArrayList<>(); List<String> names = new ArrayList<>();
for (DatasourceConfig ds : datasources) { for (DatasourceConfig ds : datasources) {
@@ -63,14 +66,18 @@ public class DatasourceConfigManager {
return names; return names;
} }
/** 根据名称查找数据源配置 */
public DatasourceConfig findByName(String name) { public DatasourceConfig findByName(String name) {
return datasources.stream() if (name == null) {
.filter(ds -> ds.getName().equals(name)) return null;
.findFirst().orElse(null); }
for (DatasourceConfig ds : datasources) {
if (name.equals(ds.getName())) {
return ds;
}
}
return null;
} }
/** 获取配置文件的路径(供设置窗口打开编辑) */
public Path getConfigFile() { public Path getConfigFile() {
return properties.getDatasourcesFilePath(); return properties.getDatasourcesFilePath();
} }
@@ -9,10 +9,14 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@@ -41,7 +45,8 @@ public class OutputHelper {
} }
fileOperationHelper.ensureDirectoryExists(filePath.getParent()); fileOperationHelper.ensureDirectoryExists(filePath.getParent());
try (CSVWriter writer = new CSVWriter(new FileWriter(filePath.toFile()))) { try (Writer fileWriter = Files.newBufferedWriter(filePath, StandardCharsets.UTF_8);
CSVWriter writer = new CSVWriter(fileWriter)) {
if (headers.length > 0) { if (headers.length > 0) {
writer.writeNext(headers); writer.writeNext(headers);
} }
@@ -67,7 +72,10 @@ public class OutputHelper {
boolean fileExists = fileOperationHelper.fileExists(filePath); boolean fileExists = fileOperationHelper.fileExists(filePath);
boolean shouldWriteHeaders = !fileExists && headers.length > 0; boolean shouldWriteHeaders = !fileExists && headers.length > 0;
try (CSVWriter writer = new CSVWriter(new FileWriter(filePath.toFile(), true))) { try (Writer fileWriter = new OutputStreamWriter(
Files.newOutputStream(filePath, StandardOpenOption.CREATE, StandardOpenOption.APPEND),
StandardCharsets.UTF_8);
CSVWriter writer = new CSVWriter(fileWriter)) {
if (shouldWriteHeaders) { if (shouldWriteHeaders) {
writer.writeNext(headers); writer.writeNext(headers);
} }