KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > servlet > DwrServlet


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.servlet;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.servlet.ServletConfig JavaDoc;
21 import javax.servlet.ServletContext JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServlet JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.directwebremoting.Container;
28 import org.directwebremoting.WebContextFactory.WebContextBuilder;
29 import org.directwebremoting.extend.ServerLoadMonitor;
30 import org.directwebremoting.impl.ContainerUtil;
31 import org.directwebremoting.impl.DefaultContainer;
32 import org.directwebremoting.impl.StartupUtil;
33 import org.directwebremoting.util.Logger;
34 import org.directwebremoting.util.ServletLoggingOutput;
35
36 /**
37  * This is the main servlet that handles all the requests to DWR.
38  * <p>It is on the large side because it can't use technologies like JSPs etc
39  * since it all needs to be deployed in a single jar file, and while it might be
40  * possible to integrate Velocity or similar I think simplicity is more
41  * important, and there are only 2 real pages both script heavy in this servlet
42  * anyway.</p>
43  * <p>There are 5 things to do, in the order that you come across them:</p>
44  * <ul>
45  * <li>The index test page that points at the classes</li>
46  * <li>The class test page that lets you execute methods</li>
47  * <li>The interface javascript that uses the engine to send requests</li>
48  * <li>The engine javascript to form the iframe request and process replies</li>
49  * <li>The exec 'page' that executes the method and returns data to the iframe</li>
50  * </ul>
51  * @author Joe Walker [joe at getahead dot ltd dot uk]
52  * @noinspection RefusedBequest
53  */

54 public class DwrServlet extends HttpServlet JavaDoc
55 {
56     /* (non-Javadoc)
57      * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
58      */

59     public void init(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc
60     {
61         super.init(servletConfig);
62         ServletContext JavaDoc servletContext = servletConfig.getServletContext();
63
64         try
65         {
66             // setupLogging() only needed for servlet logging if commons-logging is unavailable
67
// logStartup() just outputs some version numbers
68
StartupUtil.setupLogging(servletConfig, this);
69             StartupUtil.logStartup(servletConfig);
70
71             // create and setup a DefaultContainer
72
container = ContainerUtil.createDefaultContainer(servletConfig);
73             ContainerUtil.setupDefaultContainer(container, servletConfig);
74
75             webContextBuilder = StartupUtil.initWebContext(servletConfig, servletContext, container);
76             StartupUtil.initServerContext(servletConfig, servletContext, container);
77
78             ContainerUtil.prepareForWebContextFilter(servletContext, servletConfig, container, webContextBuilder, this);
79             ContainerUtil.configureContainerFully(container, servletConfig);
80             ContainerUtil.publishContainer(container, servletConfig);
81         }
82         catch (ExceptionInInitializerError JavaDoc ex)
83         {
84             log.fatal("ExceptionInInitializerError. Nested exception:", ex.getException());
85             throw new ServletException JavaDoc(ex);
86         }
87         catch (Exception JavaDoc ex)
88         {
89             log.fatal("DwrServlet.init() failed", ex);
90             throw new ServletException JavaDoc(ex);
91         }
92         finally
93         {
94             if (webContextBuilder != null)
95             {
96                 webContextBuilder.unset();
97             }
98
99             ServletLoggingOutput.unsetExecutionContext();
100         }
101     }
102
103     /* (non-Javadoc)
104      * @see javax.servlet.GenericServlet#destroy()
105      */

106     public void destroy()
107     {
108         shutdown();
109         super.destroy();
110     }
111
112     /**
113      * Kill all comet polls.
114      * <p>Technically a servlet engine ought to call this only when all the
115      * threads are already removed, however at least Tomcat doesn't do this
116      * properly (it waits for a while and then calls destroy anyway).
117      * <p>It would be good if we could get {@link #destroy()} to call this
118      * method however destroy() is only called once all threads are done so it's
119      * too late.
120      */

121     public void shutdown()
122     {
123         ServerLoadMonitor monitor = (ServerLoadMonitor) container.getBean(ServerLoadMonitor.class.getName());
124         monitor.shutdown();
125     }
126
127     /* (non-Javadoc)
128      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
129      */

130     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws IOException JavaDoc, ServletException JavaDoc
131     {
132         doPost(req, resp);
133     }
134
135     /* (non-Javadoc)
136      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
137      */

138     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
139     {
140         try
141         {
142             webContextBuilder.set(request, response, getServletConfig(), getServletContext(), container);
143             ServletLoggingOutput.setExecutionContext(this);
144
145             UrlProcessor processor = (UrlProcessor) container.getBean(UrlProcessor.class.getName());
146             processor.handle(request, response);
147         }
148         finally
149         {
150             webContextBuilder.unset();
151             ServletLoggingOutput.unsetExecutionContext();
152         }
153     }
154
155     /**
156      * Accessor for the DWR IoC container.
157      * @return DWR's IoC container
158      */

159     public Container getContainer()
160     {
161         return container;
162     }
163
164     /**
165      * Our IoC container
166      */

167     private DefaultContainer container;
168
169     /**
170      * The WebContext that keeps http objects local to a thread
171      */

172     private WebContextBuilder webContextBuilder;
173
174     /**
175      * The log stream
176      */

177     public static final Logger log = Logger.getLogger(DwrServlet.class);
178 }
179
Popular Tags