Browse Source

上传配置

AE86 3 years ago
parent
commit
cc4660f14c

+ 14 - 0
dbsyncer-biz/src/main/java/org/dbsyncer/biz/ConfigService.java

@@ -3,6 +3,7 @@ package org.dbsyncer.biz;
 import org.dbsyncer.biz.vo.ConfigVo;
 import org.dbsyncer.parser.model.ConfigModel;
 
+import java.io.File;
 import java.util.List;
 import java.util.Map;
 
@@ -48,4 +49,17 @@ public interface ConfigService {
      */
     List<ConfigModel> getConfigModelAll();
 
+    /**
+     * 校验文件格式
+     *
+     * @param filename
+     */
+    void checkFileSuffix(String filename);
+
+    /**
+     * 更新配置
+     *
+     * @param file
+     */
+    void refreshConfig(File file);
 }

+ 54 - 0
dbsyncer-biz/src/main/java/org/dbsyncer/biz/impl/ConfigServiceImpl.java

@@ -1,17 +1,29 @@
 package org.dbsyncer.biz.impl;
 
+import org.apache.commons.io.FileUtils;
 import org.dbsyncer.biz.ConfigService;
 import org.dbsyncer.biz.checker.impl.config.ConfigChecker;
 import org.dbsyncer.biz.vo.ConfigVo;
 import org.dbsyncer.common.util.CollectionUtils;
+import org.dbsyncer.common.util.JsonUtil;
 import org.dbsyncer.manager.Manager;
+import org.dbsyncer.manager.template.impl.PreloadTemplate;
+import org.dbsyncer.parser.logger.LogService;
+import org.dbsyncer.parser.logger.LogType;
 import org.dbsyncer.parser.model.Config;
 import org.dbsyncer.parser.model.ConfigModel;
+import org.dbsyncer.plugin.enums.FileSuffixEnum;
 import org.dbsyncer.storage.constant.ConfigConstant;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.util.Assert;
 
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -26,12 +38,20 @@ import java.util.stream.Collectors;
 @Service
 public class ConfigServiceImpl implements ConfigService {
 
+    private final Logger logger = LoggerFactory.getLogger(getClass());
+
     @Autowired
     private Manager manager;
 
     @Autowired
     private ConfigChecker configChecker;
 
+    @Autowired
+    private PreloadTemplate preloadTemplate;
+
+    @Autowired
+    private LogService logService;
+
     @Override
     public String edit(Map<String, String> params) {
         synchronized (this) {
@@ -77,6 +97,40 @@ public class ConfigServiceImpl implements ConfigService {
         return list;
     }
 
+    @Override
+    public void checkFileSuffix(String filename) {
+        Assert.hasText(filename, "the config filename is null.");
+        String suffix = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
+        FileSuffixEnum fileSuffix = FileSuffixEnum.getFileSuffix(suffix);
+        Assert.notNull(fileSuffix, "Illegal file suffix");
+        Assert.isTrue(FileSuffixEnum.JSON == fileSuffix, String.format("不正确的文件扩展名 \"%s\",只支持 \"%s\" 的文件扩展名。", filename, FileSuffixEnum.JSON.getName()));
+    }
+
+    @Override
+    public void refreshConfig(File file) {
+        Assert.notNull(file, "the config file is null.");
+
+        try {
+            List<String> lines = FileUtils.readLines(file, Charset.defaultCharset());
+            if (!CollectionUtils.isEmpty(lines)) {
+                StringBuilder json = new StringBuilder();
+                lines.forEach(line -> json.append(line));
+
+                Map<String, Object> map = JsonUtil.jsonToObj(json.toString(), Map.class);
+                if (!CollectionUtils.isEmpty(map)) {
+                    map.forEach((k, v) -> {
+                        // TODO 持久化配置
+                        logger.info("key:{} ,value:{}", k, v);
+                    });
+                }
+            }
+        } catch (IOException e) {
+            logService.log(LogType.CacheLog.IMPORT_ERROR);
+        } finally {
+            FileUtils.deleteQuietly(file);
+        }
+    }
+
     private ConfigVo convertConfig2Vo(Config config) {
         ConfigVo configVo = new ConfigVo();
         BeanUtils.copyProperties(config, configVo);

+ 5 - 6
dbsyncer-biz/src/main/java/org/dbsyncer/biz/impl/MappingServiceImpl.java

@@ -15,14 +15,15 @@ import org.dbsyncer.parser.enums.ModelEnum;
 import org.dbsyncer.parser.logger.LogType;
 import org.dbsyncer.parser.model.*;
 import org.dbsyncer.storage.constant.ConfigConstant;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.Assert;
 
-import java.util.*;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 
 /**
@@ -33,8 +34,6 @@ import java.util.stream.Collectors;
 @Service
 public class MappingServiceImpl extends BaseServiceImpl implements MappingService {
 
-    private final Logger logger = LoggerFactory.getLogger(getClass());
-
     @Autowired
     private Monitor monitor;
 
@@ -56,7 +55,7 @@ public class MappingServiceImpl extends BaseServiceImpl implements MappingServic
 
         // 匹配相似表 on
         String autoMatchTable = params.get("autoMatchTable");
-        if(StringUtil.isNotBlank(autoMatchTable)){
+        if (StringUtil.isNotBlank(autoMatchTable)) {
             matchSimilarTable(model);
         }
 

+ 9 - 8
dbsyncer-biz/src/main/java/org/dbsyncer/biz/impl/PluginServiceImpl.java

@@ -15,6 +15,7 @@ import org.dbsyncer.plugin.enums.FileSuffixEnum;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
+import org.springframework.util.Assert;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -70,14 +71,14 @@ public class PluginServiceImpl implements PluginService {
 
     @Override
     public void checkFileSuffix(String filename) {
-        if (StringUtil.isNotBlank(filename)) {
-            String suffix = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
-            if (null == FileSuffixEnum.getFileSuffix(suffix)) {
-                suffix = StringUtil.join(FileSuffixEnum.values(), ",").toLowerCase();
-                String msg = String.format("不正确的文件扩展名 \"%s\",只支持 \"%s\" 的文件扩展名。", filename, suffix);
-                logService.log(LogType.PluginLog.CHECK_ERROR, msg);
-                throw new BizException(msg);
-            }
+        Assert.hasText(filename, "the plugin filename is null.");
+        String suffix = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
+        FileSuffixEnum fileSuffix = FileSuffixEnum.getFileSuffix(suffix);
+        Assert.notNull(fileSuffix, "Illegal file suffix");
+        if (FileSuffixEnum.JAR != fileSuffix) {
+            String msg = String.format("不正确的文件扩展名 \"%s\",只支持 \"%s\" 的文件扩展名。", filename, FileSuffixEnum.JAR.getName());
+            logService.log(LogType.PluginLog.CHECK_ERROR, msg);
+            throw new BizException(msg);
         }
     }
 

+ 7 - 8
dbsyncer-manager/src/main/java/org/dbsyncer/manager/config/PreloadConfig.java

@@ -2,29 +2,28 @@ package org.dbsyncer.manager.config;
 
 import org.dbsyncer.manager.enums.GroupStrategyEnum;
 import org.dbsyncer.manager.enums.HandlerEnum;
-import org.dbsyncer.manager.template.Handler;
 
 public class PreloadConfig {
 
-    private String filterType;
+    private String configModelType;
 
     private GroupStrategyEnum groupStrategyEnum;
 
     private HandlerEnum handlerEnum;
 
-    public PreloadConfig(String filterType, HandlerEnum handlerEnum) {
-        this.filterType = filterType;
+    public PreloadConfig(String configModelType, HandlerEnum handlerEnum) {
+        this.configModelType = configModelType;
         this.handlerEnum = handlerEnum;
     }
 
-    public PreloadConfig(String filterType, GroupStrategyEnum groupStrategyEnum, HandlerEnum handlerEnum) {
-        this.filterType = filterType;
+    public PreloadConfig(String configModelType, GroupStrategyEnum groupStrategyEnum, HandlerEnum handlerEnum) {
+        this.configModelType = configModelType;
         this.groupStrategyEnum = groupStrategyEnum;
         this.handlerEnum = handlerEnum;
     }
 
-    public String getFilterType() {
-        return filterType;
+    public String getConfigModelType() {
+        return configModelType;
     }
 
     public GroupStrategyEnum getGroupStrategyEnum() {

+ 4 - 4
dbsyncer-manager/src/main/java/org/dbsyncer/manager/template/impl/PreloadTemplate.java

@@ -56,13 +56,13 @@ public final class PreloadTemplate extends AbstractTemplate implements Applicati
     public void execute(PreloadConfig config) {
         Query query = new Query();
         query.setType(StorageEnum.CONFIG);
-        String filterType = config.getFilterType();
-        query.addFilter(ConfigConstant.CONFIG_MODEL_TYPE, filterType);
+        String modelType = config.getConfigModelType();
+        query.addFilter(ConfigConstant.CONFIG_MODEL_TYPE, modelType);
 
         int pageNum = 1;
         int pageSize = 20;
         long total = 0;
-        for(;;){
+        for (; ; ) {
             query.setPageNum(pageNum);
             query.setPageSize(pageSize);
             Paging paging = storageService.query(query);
@@ -83,7 +83,7 @@ public final class PreloadTemplate extends AbstractTemplate implements Applicati
             pageNum ++;
         }
 
-        logger.info("PreLoad {}:{}", filterType, total);
+        logger.info("PreLoad {}:{}", modelType, total);
     }
 
     @Override

+ 2 - 1
dbsyncer-parser/src/main/java/org/dbsyncer/parser/logger/LogType.java

@@ -242,7 +242,8 @@ public interface LogType {
      */
     enum CacheLog implements LogType {
         IMPORT("70", "导入配置"),
-        EXPORT("71", "导出配置");
+        IMPORT_ERROR("71", "导入配置异常"),
+        EXPORT("72", "导出配置");
 
         private String type;
         private String message;

+ 6 - 1
dbsyncer-plugin/src/main/java/org/dbsyncer/plugin/enums/FileSuffixEnum.java

@@ -14,7 +14,12 @@ public enum FileSuffixEnum {
     /**
      * jar
      */
-    JAR("jar");
+    JAR("jar"),
+
+    /**
+     * JSON
+     */
+    JSON("json");
 
     private String name;
 

+ 14 - 9
dbsyncer-web/src/main/java/org/dbsyncer/web/controller/config/ConfigController.java

@@ -1,5 +1,6 @@
 package org.dbsyncer.web.controller.config;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.dbsyncer.biz.ConfigService;
 import org.dbsyncer.biz.vo.RestResult;
@@ -21,6 +22,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
+import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.nio.charset.Charset;
@@ -69,17 +71,20 @@ public class ConfigController {
     public RestResult upload(MultipartFile[] files) {
         try {
             if (files != null && files.length > 0) {
-                MultipartFile file = null;
                 for (int i = 0; i < files.length; i++) {
-                    file = files[i];
-                    if (file != null) {
-                        String filename = file.getOriginalFilename();
-                        // TODO checkFileSuffix(filename);
-                        logger.info(filename);
-                        String msg = String.format("导入配置文件%s。", filename);
-                        logger.info(msg);
-                        logService.log(LogType.CacheLog.IMPORT, msg);
+                    if (files[i] == null) {
+                        continue;
                     }
+                    String filename = files[i].getOriginalFilename();
+                    configService.checkFileSuffix(filename);
+                    String tmpdir = System.getProperty("java.io.tmpdir");
+                    File dest = new File(tmpdir + filename);
+                    FileUtils.deleteQuietly(dest);
+                    FileUtils.copyInputStreamToFile(files[i].getInputStream(), dest);
+                    configService.refreshConfig(dest);
+                    String msg = String.format("导入配置文件%s。", filename);
+                    logger.info(msg);
+                    logService.log(LogType.CacheLog.IMPORT, msg);
                 }
             }
             return RestResult.restSuccess("ok");

+ 2 - 1
dbsyncer-web/src/main/resources/public/config/config.html

@@ -47,7 +47,8 @@
 
                     <div class="form-group">
                         <div class="file-loading">
-                            <input id="fileConfig" type="file" name="files" multiple="multiple"/>
+                            <input id="fileConfig" type="file" name="files" multiple="multiple"
+                                   accept="application/json"/>
                         </div>
                     </div>
                 </form>