Configuration.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * FileName:Configuration.java
  3. * <p>
  4. * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (794890569@qq.com)</a>.
  5. * <p>
  6. * Licensed under the GNU General Public License, Version 3 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. * <p>
  10. * http://www.gnu.org/licenses/gpl-3.0.html
  11. * <p>
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. package cn.com.lzt.common.config;
  20. import cn.com.lzt.common.util.StringUtil;
  21. import java.io.File;
  22. import java.text.MessageFormat;
  23. import java.util.Locale;
  24. import java.util.ResourceBundle;
  25. /**
  26. * @author : hermit
  27. */
  28. public class Configuration {
  29. private static final String _RES_BASE_NAME = "resources";
  30. public static String ROOT;
  31. public static String CURRENT_PATH;
  32. public static String TEMPLATE_PATH;
  33. public static String TEMPLATE_WEB_PATH;
  34. public static String TEMPORARY_PATH;
  35. public static String LOG_PATH;
  36. public static String CLASSPATH;
  37. public static String CONFIG_PATH;
  38. public static String TEMPLATE_URI = "/WEB-INF/template/views";
  39. public static final String LINE_SEPARATOR = System.getProperty("line.separator");
  40. public static final String PATH_SEPARATOR = System.getProperty("path.separator");
  41. private static final String SECRET_CONFIG = "site.secret";
  42. private static final String DEFAULT_SECRET_KEY = "SECRET_CONFIG";
  43. static {
  44. ROOT = new File(Configuration.class.getResource("/").getPath()).getParentFile().getParent().toString();
  45. CURRENT_PATH = new File(Configuration.class.getResource("/").getPath()).toString();
  46. TEMPLATE_PATH = StringUtil.concat(new Object[]{ROOT, File.separator, "WEB-INF", File.separator, "template"});
  47. TEMPLATE_WEB_PATH = StringUtil.concat(new Object[]{TEMPLATE_PATH, File.separator, "views"});
  48. TEMPORARY_PATH = StringUtil.concat(new Object[]{ROOT, File.separator, "WEB-INF", File.separator, "tmp"});
  49. CONFIG_PATH = StringUtil.concat(new Object[]{ROOT, File.separator, "WEB-INF", File.separator, "conf"});
  50. LOG_PATH = StringUtil.concat(new Object[]{ROOT, File.separator, "WEB-INF", File.separator, "logs"});
  51. StringBuilder sb = new StringBuilder();
  52. // List<File> files = FileUtil.getFiles(new File(StringUtil.concat(new Object[] { ROOT, File.separator, "WEB-INF", File.separator, "lib" })));
  53. // sb.append(".");
  54. // for (File file : files) {
  55. // sb.append(System.getProperty("path.separator"));
  56. // sb.append(file.getAbsolutePath());
  57. // }
  58. // sb.append(System.getProperty("path.separator"));
  59. // sb.append(CURRENT_PATH);
  60. // CLASSPATH = sb.toString();
  61. // try {
  62. // Config = new XMLConfiguration();
  63. // Config.setFile(new File(StringUtil.concat(new Object[] { CURRENT_PATH, File.separator, "system.cfg.xml" })));
  64. // Config.setReloadingStrategy(new FileChangedReloadingStrategy());
  65. // Config.load();
  66. // Config.setEncoding("UTF-8");
  67. // }
  68. // catch (ConfigurationException e)
  69. // {
  70. // e.printStackTrace();
  71. // }
  72. }
  73. // public static String get(String key) {
  74. // return Config.getString(key);
  75. // }
  76. // public static String getSecurityKey() {
  77. // return StringUtil.get(get("site.secret"), "SECRET_CONFIG");
  78. // }
  79. public static String getEncoding() {
  80. return "utf-8";
  81. }
  82. public static String getContentType() {
  83. return "text/html;charset=UTF-8";
  84. }
  85. public static String getResource(String key, Object[] params) {
  86. if ((params == null) || (params.length < 1)) {
  87. return ResourceBundle.getBundle("resources", Locale.CHINA).getString(key);
  88. }
  89. return MessageFormat.format(ResourceBundle.getBundle("resources", Locale.CHINA).getString(key), params);
  90. }
  91. public static String getResource(String key) {
  92. String value = key;
  93. try {
  94. ResourceBundle resource = ResourceBundle.getBundle("resources", Locale.CHINA);
  95. String temp = resource.getString(key);
  96. if (temp != null) value = temp;
  97. } catch (Exception localException) {
  98. }
  99. return value;
  100. }
  101. }