SystemConfigServiceImpl.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package org.dbsyncer.biz.impl;
  2. import org.apache.commons.io.FileUtils;
  3. import org.dbsyncer.biz.SystemConfigService;
  4. import org.dbsyncer.biz.UserConfigService;
  5. import org.dbsyncer.biz.checker.Checker;
  6. import org.dbsyncer.biz.vo.SystemConfigVo;
  7. import org.dbsyncer.common.util.CollectionUtils;
  8. import org.dbsyncer.parser.ProfileComponent;
  9. import org.dbsyncer.parser.logger.LogService;
  10. import org.dbsyncer.parser.logger.LogType;
  11. import org.dbsyncer.parser.model.ConfigModel;
  12. import org.dbsyncer.parser.model.SystemConfig;
  13. import org.dbsyncer.manager.template.PreloadTemplate;
  14. import org.dbsyncer.plugin.enums.FileSuffixEnum;
  15. import org.springframework.beans.BeanUtils;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.util.Assert;
  18. import javax.annotation.Resource;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.nio.charset.Charset;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. /**
  27. * @author AE86
  28. * @version 1.0.0
  29. * @date 2019/10/17 23:20
  30. */
  31. @Service
  32. public class SystemConfigServiceImpl implements SystemConfigService {
  33. @Resource
  34. private ProfileComponent profileComponent;
  35. @Resource
  36. private PreloadTemplate preloadTemplate;
  37. @Resource
  38. private Checker systemConfigChecker;
  39. @Resource
  40. private LogService logService;
  41. @Resource
  42. private UserConfigService userConfigService;
  43. @Override
  44. public String edit(Map<String, String> params) {
  45. ConfigModel model = systemConfigChecker.checkEditConfigModel(params);
  46. profileComponent.editConfigModel(model);
  47. return "修改成功.";
  48. }
  49. @Override
  50. public SystemConfigVo getSystemConfigVo() {
  51. return convertConfig2Vo(getSystemConfig());
  52. }
  53. @Override
  54. public List<ConfigModel> getConfigModelAll() {
  55. List<ConfigModel> list = new ArrayList<>();
  56. list.add(getSystemConfig());
  57. list.add(userConfigService.getUserConfig());
  58. profileComponent.getConnectorAll().forEach(config -> list.add(config));
  59. profileComponent.getMappingAll().forEach(config -> list.add(config));
  60. profileComponent.getMetaAll().forEach(config -> list.add(config));
  61. return list;
  62. }
  63. @Override
  64. public void checkFileSuffix(String filename) {
  65. Assert.hasText(filename, "the config filename is null.");
  66. String suffix = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
  67. FileSuffixEnum fileSuffix = FileSuffixEnum.getFileSuffix(suffix);
  68. Assert.notNull(fileSuffix, "Illegal file suffix");
  69. Assert.isTrue(FileSuffixEnum.JSON == fileSuffix, String.format("不正确的文件扩展名 \"%s\",只支持 \"%s\" 的文件扩展名。", filename, FileSuffixEnum.JSON.getName()));
  70. }
  71. @Override
  72. public void refreshConfig(File file) {
  73. Assert.notNull(file, "the config file is null.");
  74. try {
  75. List<String> lines = FileUtils.readLines(file, Charset.defaultCharset());
  76. if (!CollectionUtils.isEmpty(lines)) {
  77. StringBuilder json = new StringBuilder();
  78. lines.forEach(line -> json.append(line));
  79. preloadTemplate.reload(json.toString());
  80. }
  81. } catch (IOException e) {
  82. logService.log(LogType.CacheLog.IMPORT_ERROR);
  83. } finally {
  84. FileUtils.deleteQuietly(file);
  85. }
  86. }
  87. @Override
  88. public boolean isEnableCDN() {
  89. return getSystemConfig().isEnableCDN();
  90. }
  91. private SystemConfig getSystemConfig() {
  92. SystemConfig config = profileComponent.getSystemConfig();
  93. if (null != config) {
  94. return config;
  95. }
  96. synchronized (this) {
  97. config = profileComponent.getSystemConfig();
  98. if (null == config) {
  99. config = (SystemConfig) systemConfigChecker.checkAddConfigModel(new HashMap<>());
  100. }
  101. return config;
  102. }
  103. }
  104. private SystemConfigVo convertConfig2Vo(SystemConfig systemConfig) {
  105. SystemConfigVo systemConfigVo = new SystemConfigVo();
  106. BeanUtils.copyProperties(systemConfig, systemConfigVo);
  107. return systemConfigVo;
  108. }
  109. }