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.taglib;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    import java.net.URL;
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 FileAvailabilityUtil {
031    
032            public static boolean isAvailable(
033                    ServletContext servletContext, String path) {
034    
035                    if (Validator.isNull(path)) {
036                            return false;
037                    }
038    
039                    if (path.charAt(0) != CharPool.SLASH) {
040                            return true;
041                    }
042    
043                    Boolean available = _availabilities.get(path);
044    
045                    if (available == null) {
046                            URL url = null;
047    
048                            try {
049                                    url = servletContext.getResource(path);
050                            }
051                            catch (Exception e) {
052                            }
053    
054                            if (url == null) {
055                                    available = Boolean.FALSE;
056                            }
057                            else {
058                                    available = Boolean.TRUE;
059                            }
060    
061                            _availabilities.put(path, available);
062                    }
063    
064                    return available;
065            }
066    
067            public static void reset() {
068                    _availabilities.clear();
069            }
070    
071            private static Map<String, Boolean> _availabilities =
072                    new ConcurrentHashMap<String, Boolean>();
073    
074    }