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