001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.spring;
017:
018: import java.io.IOException;
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import javax.servlet.ServletConfig;
023: import javax.servlet.ServletContext;
024: import javax.servlet.ServletException;
025: import javax.servlet.http.HttpServlet;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.directwebremoting.WebContextFactory.WebContextBuilder;
032: import org.directwebremoting.extend.Configurator;
033: import org.directwebremoting.impl.ContainerUtil;
034: import org.directwebremoting.impl.StartupUtil;
035: import org.directwebremoting.servlet.UrlProcessor;
036: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
037: import org.springframework.context.ApplicationContext;
038: import org.springframework.web.context.support.WebApplicationContextUtils;
039:
040: /**
041: * The servlet that handles all calls to DWR. <br>
042: * It retrieves its configuration from the Spring IoC container. This is done in two ways:
043: * <ol>
044: * <li>Use the Spring namespace. When using the Spring namespace for DWR, the confgiuration for DWR is
045: * automatically picked up by this servlet.</li>
046: * <li>Explicitly specify which configurations to pick up. When explicitly defining the DWR configuration in
047: * Spring yourself, you can explicitely specify them in the init parameters.</li>
048: * </ol>
049: * Same as with the <code>DwrServlet</code>, you can specify a <code>debug</code> init parameter on this servlet
050: * to put DWR in debug mode (allowing access to the very handy debug pages).
051: *
052: * @see org.directwebremoting.servlet.DwrServlet
053: *
054: * @author Bram Smeets
055: * @author Joe Walker [joe at getahead dot ltd dot uk]
056: */
057: public class DwrSpringServlet extends HttpServlet {
058: /**
059: * Setter for use by the Spring IoC container to tell us what Configurators
060: * exist for us to configure ourselves.
061: * @param configurators
062: */
063: public void setConfigurators(List<Configurator> configurators) {
064: this .configurators = configurators;
065: }
066:
067: /**
068: * Do we prefix the list of Configurators with a default to read the system
069: * dwr.xml file?
070: * @param includeDefaultConfig the includeDefaultConfig to set
071: */
072: public void setIncludeDefaultConfig(boolean includeDefaultConfig) {
073: this .includeDefaultConfig = includeDefaultConfig;
074: }
075:
076: /**
077: * Use provided application context rather than the default.
078: *
079: * @param applicationContext
080: */
081: public void setApplicationContext(
082: ApplicationContext applicationContext) {
083: this .applicationContext = applicationContext;
084: }
085:
086: /* (non-Javadoc)
087: * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
088: */
089: @Override
090: public void init(ServletConfig servletConfig)
091: throws ServletException {
092: super .init(servletConfig);
093: ServletContext servletContext = servletConfig
094: .getServletContext();
095:
096: try {
097: ApplicationContext appContext = getApplicationContext(servletContext);
098:
099: container = new SpringContainer();
100: container.setBeanFactory(appContext);
101: ContainerUtil.setupDefaultContainer(container,
102: servletConfig);
103:
104: StartupUtil.initContainerBeans(servletConfig,
105: servletContext, container);
106: webContextBuilder = container
107: .getBean(WebContextBuilder.class);
108:
109: ContainerUtil.prepareForWebContextFilter(servletContext,
110: servletConfig, container, webContextBuilder, this );
111: ContainerUtil.publishContainer(container, servletConfig);
112:
113: // retrieve the configurators from Spring (loaded by the ContextLoaderListener)
114: try {
115: configurators
116: .add((Configurator) appContext
117: .getBean(DwrNamespaceHandler.DEFAULT_SPRING_CONFIGURATOR_ID));
118: } catch (NoSuchBeanDefinitionException ex) {
119: throw new ServletException(
120: "No DWR configuration was found in your application context, make sure to define one",
121: ex);
122: }
123:
124: if (includeDefaultConfig) {
125: ContainerUtil.configureFromSystemDwrXml(container);
126: }
127:
128: ContainerUtil.configureFromInitParams(container,
129: servletConfig);
130: ContainerUtil.configure(container, configurators);
131: } catch (Exception ex) {
132: log.fatal("init failed", ex);
133: throw new ServletException(ex);
134: } finally {
135: webContextBuilder.unset();
136: }
137: }
138:
139: /**
140: * Allow easy override when retrieving the application context.
141: *
142: * @param servletContext
143: * @return the default application context.
144: */
145: protected ApplicationContext getApplicationContext(
146: ServletContext servletContext) {
147: return this .applicationContext == null ? WebApplicationContextUtils
148: .getRequiredWebApplicationContext(servletContext)
149: : this .applicationContext;
150: }
151:
152: /* (non-Javadoc)
153: * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
154: */
155: @Override
156: protected void doGet(HttpServletRequest req,
157: HttpServletResponse resp) throws IOException,
158: ServletException {
159: doPost(req, resp);
160: }
161:
162: /* (non-Javadoc)
163: * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
164: */
165: @Override
166: protected void doPost(HttpServletRequest request,
167: HttpServletResponse response) throws IOException,
168: ServletException {
169: try {
170: webContextBuilder.set(request, response,
171: getServletConfig(), getServletContext(), container);
172:
173: UrlProcessor processor = container
174: .getBean(UrlProcessor.class);
175: processor.handle(request, response);
176: } finally {
177: webContextBuilder.unset();
178: }
179: }
180:
181: /**
182: * The application context that has the configuration. If null,
183: * the default application context will be used.
184: */
185: private ApplicationContext applicationContext = null;
186:
187: /**
188: * DWRs IoC container (that passes stuff to Spring in this case)
189: */
190: private SpringContainer container;
191:
192: /**
193: * The WebContext that keeps http objects local to a thread
194: */
195: protected WebContextBuilder webContextBuilder;
196:
197: /**
198: * Do we prefix the list of Configurators with a default to read the system
199: * dwr.xml file?
200: */
201: private boolean includeDefaultConfig = true;
202:
203: /**
204: * What Configurators exist for us to configure ourselves.
205: */
206: private List<Configurator> configurators = new ArrayList<Configurator>();
207:
208: /**
209: * The log stream
210: */
211: private static final Log log = LogFactory
212: .getLog(DwrSpringServlet.class);
213: }
|