PageRenderTime 1019ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/serverside/guice/main/java/org/directwebremoting/guice/package-info.java

http://github.com/burris/dwr
Java | 223 lines | 1 code | 1 blank | 221 comment | 0 complexity | f847a8e051f0cf74632e3573bcd1163c MD5 | raw file
  1. /**
  2. * <p>
  3. * This package provides support for
  4. * <a href="http://code.google.com/p/google-guice/">Guice</a>
  5. * dependency injection in DWR-based web applications.
  6. * This documentation assumes you already understand Guice concepts.
  7. * </p>
  8. * <p>
  9. * To use this support minimally,
  10. * <ul>
  11. * <li> install a concrete extension of
  12. * {@link org.directwebremoting.guice.DwrGuiceServletContextListener}
  13. * as a {@code <listener>} in your web application's configuration
  14. * file ({@code web.xml}), </li>
  15. * <li> install {@link org.directwebremoting.guice.DwrGuiceServlet} for all
  16. * requests to {@code /dwr/*}. </li>
  17. * </ul>
  18. * For example:
  19. * </p>
  20. * <pre>
  21. * &lt;listener&gt;
  22. * &lt;listener-class&gt;org.myorg.myproj.MyServletContextListener&lt;/listener-class&gt;
  23. * &lt;/listener&gt;
  24. *
  25. * &lt;servlet&gt;
  26. * &lt;servlet-name&gt;dwr-invoker&lt;/servlet-name&gt;
  27. * &lt;servlet-class&gt;org.directwebremoting.guice.DwrGuiceServlet&lt;/servlet-class&gt;
  28. * &lt;/servlet&gt;
  29. *
  30. * &lt;servlet-mapping&gt;
  31. * &lt;servlet-name&gt;dwr-invoker&lt;/servlet-name&gt;
  32. * &lt;url-pattern&gt;/dwr/*&lt;/url-pattern&gt;
  33. * &lt;/servlet-mapping&gt;
  34. * </pre>
  35. * <p>
  36. * {@link org.directwebremoting.guice.DwrGuiceServletContextListener}
  37. * is also an abstract Guice module; it extends
  38. * {@link org.directwebremoting.guice.AbstractDwrModule},
  39. * which extends {@link org.directwebremoting.guice.util.AbstractModule},
  40. * which in turn extends Guice's {@link com.google.inject.AbstractModule}.
  41. * Your listener class must define
  42. * {@link org.directwebremoting.guice.DwrGuiceServletContextListener#configure configure};
  43. * this is where you do your Guice binding.
  44. * You can also put binding code in a separate class or classes with
  45. * {@link org.directwebremoting.guice.util.AbstractModule#install AbstractModule.install}.
  46. * </p>
  47. * <p>
  48. * Alternatively,
  49. * if you already have an existing {@link com.google.inject.Injector Injector} instance
  50. * with bindings from one or more {@link org.directwebremoting.guice.AbstractDwrModule}, use
  51. * {@link org.directwebremoting.guice.CustomInjectorServletContextListener}
  52. * by defining {@link org.directwebremoting.guice.CustomInjectorServletContextListener#createInjector createInjector}
  53. * to return your existing {@link com.google.inject.Injector Injector}.
  54. * </p>
  55. * <p>
  56. * To turn classes into remote DWR services, use the
  57. * {@link org.directwebremoting.guice.AbstractDwrModule#bindRemoted(Class) bindRemoted}
  58. * or
  59. * {@link org.directwebremoting.guice.AbstractDwrModule#bindRemotedAs(String,Class) bindRemotedAs}
  60. * methods. The binding instructs DWR to handle requests for the given type name (or script name,
  61. * if you use the second variant) by getting a fully-injected instance of the given class from the
  62. * Guice injector and calling the requested method on that instance.
  63. * The target type of these bindings can be an abstract class or interface
  64. * bound in the normal Guice way to a concrete class, instance, or provider;
  65. * in that case, only the methods defined on the abstract class or
  66. * interface are accessible on the remoted object, even if the implementing class has other public
  67. * methods. You can supply different bindings for different script names, including
  68. * using the same interface with different implementations for different script names,
  69. * or different interfaces for different script names mapping to the same implementation
  70. * type (assuming it implements both interfaces).
  71. * </p>
  72. * <p>
  73. * Alternatively, annotate your remoted classes with
  74. * {@link org.directwebremoting.annotations.RemoteProxy}, and remotely
  75. * callable methods with {@link org.directwebremoting.annotations.RemoteMethod},
  76. * using {@code creator=}{@link org.directwebremoting.guice.GuiceCreator}.
  77. * When a request comes in for the remoted type, it is retrieved from the Guice injector
  78. * using your bindings for that type.
  79. * </p>
  80. * <p>
  81. * In either case, your data transfer objects should be bound with
  82. * {@link org.directwebremoting.annotations.DataTransferObject}, and the
  83. * remotely accessible properties with
  84. * {@link org.directwebremoting.annotations.RemoteProperty}.
  85. * Use {@link org.directwebremoting.guice.AbstractDwrModule#bindAnnotatedClasses bindAnnotatedClasses}
  86. * to tell DWR which classes need to be scanned for annotations.
  87. * </p>
  88. * <p>
  89. * You can bind a type or type pattern string to a custom converter with
  90. * {@link org.directwebremoting.guice.AbstractDwrModule#bindConversion(Class) bindConversion},
  91. * and you can put Ajax filters on scripts with
  92. * {@link org.directwebremoting.guice.AbstractDwrModule#bindFilter(String) bindFilter}.
  93. * Note, however, that you can achieve the effect of an Ajax filter more flexibly using
  94. * {@link org.directwebremoting.guice.util.AbstractModule#bindInterceptor(Class,Class[]) bindInterceptor}.
  95. * </p>
  96. * <p>
  97. * You can install your own DWR {@link org.directwebremoting.extend.Configurator} using
  98. * {@code bind(Configurator.class).toInstance(yourConfigurator)},
  99. * which then overrides any {@code dwr.xml} configuration.
  100. * You'll probably want to use a {@link org.directwebremoting.fluent.FluentConfigurator} for this purpose.
  101. * </p>
  102. * <p>
  103. * You can still configure DWR's settings via {@code <init-param>}
  104. * directives in {@code web.xml}, but usually there is no need to. Most DWR
  105. * settings can be set with
  106. * {@link org.directwebremoting.guice.AbstractDwrModule#bindParameter(ParamName) bindParameter}.
  107. * The {@link org.directwebremoting.guice.ParamName}
  108. * enum type lists the available parameters.
  109. * </p>
  110. * <p>
  111. * For creating your own scopes where the instance injected depends on some
  112. * run-time value, create a concrete extension of
  113. * {@link org.directwebremoting.guice.util.AbstractContextScope}.
  114. * </p>
  115. * <p>For example:</p>
  116. * <pre>
  117. * public final class MyServletContextListener extends DwrGuiceServletContextListener
  118. * {
  119. * protected void configure()
  120. * {
  121. * bindRemotedAs("Hello", HelloService.class)
  122. * .to(HelloServiceImpl.class)
  123. * .in(ApplicationScoped.class);
  124. *
  125. * bindInterceptor(
  126. * HelloService.class,
  127. * AuthenticationInterceptor.class);
  128. *
  129. * bindFilter("Hello")
  130. * .to(TraceFilter.class);
  131. *
  132. * bind(MessageService.class)
  133. * .to(MessageServiceImpl.class)
  134. * .in(ScriptSessionScoped.class);
  135. *
  136. * bindAnnotatedClasses(
  137. * DomainService.class, // @RemoteProxy(creator=GuiceCreator.class)/@RemoteMethod
  138. * HelloRecordImpl.class // @DataTransferObject/@RemoteProperty
  139. * );
  140. *
  141. * // When converting HelloRecord, use existing converter for HelloRecordImpl.
  142. * bindConversion(
  143. * HelloRecord.class,
  144. * HelloRecordImpl.class);
  145. *
  146. * bindConversion(DateTime.class)
  147. * .toInstance(DateTimeConverter.get("yyyy-MM-dd hh:mm a"));
  148. *
  149. * bind(Configurator.class).toInstance(new FluentConfigurator()
  150. * {
  151. * public void configure() {
  152. * String localTime = "localTime";
  153. * withConverterType(localTime, DateTimeConverter.class.getName());
  154. * withConverter(localTime, LocalTime.class.getName())
  155. * .addParam("format", "yyyy-MM-dd");
  156. * }
  157. * });
  158. *
  159. * bindParameter(DEBUG).to(true);
  160. * }
  161. * }
  162. * </pre>
  163. * <p>
  164. * This example illustrates:
  165. * <ul>
  166. * <li>two ways to define remoted objects,
  167. * <ol>
  168. * <li>calling {@link org.directwebremoting.guice.AbstractDwrModule#bindRemotedAs(String,Class) bindRemotedAs} and</li>
  169. * <li>annotating with {@link org.directwebremoting.annotations.RemoteProxy};</li>
  170. * </ol>
  171. * </li>
  172. * <li>two ways to define conversions,
  173. * <ol>
  174. * <li>using {@link org.directwebremoting.guice.AbstractDwrModule#bindConversion(Class) bindConversion} and</li>
  175. * <li>using a custom {@link org.directwebremoting.extend.Configurator};</li>
  176. * </ol>
  177. * </li>
  178. * <li>two ways to intercept remote method calls,
  179. * <ol>
  180. * <li>binding a MethodInterceptor with
  181. * {@link org.directwebremoting.guice.util.AbstractModule#bindInterceptor(Class,Class[]) bindInterceptor(Class<?>, Class<?>...)}
  182. * and</li>
  183. * <li>binding a script name to an {@link org.directwebremoting.AjaxFilter},</li>
  184. * </ol>
  185. * <li>how to register annotated classes at bind-time; and</li>
  186. * <li>how to set a DWR parameter
  187. * ({@link org.directwebremoting.guice.ParamName#DEBUG DEBUG}, in this case) at bind-time.</li>
  188. * </ul>
  189. * It does not use an {@code <init-param>} directive, and it doesn't have
  190. * a {@code dwr.xml}.
  191. * </p>
  192. * <p>
  193. * Note that because application scope is larger than script session scope,
  194. * {@code HelloServiceImpl} would have an injected constructor (not shown here)
  195. * taking a {@code Provider<MessageService>} rather than a plain {@code MessageService}.
  196. * </p>
  197. * <p>
  198. * There are several classes in this package with names that start with {@code Internal}.
  199. * These classes have to be public with a parameterless constructor so the non-Guicy DWR
  200. * machinery can create them. They are not meant to be used directly.
  201. * </p>
  202. * <p>
  203. * The classes that handle DWR scopes are modeled on the classes in the
  204. * {@code com.google.inject.servlet} package, but are independent of them.
  205. * You do <em>not</em> need to install com.google.inject.servlet.GuiceFilter and
  206. * the Guice com.google.inject.servlet.ServletModule to use the DWR scopes.
  207. * There is the potential for binding conflict with this module, however, so this package creates
  208. * bindings for the potentially conflicting types using a special {@link org.directwebremoting.guice.Dwr @Dwr}
  209. * annotation. It also makes the standard binding if the Guice servlet module is not found in the
  210. * class loader. The machinery to achieve this is called automatically with
  211. * {@link org.directwebremoting.guice.DwrGuiceServletContextListener}.
  212. * If you use
  213. * {@link org.directwebremoting.guice.CustomInjectorServletContextListener},
  214. * remember to call {@link org.directwebremoting.guice.AbstractDwrModule#bindDwrScopes() bindDwrScopes}
  215. * before doing any other bindings.
  216. * In both cases, the standard behavior can be overridden explicitly;
  217. * see {@link org.directwebremoting.guice.AbstractDwrModule#bindDwrScopes(boolean) bindDwrScopes(boolean)}
  218. * and {@link org.directwebremoting.guice.AbstractDwrModule#bindPotentiallyConflictingTypes(boolean) bindPotentiallyConflictingTypes}.
  219. * </p>
  220. * @author Tim Peierls [tim at peierls dot net]
  221. */
  222. package org.directwebremoting.guice;