PageRenderTime 1166ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/vraptor-core/src/main/java/br/com/caelum/vraptor/ioc/guice/GuiceProvider.java

http://github.com/caelum/vraptor
Java | 151 lines | 99 code | 20 blank | 32 comment | 5 complexity | 82b21c51e13e91bb94e706283c110262 MD5 | raw file
Possible License(s): Apache-2.0
  1. /***
  2. * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource All rights reserved.
  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. package br.com.caelum.vraptor.ioc.guice;
  17. import java.util.Set;
  18. import javax.servlet.ServletContext;
  19. import br.com.caelum.vraptor.ComponentRegistry;
  20. import br.com.caelum.vraptor.config.BasicConfiguration;
  21. import br.com.caelum.vraptor.core.Execution;
  22. import br.com.caelum.vraptor.core.RequestInfo;
  23. import br.com.caelum.vraptor.ioc.Container;
  24. import br.com.caelum.vraptor.ioc.ContainerProvider;
  25. import br.com.caelum.vraptor.ioc.StereotypeHandler;
  26. import br.com.caelum.vraptor.ioc.spring.VRaptorRequestHolder;
  27. import br.com.caelum.vraptor.scan.WebAppBootstrap;
  28. import br.com.caelum.vraptor.scan.WebAppBootstrapFactory;
  29. import com.google.inject.Binder;
  30. import com.google.inject.ConfigurationException;
  31. import com.google.inject.Guice;
  32. import com.google.inject.Injector;
  33. import com.google.inject.Key;
  34. import com.google.inject.Module;
  35. import com.google.inject.Stage;
  36. import com.google.inject.TypeLiteral;
  37. import com.google.inject.multibindings.Multibinder;
  38. import com.google.inject.util.Modules;
  39. /**
  40. *
  41. * A Container Provider that uses Google Guice as DI container.
  42. *
  43. * @author Lucas Cavalcanti
  44. * @author Sergio Lopes
  45. * @since 3.2
  46. *
  47. */
  48. public class GuiceProvider implements ContainerProvider {
  49. private boolean stopSession = false;
  50. static final RequestCustomScope REQUEST = new RequestCustomScope();
  51. static final SessionCustomScope SESSION = new SessionCustomScope();
  52. static final ApplicationCustomScope APPLICATION = new ApplicationCustomScope();
  53. private final class GuiceContainer implements Container {
  54. public <T> T instanceFor(Class<T> type) {
  55. return injector.getInstance(type);
  56. }
  57. public <T> boolean canProvide(Class<T> type) {
  58. try {
  59. return injector.getProvider(type) != null;
  60. } catch (ConfigurationException e) {
  61. return false;
  62. }
  63. }
  64. }
  65. private Injector injector;
  66. private GuiceContainer container;
  67. protected ServletContext context;
  68. public <T> T provideForRequest(RequestInfo request, Execution<T> execution) {
  69. VRaptorRequestHolder.setRequestForCurrentThread(request);
  70. REQUEST.start();
  71. try {
  72. return execution.insideRequest(container);
  73. } finally {
  74. REQUEST.stop();
  75. VRaptorRequestHolder.resetRequestForCurrentThread();
  76. }
  77. }
  78. public Container getContainer() {
  79. return container;
  80. }
  81. public void start(ServletContext context) {
  82. this.context = context;
  83. APPLICATION.start();
  84. container = new GuiceContainer();
  85. injector = Guice.createInjector(Stage.PRODUCTION, Modules.override(new VRaptorAbstractModule(context, container)).with(customModule()));
  86. executeStereotypeHandlers();
  87. injector.injectMembers(REQUEST);
  88. injector.injectMembers(SESSION);
  89. }
  90. private void executeStereotypeHandlers() {
  91. Set<StereotypeHandler> handlers = injector.getInstance(Key.get(new TypeLiteral<Set<StereotypeHandler>>() {}));
  92. for (Key<?> key : injector.getAllBindings().keySet()) {
  93. for (StereotypeHandler handler : handlers) {
  94. Class<?> type = key.getTypeLiteral().getRawType();
  95. if (type.isAnnotationPresent(handler.stereotype())) {
  96. handler.handle(type);
  97. }
  98. }
  99. }
  100. }
  101. protected Module customModule() {
  102. return new Module() {
  103. public void configure(Binder binder) {
  104. ComponentRegistry registry = new GuiceComponentRegistry(binder, Multibinder.newSetBinder(binder, StereotypeHandler.class));
  105. BasicConfiguration config = new BasicConfiguration(context);
  106. // using the new vraptor.scan
  107. WebAppBootstrap webAppBootstrap = new WebAppBootstrapFactory().create(config);
  108. webAppBootstrap.configure(registry);
  109. // call old-style custom components registration
  110. registerCustomComponents(registry);
  111. }
  112. };
  113. }
  114. protected void registerCustomComponents(ComponentRegistry registry) {
  115. /* TODO: For now, this is an empty hook method to enable subclasses to use
  116. * the scanner and register their specific components.
  117. *
  118. * In the future, if we scan the classpath for StereotypeHandlers, we can
  119. * eliminate this hook.
  120. */
  121. }
  122. protected void stopSession(Boolean value) {
  123. this.stopSession = value;
  124. }
  125. public void stop() {
  126. if(stopSession) {
  127. SESSION.stopAll();
  128. }
  129. APPLICATION.stop();
  130. }
  131. }