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 return file.lastModified();
113 }
114
115 protected Servlet reloadDependants(
116 String path, Servlet servlet, ServletInfo servletInfo) {
117
118 if (!_reloadDependants) {
119 return servlet;
120 }
121
122 try {
123 Method method = ReflectionUtil.getDeclaredMethod(
124 servlet.getClass(), "getDependants");
125
126 Collection<String> dependants = null;
127
128 if (JasperVersionDetector.hasJspServletDependantsMap()) {
129 Map<String, ?> dependantsMap = (Map<String, ?>)method.invoke(
130 servlet);
131
132 if (dependantsMap != null) {
133 dependants = dependantsMap.keySet();
134 }
135 }
136 else {
137 dependants = (List<String>)method.invoke(servlet);
138 }
139
140 if (dependants == null) {
141 return servlet;
142 }
143
144 boolean reloadServlet = false;
145
146 for (String dependant : dependants) {
147 long lastModified = getFileLastModified(dependant, servlet);
148
149 Long previousLastModified = _dependantTimestamps.get(dependant);
150
151 if (previousLastModified == null) {
152 _dependantTimestamps.put(dependant, lastModified);
153
154 previousLastModified = lastModified;
155 }
156
157 if ((lastModified == 0) ||
158 (lastModified != previousLastModified.longValue())) {
159
160 reloadServlet = true;
161
162 _dependantTimestamps.put(dependant, lastModified);
163
164 if (_log.isDebugEnabled()) {
165 _log.debug("Reload dependant " + dependant);
166 }
167 }
168 }
169
170 if (reloadServlet) {
171 _servletInfos.remove(path);
172
173 updateFileLastModified(path, servlet);
174
175 servlet = null;
176 }
177 }
178 catch (NoSuchMethodException nsme) {
179 if (_log.isWarnEnabled()) {
180 _log.warn(
181 "Reloading of dependant JSP is disabled because your " +
182 "Servlet container is not a variant of Jasper");
183 }
184
185 _reloadDependants = false;
186 }
187 catch (Exception e) {
188 _log.error(e, e);
189 }
190
191 return servlet;
192 }
193
194 protected void updateFileLastModified(String path, Servlet servlet) {
195 ServletConfig servletConfig = servlet.getServletConfig();
196
197 ServletContext servletContext = servletConfig.getServletContext();
198
199 String rootPath = servletContext.getRealPath(StringPool.BLANK);
200
201 File file = new File(rootPath, path);
202
203 file.setLastModified(System.currentTimeMillis());
204 }
205
206 private static Log _log = LogFactoryUtil.getLog(
207 DirectServletRegistryImpl.class);
208
209 private Map<String, Long> _dependantTimestamps =
210 new ConcurrentHashMap<String, Long>();
211 private boolean _reloadDependants = true;
212 private Map<String, ServletInfo> _servletInfos =
213 new ConcurrentHashMap<String, ServletInfo>();
214
215 private class ServletInfo {
216
217 public long getLastModified() {
218 return _lastModified;
219 }
220
221 public Servlet getServlet() {
222 return _servlet;
223 }
224
225 public void setLastModified(long lastModified) {
226 _lastModified = lastModified;
227 }
228
229 public void setServlet(Servlet servlet) {
230 _servlet = servlet;
231 }
232
233 private long _lastModified;
234 private Servlet _servlet;
235
236 }
237
238 }