1
22
23 package com.liferay.portal.kernel.servlet;
24
25 import com.liferay.portal.kernel.util.Http;
26 import com.liferay.portal.kernel.util.StringPool;
27
28 import java.io.File;
29
30 import java.util.Set;
31
32 import javax.servlet.ServletContext;
33
34
40 public class ServletContextUtil {
41
42 public static long getLastModified(ServletContext servletContext) {
43 return getLastModified(servletContext, StringPool.SLASH);
44 }
45
46 public static long getLastModified(
47 ServletContext servletContext, String resourcePath) {
48
49 return getLastModified(servletContext, resourcePath, false);
50 }
51
52 public static long getLastModified(
53 ServletContext servletContext, String resourcePath, boolean cache) {
54
55 if (resourcePath.startsWith(Http.HTTP) ||
56 resourcePath.startsWith(Http.HTTPS)) {
57
58 int pos = resourcePath.indexOf(Http.PROTOCOL_DELIMITER);
59
60 resourcePath = resourcePath.substring(pos + 3);
61 }
62
63 int pos = resourcePath.indexOf(StringPool.SLASH);
64
65 if (pos != -1) {
66 resourcePath = resourcePath.substring(pos);
67 }
68
69 if (cache) {
70 Long lastModified = (Long)servletContext.getAttribute(
71 ServletContextUtil.class.getName() + StringPool.PERIOD +
72 resourcePath);
73
74 if (lastModified != null) {
75 return lastModified.longValue();
76 }
77 }
78
79 long lastModified = 0;
80
81 Set<String> resourcePaths = servletContext.getResourcePaths(
82 resourcePath);
83
84 if (resourcePaths != null) {
85 for (String curResourcePath : resourcePaths) {
86 if (curResourcePath.endsWith(StringPool.SLASH)) {
87 long curLastModified = getLastModified(
88 servletContext, curResourcePath);
89
90 if (curLastModified > lastModified) {
91 lastModified = curLastModified;
92 }
93 }
94 else {
95 File file = new File(
96 servletContext.getRealPath(curResourcePath));
97
98 if (file.lastModified() > lastModified) {
99 lastModified = file.lastModified();
100 }
101 }
102 }
103 }
104
105 if (cache) {
106 servletContext.setAttribute(
107 ServletContextUtil.class.getName() + StringPool.PERIOD +
108 resourcePath,
109 new Long(lastModified));
110 }
111
112 return lastModified;
113 }
114
115 }