Global.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
  3. */
  4. package com.jeeplus.common.config;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.Reader;
  9. import java.util.Map;
  10. import java.util.Properties;
  11. import com.jeeplus.common.utils.FileUtils;
  12. import com.jeeplus.core.web.Servlets;
  13. import com.jeeplus.modules.sys.security.SystemAuthorizingRealm;
  14. import com.jeeplus.modules.sys.utils.UserUtils;
  15. import org.apache.ibatis.io.Resources;
  16. import org.springframework.core.io.DefaultResourceLoader;
  17. import com.google.common.collect.Maps;
  18. import com.jeeplus.common.utils.PropertiesLoader;
  19. import com.jeeplus.common.utils.StringUtils;
  20. /**
  21. * 全局配置类
  22. * @author jeeplus
  23. * @version 2017-06-25
  24. */
  25. public class Global {
  26. /**
  27. * 当前对象实例
  28. */
  29. private static Global global = new Global();
  30. /**
  31. * 保存全局属性值
  32. */
  33. private static Map<String, String> map = Maps.newHashMap();
  34. /**
  35. * 属性文件加载对象
  36. */
  37. private static PropertiesLoader loader = new PropertiesLoader("/properties/jeeplus.properties");
  38. /**
  39. * 显示/隐藏
  40. */
  41. public static final String SHOW = "1";
  42. public static final String HIDE = "0";
  43. /**
  44. * 是/否
  45. */
  46. public static final String YES = "1";
  47. public static final String NO = "0";
  48. /**
  49. * 对/错
  50. */
  51. public static final String TRUE = "true";
  52. public static final String FALSE = "false";
  53. /**
  54. * 上传文件基础虚拟路径
  55. */
  56. public static final String USERFILES_BASE_URL = "/userfiles/";
  57. /**
  58. * 共享文档物理存储地址
  59. * @return
  60. */
  61. public static String getShareBaseDir(){
  62. String dir = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL + "共享文档/";
  63. FileUtils.createDirectory(dir);
  64. return dir;
  65. }
  66. /**
  67. * 共享文档网络访问地址
  68. * @return
  69. */
  70. public static String getShareBaseUrl(){
  71. SystemAuthorizingRealm.Principal principal = (SystemAuthorizingRealm.Principal) UserUtils.getPrincipal();
  72. return Servlets.getRequest().getContextPath() + Global.USERFILES_BASE_URL + "/共享文档/";
  73. }
  74. /**
  75. * 我的文档物理存储地址
  76. * @return
  77. */
  78. public static String getMyDocDir(){
  79. SystemAuthorizingRealm.Principal principal = (SystemAuthorizingRealm.Principal) UserUtils.getPrincipal();
  80. String dir = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL + principal + "/我的文档/";
  81. FileUtils.createDirectory(dir);
  82. return dir;
  83. }
  84. /**
  85. * 我的文档网络访问地址
  86. * @return
  87. */
  88. public static String getMyDocUrl(){
  89. SystemAuthorizingRealm.Principal principal = (SystemAuthorizingRealm.Principal) UserUtils.getPrincipal();
  90. return Servlets.getRequest().getContextPath() + Global.USERFILES_BASE_URL + principal + "/我的文档/";
  91. }
  92. /**
  93. * 程序附件物理存储地址
  94. * @return
  95. */
  96. public static String getAttachmentDir(){
  97. SystemAuthorizingRealm.Principal principal = (SystemAuthorizingRealm.Principal) UserUtils.getPrincipal();
  98. String dir = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL + principal + "/程序附件/";
  99. FileUtils.createDirectory(dir);
  100. return dir;
  101. }
  102. /**
  103. * 程序附件物理存储地址
  104. * @return
  105. */
  106. public static String getAttachmentDirApi(){
  107. String dir = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL + "/程序附件/";
  108. FileUtils.createDirectory(dir);
  109. return dir;
  110. }
  111. /**
  112. * 程序附件网络访问地址
  113. * @return
  114. */
  115. public static String getAttachmentUrl(){
  116. SystemAuthorizingRealm.Principal principal = (SystemAuthorizingRealm.Principal) UserUtils.getPrincipal();
  117. return Servlets.getRequest().getContextPath() + Global.USERFILES_BASE_URL + principal + "/程序附件/";
  118. }
  119. /**
  120. * 程序附件网络访问地址
  121. * @return
  122. */
  123. public static String getAttachmentUrlApi(){
  124. return Servlets.getRequest().getContextPath() + Global.USERFILES_BASE_URL + "/程序附件/";
  125. }
  126. /**
  127. * 绝对地址转换为网络地址
  128. * @return
  129. */
  130. public static String transDirToUrl(String dir){
  131. return Servlets.getRequest().getContextPath()+"/" + dir.substring(Global.getUserfilesBaseDir().length());
  132. }
  133. /**
  134. * 获取当前对象实例
  135. */
  136. public static Global getInstance() {
  137. return global;
  138. }
  139. /**
  140. * 获取配置
  141. */
  142. public static String getConfig(String key) {
  143. String value = map.get(key);
  144. if (value == null){
  145. value = loader.getProperty(key);
  146. map.put(key, value != null ? value : StringUtils.EMPTY);
  147. }
  148. return value;
  149. }
  150. /**
  151. * 获取管理端根路径
  152. */
  153. public static String getAdminPath() {
  154. return getConfig("adminPath");
  155. }
  156. /**
  157. * 获取管理端根路径
  158. */
  159. public static String getDefaultTheme() {
  160. return getConfig("defaultTheme");
  161. }
  162. /**
  163. * 获取前端根路径
  164. */
  165. public static String getFrontPath() {
  166. return getConfig("frontPath");
  167. }
  168. /**
  169. * 获取URL后缀
  170. */
  171. public static String getUrlSuffix() {
  172. return getConfig("urlSuffix");
  173. }
  174. /**
  175. * 是否是演示模式,演示模式下不能修改用户、角色、密码、菜单、授权
  176. */
  177. public static Boolean isDemoMode() {
  178. String dm = getConfig("demoMode");
  179. return "true".equals(dm) || "1".equals(dm);
  180. }
  181. /**
  182. * 在修改系统用户和角色时是否同步到Activiti
  183. */
  184. public static Boolean isSynActivitiIndetity() {
  185. String dm = getConfig("activiti.isSynActivitiIndetity");
  186. return "true".equals(dm) || "1".equals(dm);
  187. }
  188. /**
  189. * 页面获取常量
  190. */
  191. public static Object getConst(String field) {
  192. try {
  193. return Global.class.getField(field).get(null);
  194. } catch (Exception e) {
  195. // 异常代表无配置,这里什么也不做
  196. }
  197. return null;
  198. }
  199. /**
  200. * 获取上传文件的根目录
  201. * @return
  202. */
  203. public static String getUserfilesBaseDir() {
  204. String dir = getConfig("userfiles.basedir");
  205. if (StringUtils.isBlank(dir)){
  206. return "";
  207. }
  208. if(!dir.endsWith("/")) {
  209. dir += "/";
  210. }
  211. // System.out.println("userfiles.basedir: " + dir);
  212. return dir;
  213. }
  214. /**
  215. * 获取工程路径
  216. * @return
  217. */
  218. public static String getProjectPath(){
  219. // 如果配置了工程路径,则直接返回,否则自动获取。
  220. String projectPath = Global.getConfig("projectPath");
  221. if (StringUtils.isNotBlank(projectPath)){
  222. return projectPath;
  223. }
  224. try {
  225. File file = new DefaultResourceLoader().getResource("").getFile();
  226. if (file != null){
  227. while(true){
  228. File f = new File(file.getPath() + File.separator + "src" + File.separator + "main");
  229. if (f == null || f.exists()){
  230. break;
  231. }
  232. if (file.getParentFile() != null){
  233. file = file.getParentFile();
  234. }else{
  235. break;
  236. }
  237. }
  238. projectPath = file.toString();
  239. }
  240. } catch (IOException e) {
  241. e.printStackTrace();
  242. }
  243. return projectPath;
  244. }
  245. /**
  246. * 写入properties信息
  247. *
  248. * @param key
  249. * 名称
  250. * @param value
  251. * 值
  252. */
  253. public static void modifyConfig(String key, String value) {
  254. try {
  255. // 从输入流中读取属性列表(键和元素对)
  256. Properties prop = getProperties();
  257. prop.setProperty(key, value);
  258. String path = Global.class.getResource("/properties/jeeplus.properties").getPath();
  259. FileOutputStream outputFile = new FileOutputStream(path);
  260. prop.store(outputFile, "modify");
  261. outputFile.close();
  262. outputFile.flush();
  263. } catch (Exception e) {
  264. e.printStackTrace();
  265. }
  266. }
  267. /**
  268. * 返回 Properties
  269. * @param
  270. * @return
  271. */
  272. public static Properties getProperties(){
  273. Properties prop = new Properties();
  274. try {
  275. Reader reader = Resources.getResourceAsReader("/properties/jeeplus.properties");
  276. prop.load(reader);
  277. } catch (Exception e) {
  278. return null;
  279. }
  280. return prop;
  281. }
  282. }