001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.Validator;
021    
022    import java.io.File;
023    import java.io.IOException;
024    import java.io.InputStream;
025    
026    import java.net.URL;
027    import java.net.URLConnection;
028    
029    import java.util.Map;
030    import java.util.concurrent.ConcurrentHashMap;
031    
032    import javax.servlet.ServletContext;
033    
034    /**
035     * @author Shuyang Zhou
036     */
037    public class FileTimestampUtil {
038    
039            public static long getTimestamp(
040                    ServletContext servletContext, String path) {
041    
042                    if (Validator.isNull(path)) {
043                            return 0;
044                    }
045    
046                    if (path.charAt(0) != CharPool.SLASH) {
047                            return 0;
048                    }
049    
050                    Long timestamp = _timestamps.get(path);
051    
052                    if (timestamp != null) {
053                            return timestamp;
054                    }
055    
056                    timestamp = 0L;
057    
058                    String uriRealPath = servletContext.getRealPath(path);
059    
060                    if (uriRealPath != null) {
061                            File uriFile = new File(uriRealPath);
062    
063                            if (uriFile.exists()) {
064                                    timestamp = uriFile.lastModified();
065    
066                                    _timestamps.put(path, timestamp);
067    
068                                    return timestamp;
069                            }
070                    }
071    
072                    try {
073                            URL url = servletContext.getResource(path);
074    
075                            if (url == null) {
076                                    _log.error("Resource URL for " + path + " is null");
077                            }
078                            else {
079                                    URLConnection urlConnection = null;
080    
081                                    try {
082                                            urlConnection = url.openConnection();
083    
084                                            timestamp = urlConnection.getLastModified();
085                                    }
086                                    finally {
087                                            if (urlConnection != null) {
088                                                    try {
089                                                            InputStream inputStream =
090                                                                    urlConnection.getInputStream();
091    
092                                                            inputStream.close();
093                                                    }
094                                                    catch (IOException ioe) {
095                                                    }
096                                            }
097                                    }
098                            }
099                    }
100                    catch (IOException ioe) {
101                            _log.error(ioe, ioe);
102                    }
103    
104                    _timestamps.put(path, timestamp);
105    
106                    return timestamp;
107            }
108    
109            public static void reset() {
110                    _timestamps.clear();
111            }
112    
113            private static Log _log = LogFactoryUtil.getLog(FileTimestampUtil.class);
114    
115            private static Map<String, Long> _timestamps =
116                    new ConcurrentHashMap<String, Long>();
117    
118    }