001    /**
002     * Copyright (c) 2000-2012 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.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    }