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.servlet;
017:
018: import java.io.IOException;
019:
020: import javax.servlet.ServletConfig;
021: import javax.servlet.ServletContext;
022: import javax.servlet.ServletException;
023: import javax.servlet.http.HttpServlet;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.commons.logging.Log;
029: import org.directwebremoting.Container;
030: import org.directwebremoting.WebContextFactory.WebContextBuilder;
031: import org.directwebremoting.extend.ServerLoadMonitor;
032: import org.directwebremoting.impl.ContainerUtil;
033: import org.directwebremoting.impl.StartupUtil;
034:
035: /**
036: * This is the main servlet that handles all the requests to DWR.
037: * <p>It is on the large side because it can't use technologies like JSPs etc
038: * since it all needs to be deployed in a single jar file, and while it might be
039: * possible to integrate Velocity or similar I think simplicity is more
040: * important, and there are only 2 real pages both script heavy in this servlet
041: * anyway.</p>
042: * <p>There are 5 things to do, in the order that you come across them:</p>
043: * <ul>
044: * <li>The index test page that points at the classes</li>
045: * <li>The class test page that lets you execute methods</li>
046: * <li>The interface javascript that uses the engine to send requests</li>
047: * <li>The engine javascript to form the iframe request and process replies</li>
048: * <li>The exec 'page' that executes the method and returns data to the iframe</li>
049: * </ul>
050: * @author Joe Walker [joe at getahead dot ltd dot uk]
051: * @noinspection RefusedBequest
052: */
053: public class DwrServlet extends HttpServlet {
054: /* (non-Javadoc)
055: * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
056: */
057: @Override
058: public void init(ServletConfig servletConfig)
059: throws ServletException {
060: super .init(servletConfig);
061: ServletContext servletContext = servletConfig
062: .getServletContext();
063:
064: try {
065: // setupLogging() only needed for servlet logging if commons-logging is unavailable
066: // logStartup() just outputs some version numbers
067: StartupUtil.logStartup(servletConfig);
068:
069: // create and setup a DefaultContainer
070: container = ContainerUtil
071: .createAndSetupDefaultContainer(servletConfig);
072:
073: StartupUtil.initContainerBeans(servletConfig,
074: servletContext, container);
075: webContextBuilder = container
076: .getBean(WebContextBuilder.class);
077:
078: ContainerUtil.prepareForWebContextFilter(servletContext,
079: servletConfig, container, webContextBuilder, this );
080: ContainerUtil.publishContainer(container, servletConfig);
081: ContainerUtil.configureContainerFully(container,
082: servletConfig);
083: } catch (ExceptionInInitializerError ex) {
084: log.fatal("ExceptionInInitializerError. Nested exception:",
085: ex.getException());
086: throw new ServletException(ex);
087: } catch (Exception ex) {
088: log.fatal("DwrServlet.init() failed", ex);
089: throw new ServletException(ex);
090: } finally {
091: if (webContextBuilder != null) {
092: webContextBuilder.unset();
093: }
094: }
095: }
096:
097: /* (non-Javadoc)
098: * @see javax.servlet.GenericServlet#destroy()
099: */
100: @Override
101: public void destroy() {
102: shutdown();
103: super .destroy();
104: }
105:
106: /**
107: * Kill all comet polls.
108: * <p>Technically a servlet engine ought to call this only when all the
109: * threads are already removed, however at least Tomcat doesn't do this
110: * properly (it waits for a while and then calls destroy anyway).
111: * <p>It would be good if we could get {@link #destroy()} to call this
112: * method however destroy() is only called once all threads are done so it's
113: * too late.
114: */
115: public void shutdown() {
116: ServerLoadMonitor monitor = container
117: .getBean(ServerLoadMonitor.class);
118: monitor.shutdown();
119: }
120:
121: /* (non-Javadoc)
122: * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
123: */
124: @Override
125: protected void doGet(HttpServletRequest req,
126: HttpServletResponse resp) throws IOException,
127: ServletException {
128: doPost(req, resp);
129: }
130:
131: /* (non-Javadoc)
132: * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
133: */
134: @Override
135: protected void doPost(HttpServletRequest request,
136: HttpServletResponse response) throws IOException,
137: ServletException {
138: try {
139: webContextBuilder.set(request, response,
140: getServletConfig(), getServletContext(), container);
141:
142: UrlProcessor processor = container
143: .getBean(UrlProcessor.class);
144: processor.handle(request, response);
145: } finally {
146: webContextBuilder.unset();
147: }
148: }
149:
150: /**
151: * Accessor for the DWR IoC container.
152: * @return DWR's IoC container
153: */
154: public Container getContainer() {
155: return container;
156: }
157:
158: /**
159: * Our IoC container
160: */
161: private Container container = null;
162:
163: /**
164: * The WebContext that keeps http objects local to a thread
165: */
166: private WebContextBuilder webContextBuilder = null;
167:
168: /**
169: * The log stream
170: */
171: private static final Log log = LogFactory.getLog(DwrServlet.class);
172: }
|