001
014
015 package com.liferay.portal.kernel.servlet;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.CharPool;
020 import com.liferay.portal.kernel.util.URLUtil;
021 import com.liferay.portal.kernel.util.Validator;
022
023 import java.io.File;
024 import java.io.IOException;
025
026 import java.net.URL;
027
028 import java.util.Map;
029 import java.util.concurrent.ConcurrentHashMap;
030
031 import javax.servlet.ServletContext;
032
033
036 public class FileTimestampUtil {
037
038 public static long getTimestamp(
039 ServletContext servletContext, String path) {
040
041 if (Validator.isNull(path)) {
042 return 0;
043 }
044
045 if (path.charAt(0) != CharPool.SLASH) {
046 return 0;
047 }
048
049 Long timestamp = _timestamps.get(path);
050
051 if (timestamp != null) {
052 return timestamp;
053 }
054
055 timestamp = 0L;
056
057 String uriRealPath = servletContext.getRealPath(path);
058
059 if (uriRealPath != null) {
060 File uriFile = new File(uriRealPath);
061
062 if (uriFile.exists()) {
063 timestamp = uriFile.lastModified();
064
065 _timestamps.put(path, timestamp);
066
067 return timestamp;
068 }
069 }
070
071 try {
072 URL url = servletContext.getResource(path);
073
074 if (url == null) {
075 _log.error("Resource URL for " + path + " is null");
076 }
077 else {
078 timestamp = URLUtil.getLastModifiedTime(url);
079 }
080 }
081 catch (IOException ioe) {
082 _log.error(ioe, ioe);
083 }
084
085 _timestamps.put(path, timestamp);
086
087 return timestamp;
088 }
089
090 public static void reset() {
091 _timestamps.clear();
092 }
093
094 private static final Log _log = LogFactoryUtil.getLog(
095 FileTimestampUtil.class);
096
097 private static final Map<String, Long> _timestamps =
098 new ConcurrentHashMap<>();
099
100 }