PageRenderTime 763ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/burris/dwr
Java | 118 lines | 59 code | 16 blank | 43 comment | 4 complexity | 8bf28372800daa18e7ebdf16af595386 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 java.util.prefs.Preferences;
  18. import com.google.inject.Guice;
  19. import com.google.inject.Injector;
  20. import com.google.inject.Stage;
  21. import org.directwebremoting.guice.util.AbstractModule;
  22. /**
  23. * Register a concrete subclass of this as a servlet context listener to
  24. * configure an {@link Injector} with this as the only {@link com.google.inject.Module}
  25. * and stash it in the servlet context.
  26. * @author Tim Peierls [tim at peierls dot net]
  27. */
  28. public abstract class DwrGuiceServletContextListener extends AbstractDwrGuiceServletContextListener
  29. {
  30. /**
  31. * Creates an Injector built from this module, with the Stage value returned by {@link #getStage}.
  32. */
  33. @Override
  34. protected final Injector createInjector()
  35. {
  36. return Guice.createInjector(getStage(), new DwrScopeBinder(this));
  37. }
  38. /**
  39. * Define this method to configure bindings at servlet context initialization.
  40. * Call {@link AbstractModule#install AbstractModule.install(Module)} within
  41. * this method to use binding code from other modules.
  42. */
  43. @Override
  44. protected abstract void configure();
  45. /**
  46. * Override this method to specify which stage to run Guice in.
  47. * Default behavior is to look first in user preferences and then
  48. * in system preferences for node "org/directwebremoting/guice"
  49. * with a value for key "stage". If not found, the default is
  50. * Stage.PRODUCTION.
  51. */
  52. protected Stage getStage()
  53. {
  54. Stage stage = Stage.PRODUCTION;
  55. try
  56. {
  57. Preferences userNode = Preferences.userNodeForPackage(PACKAGE);
  58. String userStage = userNode.get(STAGE_KEY, null);
  59. if (userStage != null)
  60. {
  61. stage = Stage.valueOf(userStage);
  62. }
  63. else
  64. {
  65. Preferences systemNode = Preferences.systemNodeForPackage(PACKAGE);
  66. String systemStage = systemNode.get(STAGE_KEY, null);
  67. if (systemStage != null)
  68. {
  69. stage = Stage.valueOf(systemStage);
  70. }
  71. }
  72. }
  73. catch (Exception e)
  74. {
  75. // ignore errors reading Preferences
  76. }
  77. return stage;
  78. }
  79. /**
  80. * Copies the Boolean that determines the behavior of {@link #bindDwrScopes()}
  81. * from another module and calls that method during configuration.
  82. */
  83. private static class DwrScopeBinder extends AbstractDwrModule
  84. {
  85. DwrScopeBinder(AbstractDwrModule module)
  86. {
  87. this.bindPotentiallyConflictingTypes = module.bindPotentiallyConflictingTypes;
  88. this.module = module;
  89. }
  90. @Override
  91. protected void configure()
  92. {
  93. bindDwrScopes();
  94. install(module);
  95. }
  96. final AbstractDwrModule module;
  97. }
  98. /** The name of the node to examine for a STAGE property. */
  99. private static final Class<?> PACKAGE = DwrGuiceServletContextListener.class;
  100. /** The node property to examine for a value for Stage. */
  101. private static final String STAGE_KEY = "stage";
  102. }