001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.servlet;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    import java.io.File;
021    
022    import java.util.Map;
023    import java.util.concurrent.ConcurrentHashMap;
024    
025    import javax.servlet.ServletContext;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class FileTimestampUtil {
031    
032            public static long getTimestamp(
033                    ServletContext servletContext, String path) {
034    
035                    return getTimestamp(servletContext, path, 0);
036            }
037    
038            public static long getTimestamp(
039                    ServletContext servletContext, String path, long defaultTimestamp) {
040    
041                    if (Validator.isNull(path)) {
042                            return defaultTimestamp;
043                    }
044    
045                    if (path.charAt(0) != CharPool.SLASH) {
046                            return defaultTimestamp;
047                    }
048    
049                    Long timestamp = _timestamps.get(path);
050    
051                    if (timestamp != null) {
052                            return timestamp;
053                    }
054    
055                    timestamp = defaultTimestamp;
056    
057                    String uriRealPath = ServletContextUtil.getRealPath(
058                            servletContext, path);
059    
060                    if (uriRealPath != null) {
061                            File uriFile = new File(uriRealPath);
062    
063                            if (uriFile.exists()) {
064                                    timestamp = uriFile.lastModified();
065                            }
066                    }
067    
068                    _timestamps.put(path, timestamp);
069    
070                    return timestamp;
071            }
072    
073            public static void reset() {
074                    _timestamps.clear();
075            }
076    
077            private static Map<String, Long> _timestamps =
078                    new ConcurrentHashMap<String, Long>();
079    
080    }