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