|
@@ -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);
|