SystemProperties.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package cn.com.qmth.examcloud.web.config;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import org.apache.commons.io.FileUtils;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.springframework.boot.context.properties.ConfigurationProperties;
  7. import org.springframework.stereotype.Component;
  8. import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
  9. import cn.com.qmth.examcloud.commons.util.PathUtil;
  10. import cn.com.qmth.examcloud.web.support.ClasspathHelper;
  11. /**
  12. * 系统配置
  13. *
  14. * @author WANGWEI
  15. * @date 2018年9月7日
  16. * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  17. */
  18. @Component
  19. @ConfigurationProperties("examcloud.web.sys")
  20. public class SystemProperties {
  21. private String dataDir;
  22. private String tempDataDir;
  23. private String logDir;
  24. /**
  25. * 构造函数
  26. *
  27. */
  28. public SystemProperties() {
  29. if (StringUtils.isBlank(dataDir)) {
  30. String classpath = ClasspathHelper.getClasspath();
  31. String path = new File(classpath).getParent() + File.separator + "data";
  32. dataDir = PathUtil.getCanonicalPath(new File(path));
  33. }
  34. if (StringUtils.isBlank(tempDataDir)) {
  35. String classpath = ClasspathHelper.getClasspath();
  36. String path = new File(classpath).getParent() + File.separator + "temp";
  37. tempDataDir = PathUtil.getCanonicalPath(new File(path));
  38. }
  39. if (StringUtils.isBlank(logDir)) {
  40. String classpath = ClasspathHelper.getClasspath();
  41. String path = new File(classpath).getParent() + File.separator + "logs";
  42. logDir = PathUtil.getCanonicalPath(new File(path));
  43. }
  44. try {
  45. FileUtils.forceMkdir(new File(dataDir));
  46. } catch (IOException e) {
  47. throw new ExamCloudRuntimeException("fail to make data dir. path=" + dataDir);
  48. }
  49. try {
  50. FileUtils.forceMkdir(new File(tempDataDir));
  51. } catch (IOException e) {
  52. throw new ExamCloudRuntimeException("fail to make temp data dir. path=" + tempDataDir);
  53. }
  54. System.setProperty("dataDir", dataDir);
  55. System.setProperty("empDataDir", tempDataDir);
  56. System.setProperty("logDir", logDir);
  57. }
  58. public String getDataDir() {
  59. return dataDir;
  60. }
  61. public void setDataDir(String dataDir) {
  62. this.dataDir = dataDir;
  63. }
  64. public String getTempDataDir() {
  65. return tempDataDir;
  66. }
  67. public void setTempDataDir(String tempDataDir) {
  68. this.tempDataDir = tempDataDir;
  69. }
  70. public String getLogDir() {
  71. return logDir;
  72. }
  73. public void setLogDir(String logDir) {
  74. this.logDir = logDir;
  75. }
  76. }