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.jaxer.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.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.directwebremoting.WebContextFactory.WebContextBuilder;
030: import org.directwebremoting.extend.DwrConstants;
031: import org.directwebremoting.extend.ServerLoadMonitor;
032: import org.directwebremoting.impl.ContainerUtil;
033: import org.directwebremoting.impl.DwrXmlConfigurator;
034: import org.directwebremoting.impl.StartupUtil;
035: import org.directwebremoting.jaxer.impl.JaxerContainer;
036: import org.directwebremoting.servlet.UrlProcessor;
037:
038: /**
039: * This is the main servlet for the DWR/Jaxer integration.
040: * It handles all the requests to DWR from the Jaxer server. Currently this
041: * communication looks just like normal DWR, however as this project evolves
042: * it is likely that some of the processing done by {@link UrlProcessor} will
043: * be superseded by a protocol that takes advantage of a single connection.
044: * @author Joe Walker [joe at getahead dot ltd dot uk]
045: * @noinspection RefusedBequest
046: */
047: public class DwrJaxerServlet extends HttpServlet {
048: /* (non-Javadoc)
049: * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
050: */
051: @Override
052: public void init(ServletConfig servletConfig)
053: throws ServletException {
054: super .init(servletConfig);
055: ServletContext servletContext = servletConfig
056: .getServletContext();
057:
058: try {
059: ContainerUtil.resolveContainerAbstraction(container,
060: servletConfig);
061: container.setupFinished();
062:
063: StartupUtil.initContainerBeans(servletConfig,
064: servletContext, container);
065: webContextBuilder = container
066: .getBean(WebContextBuilder.class);
067:
068: ContainerUtil.prepareForWebContextFilter(servletContext,
069: servletConfig, container, webContextBuilder, this );
070:
071: DwrXmlConfigurator system = new DwrXmlConfigurator();
072: system.setClassResourceName(DwrConstants.FILE_DWR_XML);
073: system.configure(container);
074:
075: DwrXmlConfigurator custom = new DwrXmlConfigurator();
076: custom
077: .setClassResourceName("/org/directwebremoting/jaxer/dwr.xml");
078: custom.configure(container);
079:
080: ContainerUtil.publishContainer(container, servletConfig);
081: } catch (ExceptionInInitializerError ex) {
082: log.fatal("ExceptionInInitializerError. Nested exception:",
083: ex.getException());
084: throw new ServletException(ex);
085: } catch (Exception ex) {
086: log.fatal("DwrServlet.init() failed", ex);
087: throw new ServletException(ex);
088: } finally {
089: if (webContextBuilder != null) {
090: webContextBuilder.unset();
091: }
092: }
093: }
094:
095: /* (non-Javadoc)
096: * @see javax.servlet.GenericServlet#destroy()
097: */
098: @Override
099: public void destroy() {
100: shutdown();
101: super .destroy();
102: }
103:
104: /**
105: * Kill all comet polls.
106: * <p>Technically a servlet engine ought to call this only when all the
107: * threads are already removed, however at least Tomcat doesn't do this
108: * properly (it waits for a while and then calls destroy anyway).
109: * <p>It would be good if we could get {@link #destroy()} to call this
110: * method however destroy() is only called once all threads are done so it's
111: * too late.
112: */
113: public void shutdown() {
114: ServerLoadMonitor monitor = container
115: .getBean(ServerLoadMonitor.class);
116: monitor.shutdown();
117: }
118:
119: /* (non-Javadoc)
120: * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
121: */
122: @Override
123: protected void doGet(HttpServletRequest req,
124: HttpServletResponse resp) throws IOException,
125: ServletException {
126: doPost(req, resp);
127: }
128:
129: /* (non-Javadoc)
130: * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
131: */
132: @Override
133: protected void doPost(HttpServletRequest request,
134: HttpServletResponse response) throws IOException,
135: ServletException {
136: try {
137: webContextBuilder.set(request, response,
138: getServletConfig(), getServletContext(), container);
139:
140: UrlProcessor processor = container
141: .getBean(UrlProcessor.class);
142: processor.handle(request, response);
143: } finally {
144: webContextBuilder.unset();
145: }
146: }
147:
148: /**
149: * Our IoC container
150: */
151: private JaxerContainer container = new JaxerContainer();
152:
153: /**
154: * The WebContext that keeps http objects local to a thread
155: */
156: private WebContextBuilder webContextBuilder = null;
157:
158: /**
159: * The log stream
160: */
161: private static final Log log = LogFactory
162: .getLog(DwrJaxerServlet.class);
163: }
|