001    /**
002     * Copyright (c) 2000-2013 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.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                            return available;
050                    }
051    
052                    URL url = null;
053    
054                    try {
055                            url = AccessController.doPrivileged(
056                                    new ResourcePrivilegedExceptionAction(servletContext, path));
057                    }
058                    catch (Exception e) {
059                    }
060    
061                    if (url == null) {
062                            available = Boolean.FALSE;
063                    }
064                    else {
065                            available = Boolean.TRUE;
066                    }
067    
068                    _availabilities.put(path, available);
069    
070                    return available;
071            }
072    
073            public static void reset() {
074                    _availabilities.clear();
075            }
076    
077            private static Map<String, Boolean> _availabilities =
078                    new ConcurrentHashMap<String, Boolean>();
079    
080            private static class ResourcePrivilegedExceptionAction
081                    implements PrivilegedExceptionAction<URL> {
082    
083                    public ResourcePrivilegedExceptionAction(
084                            ServletContext servletContext, String path) {
085    
086                            _servletContext = servletContext;
087                            _path = path;
088                    }
089    
090                    @Override
091                    public URL run() throws Exception {
092                            return _servletContext.getResource(_path);
093                    }
094    
095                    private String _path;
096                    private ServletContext _servletContext;
097    
098            }
099    
100    }