ConfigRefreshComponent.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 notTrustHost;
  44. String pdfPresentationModeDisable;
  45. String pdfOpenFileDisable;
  46. String pdfPrintDisable;
  47. String pdfDownloadDisable;
  48. String pdfBookmarkDisable;
  49. String pdfDisableEditing;
  50. boolean fileUploadDisable;
  51. String tifPreviewType;
  52. String prohibit;
  53. String[] prohibitArray;
  54. String beian;
  55. String size;
  56. String password;
  57. int pdf2JpgDpi;
  58. String officeTypeWeb;
  59. String cadPreviewType;
  60. boolean deleteSourceFile;
  61. boolean deleteCaptcha;
  62. String officPageRange;
  63. String officWatermark;
  64. String officQuality;
  65. String officMaxImageResolution;
  66. boolean officExportBookmarks;
  67. boolean officeExportNotes;
  68. boolean officeDocumentOpenPasswords;
  69. String cadTimeout;
  70. int cadThread;
  71. String homePageNumber;
  72. String homePagination;
  73. String homePageSize;
  74. String homeSearch;
  75. while (true) {
  76. FileReader fileReader = new FileReader(configFilePath);
  77. BufferedReader bufferedReader = new BufferedReader(fileReader);
  78. properties.load(bufferedReader);
  79. ConfigUtils.restorePropertiesFromEnvFormat(properties);
  80. cacheEnabled = Boolean.parseBoolean(properties.getProperty("cache.enabled", ConfigConstants.DEFAULT_CACHE_ENABLED));
  81. text = properties.getProperty("simText", ConfigConstants.DEFAULT_TXT_TYPE);
  82. media = properties.getProperty("media", ConfigConstants.DEFAULT_MEDIA_TYPE);
  83. officePreviewType = properties.getProperty("office.preview.type", ConfigConstants.DEFAULT_OFFICE_PREVIEW_TYPE);
  84. officePreviewSwitchDisabled = properties.getProperty("office.preview.switch.disabled", ConfigConstants.DEFAULT_OFFICE_PREVIEW_SWITCH_DISABLED);
  85. ftpUsername = properties.getProperty("ftp.username", ConfigConstants.DEFAULT_FTP_USERNAME);
  86. ftpPassword = properties.getProperty("ftp.password", ConfigConstants.DEFAULT_FTP_PASSWORD);
  87. ftpControlEncoding = properties.getProperty("ftp.control.encoding", ConfigConstants.DEFAULT_FTP_CONTROL_ENCODING);
  88. textArray = text.split(",");
  89. mediaArray = media.split(",");
  90. baseUrl = properties.getProperty("base.url", ConfigConstants.DEFAULT_VALUE);
  91. trustHost = properties.getProperty("trust.host", ConfigConstants.DEFAULT_VALUE);
  92. notTrustHost = properties.getProperty("not.trust.host", ConfigConstants.DEFAULT_VALUE);
  93. pdfPresentationModeDisable = properties.getProperty("pdf.presentationMode.disable", ConfigConstants.DEFAULT_PDF_PRESENTATION_MODE_DISABLE);
  94. pdfOpenFileDisable = properties.getProperty("pdf.openFile.disable", ConfigConstants.DEFAULT_PDF_OPEN_FILE_DISABLE);
  95. pdfPrintDisable = properties.getProperty("pdf.print.disable", ConfigConstants.DEFAULT_PDF_PRINT_DISABLE);
  96. pdfDownloadDisable = properties.getProperty("pdf.download.disable", ConfigConstants.DEFAULT_PDF_DOWNLOAD_DISABLE);
  97. pdfBookmarkDisable = properties.getProperty("pdf.bookmark.disable", ConfigConstants.DEFAULT_PDF_BOOKMARK_DISABLE);
  98. pdfDisableEditing = properties.getProperty("pdf.disable.editing", ConfigConstants.DEFAULT_PDF_DISABLE_EDITING);
  99. fileUploadDisable = Boolean.parseBoolean(properties.getProperty("file.upload.disable", ConfigConstants.DEFAULT_FILE_UPLOAD_DISABLE));
  100. tifPreviewType = properties.getProperty("tif.preview.type", ConfigConstants.DEFAULT_TIF_PREVIEW_TYPE);
  101. cadPreviewType = properties.getProperty("cad.preview.type", ConfigConstants.DEFAULT_CAD_PREVIEW_TYPE);
  102. size = properties.getProperty("spring.servlet.multipart.max-file-size", ConfigConstants.DEFAULT_SIZE);
  103. beian = properties.getProperty("beian", ConfigConstants.DEFAULT_BEIAN);
  104. prohibit = properties.getProperty("prohibit", ConfigConstants.DEFAULT_PROHIBIT);
  105. password = properties.getProperty("delete.password", ConfigConstants.DEFAULT_PASSWORD);
  106. pdf2JpgDpi = Integer.parseInt(properties.getProperty("pdf2jpg.dpi", ConfigConstants.DEFAULT_PDF2_JPG_DPI));
  107. officeTypeWeb = properties.getProperty("office.type.web", ConfigConstants.DEFAULT_OFFICE_TYPE_WEB);
  108. deleteSourceFile = Boolean.parseBoolean(properties.getProperty("delete.source.file", ConfigConstants.DEFAULT_DELETE_SOURCE_FILE));
  109. deleteCaptcha = Boolean.parseBoolean(properties.getProperty("delete.captcha", ConfigConstants.DEFAULT_DELETE_CAPTCHA));
  110. officPageRange = properties.getProperty("office.pagerange", ConfigConstants.DEFAULT_OFFICE_PAQERANQE);
  111. officWatermark = properties.getProperty("office.watermark", ConfigConstants.DEFAULT_OFFICE_WATERMARK);
  112. officQuality = properties.getProperty("office.quality", ConfigConstants.DEFAULT_OFFICE_QUALITY);
  113. officMaxImageResolution = properties.getProperty("office.maximageresolution", ConfigConstants.DEFAULT_OFFICE_MAXIMAQERESOLUTION);
  114. officExportBookmarks = Boolean.parseBoolean(properties.getProperty("office.exportbookmarks", ConfigConstants.DEFAULT_OFFICE_EXPORTBOOKMARKS));
  115. officeExportNotes = Boolean.parseBoolean(properties.getProperty("office.exportnotes", ConfigConstants.DEFAULT_OFFICE_EXPORTNOTES));
  116. officeDocumentOpenPasswords = Boolean.parseBoolean(properties.getProperty("office.documentopenpasswords", ConfigConstants.DEFAULT_OFFICE_EOCUMENTOPENPASSWORDS));
  117. cadTimeout = properties.getProperty("cad.timeout", ConfigConstants.DEFAULT_CAD_TIMEOUT);
  118. homePageNumber = properties.getProperty("home.pagenumber", ConfigConstants.DEFAULT_HOME_PAGENUMBER);
  119. homePagination = properties.getProperty("home.pagination", ConfigConstants.DEFAULT_HOME_PAGINATION);
  120. homePageSize = properties.getProperty("home.pagesize", ConfigConstants.DEFAULT_HOME_PAGSIZE);
  121. homeSearch = properties.getProperty("home.search", ConfigConstants.DEFAULT_HOME_SEARCH);
  122. cadThread = Integer.parseInt(properties.getProperty("cad.thread", ConfigConstants.DEFAULT_CAD_THREAD));
  123. prohibitArray = prohibit.split(",");
  124. ConfigConstants.setCacheEnabledValueValue(cacheEnabled);
  125. ConfigConstants.setSimTextValue(textArray);
  126. ConfigConstants.setMediaValue(mediaArray);
  127. ConfigConstants.setOfficePreviewTypeValue(officePreviewType);
  128. ConfigConstants.setFtpUsernameValue(ftpUsername);
  129. ConfigConstants.setFtpPasswordValue(ftpPassword);
  130. ConfigConstants.setFtpControlEncodingValue(ftpControlEncoding);
  131. ConfigConstants.setBaseUrlValue(baseUrl);
  132. ConfigConstants.setTrustHostValue(trustHost);
  133. ConfigConstants.setNotTrustHostValue(notTrustHost);
  134. ConfigConstants.setOfficePreviewSwitchDisabledValue(officePreviewSwitchDisabled);
  135. ConfigConstants.setPdfPresentationModeDisableValue(pdfPresentationModeDisable);
  136. ConfigConstants.setPdfOpenFileDisableValue(pdfOpenFileDisable);
  137. ConfigConstants.setPdfPrintDisableValue(pdfPrintDisable);
  138. ConfigConstants.setPdfDownloadDisableValue(pdfDownloadDisable);
  139. ConfigConstants.setPdfBookmarkDisableValue(pdfBookmarkDisable);
  140. ConfigConstants.setPdfDisableEditingValue(pdfDisableEditing);
  141. ConfigConstants.setFileUploadDisableValue(fileUploadDisable);
  142. ConfigConstants.setTifPreviewTypeValue(tifPreviewType);
  143. ConfigConstants.setCadPreviewTypeValue(cadPreviewType);
  144. ConfigConstants.setBeianValue(beian);
  145. ConfigConstants.setSizeValue(size);
  146. ConfigConstants.setProhibitValue(prohibitArray);
  147. ConfigConstants.setPasswordValue(password);
  148. ConfigConstants.setPdf2JpgDpiValue(pdf2JpgDpi);
  149. ConfigConstants.setOfficeTypeWebValue(officeTypeWeb);
  150. ConfigConstants.setOfficePageRangeValue(officPageRange);
  151. ConfigConstants.setOfficeWatermarkValue(officWatermark);
  152. ConfigConstants.setOfficeQualityValue(officQuality);
  153. ConfigConstants.setOfficeMaxImageResolutionValue(officMaxImageResolution);
  154. ConfigConstants.setOfficeExportBookmarksValue(officExportBookmarks);
  155. ConfigConstants.setOfficeExportNotesValue(officeExportNotes);
  156. ConfigConstants.setOfficeDocumentOpenPasswordsValue(officeDocumentOpenPasswords);
  157. ConfigConstants.setDeleteSourceFileValue(deleteSourceFile);
  158. ConfigConstants.setDeleteCaptchaValue(deleteCaptcha);
  159. ConfigConstants.setCadTimeoutValue(cadTimeout);
  160. ConfigConstants.setCadThreadValue(cadThread);
  161. ConfigConstants.setHomePageNumberValue(homePageNumber);
  162. ConfigConstants.setHomePaginationValue(homePagination);
  163. ConfigConstants.setHomePageSizeValue(homePageSize);
  164. ConfigConstants.setHomeSearchValue(homeSearch);
  165. setWatermarkConfig(properties);
  166. bufferedReader.close();
  167. fileReader.close();
  168. TimeUnit.SECONDS.sleep(1);
  169. }
  170. } catch (IOException | InterruptedException e) {
  171. LOGGER.error("读取配置文件异常", e);
  172. }
  173. }
  174. private void setWatermarkConfig(Properties properties) {
  175. String watermarkTxt = properties.getProperty("watermark.txt", WatermarkConfigConstants.DEFAULT_WATERMARK_TXT);
  176. String watermarkXSpace = properties.getProperty("watermark.x.space", WatermarkConfigConstants.DEFAULT_WATERMARK_X_SPACE);
  177. String watermarkYSpace = properties.getProperty("watermark.y.space", WatermarkConfigConstants.DEFAULT_WATERMARK_Y_SPACE);
  178. String watermarkFont = properties.getProperty("watermark.font", WatermarkConfigConstants.DEFAULT_WATERMARK_FONT);
  179. String watermarkFontsize = properties.getProperty("watermark.fontsize", WatermarkConfigConstants.DEFAULT_WATERMARK_FONTSIZE);
  180. String watermarkColor = properties.getProperty("watermark.color", WatermarkConfigConstants.DEFAULT_WATERMARK_COLOR);
  181. String watermarkAlpha = properties.getProperty("watermark.alpha", WatermarkConfigConstants.DEFAULT_WATERMARK_ALPHA);
  182. String watermarkWidth = properties.getProperty("watermark.width", WatermarkConfigConstants.DEFAULT_WATERMARK_WIDTH);
  183. String watermarkHeight = properties.getProperty("watermark.height", WatermarkConfigConstants.DEFAULT_WATERMARK_HEIGHT);
  184. String watermarkAngle = properties.getProperty("watermark.angle", WatermarkConfigConstants.DEFAULT_WATERMARK_ANGLE);
  185. WatermarkConfigConstants.setWatermarkTxtValue(watermarkTxt);
  186. WatermarkConfigConstants.setWatermarkXSpaceValue(watermarkXSpace);
  187. WatermarkConfigConstants.setWatermarkYSpaceValue(watermarkYSpace);
  188. WatermarkConfigConstants.setWatermarkFontValue(watermarkFont);
  189. WatermarkConfigConstants.setWatermarkFontsizeValue(watermarkFontsize);
  190. WatermarkConfigConstants.setWatermarkColorValue(watermarkColor);
  191. WatermarkConfigConstants.setWatermarkAlphaValue(watermarkAlpha);
  192. WatermarkConfigConstants.setWatermarkWidthValue(watermarkWidth);
  193. WatermarkConfigConstants.setWatermarkHeightValue(watermarkHeight);
  194. WatermarkConfigConstants.setWatermarkAngleValue(watermarkAngle);
  195. }
  196. }
  197. }