PageRenderTime 588ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/serverside/guice/main/java/org/directwebremoting/guice/AbstractDwrGuiceServletContextListener.java

http://github.com/burris/dwr
Java | 134 lines | 60 code | 23 blank | 51 comment | 3 complexity | ce75f804e9734cc86e6cab2d10916188 MD5 | raw file
  1. /*
  2. * Copyright 2007 Tim Peierls
  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.guice;
  17. import static org.directwebremoting.guice.DwrGuiceUtil.INJECTOR;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import javax.servlet.ServletContext;
  21. import javax.servlet.ServletContextEvent;
  22. import javax.servlet.ServletContextListener;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import static org.directwebremoting.guice.util.ContextCloseHandlers.newExceptionLoggingCloseableHandler;
  26. import com.google.inject.Injector;
  27. /**
  28. * Not for subclassing directly; this is a common base for two different approaches
  29. * to {@code Injector} creation and configuration, {@link DwrGuiceServletContextListener}
  30. * and {@link CustomInjectorServletContextListener}.
  31. * @author Tim Peierls [tim at peierls dot net]
  32. */
  33. public abstract class AbstractDwrGuiceServletContextListener extends AbstractDwrModule implements ServletContextListener
  34. {
  35. /* (non-Javadoc)
  36. * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
  37. */
  38. public void contextInitialized(ServletContextEvent servletContextEvent)
  39. {
  40. final ServletContext servletContext = servletContextEvent.getServletContext();
  41. DwrGuiceUtil.withServletContext(servletContext, new Runnable()
  42. {
  43. public void run()
  44. {
  45. publishInjector(servletContext, createInjector());
  46. }
  47. });
  48. }
  49. /* (non-Javadoc)
  50. * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
  51. */
  52. public void contextDestroyed(ServletContextEvent servletContextEvent)
  53. {
  54. List<Exception> exceptions = new ArrayList<Exception>();
  55. DwrScopes.GLOBAL.closeAll(newExceptionLoggingCloseableHandler(exceptions));
  56. for (Exception e : exceptions)
  57. {
  58. log.warn("During context destroy, closing GLOBAL-scoped Closeables: " + e, e);
  59. }
  60. }
  61. /**
  62. * Abstract subclasses define either this method or {@link #configure},
  63. * but not both.
  64. */
  65. protected abstract Injector createInjector();
  66. /**
  67. * Abstract subclasses define either this method or {@link #createInjector},
  68. * but not both.
  69. */
  70. @Override
  71. protected abstract void configure();
  72. /**
  73. * Subclasses can use this during stage determination and binding to
  74. * read values from the current servlet context.
  75. */
  76. protected final ServletContext getServletContext()
  77. {
  78. return DwrGuiceUtil.getServletContext();
  79. }
  80. /**
  81. * Returns the Injector instance installed in the given ServletContext.
  82. * @param servletContext the servlet context from which to get the injector
  83. */
  84. protected static Injector getPublishedInjector(ServletContext servletContext)
  85. {
  86. Injector injector = (Injector) servletContext.getAttribute(INJECTOR);
  87. if (injector == null)
  88. {
  89. throw new IllegalStateException("Cannot find Injector in servlet context."
  90. + " You need to register a concrete extension of either "
  91. + DwrGuiceServletContextListener.class.getName()
  92. + " or "
  93. + CustomInjectorServletContextListener.class.getName()
  94. + " as a servlet context listener in your web.xml.");
  95. }
  96. return injector;
  97. }
  98. /**
  99. * Stores the Injector instance in the given ServletContext.
  100. * @param servletContext the servlet context in which to store the injector
  101. * @param injector the injector to store
  102. */
  103. protected static void publishInjector(ServletContext servletContext, Injector injector)
  104. {
  105. servletContext.setAttribute(INJECTOR, injector);
  106. }
  107. /**
  108. * The log stream
  109. */
  110. private static final Log log = LogFactory.getLog(AbstractDwrGuiceServletContextListener.class);
  111. }