1
22
23 package com.liferay.portal.kernel.servlet;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.util.FileUtil;
28 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
29 import com.liferay.portal.kernel.util.ServerDetector;
30 import com.liferay.portal.kernel.util.StringPool;
31
32 import java.io.File;
33
34 import java.util.Set;
35
36 import javax.servlet.ServletContext;
37
38
44 public class ServletContextUtil {
45
46 public static long getLastModified(ServletContext servletContext) {
47 return getLastModified(servletContext, StringPool.SLASH);
48 }
49
50 public static long getLastModified(
51 ServletContext servletContext, String resourcePath) {
52
53 return getLastModified(servletContext, resourcePath, false);
54 }
55
56 public static long getLastModified(
57 ServletContext servletContext, String resourcePath, boolean cache) {
58
59 if (cache) {
60 Long lastModified = (Long)servletContext.getAttribute(
61 ServletContextUtil.class.getName() + StringPool.PERIOD +
62 resourcePath);
63
64 if (lastModified != null) {
65 return lastModified.longValue();
66 }
67 }
68
69 long lastModified = 0;
70
71 Set<String> resourcePaths = servletContext.getResourcePaths(
72 resourcePath);
73
74 if (resourcePaths != null) {
75 for (String curResourcePath : resourcePaths) {
76 if (curResourcePath.endsWith(StringPool.SLASH)) {
77 long curLastModified = getLastModified(
78 servletContext, curResourcePath);
79
80 if (curLastModified > lastModified) {
81 lastModified = curLastModified;
82 }
83 }
84 else {
85 String realPath = getRealPath(
86 servletContext, curResourcePath);
87
88 if (realPath == null) {
89 _log.error(
90 "Real path for " + curResourcePath + " is null");
91
92 continue;
93 }
94
95 File file = new File(realPath);
96
97 if (file.lastModified() > lastModified) {
98 lastModified = file.lastModified();
99 }
100 }
101 }
102 }
103
104 if (cache) {
105 servletContext.setAttribute(
106 ServletContextUtil.class.getName() + StringPool.PERIOD +
107 resourcePath,
108 new Long(lastModified));
109 }
110
111 return lastModified;
112 }
113
114 public static String getRealPath(
115 ServletContext servletContext, String path) {
116
117 String realPath = servletContext.getRealPath(path);
118
119 if ((realPath == null) && ServerDetector.isWebLogic()) {
120 String rootDir = getRootDir(servletContext);
121
122 if (path.startsWith(StringPool.SLASH)) {
123 realPath = rootDir + path.substring(1);
124 }
125 else {
126 realPath = rootDir + path;
127 }
128
129 if (!FileUtil.exists(realPath)) {
130 realPath = null;
131 }
132 }
133
134 return realPath;
135 }
136
137 protected static String getRootDir(ServletContext servletContext) {
138 String key = ServletContextUtil.class.getName() + ".rootDir";
139
140 String rootDir = (String)servletContext.getAttribute(key);
141
142 if (rootDir == null) {
143 ClassLoader classLoader = (ClassLoader)servletContext.getAttribute(
144 PortletServlet.PORTLET_CLASS_LOADER);
145
146 if (classLoader == null) {
147 classLoader = PortalClassLoaderUtil.getClassLoader();
148 }
149
150 rootDir = WebDirDetector.getRootDir(classLoader);
151
152 servletContext.setAttribute(key, rootDir);
153 }
154
155 return rootDir;
156 }
157
158 private static Log _log = LogFactoryUtil.getLog(ServletContextUtil.class);
159
160 }