001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.servlet.DirectServletRegistry;
020    import com.liferay.portal.kernel.util.ReflectionUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.security.lang.PortalSecurityManagerThreadLocal;
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    /**
039     * @author Shuyang Zhou
040     */
041    public class DirectServletRegistryImpl implements DirectServletRegistry {
042    
043            public void clearServlets() {
044                    _servletInfos.clear();
045            }
046    
047            public Servlet getServlet(String path) {
048                    ServletInfo servletInfo = _servletInfos.get(path);
049    
050                    if (servletInfo == null) {
051                            return null;
052                    }
053    
054                    Servlet servlet = servletInfo.getServlet();
055    
056                    if (PropsValues.DIRECT_SERVLET_CONTEXT_RELOAD) {
057                            long lastModified = getFileLastModified(path, servlet);
058    
059                            if ((lastModified == 0) ||
060                                    (lastModified != servletInfo.getLastModified())) {
061    
062                                    _servletInfos.remove(path);
063    
064                                    servlet = null;
065    
066                                    if (_log.isDebugEnabled()) {
067                                            _log.debug("Reload " + path);
068                                    }
069                            }
070                            else {
071                                    servlet = reloadDependants(path, servlet, servletInfo);
072                            }
073                    }
074    
075                    return servlet;
076            }
077    
078            public void putServlet(String path, Servlet servlet) {
079                    if (_servletInfos.containsKey(path)) {
080                            return;
081                    }
082    
083                    long lastModified = 1;
084    
085                    if (PropsValues.DIRECT_SERVLET_CONTEXT_RELOAD) {
086                            lastModified = getFileLastModified(path, servlet);
087                    }
088    
089                    if (lastModified > 0) {
090                            ServletInfo servletInfo = new ServletInfo();
091    
092                            servletInfo.setLastModified(lastModified);
093                            servletInfo.setServlet(servlet);
094    
095                            _servletInfos.put(path, servletInfo);
096                    }
097            }
098    
099            protected long getFileLastModified(String path, Servlet servlet) {
100                    ServletConfig servletConfig = servlet.getServletConfig();
101    
102                    ServletContext servletContext = servletConfig.getServletContext();
103    
104                    String rootPath = servletContext.getRealPath(StringPool.BLANK);
105    
106                    File file = new File(rootPath, path);
107    
108                    return file.lastModified();
109            }
110    
111            protected Servlet reloadDependants(
112                    String path, Servlet servlet, ServletInfo servletInfo) {
113    
114                    if (!_reloadDependants) {
115                            return servlet;
116                    }
117    
118                    boolean enabled = PortalSecurityManagerThreadLocal.isEnabled();
119    
120                    try {
121                            PortalSecurityManagerThreadLocal.setEnabled(false);
122    
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                    finally {
191                            PortalSecurityManagerThreadLocal.setEnabled(enabled);
192                    }
193    
194                    return servlet;
195            }
196    
197            protected void updateFileLastModified(String path, Servlet servlet) {
198                    ServletConfig servletConfig = servlet.getServletConfig();
199    
200                    ServletContext servletContext = servletConfig.getServletContext();
201    
202                    String rootPath = servletContext.getRealPath(StringPool.BLANK);
203    
204                    File file = new File(rootPath, path);
205    
206                    file.setLastModified(System.currentTimeMillis());
207            }
208    
209            private static Log _log = LogFactoryUtil.getLog(
210                    DirectServletRegistryImpl.class);
211    
212            private Map<String, Long> _dependantTimestamps =
213                    new ConcurrentHashMap<String, Long>();
214            private boolean _reloadDependants = true;
215            private Map<String, ServletInfo> _servletInfos =
216                    new ConcurrentHashMap<String, ServletInfo>();
217    
218            private class ServletInfo {
219    
220                    public long getLastModified() {
221                            return _lastModified;
222                    }
223    
224                    public Servlet getServlet() {
225                            return _servlet;
226                    }
227    
228                    public void setLastModified(long lastModified) {
229                            _lastModified = lastModified;
230                    }
231    
232                    public void setServlet(Servlet servlet) {
233                            _servlet = servlet;
234                    }
235    
236                    private long _lastModified;
237                    private Servlet _servlet;
238    
239            }
240    
241    }