|
1 | 1 | package org.maxkey.util; |
2 | 2 |
|
3 | 3 | import java.io.UnsupportedEncodingException; |
4 | | - |
5 | 4 | import org.slf4j.Logger; |
6 | 5 | import org.slf4j.LoggerFactory; |
7 | 6 |
|
8 | 7 | public class PathUtils { |
9 | | - private final Logger _logger = LoggerFactory.getLogger(PathUtils.class); |
10 | | - private static PathUtils instance = null; |
11 | | - private String classPath; |
12 | | - private String appPath; |
13 | | - public static String WEB_INFO = "/WEB-INF/"; |
14 | | - |
15 | | - public static synchronized PathUtils getInstance() { |
16 | | - if (instance == null) { |
17 | | - instance = new PathUtils(); |
18 | | - instance._logger.debug("getInstance()" +" new PathUtils instance"); |
19 | | - } |
20 | | - return instance; |
21 | | - } |
| 8 | + private static final Logger _logger = LoggerFactory.getLogger(PathUtils.class); |
| 9 | + private static PathUtils instance = null; |
| 10 | + private String classPath; |
| 11 | + private String appPath; |
| 12 | + public static String WEB_INFO = "/WEB-INF/"; |
| 13 | + |
| 14 | + /** |
| 15 | + * getInstance . |
| 16 | + * @return |
| 17 | + */ |
| 18 | + public static synchronized PathUtils getInstance() { |
| 19 | + if (instance == null) { |
| 20 | + instance = new PathUtils(); |
| 21 | + PathUtils._logger.debug("getInstance()" + " new PathUtils instance"); |
| 22 | + } |
| 23 | + return instance; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * PathUtils. |
| 28 | + */ |
| 29 | + public PathUtils() { |
| 30 | + try { |
| 31 | + classPath = java.net.URLDecoder.decode( |
| 32 | + PathUtils.class.getResource("PathUtils.xml").getFile(), |
| 33 | + "UTF-8" |
| 34 | + ); |
| 35 | + _logger.debug("PathUtils() PathUtils " + classPath); |
| 36 | + } catch (UnsupportedEncodingException e) { |
| 37 | + e.printStackTrace(); |
| 38 | + } |
| 39 | + |
| 40 | + String fileProtocol = PathUtils.class.getResource("PathUtils.properties").getProtocol(); |
| 41 | + _logger.info("Properties Protocol:" |
| 42 | + + PathUtils.class.getResource("PathUtils.properties").getProtocol()); |
| 43 | + |
| 44 | + if (fileProtocol.equalsIgnoreCase("file") && classPath.indexOf("file:") == 0) { |
| 45 | + classPath = classPath.substring(5, classPath.length()); |
| 46 | + } else if (fileProtocol.equalsIgnoreCase("jar") && classPath.indexOf("file:") == 0) { |
| 47 | + // file:/Server/webapps/app |
| 48 | + classPath = classPath.substring(5, classPath.length()); |
| 49 | + } else if (fileProtocol.equalsIgnoreCase("wsjar") && classPath.indexOf("file:") == 0) { |
| 50 | + classPath = classPath.substring(5, classPath.length()); |
| 51 | + } else if (classPath.equalsIgnoreCase("file:")) { |
| 52 | + classPath = classPath.substring(5, classPath.length()); |
| 53 | + } |
| 54 | + |
| 55 | + _logger.info("PathUtils Class Path : " + classPath); |
| 56 | + classPath = classPath.substring(0, classPath.indexOf("/org/maxkey/util/PathUtils.xml")); |
| 57 | + if (classPath.indexOf(WEB_INFO) == -1) { |
| 58 | + appPath = classPath.substring(0, classPath.lastIndexOf("/")); |
| 59 | + } else { |
| 60 | + appPath = classPath.substring(0, classPath.lastIndexOf(WEB_INFO)); |
| 61 | + } |
| 62 | + |
| 63 | + System.setProperty("APP_PATH", appPath); |
| 64 | + System.setProperty("CLASSES_PATH", classPath); |
| 65 | + |
| 66 | + _logger.info("PathUtils App Path : " + appPath); |
| 67 | + _logger.info("PathUtils Class Path : " + classPath); |
| 68 | + } |
| 69 | + |
| 70 | + public String getAppPath() { |
| 71 | + return appPath + "/"; |
| 72 | + } |
| 73 | + |
| 74 | + public String getClassPath() { |
| 75 | + return classPath + "/"; |
| 76 | + } |
22 | 77 |
|
23 | | - public PathUtils() { |
24 | | - try { |
25 | | - classPath = java.net.URLDecoder.decode(PathUtils.class.getResource("PathUtils.properties").getFile(),"UTF-8"); |
26 | | - _logger.debug("PathUtils() PathUtils " +classPath); |
27 | | - } catch (UnsupportedEncodingException e) { |
28 | | - e.printStackTrace(); |
29 | | - } |
30 | | - |
31 | | - String fileProtocol=PathUtils.class.getResource("PathUtils.properties").getProtocol(); |
32 | | - _logger.info("Properties Protocol:"+PathUtils.class.getResource("PathUtils.properties").getProtocol()); |
33 | | - |
34 | | - if(fileProtocol.equalsIgnoreCase("file")&&classPath.indexOf("file:")==0){ |
35 | | - classPath=classPath.substring(5, classPath.length()); |
36 | | - }else if(fileProtocol.equalsIgnoreCase("jar")&&classPath.indexOf("file:")==0){ |
37 | | - // file:/Server/webapps/app |
38 | | - classPath=classPath.substring(5, classPath.length()); |
39 | | - }else if(fileProtocol.equalsIgnoreCase("wsjar")&&classPath.indexOf("file:")==0){ |
40 | | - classPath=classPath.substring(5, classPath.length()); |
41 | | - }else if(classPath.equalsIgnoreCase("file:")){ |
42 | | - classPath=classPath.substring(5, classPath.length()); |
43 | | - } |
44 | | - |
45 | | - _logger.info("PathUtils Class Path : " + classPath); |
46 | | - classPath=classPath.substring(0,classPath.indexOf("/org/maxkey/util/PathUtils.properties")); |
47 | | - if(classPath.indexOf(WEB_INFO)==-1) { |
48 | | - appPath=classPath.substring(0,classPath.lastIndexOf("/")); |
49 | | - }else { |
50 | | - appPath=classPath.substring(0,classPath.lastIndexOf(WEB_INFO)); |
51 | | - } |
52 | | - |
53 | | - System.setProperty("APP_PATH", appPath); |
54 | | - System.setProperty("CLASSES_PATH", classPath); |
55 | | - |
56 | | - _logger.info("PathUtils App Path : " + appPath); |
57 | | - _logger.info("PathUtils Class Path : " + classPath); |
58 | | - } |
59 | | - |
60 | | - public String getAppPath(){ |
61 | | - return appPath + "/"; |
62 | | - } |
63 | | - |
64 | | - public String getClassPath(){ |
65 | | - return classPath + "/"; |
66 | | - } |
67 | | - |
68 | | - public String getWebInf(){ |
69 | | - return (classPath.lastIndexOf(WEB_INFO)>-1)? (appPath + WEB_INFO) : ""; |
70 | | - } |
| 78 | + public String getWebInf() { |
| 79 | + return (classPath.lastIndexOf(WEB_INFO) > -1) ? (appPath + WEB_INFO) : ""; |
| 80 | + } |
71 | 81 | } |
0 commit comments