This commit is contained in:
六如
2024-11-04 11:12:04 +08:00
parent a598bf3404
commit 7334fad2ab

View File

@@ -8,23 +8,32 @@ import com.gitee.sop.support.service.dto.RegisterDTO;
import com.gitee.sop.support.service.dto.RegisterResult; import com.gitee.sop.support.service.dto.RegisterResult;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.dubbo.config.annotation.DubboService; import org.apache.dubbo.config.annotation.DubboService;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Parameter; import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@@ -35,6 +44,8 @@ import java.util.stream.Stream;
*/ */
public class ApiRegister { public class ApiRegister {
Logger log = Logger.getLogger(ApiRegister.class.getName());
private static final Log LOG = LogFactory.getLog(ApiRegister.class); private static final Log LOG = LogFactory.getLog(ApiRegister.class);
public static final String I18N_ROOT = "i18n"; public static final String I18N_ROOT = "i18n";
@@ -60,42 +71,85 @@ public class ApiRegister {
protected void initMessage() { protected void initMessage() {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
URL i18n = contextClassLoader.getResource(I18N_ROOT); URL i18nFolder = contextClassLoader.getResource(I18N_ROOT);
if (i18n == null) { if (i18nFolder == null) {
return; return;
} }
String file = i18n.getFile();
File i18nFolder = new File(file); try {
Path path = Paths.get(i18nFolder.toURI());
try (Stream<Path> walk = Files.walk(path)) {
Optional<Path> i18nRoot = walk.findFirst();
if (!i18nRoot.isPresent()) {
return;
}
File root = i18nRoot.get().toFile();
List<FileTree> fileTrees = buildFileTree(root);
Set<String> isvModuleList = buildIsvModuleList(fileTrees);
OpenMessageFactory.initMessageSource(new ArrayList<>(isvModuleList));
}
} catch (URISyntaxException | IOException e) {
log.warning("初始化i18n模块错误:" + e.getMessage());
throw new RuntimeException(e);
}
}
private Set<String> buildIsvModuleList(List<FileTree> fileTreeList) {
if (fileTreeList == null) {
return Collections.emptySet();
}
Set<String> isvModuleList = new HashSet<>(); Set<String> isvModuleList = new HashSet<>();
File[] folders = i18nFolder.listFiles();
if (folders == null) { List<FileTree> leafList = fileTreeList.stream().filter(FileTree::isLeaf).collect(Collectors.toList());
for (FileTree fileTree : leafList) {
LinkedList<String> nameList = new LinkedList<>();
appendNames(nameList, fileTree);
String moduleName = String.join("/", nameList);
isvModuleList.add(moduleName);
}
return isvModuleList;
}
private void appendNames(LinkedList<String> nameList, FileTree fileTree) {
nameList.addFirst(fileTree.getName());
FileTree parent = fileTree.getParent();
if (parent != null) {
appendNames(nameList, parent);
}
}
private List<FileTree> buildFileTree(File file) {
List<FileTree> fileTrees = new ArrayList<>();
this.appendFileTree(fileTrees, file, null);
return fileTrees;
}
private void appendFileTree(List<FileTree> fileTrees, File file, FileTree parent) {
FileTree i18nTree = new FileTree();
i18nTree.setParent(parent);
fileTrees.add(i18nTree);
if (file.isDirectory()) {
i18nTree.setName(file.getName());
File[] files = file.listFiles();
if (files == null || files.length == 0) {
return; return;
} }
for (File listFile : folders) { for (File childFile : files) {
if (listFile.isDirectory()) { appendFileTree(fileTrees, childFile, i18nTree);
File[] files = listFile.listFiles();
if (files == null) {
continue;
}
for (File subFile : files) {
// i18n/isp/bizerror_en.properties
String name = subFile.getName();
int i = name.indexOf("_");
String moduleName = name.substring(0, i);
String fullModuleName = String.join("/", I18N_ROOT, listFile.getName(), moduleName);
isvModuleList.add(fullModuleName);
} }
} else { } else {
// i18n/isp/bizerror_en.properties // i18n/isp/bizerror_en.properties
String name = listFile.getName(); String name = file.getName();
int i = name.indexOf("_"); int i = name.indexOf("_");
String moduleName = name.substring(0, i); String module = name.substring(0, i);
String fullModuleName = String.join("/", I18N_ROOT, moduleName);
isvModuleList.add(fullModuleName); i18nTree.setName(module);
i18nTree.setLeaf(true);
} }
} }
OpenMessageFactory.initMessageSource(new ArrayList<>(isvModuleList));
}
protected void doWithMethod(Class<?> objClass, RegisterCallback callback) { protected void doWithMethod(Class<?> objClass, RegisterCallback callback) {
if (objClass.getAnnotation(DubboService.class) == null) { if (objClass.getAnnotation(DubboService.class) == null) {
@@ -216,5 +270,21 @@ public class ApiRegister {
private String actualType; private String actualType;
} }
@Data
private static class FileTree {
private String name;
private FileTree parent;
private boolean isLeaf;
@Override
public String toString() {
return "FileTree{" +
"name='" + name + '\'' +
'}';
}
}
} }