ConfigRefreshComponent.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package cn.keking.config;
  2. import cn.keking.utils.ConfigUtils;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.stereotype.Component;
  6. import javax.annotation.PostConstruct;
  7. import java.io.BufferedReader;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.util.Properties;
  11. import java.util.concurrent.TimeUnit;
  12. /**
  13. * @auther: chenjh
  14. * @time: 2019/4/10 16:16
  15. * @description 每隔1s读取并更新一次配置文件
  16. */
  17. @Component
  18. public class ConfigRefreshComponent {
  19. private static final Logger LOGGER = LoggerFactory.getLogger(ConfigRefreshComponent.class);
  20. @PostConstruct
  21. void refresh() {
  22. Thread configRefreshThread = new Thread(new ConfigRefreshThread());
  23. configRefreshThread.start();
  24. }
  25. static class ConfigRefreshThread implements Runnable {
  26. @Override
  27. public void run() {
  28. try {
  29. Properties properties = new Properties();
  30. String text;
  31. String media;
  32. boolean cacheEnabled;
  33. String[] textArray;
  34. String[] mediaArray;
  35. String officePreviewType;
  36. String officePreviewSwitchDisabled;
  37. String ftpUsername;
  38. String ftpPassword;
  39. String ftpControlEncoding;
  40. String configFilePath = ConfigUtils.getCustomizedConfigPath();
  41. String baseUrl;
  42. String trustHost;
  43. String pdfPresentationModeDisable;
  44. String pdfOpenFileDisable;
  45. String pdfPrintDisable;
  46. String pdfDownloadDisable;
  47. String pdfBookmarkDisable;
  48. boolean fileUploadDisable;
  49. String tifPreviewType;
  50. String prohibit;
  51. String[] prohibitArray;
  52. String beian;
  53. String size;
  54. String password;
  55. int pdf2JpgDpi;
  56. String officeTypeWeb;
  57. String cadPreviewType;
  58. boolean deleteSourceFile;
  59. boolean deleteCaptcha;
  60. String officPageRange;
  61. String officWatermark;
  62. String officQuality;
  63. String officMaxImageResolution;
  64. boolean officExportBookmarks;
  65. boolean officeExportNotes;
  66. boolean officeDocumentOpenPasswords;
  67. String cadTimeout;
  68. int cadThread;
  69. while (true) {
  70. FileReader fileReader = new FileReader(configFilePath);
  71. BufferedReader bufferedReader = new BufferedReader(fileReader);
  72. properties.load(bufferedReader);
  73. ConfigUtils.restorePropertiesFromEnvFormat(properties);
  74. cacheEnabled = Boolean.parseBoolean(properties.getProperty("cache.enabled", ConfigConstants.DEFAULT_CACHE_ENABLED));
  75. text = properties.getProperty("simText", ConfigConstants.DEFAULT_TXT_TYPE);
  76. media = properties.getProperty("media", ConfigConstants.DEFAULT_MEDIA_TYPE);
  77. officePreviewType = properties.getProperty("office.preview.type", ConfigConstants.DEFAULT_OFFICE_PREVIEW_TYPE);
  78. officePreviewSwitchDisabled = properties.getProperty("office.preview.switch.disabled", ConfigConstants.DEFAULT_OFFICE_PREVIEW_SWITCH_DISABLED);
  79. ftpUsername = properties.getProperty("ftp.username", ConfigConstants.DEFAULT_FTP_USERNAME);
  80. ftpPassword = properties.getProperty("ftp.password", ConfigConstants.DEFAULT_FTP_PASSWORD);
  81. ftpControlEncoding = properties.getProperty("ftp.control.encoding", ConfigConstants.DEFAULT_FTP_CONTROL_ENCODING);
  82. textArray = text.split(",");
  83. mediaArray = media.split(",");
  84. baseUrl = properties.getProperty("base.url", ConfigConstants.DEFAULT_BASE_URL);
  85. trustHost = properties.getProperty("trust.host", ConfigConstants.DEFAULT_TRUST_HOST);
  86. pdfPresentationModeDisable = properties.getProperty("pdf.presentationMode.disable", ConfigConstants.DEFAULT_PDF_PRESENTATION_MODE_DISABLE);
  87. pdfOpenFileDisable = properties.getProperty("pdf.openFile.disable", ConfigConstants.DEFAULT_PDF_OPEN_FILE_DISABLE);
  88. pdfPrintDisable = properties.getProperty("pdf.print.disable", ConfigConstants.DEFAULT_PDF_PRINT_DISABLE);
  89. pdfDownloadDisable = properties.getProperty("pdf.download.disable", ConfigConstants.DEFAULT_PDF_DOWNLOAD_DISABLE);
  90. pdfBookmarkDisable = properties.getProperty("pdf.bookmark.disable", ConfigConstants.DEFAULT_PDF_BOOKMARK_DISABLE);
  91. fileUploadDisable = Boolean.parseBoolean(properties.getProperty("file.upload.disable", ConfigConstants.DEFAULT_FILE_UPLOAD_DISABLE));
  92. tifPreviewType = properties.getProperty("tif.preview.type", ConfigConstants.DEFAULT_TIF_PREVIEW_TYPE);
  93. cadPreviewType = properties.getProperty("cad.preview.type", ConfigConstants.DEFAULT_CAD_PREVIEW_TYPE);
  94. size = properties.getProperty("spring.servlet.multipart.max-file-size", ConfigConstants.DEFAULT_SIZE);
  95. beian = properties.getProperty("beian", ConfigConstants.DEFAULT_BEIAN);
  96. prohibit = properties.getProperty("prohibit", ConfigConstants.DEFAULT_PROHIBIT);
  97. password = properties.getProperty("delete.password", ConfigConstants.DEFAULT_PASSWORD);
  98. pdf2JpgDpi = Integer.parseInt(properties.getProperty("pdf2jpg.dpi", ConfigConstants.DEFAULT_PDF2_JPG_DPI));
  99. officeTypeWeb = properties.getProperty("office.type.web", ConfigConstants.DEFAULT_OFFICE_TYPE_WEB);
  100. deleteSourceFile = Boolean.parseBoolean(properties.getProperty("delete.source.file", ConfigConstants.DEFAULT_DELETE_SOURCE_FILE));
  101. deleteCaptcha = Boolean.parseBoolean(properties.getProperty("delete.captcha", ConfigConstants.DEFAULT_DELETE_CAPTCHA));
  102. officPageRange = properties.getProperty("office.pagerange", ConfigConstants.DEFAULT_OFFICE_PAQERANQE);
  103. officWatermark = properties.getProperty("office.watermark", ConfigConstants.DEFAULT_OFFICE_WATERMARK);
  104. officQuality = properties.getProperty("office.quality", ConfigConstants.DEFAULT_OFFICE_QUALITY);
  105. officMaxImageResolution = properties.getProperty("office.maximageresolution", ConfigConstants.DEFAULT_OFFICE_MAXIMAQERESOLUTION);
  106. officExportBookmarks = Boolean.parseBoolean(properties.getProperty("office.exportbookmarks", ConfigConstants.DEFAULT_OFFICE_EXPORTBOOKMARKS));
  107. officeExportNotes = Boolean.parseBoolean(properties.getProperty("office.exportnotes", ConfigConstants.DEFAULT_OFFICE_EXPORTNOTES));
  108. officeDocumentOpenPasswords = Boolean.parseBoolean(properties.getProperty("office.documentopenpasswords", ConfigConstants.DEFAULT_OFFICE_EOCUMENTOPENPASSWORDS));
  109. cadTimeout = properties.getProperty("cad.timeout", ConfigConstants.DEFAULT_CAD_TIMEOUT);
  110. cadThread = Integer.parseInt(properties.getProperty("cad.thread", ConfigConstants.DEFAULT_CAD_THREAD));
  111. prohibitArray = prohibit.split(",");
  112. ConfigConstants.setCacheEnabledValueValue(cacheEnabled);
  113. ConfigConstants.setSimTextValue(textArray);
  114. ConfigConstants.setMediaValue(mediaArray);
  115. ConfigConstants.setOfficePreviewTypeValue(officePreviewType);
  116. ConfigConstants.setFtpUsernameValue(ftpUsername);
  117. ConfigConstants.setFtpPasswordValue(ftpPassword);
  118. ConfigConstants.setFtpControlEncodingValue(ftpControlEncoding);
  119. ConfigConstants.setBaseUrlValue(baseUrl);
  120. ConfigConstants.setTrustHostValue(trustHost);
  121. ConfigConstants.setOfficePreviewSwitchDisabledValue(officePreviewSwitchDisabled);
  122. ConfigConstants.setPdfPresentationModeDisableValue(pdfPresentationModeDisable);
  123. ConfigConstants.setPdfOpenFileDisableValue(pdfOpenFileDisable);
  124. ConfigConstants.setPdfPrintDisableValue(pdfPrintDisable);
  125. ConfigConstants.setPdfDownloadDisableValue(pdfDownloadDisable);
  126. ConfigConstants.setPdfBookmarkDisableValue(pdfBookmarkDisable);
  127. ConfigConstants.setFileUploadDisableValue(fileUploadDisable);
  128. ConfigConstants.setTifPreviewTypeValue(tifPreviewType);
  129. ConfigConstants.setCadPreviewTypeValue(cadPreviewType);
  130. ConfigConstants.setBeianValue(beian);
  131. ConfigConstants.setSizeValue(size);
  132. ConfigConstants.setProhibitValue(prohibitArray);
  133. ConfigConstants.setPasswordValue(password);
  134. ConfigConstants.setPdf2JpgDpiValue(pdf2JpgDpi);
  135. ConfigConstants.setOfficeTypeWebValue(officeTypeWeb);
  136. ConfigConstants.setOfficePageRangeValue(officPageRange);
  137. ConfigConstants.setOfficeWatermarkValue(officWatermark);
  138. ConfigConstants.setOfficeQualityValue(officQuality);
  139. ConfigConstants.setOfficeMaxImageResolutionValue(officMaxImageResolution);
  140. ConfigConstants.setOfficeExportBookmarksValue(officExportBookmarks);
  141. ConfigConstants.setOfficeExportNotesValue(officeExportNotes);
  142. ConfigConstants.setOfficeDocumentOpenPasswordsValue(officeDocumentOpenPasswords);
  143. ConfigConstants.setDeleteSourceFileValue(deleteSourceFile);
  144. ConfigConstants.setDeleteCaptchaValue(deleteCaptcha);
  145. ConfigConstants.setCadTimeoutValue(cadTimeout);
  146. ConfigConstants.setCadThreadValue(cadThread);
  147. setWatermarkConfig(properties);
  148. bufferedReader.close();
  149. fileReader.close();
  150. TimeUnit.SECONDS.sleep(1);
  151. }
  152. } catch (IOException | InterruptedException e) {
  153. LOGGER.error("读取配置文件异常", e);
  154. }
  155. }
  156. private void setWatermarkConfig(Properties properties) {
  157. String watermarkTxt = properties.getProperty("watermark.txt", WatermarkConfigConstants.DEFAULT_WATERMARK_TXT);
  158. String watermarkXSpace = properties.getProperty("watermark.x.space", WatermarkConfigConstants.DEFAULT_WATERMARK_X_SPACE);
  159. String watermarkYSpace = properties.getProperty("watermark.y.space", WatermarkConfigConstants.DEFAULT_WATERMARK_Y_SPACE);
  160. String watermarkFont = properties.getProperty("watermark.font", WatermarkConfigConstants.DEFAULT_WATERMARK_FONT);
  161. String watermarkFontsize = properties.getProperty("watermark.fontsize", WatermarkConfigConstants.DEFAULT_WATERMARK_FONTSIZE);
  162. String watermarkColor = properties.getProperty("watermark.color", WatermarkConfigConstants.DEFAULT_WATERMARK_COLOR);
  163. String watermarkAlpha = properties.getProperty("watermark.alpha", WatermarkConfigConstants.DEFAULT_WATERMARK_ALPHA);
  164. String watermarkWidth = properties.getProperty("watermark.width", WatermarkConfigConstants.DEFAULT_WATERMARK_WIDTH);
  165. String watermarkHeight = properties.getProperty("watermark.height", WatermarkConfigConstants.DEFAULT_WATERMARK_HEIGHT);
  166. String watermarkAngle = properties.getProperty("watermark.angle", WatermarkConfigConstants.DEFAULT_WATERMARK_ANGLE);
  167. WatermarkConfigConstants.setWatermarkTxtValue(watermarkTxt);
  168. WatermarkConfigConstants.setWatermarkXSpaceValue(watermarkXSpace);
  169. WatermarkConfigConstants.setWatermarkYSpaceValue(watermarkYSpace);
  170. WatermarkConfigConstants.setWatermarkFontValue(watermarkFont);
  171. WatermarkConfigConstants.setWatermarkFontsizeValue(watermarkFontsize);
  172. WatermarkConfigConstants.setWatermarkColorValue(watermarkColor);
  173. WatermarkConfigConstants.setWatermarkAlphaValue(watermarkAlpha);
  174. WatermarkConfigConstants.setWatermarkWidthValue(watermarkWidth);
  175. WatermarkConfigConstants.setWatermarkHeightValue(watermarkHeight);
  176. WatermarkConfigConstants.setWatermarkAngleValue(watermarkAngle);
  177. }
  178. }
  179. }