001
014
015 package com.liferay.portal.servlet;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.security.pacl.DoPrivileged;
020 import com.liferay.portal.kernel.servlet.DirectServletRegistry;
021 import com.liferay.portal.kernel.util.ReflectionUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.util.PropsValues;
024
025 import java.io.File;
026
027 import java.lang.reflect.Method;
028
029 import java.util.Collection;
030 import java.util.List;
031 import java.util.Map;
032 import java.util.concurrent.ConcurrentHashMap;
033
034 import javax.servlet.Servlet;
035 import javax.servlet.ServletConfig;
036 import javax.servlet.ServletContext;
037
038
041 @DoPrivileged
042 public class DirectServletRegistryImpl implements DirectServletRegistry {
043
044 @Override
045 public void clearServlets() {
046 _servletInfos.clear();
047 }
048
049 @Override
050 public Servlet getServlet(String path) {
051 ServletInfo servletInfo = _servletInfos.get(path);
052
053 if (servletInfo == null) {
054 return null;
055 }
056
057 Servlet servlet = servletInfo.getServlet();
058
059 if (PropsValues.DIRECT_SERVLET_CONTEXT_RELOAD) {
060 long lastModified = getFileLastModified(path, servlet);
061
062 if ((lastModified == 0) ||
063 (lastModified != servletInfo.getLastModified())) {
064
065 _servletInfos.remove(path);
066
067 servlet = null;
068
069 if (_log.isDebugEnabled()) {
070 _log.debug("Reload " + path);
071 }
072 }
073 else {
074 servlet = reloadDependants(path, servlet, servletInfo);
075 }
076 }
077
078 return servlet;
079 }
080
081 @Override
082 public void putServlet(String path, Servlet servlet) {
083 if (_servletInfos.containsKey(path)) {
084 return;
085 }
086
087 long lastModified = 1;
088
089 if (PropsValues.DIRECT_SERVLET_CONTEXT_RELOAD) {
090 lastModified = getFileLastModified(path, servlet);
091 }
092
093 if (lastModified > 0) {
094 ServletInfo servletInfo = new ServletInfo();
095
096 servletInfo.setLastModified(lastModified);
097 servletInfo.setServlet(servlet);
098
099 _servletInfos.put(path, servletInfo);
100 }
101 }
102
103 protected long getFileLastModified(String path, Servlet servlet) {
104 ServletConfig servletConfig = servlet.getServletConfig();
105
106 ServletContext servletContext = servletConfig.getServletContext();
107
108 String rootPath = servletContext.getRealPath(StringPool.BLANK);
109
110 File file = new File(rootPath, path);
111
112 if (file.exists()) {
113 return file.lastModified();
114 }
115
116 return -1;
117 }
118
119 protected Servlet reloadDependants(
120 String path, Servlet servlet, ServletInfo servletInfo) {
121
122 if (!_reloadDependants) {
123 return servlet;
124 }
125
126 try {
127 Method method = ReflectionUtil.getDeclaredMethod(
128 servlet.getClass(), "getDependants");
129
130 Collection<String> dependants = null;
131
132 if (JasperVersionDetector.hasJspServletDependantsMap()) {
133 Map<String, ?> dependantsMap = (Map<String, ?>)method.invoke(
134 servlet);
135
136 if (dependantsMap != null) {
137 dependants = dependantsMap.keySet();
138 }
139 }
140 else {
141 dependants = (List<String>)method.invoke(servlet);
142 }
143
144 if (dependants == null) {
145 return servlet;
146 }
147
148 boolean reloadServlet = false;
149
150 for (String dependant : dependants) {
151 long lastModified = getFileLastModified(dependant, servlet);
152
153 Long previousLastModified = _dependantTimestamps.get(dependant);
154
155 if (previousLastModified == null) {
156 _dependantTimestamps.put(dependant, lastModified);
157
158 previousLastModified = lastModified;
159 }
160
161 if ((lastModified == 0) ||
162 (lastModified != previousLastModified.longValue())) {
163
164 reloadServlet = true;
165
166 _dependantTimestamps.put(dependant, lastModified);
167
168 if (_log.isDebugEnabled()) {
169 _log.debug("Reload dependant " + dependant);
170 }
171 }
172 }
173
174 if (reloadServlet) {
175 _servletInfos.remove(path);
176
177 updateFileLastModified(path, servlet);
178
179 servlet = null;
180 }
181 }
182 catch (NoSuchMethodException nsme) {
183 if (_log.isWarnEnabled()) {
184 _log.warn(
185 "Reloading of dependant JSP is disabled because your " +
186 "Servlet container is not a variant of Jasper");
187 }
188
189 _reloadDependants = false;
190 }
191 catch (Exception e) {
192 _log.error(e, e);
193 }
194
195 return servlet;
196 }
197
198 protected void updateFileLastModified(String path, Servlet servlet) {
199 ServletConfig servletConfig = servlet.getServletConfig();
200
201 ServletContext servletContext = servletConfig.getServletContext();
202
203 String rootPath = servletContext.getRealPath(StringPool.BLANK);
204
205 File file = new File(rootPath, path);
206
207 file.setLastModified(System.currentTimeMillis());
208 }
209
210 private static final Log _log = LogFactoryUtil.getLog(
211 DirectServletRegistryImpl.class);
212
213 private final Map<String, Long> _dependantTimestamps =
214 new ConcurrentHashMap<>();
215 private boolean _reloadDependants = true;
216 private final Map<String, ServletInfo> _servletInfos =
217 new ConcurrentHashMap<>();
218
219 private class ServletInfo {
220
221 public long getLastModified() {
222 return _lastModified;
223 }
224
225 public Servlet getServlet() {
226 return _servlet;
227 }
228
229 public void setLastModified(long lastModified) {
230 _lastModified = lastModified;
231 }
232
233 public void setServlet(Servlet servlet) {
234 _servlet = servlet;
235 }
236
237 private long _lastModified;
238 private Servlet _servlet;
239
240 }
241
242 }