001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.common.web.servlet;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.riotfamily.common.xml.BeanConfigurationWatcher;
030: import org.riotfamily.common.xml.ConfigurableBean;
031: import org.springframework.beans.BeansException;
032: import org.springframework.beans.factory.BeanFactoryUtils;
033: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
034: import org.springframework.context.ApplicationContext;
035: import org.springframework.web.servlet.DispatcherServlet;
036:
037: /**
038: * DispatcherServlet that checks whether one of the configuration files has
039: * been modified. If a change is detected the servlet is re-initalized and the
040: * underlying BeanFactory is refreshed.
041: * <p>
042: * As checks are performed upon each request you might want to set the
043: * <code>reloadable</code> init parameter to <code>false</code> when used in
044: * a production environment. Alternatively you can add a
045: * {@link ReloadableDispatcherServletConfig} bean to your ApplicationContext
046: * which allows you to set the <code>reloadable</code> property without
047: * modifying the web.xml.
048: *
049: * @author Felix Gnass [fgnass at neteye dot de]
050: * @author Jan-Frederic Linde [jfl at neteye dot de]
051: */
052: public class ReloadableDispatcherServlet extends DispatcherServlet
053: implements ConfigurableBean {
054:
055: private boolean reloadable = true;
056:
057: private BeanConfigurationWatcher watcher = new BeanConfigurationWatcher(
058: this );
059:
060: public Class getContextClass() {
061: return ResourceAwareContext.class;
062: }
063:
064: public void setReloadable(boolean reloadable) {
065: this .reloadable = reloadable;
066: }
067:
068: public boolean isReloadable() {
069: return reloadable;
070: }
071:
072: private void configureFromContext(ApplicationContext context) {
073: try {
074: ReloadableDispatcherServletConfig config = (ReloadableDispatcherServletConfig) BeanFactoryUtils
075: .beanOfType(context,
076: ReloadableDispatcherServletConfig.class);
077:
078: setReloadable(config.isReloadable());
079: } catch (NoSuchBeanDefinitionException e) {
080: }
081: }
082:
083: protected void onRefresh(ApplicationContext context)
084: throws BeansException {
085: super .onRefresh(context);
086: configureFromContext(context);
087: ResourceAwareContext ctx = (ResourceAwareContext) context;
088: watcher.setResources(ctx.getConfigResources());
089: }
090:
091: protected void doDispatch(HttpServletRequest request,
092: HttpServletResponse response) throws Exception {
093:
094: watcher.checkForModifications();
095: super .doDispatch(request, response);
096: }
097:
098: public void configure() {
099: refresh();
100: }
101: }
|