PageRenderTime 82ms CodeModel.GetById 56ms RepoModel.GetById 1ms app.codeStats 0ms

/extras/guice/src/main/java/org/atmosphere/guice/AtmosphereGuiceServlet.java

https://github.com/ayush/atmosphere
Java | 183 lines | 56 code | 17 blank | 110 comment | 6 complexity | 9f50353e738eac8a4f63ce1ca639c109 MD5 | raw file
  1. /*
  2. * Copyright 2011 Jeanfrancois Arcand
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. /*
  17. *
  18. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  19. *
  20. * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
  21. *
  22. * The contents of this file are subject to the terms of either the GNU
  23. * General Public License Version 2 only ("GPL") or the Common Development
  24. * and Distribution License("CDDL") (collectively, the "License"). You
  25. * may not use this file except in compliance with the License. You can obtain
  26. * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html
  27. * or jersey/legal/LICENSE.txt. See the License for the specific
  28. * language governing permissions and limitations under the License.
  29. *
  30. * When distributing the software, include this License Header Notice in each
  31. * file and include the License file at jersey/legal/LICENSE.txt.
  32. * Sun designates this particular file as subject to the "Classpath" exception
  33. * as provided by Sun in the GPL Version 2 section of the License file that
  34. * accompanied this code. If applicable, add the following below the License
  35. * Header, with the fields enclosed by brackets [] replaced by your own
  36. * identifying information: "Portions Copyrighted [year]
  37. * [name of copyright owner]"
  38. *
  39. * Contributor(s):
  40. *
  41. * If you wish your version of this file to be governed by only the CDDL or
  42. * only the GPL Version 2, indicate your decision by adding "[Contributor]
  43. * elects to include this software in this distribution under the [CDDL or GPL
  44. * Version 2] license." If you don't indicate a single choice of license, a
  45. * recipient has the option to distribute your version of this file under
  46. * either the CDDL, the GPL Version 2 or to extend the choice of license to
  47. * its licensees as provided above. However, if you add GPL Version 2 code
  48. * and therefore, elected the GPL Version 2 license, then the option applies
  49. * only if the new code is made subject to such option by the copyright
  50. * holder.
  51. */
  52. package org.atmosphere.guice;
  53. import com.google.inject.Injector;
  54. import com.google.inject.Key;
  55. import com.google.inject.TypeLiteral;
  56. import com.google.inject.name.Names;
  57. import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
  58. import org.atmosphere.cpr.AtmosphereServlet;
  59. import org.atmosphere.handler.ReflectorServletProcessor;
  60. import org.slf4j.Logger;
  61. import org.slf4j.LoggerFactory;
  62. import javax.servlet.ServletConfig;
  63. import javax.servlet.ServletException;
  64. import java.util.Map;
  65. /**
  66. * Google Guice Integration. To use it, just do in web.xml:
  67. *
  68. * <blockquote><code>
  69. &lt;web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
  70. xmlns:j2ee = "http://java.sun.com/xml/ns/j2ee"
  71. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  72. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
  73. &lt;listener&gt;
  74. &lt;listener-class&gt;org.atmosphere.samples.guice.GuiceChatConfig&lt;/listener-class&gt;
  75. &lt;/listener&gt;
  76. &lt;description&gt;Atmosphere Chat&lt;/description&gt;
  77. &lt;display-name&gt;Atmosphere Chat&lt;/display-name&gt;
  78. &lt;servlet&gt;
  79. &lt;description&gt;AtmosphereServlet&lt;/description&gt;
  80. &lt;servlet-name&gt;AtmosphereServlet&lt;/servlet-name&gt;
  81. &lt;servlet-class&gt;org.atmosphere.guice.AtmosphereGuiceServlet&lt;/servlet-class&gt;
  82. &lt;load-on-startup&gt;0&lt;/load-on-startup&gt;
  83. &lt;/servlet&gt;
  84. &lt;servlet-mapping&gt;
  85. &lt;servlet-name&gt;AtmosphereServlet&lt;/servlet-name&gt;
  86. &lt;url-pattern&gt;/chat/*&lt;/url-pattern&gt;
  87. &lt;/servlet-mapping&gt;
  88. &lt;/web-app&gt;
  89. and then
  90. public class GuiceConfig extends GuiceServletContextListener {
  91. @Override
  92. protected Injector getInjector() {
  93. return Guice.createInjector(new ServletModule() {
  94. @Override
  95. protected void configureServlets() {
  96. bind(PubSubTest.class);
  97. bind(new TypeLiteral&lt;Map&lt;String, String&gt;&gt;() {
  98. }).annotatedWith(Names.named(AtmosphereGuiceServlet.JERSEY_PROPERTIES)).toInstance(
  99. Collections.&lt;String, String>emptyMap());
  100. }
  101. });
  102. }
  103. }
  104. </code></blockquote>
  105. *
  106. * @author Jeanfrancois Arcand
  107. * @author Richard Wallace
  108. */
  109. public class AtmosphereGuiceServlet extends AtmosphereServlet {
  110. private static final Logger logger = LoggerFactory.getLogger(AtmosphereGuiceServlet.class);
  111. public static final String JERSEY_PROPERTIES = AtmosphereGuiceServlet.class.getName() + ".properties";
  112. private static final String GUICE_FILTER = "com.google.inject.servlet.GuiceFilter";
  113. private boolean guiceInstalled = false;
  114. /**
  115. * Install Guice event if other extension has been already installed.
  116. * @param sc {@link javax.servlet.ServletConfig}
  117. * @throws ServletException
  118. */
  119. @Override
  120. protected void loadConfiguration(ServletConfig sc) throws ServletException {
  121. super.loadConfiguration(sc);
  122. if (!guiceInstalled) {
  123. detectSupportedFramework(sc);
  124. }
  125. }
  126. /**
  127. * Auto-detect Jersey when no atmosphere.xml file are specified.
  128. *
  129. * @param sc {@link javax.servlet.ServletConfig}
  130. * @return true if Jersey classes are detected
  131. */
  132. @Override
  133. protected boolean detectSupportedFramework(ServletConfig sc) {
  134. Injector injector = (Injector) config.getServletContext().getAttribute(Injector.class.getName());
  135. GuiceContainer guiceServlet = injector.getInstance(GuiceContainer.class);
  136. setUseStreamForFlushingComments(false);
  137. ReflectorServletProcessor rsp = new ReflectorServletProcessor();
  138. setDefaultBroadcasterClassName(JERSEY_BROADCASTER);
  139. setUseStreamForFlushingComments(true);
  140. rsp.setServlet(guiceServlet);
  141. rsp.setFilterClassName(GUICE_FILTER);
  142. getAtmosphereConfig().setSupportSession(false);
  143. String mapping = sc.getInitParameter(PROPERTY_SERVLET_MAPPING);
  144. if (mapping == null) {
  145. mapping = "/*";
  146. }
  147. try {
  148. Map<String, String> props = injector.getInstance(
  149. Key.get(new TypeLiteral<Map<String, String>>() {},Names.named(JERSEY_PROPERTIES)));
  150. if (props != null) {
  151. for (String p : props.keySet()) {
  152. addInitParameter(p, props.get(p));
  153. }
  154. }
  155. } catch (Exception ex) {
  156. // Do not fail
  157. logger.debug("failed to add Jersey init parameters to Atmosphere servlet", ex);
  158. }
  159. addAtmosphereHandler(mapping, rsp);
  160. guiceInstalled = true;
  161. return true;
  162. }
  163. }