PathUtils.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.maxkey.util;
  17. import java.io.UnsupportedEncodingException;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. public class PathUtils {
  21. private static final Logger _logger = LoggerFactory.getLogger(PathUtils.class);
  22. private static PathUtils instance = null;
  23. private String classPath;
  24. private String appPath;
  25. public static String WEB_INFO = "/WEB-INF/";
  26. private static final String PATH_FILE_NAME = "PathUtils.properties";
  27. /**
  28. * getInstance .
  29. * @return
  30. */
  31. public static synchronized PathUtils getInstance() {
  32. if (instance == null) {
  33. instance = new PathUtils();
  34. PathUtils._logger.trace("getInstance()" + " new PathUtils instance");
  35. }
  36. return instance;
  37. }
  38. /**
  39. * PathUtils.
  40. */
  41. public PathUtils() {
  42. try {
  43. classPath = java.net.URLDecoder.decode(
  44. PathUtils.class.getResource(PATH_FILE_NAME).getFile(),
  45. "UTF-8"
  46. );
  47. _logger.trace("PathUtils() PathUtils " + classPath);
  48. } catch (UnsupportedEncodingException e) {
  49. e.printStackTrace();
  50. }
  51. String fileProtocol = PathUtils.class.getResource(PATH_FILE_NAME).getProtocol();
  52. _logger.trace("Properties Protocol:"
  53. + PathUtils.class.getResource("PathUtils.properties").getProtocol());
  54. if (fileProtocol.equalsIgnoreCase("file") && classPath.indexOf("file:") == 0) {
  55. classPath = classPath.substring(5, classPath.length());
  56. } else if (fileProtocol.equalsIgnoreCase("jar") && classPath.indexOf("file:") == 0) {
  57. // file:/Server/webapps/app
  58. classPath = classPath.substring(5, classPath.length());
  59. } else if (fileProtocol.equalsIgnoreCase("wsjar") && classPath.indexOf("file:") == 0) {
  60. classPath = classPath.substring(5, classPath.length());
  61. } else if (classPath.equalsIgnoreCase("file:")) {
  62. classPath = classPath.substring(5, classPath.length());
  63. }
  64. _logger.trace("PathUtils Class Path : " + classPath);
  65. classPath = classPath.substring(0,
  66. classPath.indexOf("/org/maxkey/util/" + PATH_FILE_NAME));
  67. if (classPath.indexOf(WEB_INFO) == -1) {
  68. appPath = classPath.substring(0, classPath.lastIndexOf("/"));
  69. } else {
  70. appPath = classPath.substring(0, classPath.lastIndexOf(WEB_INFO));
  71. }
  72. System.setProperty("APP_PATH", appPath);
  73. System.setProperty("CLASSES_PATH", classPath);
  74. _logger.trace("PathUtils App Path : " + appPath);
  75. _logger.trace("PathUtils Class Path : " + classPath);
  76. }
  77. public String getAppPath() {
  78. return appPath + "/";
  79. }
  80. public String getClassPath() {
  81. return classPath + "/";
  82. }
  83. public String getWebInf() {
  84. return (classPath.lastIndexOf(WEB_INFO) > -1) ? (appPath + WEB_INFO) : "";
  85. }
  86. }