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.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.security.AccessController;
023    import java.security.PrivilegedExceptionAction;
024    
025    import java.util.Map;
026    import java.util.concurrent.ConcurrentHashMap;
027    
028    import javax.servlet.ServletContext;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public class FileAvailabilityUtil {
034    
035            public static boolean isAvailable(
036                    ServletContext servletContext, String path) {
037    
038                    if (Validator.isNull(path)) {
039                            return false;
040                    }
041    
042                    if (path.charAt(0) != CharPool.SLASH) {
043                            return true;
044                    }
045    
046                    Boolean available = _availabilities.get(path);
047    
048                    if (available == null) {
049                            URL url = null;
050    
051                            try {
052                                    url = AccessController.doPrivileged(
053                                            new ResourcePrivilegedExceptionAction(
054                                                    servletContext, path));
055                            }
056                            catch (Exception e) {
057                            }
058    
059                            if (url == null) {
060                                    available = Boolean.FALSE;
061                            }
062                            else {
063                                    available = Boolean.TRUE;
064                            }
065    
066                            _availabilities.put(path, available);
067                    }
068    
069                    return available;
070            }
071    
072            public static void reset() {
073                    _availabilities.clear();
074            }
075    
076            private static Map<String, Boolean> _availabilities =
077                    new ConcurrentHashMap<String, Boolean>();
078    
079            private static class ResourcePrivilegedExceptionAction
080                    implements PrivilegedExceptionAction<URL> {
081    
082                    public ResourcePrivilegedExceptionAction(
083                            ServletContext servletContext, String path) {
084    
085                            _servletContext = servletContext;
086                            _path = path;
087                    }
088    
089                    public URL run() throws Exception {
090                            return _servletContext.getResource(_path);
091                    }
092    
093                    private String _path;
094                    private ServletContext _servletContext;
095    
096            }
097    
098    }