001
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
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 }