Advertisement
Guest User

Untitled

a guest
Oct 29th, 2012
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. package org.mike.sample.camel.postprocessor;
  2.  
  3. import java.util.LinkedHashSet;
  4. import java.util.Map;
  5. import java.util.Set;
  6.  
  7. import javax.xml.bind.annotation.XmlTransient;
  8.  
  9. import org.apache.camel.CamelContext;
  10. import org.apache.camel.Endpoint;
  11. import org.apache.camel.Service;
  12. import org.apache.camel.core.xml.CamelJMXAgentDefinition;
  13. import org.apache.camel.impl.CamelPostProcessorHelper;
  14. import org.apache.camel.impl.DefaultCamelBeanPostProcessor;
  15. import org.apache.camel.spring.GenericBeansException;
  16. import org.apache.camel.util.ServiceHelper;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import org.springframework.beans.BeanInstantiationException;
  20. import org.springframework.beans.BeansException;
  21. import org.springframework.beans.factory.config.BeanPostProcessor;
  22. import org.springframework.context.ApplicationContext;
  23. import org.springframework.context.ApplicationContextAware;
  24.  
  25. public class CustomCamelBeanPostProcessor implements BeanPostProcessor,
  26. ApplicationContextAware {
  27.  
  28. private static final transient Logger LOG = LoggerFactory
  29. .getLogger(CustomCamelBeanPostProcessor.class);
  30. @XmlTransient
  31. Set<String> prototypeBeans = new LinkedHashSet<String>();
  32. @XmlTransient
  33. private CamelContext camelContext;
  34. @XmlTransient
  35. private ApplicationContext applicationContext;
  36. @XmlTransient
  37. private String camelId;
  38.  
  39. // must use a delegate, as we cannot extend DefaultCamelBeanPostProcessor,
  40. // as this will cause the
  41. // XSD schema generator to include the DefaultCamelBeanPostProcessor as a
  42. // type, which we do not want to
  43. @XmlTransient
  44. private final DefaultCamelBeanPostProcessor delegate = new DefaultCamelBeanPostProcessor() {
  45. @Override
  46. public CamelContext getOrLookupCamelContext() {
  47. if (camelContext == null) {
  48. if (camelId != null) {
  49. LOG.trace(
  50. "Looking up CamelContext by id: {} from Spring ApplicationContext: {}",
  51. camelId, applicationContext);
  52. camelContext = applicationContext.getBean(camelId,
  53. CamelContext.class);
  54. } else {
  55. // lookup by type and grab the single CamelContext if exists
  56. LOG.trace(
  57. "Looking up CamelContext by type from Spring ApplicationContext: {}",
  58. applicationContext);
  59. Map<String, CamelContext> contexts = applicationContext
  60. .getBeansOfType(CamelContext.class);
  61. if (contexts != null && contexts.size() == 1) {
  62. camelContext = contexts.values().iterator().next();
  63. }
  64. }
  65. }
  66. return camelContext;
  67. }
  68.  
  69. @Override
  70. public boolean canPostProcessBean(Object bean, String beanName) {
  71. // the JMXAgent is a bit strange and causes Spring issues if we let
  72. // it being
  73. // post processed by this one. It does not need it anyway so we are
  74. // good to go.
  75. // We should also avoid to process the null object bean (in Spring
  76. // 2.5.x)
  77.  
  78. // TODO - DO YOUR STUFF HERE STRELOK
  79.  
  80. if (bean == null || bean instanceof CamelJMXAgentDefinition) {
  81. return false;
  82. }
  83.  
  84. return super.canPostProcessBean(bean, beanName);
  85. }
  86.  
  87. @Override
  88. public CamelPostProcessorHelper getPostProcessorHelper() {
  89. // lets lazily create the post processor
  90. if (camelPostProcessorHelper == null) {
  91. camelPostProcessorHelper = new CamelPostProcessorHelper() {
  92.  
  93. @Override
  94. public CamelContext getCamelContext() {
  95. // lets lazily lookup the camel context here
  96. // as doing this will cause this context to be started
  97. // immediately
  98. // breaking the lifecycle ordering of different camel
  99. // contexts
  100. // so we only want to do this on demand
  101. return delegate.getOrLookupCamelContext();
  102. }
  103.  
  104. @Override
  105. protected RuntimeException createProxyInstantiationRuntimeException(
  106. Class<?> type, Endpoint endpoint, Exception e) {
  107. return new BeanInstantiationException(type,
  108. "Could not instantiate proxy of type "
  109. + type.getName() + " on endpoint "
  110. + endpoint, e);
  111. }
  112.  
  113. protected boolean isSingleton(Object bean, String beanName) {
  114. // no application context has been injected which means
  115. // the bean
  116. // has not been enlisted in Spring application context
  117. if (applicationContext == null || beanName == null) {
  118. return super.isSingleton(bean, beanName);
  119. } else {
  120. return applicationContext.isSingleton(beanName);
  121. }
  122. }
  123.  
  124. protected void startService(Service service, Object bean,
  125. String beanName) throws Exception {
  126. if (isSingleton(bean, beanName)) {
  127. getCamelContext().addService(service);
  128. } else {
  129. // only start service and do not add it to
  130. // CamelContext
  131. ServiceHelper.startService(service);
  132. if (prototypeBeans.add(beanName)) {
  133. // do not spam the log with WARN so do this only
  134. // once per bean name
  135. CustomCamelBeanPostProcessor.LOG
  136. .warn("The bean with id ["
  137. + beanName
  138. + "] is prototype scoped and cannot stop the injected service when bean is destroyed: "
  139. + service
  140. + ". You may want to stop the service manually from the bean.");
  141. }
  142. }
  143. }
  144. };
  145. }
  146. return camelPostProcessorHelper;
  147. }
  148. };
  149.  
  150. public CustomCamelBeanPostProcessor() {
  151. }
  152.  
  153. public Object postProcessBeforeInitialization(Object bean, String beanName)
  154. throws BeansException {
  155. try {
  156. return delegate.postProcessBeforeInitialization(bean, beanName);
  157. } catch (Exception e) {
  158. // do not wrap already beans exceptions
  159. if (e instanceof BeansException) {
  160. throw (BeansException) e;
  161. }
  162. throw new GenericBeansException("Error post processing bean: "
  163. + beanName, e);
  164. }
  165. }
  166.  
  167. public Object postProcessAfterInitialization(Object bean, String beanName)
  168. throws BeansException {
  169. try {
  170. return delegate.postProcessAfterInitialization(bean, beanName);
  171. } catch (Exception e) {
  172. // do not wrap already beans exceptions
  173. if (e instanceof BeansException) {
  174. throw (BeansException) e;
  175. }
  176. throw new GenericBeansException("Error post processing bean: "
  177. + beanName, e);
  178. }
  179. }
  180.  
  181. // Properties
  182. // -------------------------------------------------------------------------
  183.  
  184. public void setApplicationContext(ApplicationContext applicationContext)
  185. throws BeansException {
  186. this.applicationContext = applicationContext;
  187. }
  188.  
  189. public CamelContext getCamelContext() {
  190. return camelContext;
  191. }
  192.  
  193. public void setCamelContext(CamelContext camelContext) {
  194. this.camelContext = camelContext;
  195. }
  196.  
  197. public String getCamelId() {
  198. return camelId;
  199. }
  200.  
  201. public void setCamelId(String camelId) {
  202. this.camelId = camelId;
  203. }
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement