PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/dwr/java/src/ca/flop/jpublish/dwr/DWRProcessor.java

http://jpublish.googlecode.com/
Java | 188 lines | 108 code | 29 blank | 51 comment | 9 complexity | 8736ca63f678e548a68f15195ec9b171 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /*
  2. *
  3. * Copyright 2007 Florin T.PATRASCU
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package ca.flop.jpublish.dwr;
  19. import com.anthonyeden.lib.config.Configuration;
  20. import com.atlassian.util.profiling.UtilTimerStack;
  21. import org.directwebremoting.WebContextFactory;
  22. import org.directwebremoting.extend.ServerLoadMonitor;
  23. import org.directwebremoting.impl.ContainerUtil;
  24. import org.directwebremoting.impl.DefaultContainer;
  25. import org.directwebremoting.impl.StartupUtil;
  26. import org.directwebremoting.servlet.PathConstants;
  27. import org.directwebremoting.util.Logger;
  28. import org.directwebremoting.util.ServletLoggingOutput;
  29. import org.directwebremoting.util.VersionUtil;
  30. import org.jpublish.JPublishContext;
  31. import org.jpublish.SiteContext;
  32. import javax.servlet.ServletConfig;
  33. import javax.servlet.ServletException;
  34. import javax.servlet.http.HttpServletRequest;
  35. import javax.servlet.http.HttpServletResponse;
  36. import java.io.IOException;
  37. import java.util.Iterator;
  38. import java.util.Map;
  39. import java.util.Set;
  40. /**
  41. * @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
  42. * @since $Revision$ (created: Oct 1, 2006 3:24:22 PM)
  43. */
  44. public class DWRProcessor {
  45. public static final String EMPTY_STRING = "";
  46. /**
  47. * Our IoC container
  48. */
  49. protected DefaultContainer container;
  50. /**
  51. * The processor will actually handle the http requests
  52. */
  53. protected DWRUrlProcessor processor;
  54. /**
  55. * The WebContext that keeps http objects local to a thread
  56. */
  57. protected WebContextFactory.WebContextBuilder webContextBuilder;
  58. /**
  59. * The log stream
  60. */
  61. private static final Logger log = Logger.getLogger(DWRProcessor.class);
  62. /* (non-Javadoc)
  63. * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
  64. */
  65. private SiteContext site;
  66. private FakeServletConfig fakeServletConfig;
  67. private static final String DWR_PROFIILING_PREFIX = " dwr> ";
  68. public void init(SiteContext site) throws ServletException {
  69. this.site = site;
  70. fakeServletConfig = new FakeServletConfig(site.getServletContext());
  71. fakeServletConfig.setInitParameter("config", (String) site.getAttribute("dwrConfigFile"));
  72. Map dwrServletInitParamsMap = (Map) site.getAttribute("dwrServletInitParamsMap");
  73. if (dwrServletInitParamsMap != null) {
  74. Set keys = dwrServletInitParamsMap.keySet();
  75. for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
  76. String key = (String) iterator.next();
  77. fakeServletConfig.setInitParameter(key, (String) dwrServletInitParamsMap.get(key));
  78. }
  79. }
  80. try {
  81. // Setup logging
  82. ServletLoggingOutput.setExecutionContext(site.getJPublishServlet());
  83. ServletConfig config = site.getServletConfig();
  84. String logLevel = config.getInitParameter(ContainerUtil.INIT_LOGLEVEL);
  85. if (logLevel != null) {
  86. ServletLoggingOutput.setLevel(logLevel);
  87. }
  88. log.info("DWR Version " + VersionUtil.getVersion() + " starting."); //$NON-NLS-1$ //$NON-NLS-2$
  89. container = ContainerUtil.createDefaultContainer(fakeServletConfig);
  90. ContainerUtil.setupDefaultContainer(container, fakeServletConfig);
  91. // overwrite the interface and the test handler with a clone of Joe's code but where
  92. // the requestPath will obey to the dwrPrefix. I have to find a better solution [florin]
  93. container.addParameter(PathConstants.URL_PREFIX + "/interface/", DWRInterfaceHandler.class.getName());
  94. container.addParameter(PathConstants.URL_PREFIX + "/test/", DWRTestHandler.class.getName());
  95. container.addParameter(DWRUrlProcessor.class.getName(), DWRUrlProcessor.class.getName());
  96. container.setupFinished();
  97. webContextBuilder =
  98. StartupUtil.initWebContext(fakeServletConfig,
  99. site.getServletContext(), container);
  100. StartupUtil.initServerContext(fakeServletConfig, site.getServletContext(), container);
  101. ContainerUtil.prepareForWebContextFilter(
  102. site.getServletContext(), fakeServletConfig,
  103. container, webContextBuilder, site.getJPublishServlet());
  104. ContainerUtil.configureContainerFully(container, fakeServletConfig);
  105. ContainerUtil.publishContainer(container, fakeServletConfig);
  106. }
  107. catch (ExceptionInInitializerError ex) {
  108. log.fatal("ExceptionInInitializerError. Nested exception:", ex.getException());
  109. throw new ServletException(ex);
  110. }
  111. catch (Exception ex) {
  112. log.fatal("DwrServlet.init() failed", ex);
  113. throw new ServletException(ex);
  114. }
  115. finally {
  116. if (webContextBuilder != null) {
  117. webContextBuilder.unset();
  118. }
  119. ServletLoggingOutput.unsetExecutionContext();
  120. }
  121. }
  122. /* (non-Javadoc)
  123. * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
  124. */
  125. protected void execute(JPublishContext context, Configuration configuration) throws IOException, ServletException {
  126. HttpServletRequest request = (HttpServletRequest) context.get("request");
  127. HttpServletResponse response = (HttpServletResponse) context.get("response");
  128. String reqPathInfo = EMPTY_STRING;
  129. if (request != null) {
  130. reqPathInfo = request.getPathInfo();
  131. }
  132. UtilTimerStack.push(DWR_PROFIILING_PREFIX + reqPathInfo);
  133. try {
  134. webContextBuilder.set(
  135. request, response, fakeServletConfig, site.getServletContext(), container);
  136. ServletLoggingOutput.setExecutionContext(site.getJPublishServlet());
  137. DWRUrlProcessor processor = (DWRUrlProcessor) container.getBean(DWRUrlProcessor.class.getName());
  138. processor.handle(context, configuration);
  139. }
  140. finally {
  141. webContextBuilder.unset();
  142. ServletLoggingOutput.unsetExecutionContext();
  143. UtilTimerStack.pop(DWR_PROFIILING_PREFIX + reqPathInfo);
  144. }
  145. }
  146. /**
  147. * Kill all comet polls.
  148. * <p>Technically a servlet engine ought to call this only when all the
  149. * threads are already removed, however at least Tomcat doesn't do this
  150. * properly (it waits for a while and then calls destroy anyway).
  151. * <p>It would be good if we could get destroy() to call this
  152. * method however destroy() is only called once all threads are done so it's
  153. * too late.
  154. */
  155. public void shutdown() {
  156. ServerLoadMonitor monitor = (ServerLoadMonitor) container.getBean(ServerLoadMonitor.class.getName());
  157. monitor.shutdown();
  158. }
  159. }