KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > spring > DwrSpringServlet


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.spring;
17
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.servlet.ServletConfig JavaDoc;
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServlet JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.directwebremoting.WebContextFactory.WebContextBuilder;
30 import org.directwebremoting.impl.ContainerUtil;
31 import org.directwebremoting.impl.StartupUtil;
32 import org.directwebremoting.servlet.UrlProcessor;
33 import org.directwebremoting.util.Logger;
34 import org.springframework.beans.factory.BeanCreationException;
35 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
36 import org.springframework.web.context.WebApplicationContext;
37 import org.springframework.web.context.support.WebApplicationContextUtils;
38
39 /**
40  * The servlet that handles all calls to DWR. <br>
41  * It retrieves its configuration from the Spring IoC container. This is done in two ways:
42  * <ol>
43  * <li>Use the Spring namespace. When using the Spring namespace for DWR, the confgiuration for DWR is
44  * automatically picked up by this servlet.</li>
45  * <li>Explicitly specify which configurations to pick up. When explicitly defining the DWR configuration in
46  * Spring yourself, you can explicitely specify them in the init parameters.</li>
47  * </ol>
48  * Same as with the <code>DwrServlet</code>, you can specify a <code>debug</code> init parameter on this servlet
49  * to put DWR in debug mode (allowing access to the very handy debug pages).
50  *
51  * @see org.directwebremoting.servlet.DwrServlet
52  *
53  * @author Bram Smeets
54  * @author Joe Walker [joe at getahead dot ltd dot uk]
55  */

56 public class DwrSpringServlet extends HttpServlet JavaDoc
57 {
58     /**
59      * Setter for use by the Spring IoC container to tell us what Configurators
60      * exist for us to configure ourselves.
61      * @param configurators
62      */

63     public void setConfigurators(List JavaDoc configurators)
64     {
65         this.configurators = configurators;
66     }
67
68     /**
69      * Do we prefix the list of Configurators with a default to read the system
70      * dwr.xml file?
71      * @param includeDefaultConfig the includeDefaultConfig to set
72      */

73     public void setIncludeDefaultConfig(boolean includeDefaultConfig)
74     {
75         this.includeDefaultConfig = includeDefaultConfig;
76     }
77
78     /* (non-Javadoc)
79      * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
80      */

81     public void init(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc
82     {
83         super.init(servletConfig);
84         ServletContext JavaDoc servletContext = servletConfig.getServletContext();
85
86         try
87         {
88             WebApplicationContext webappContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
89
90             container = new SpringContainer();
91             container.setBeanFactory(webappContext);
92             ContainerUtil.setupDefaults(container, servletConfig);
93             ContainerUtil.setupFromServletConfig(container, servletConfig);
94
95             container.setupFinished();
96
97             webContextBuilder = StartupUtil.initWebContext(servletConfig, servletContext, container);
98             StartupUtil.initServerContext(servletConfig, servletContext, container);
99
100             ContainerUtil.prepareForWebContextFilter(servletContext, servletConfig, container, webContextBuilder, this);
101             // retrieve the configurators from Spring (loaded by the ContextLoaderListener)
102
try
103             {
104                 configurators.add(webappContext.getBean(DwrNamespaceHandler.DEFAULT_SPRING_CONFIGURATOR_ID));
105             }
106             catch (NoSuchBeanDefinitionException ex)
107             {
108                 throw new ServletException JavaDoc("No DWR configuration was found in your application context, make sure to define one", ex);
109             }
110
111             if (includeDefaultConfig)
112             {
113                 ContainerUtil.configureFromSystemDwrXml(container);
114             }
115
116             ContainerUtil.configureFromInitParams(container, servletConfig);
117             ContainerUtil.configure(container, configurators);
118
119             ContainerUtil.publishContainer(container, servletConfig);
120         }
121         catch (InstantiationException JavaDoc ex)
122         {
123             throw new BeanCreationException("Failed to instansiate", ex);
124         }
125         catch (IllegalAccessException JavaDoc ex)
126         {
127             throw new BeanCreationException("Access error", ex);
128         }
129         catch (Exception JavaDoc ex)
130         {
131             log.fatal("init failed", ex);
132             throw new ServletException JavaDoc(ex);
133         }
134         finally
135         {
136             webContextBuilder.unset();
137         }
138     }
139
140     /* (non-Javadoc)
141      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
142      */

143     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws IOException JavaDoc, ServletException JavaDoc
144     {
145         doPost(req, resp);
146     }
147
148     /* (non-Javadoc)
149      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
150      */

151     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
152     {
153         try
154         {
155             webContextBuilder.set(request, response, getServletConfig(), getServletContext(), container);
156
157             UrlProcessor processor = (UrlProcessor) container.getBean(UrlProcessor.class.getName());
158             processor.handle(request, response);
159         }
160         finally
161         {
162             webContextBuilder.unset();
163         }
164     }
165
166     /**
167      * DWRs IoC container (that passes stuff to Spring in this case)
168      */

169     private SpringContainer container;
170
171     /**
172      * The WebContext that keeps http objects local to a thread
173      */

174     protected WebContextBuilder webContextBuilder;
175
176     /**
177      * Do we prefix the list of Configurators with a default to read the system
178      * dwr.xml file?
179      */

180     private boolean includeDefaultConfig = true;
181
182     /**
183      * What Configurators exist for us to configure ourselves.
184      */

185     private List JavaDoc configurators = new ArrayList JavaDoc();
186
187     /**
188      * The log stream
189      */

190     private static final Logger log = Logger.getLogger(DwrSpringServlet.class);
191 }
192
Popular Tags