PageRenderTime 314ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/components/camel-guice/src/main/java/org/apache/camel/guice/GuiceCamelContext.java

https://gitlab.com/matticala/apache-camel
Java | 176 lines | 129 code | 23 blank | 24 comment | 3 complexity | cf521b130146f1c72133a56a3bdd3e25 MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.guice;
  18. import java.util.List;
  19. import java.util.Set;
  20. import javax.annotation.PostConstruct;
  21. import javax.annotation.PreDestroy;
  22. import javax.naming.Context;
  23. import javax.naming.InitialContext;
  24. import com.google.inject.Binding;
  25. import com.google.inject.Inject;
  26. import org.apache.camel.ErrorHandlerFactory;
  27. import org.apache.camel.RoutesBuilder;
  28. import org.apache.camel.TypeConverter;
  29. import org.apache.camel.guice.impl.GuiceInjector;
  30. import org.apache.camel.guice.inject.Injectors;
  31. import org.apache.camel.impl.DefaultCamelContext;
  32. import org.apache.camel.impl.JndiRegistry;
  33. import org.apache.camel.spi.ComponentResolver;
  34. import org.apache.camel.spi.Injector;
  35. import org.apache.camel.spi.InterceptStrategy;
  36. import org.apache.camel.spi.LanguageResolver;
  37. import org.apache.camel.spi.LifecycleStrategy;
  38. import org.apache.camel.spi.Registry;
  39. import org.apache.camel.util.ObjectHelper;
  40. /**
  41. * The default CamelContext implementation for working with Guice.
  42. * <p/>
  43. * It is recommended you use this implementation with the
  44. * <a href="http://code.google.com/p/guiceyfruit/wiki/GuiceyJndi">Guicey JNDI Provider</a>
  45. *
  46. * @version
  47. */
  48. public class GuiceCamelContext extends DefaultCamelContext {
  49. private final com.google.inject.Injector injector;
  50. @Inject
  51. public GuiceCamelContext(com.google.inject.Injector injector) {
  52. this.injector = injector;
  53. }
  54. @PostConstruct
  55. @Override
  56. public void start() {
  57. try {
  58. super.start();
  59. } catch (Exception e) {
  60. throw ObjectHelper.wrapRuntimeCamelException(e);
  61. }
  62. }
  63. @PreDestroy
  64. @Override
  65. public void stop() {
  66. try {
  67. super.stop();
  68. } catch (Exception e) {
  69. throw ObjectHelper.wrapRuntimeCamelException(e);
  70. }
  71. }
  72. @Inject
  73. public void setRouteBuilders(Set<RoutesBuilder> routeBuilders) {
  74. for (RoutesBuilder builder : routeBuilders) {
  75. try {
  76. addRoutes(builder);
  77. } catch (Exception e) {
  78. throw ObjectHelper.wrapRuntimeCamelException(e);
  79. }
  80. }
  81. }
  82. @Override
  83. @Inject(optional = true)
  84. public void setRegistry(Registry registry) {
  85. super.setRegistry(registry);
  86. }
  87. @Override
  88. @Inject(optional = true)
  89. public void setJndiContext(Context jndiContext) {
  90. super.setJndiContext(jndiContext);
  91. }
  92. @Override
  93. @Inject(optional = true)
  94. public void setInjector(Injector injector) {
  95. super.setInjector(injector);
  96. }
  97. @Override
  98. @Inject(optional = true)
  99. public void setComponentResolver(ComponentResolver componentResolver) {
  100. super.setComponentResolver(componentResolver);
  101. }
  102. @Override
  103. @Inject(optional = true)
  104. public void setAutoCreateComponents(boolean autoCreateComponents) {
  105. super.setAutoCreateComponents(autoCreateComponents);
  106. }
  107. @Override
  108. @Inject(optional = true)
  109. public void setErrorHandlerBuilder(ErrorHandlerFactory errorHandlerBuilder) {
  110. super.setErrorHandlerBuilder(errorHandlerBuilder);
  111. }
  112. @Override
  113. @Inject(optional = true)
  114. public void setInterceptStrategies(List<InterceptStrategy> interceptStrategies) {
  115. super.setInterceptStrategies(interceptStrategies);
  116. }
  117. @Override
  118. @Inject(optional = true)
  119. public void setLanguageResolver(LanguageResolver languageResolver) {
  120. super.setLanguageResolver(languageResolver);
  121. }
  122. @Override
  123. @Inject(optional = true)
  124. public void setLifecycleStrategies(List<LifecycleStrategy> lifecycleStrategies) {
  125. super.setLifecycleStrategies(lifecycleStrategies);
  126. }
  127. @Override
  128. @Inject(optional = true)
  129. public void setTypeConverter(TypeConverter typeConverter) {
  130. super.setTypeConverter(typeConverter);
  131. }
  132. @Override
  133. protected Injector createInjector() {
  134. return new GuiceInjector(injector);
  135. }
  136. @Override
  137. protected Registry createRegistry() {
  138. Context context = createContext();
  139. return new JndiRegistry(context);
  140. }
  141. protected Context createContext() {
  142. Set<Binding<?>> bindings = Injectors.getBindingsOf(injector, Context.class);
  143. try {
  144. if (bindings.isEmpty()) {
  145. return new InitialContext();
  146. } else {
  147. return injector.getInstance(Context.class);
  148. }
  149. } catch (Exception e) {
  150. throw ObjectHelper.wrapRuntimeCamelException(e);
  151. }
  152. }
  153. }