PageRenderTime 3006ms CodeModel.GetById 36ms RepoModel.GetById 5ms app.codeStats 0ms

/contribs/jersey-guice/src/main/java/com/sun/jersey/guice/spi/container/servlet/package-info.java

https://github.com/frodooooo39/jersey
Java | 180 lines | 1 code | 1 blank | 178 comment | 0 complexity | 2dceda04d7b16e3e5e178b1ad27b0797 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * The contents of this file are subject to the terms of either the GNU
  7. * General Public License Version 2 only ("GPL") or the Common Development
  8. * and Distribution License("CDDL") (collectively, the "License"). You
  9. * may not use this file except in compliance with the License. You can
  10. * obtain a copy of the License at
  11. * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  12. * or packager/legal/LICENSE.txt. See the License for the specific
  13. * language governing permissions and limitations under the License.
  14. *
  15. * When distributing the software, include this License Header Notice in each
  16. * file and include the License file at packager/legal/LICENSE.txt.
  17. *
  18. * GPL Classpath Exception:
  19. * Oracle designates this particular file as subject to the "Classpath"
  20. * exception as provided by Oracle in the GPL Version 2 section of the License
  21. * file that accompanied this code.
  22. *
  23. * Modifications:
  24. * If applicable, add the following below the License Header, with the fields
  25. * enclosed by brackets [] replaced by your own identifying information:
  26. * "Portions Copyright [year] [name of copyright owner]"
  27. *
  28. * Contributor(s):
  29. * If you wish your version of this file to be governed by only the CDDL or
  30. * only the GPL Version 2, indicate your decision by adding "[Contributor]
  31. * elects to include this software in this distribution under the [CDDL or GPL
  32. * Version 2] license." If you don't indicate a single choice of license, a
  33. * recipient has the option to distribute your version of this file under
  34. * either the CDDL, the GPL Version 2 or to extend the choice of license to
  35. * its licensees as provided above. However, if you add GPL Version 2 code
  36. * and therefore, elected the GPL Version 2 license, then the option applies
  37. * only if the new code is made subject to such option by the copyright
  38. * holder.
  39. */
  40. /**
  41. * Provides support for Guice-based Web applications.
  42. * <p>
  43. * Guice support is enabled by referencing the Guice filter
  44. * {@link com.google.inject.servlet.GuiceFilter} and an application
  45. * specific {@link javax.servlet.ServletContextListener} that extends from
  46. * {@link com.google.inject.servlet.GuiceServletContextListener} in the web.xml.
  47. * For example, the web.xml may be as follows:
  48. * <blockquote><pre>
  49. * &lt;web-app&gt;
  50. * &lt;listener&gt;
  51. * &lt;listener-class&gt;foo.MyGuiceConfig&lt;/listener-class&gt;
  52. * &lt;/listener&gt;
  53. * &lt;filter&gt;
  54. * &lt;filter-name&gt;Guice Filter&lt;/filter-name&gt;
  55. * &lt;filter-class&gt;com.google.inject.servlet.GuiceFilter&lt;/filter-class&gt;
  56. * &lt;/filter&gt;
  57. * &lt;filter-mapping&gt;
  58. * &lt;filter-name>Guice Filter&lt;/filter-name&gt;
  59. * &lt;url-pattern>/*&lt;/url-pattern&gt;
  60. * &lt;/filter-mapping&gt;
  61. * &lt;/web-app&gt;
  62. * </blockquote></pre>
  63. * and the application specific servlet context listener may be as follows:
  64. * <blockquote><pre>
  65. * package foo;
  66. *
  67. * import com.google.inject.Guice;
  68. * import com.google.inject.Injector;
  69. * import com.google.inject.servlet.GuiceServletContextListener;
  70. * import com.google.inject.servlet.ServletModule;
  71. * import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
  72. * import foo.GuiceResource
  73. *
  74. * public class MyGuiceConfig extends GuiceServletContextListener {
  75. *
  76. * &#64;Override
  77. * protected Injector getInjector() {
  78. * return Guice.createInjector(new JerseyServletModule() {
  79. *
  80. * &#64;Override
  81. * protected void configureServlets() {
  82. * bind(GuiceResource.class);
  83. *
  84. * serve("/*").with(GuiceContainer.class);
  85. * }
  86. * }
  87. * });
  88. * }
  89. * }
  90. * </blockquote></pre>
  91. * Notice that one class <code>GuiceResource</code> is bound and the
  92. * {@link com.sun.jersey.guice.spi.container.servlet.GuiceContainer} is
  93. * declared in the <code>serve</code> method. A instance of
  94. * module {@link com.sun.jersey.guice.JerseyServletModule} is created. This
  95. * module extends from {@link com.google.inject.servlet.ServletModule} and
  96. * provides JAX-RS and Jersey bindings.
  97. * <p>
  98. * Instances of
  99. * <code>GuiceResource</code> will be managed according to the scope declared
  100. * using Guice defined scopes. For example the <code>GuiceResource</code>
  101. * could be as follows:
  102. * <blockquote><pre>
  103. * &#64;Path("bound/perrequest")
  104. * &#64;RequestScoped
  105. * public static class GuiceResource {
  106. *
  107. * &#64;QueryParam("x") String x;
  108. *
  109. * &#64;GET
  110. * &#64;Produces("text/plain")
  111. * public String getIt() {
  112. * return "Hello From Guice: " + x;
  113. * }
  114. * }
  115. * </blockquote></pre>
  116. * <p>
  117. * Any root resource or provider classes bound by Guice
  118. * will be automatically registered. It is possible to intermix Guice and
  119. * non-Guice registration of classes by additionally using the normal
  120. * Jersey-based registration mechanisms in the servlet context listener
  121. * implementation. For example:
  122. * <blockquote><pre>
  123. * package foo;
  124. *
  125. * import com.google.inject.Guice;
  126. * import com.google.inject.Injector;
  127. * import com.google.inject.servlet.GuiceServletContextListener;
  128. * import com.google.inject.servlet.ServletModule;
  129. * import com.sun.jersey.api.core.PackagesResourceConfig;
  130. * import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
  131. * import foo.GuiceResource
  132. * import java.util.HashMap;
  133. * import java.util.Map;
  134. *
  135. * public class GuiceServletConfig extends GuiceServletContextListener {
  136. *
  137. * &#64;Override
  138. * protected Injector getInjector() {
  139. * return Guice.createInjector(new JerseyServletModule() {
  140. *
  141. * &#64;Override
  142. * protected void configureServlets() {
  143. * bind(GuiceResource.class);
  144. *
  145. * Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
  146. * params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "unbound");
  147. * serve("/*").with(GuiceContainer.class, params);
  148. * }
  149. * }
  150. * });
  151. * }
  152. * }
  153. * </blockquote></pre>
  154. * <p>
  155. * Any root resource or provider classes found in the package <code>unbound</code>
  156. * or sub-packages of will be registered whether they be Guice-bound nor not.
  157. * <p>
  158. * Sometimes it is convenient for developers not to explicitly bind a
  159. * resource or provider, let Guice instantiate, and let Jersey manage
  160. * the life-cycle. This behavior can be enabled for a resource or
  161. * provider class as follows:
  162. * <ol>
  163. * <li>a class constructor is annotated with {@link com.google.inject.Inject};
  164. * <li>the class is not explicitly bound in Guice; and
  165. * <li>the class is registered using a Jersey based registration mechanism,
  166. * for example using package scanning registration.
  167. * </ol>
  168. * <p>
  169. * In other cases it is convenient to let Jersey instantiate and manage
  170. * the life-cycle and let Guice perform injection. This behavior can be
  171. * enabled for a resource or provider class as follows:
  172. * <ol>
  173. * <li>a field or method is annotated with {@link com.google.inject.Inject};
  174. * <li>the class is not explicitly bound in Guice; and
  175. * <li>the class is registered using a Jersey based registration mechanism,
  176. * for example using package scanning registration.
  177. * </ol>
  178. */
  179. package com.sun.jersey.guice.spi.container.servlet;