KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > servlet > ReloadableDispatcherServlet


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.servlet;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.riotfamily.common.xml.BeanConfigurationWatcher;
30 import org.riotfamily.common.xml.ConfigurableBean;
31 import org.springframework.beans.BeansException;
32 import org.springframework.beans.factory.BeanFactoryUtils;
33 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
34 import org.springframework.context.ApplicationContext;
35 import org.springframework.web.servlet.DispatcherServlet;
36
37 /**
38  * DispatcherServlet that checks whether one of the configuration files has
39  * been modified. If a change is detected the servlet is re-initalized and the
40  * underlying BeanFactory is refreshed.
41  * <p>
42  * As checks are performed upon each request you might want to set the
43  * <code>reloadable</code> init parameter to <code>false</code> when used in
44  * a production environment. Alternatively you can add a
45  * {@link ReloadableDispatcherServletConfig} bean to your ApplicationContext
46  * which allows you to set the <code>reloadable</code> property without
47  * modifying the web.xml.
48  *
49  * @author Felix Gnass [fgnass at neteye dot de]
50  * @author Jan-Frederic Linde [jfl at neteye dot de]
51  */

52 public class ReloadableDispatcherServlet extends DispatcherServlet
53         implements ConfigurableBean {
54
55     private boolean reloadable = true;
56
57     private BeanConfigurationWatcher watcher = new BeanConfigurationWatcher(this);
58
59     public Class JavaDoc getContextClass() {
60         return ResourceAwareContext.class;
61     }
62
63     public void setReloadable(boolean reloadable) {
64         this.reloadable = reloadable;
65     }
66
67     public boolean isReloadable() {
68         return reloadable;
69     }
70
71     private void configureFromContext(ApplicationContext context) {
72         try {
73             ReloadableDispatcherServletConfig config =
74                     (ReloadableDispatcherServletConfig)
75                     BeanFactoryUtils.beanOfType(context,
76                     ReloadableDispatcherServletConfig.class);
77
78             setReloadable(config.isReloadable());
79         }
80         catch (NoSuchBeanDefinitionException e) {
81         }
82     }
83
84     protected void onRefresh(ApplicationContext context) throws BeansException {
85         super.onRefresh(context);
86         configureFromContext(context);
87         ResourceAwareContext ctx = (ResourceAwareContext) context;
88         watcher.setResources(ctx.getConfigResources());
89     }
90
91     protected void doDispatch(HttpServletRequest JavaDoc request,
92             HttpServletResponse JavaDoc response) throws Exception JavaDoc {
93
94         watcher.checkForModifications();
95         super.doDispatch(request, response);
96     }
97
98     public void configure() {
99         refresh();
100     }
101 }
102
Popular Tags