| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package org.apache.camel.spring; |
| 19 |
|
|
| 20 |
|
import org.apache.camel.impl.DefaultCamelContext; |
| 21 |
|
import org.apache.camel.spi.Injector; |
| 22 |
|
import org.apache.camel.spi.ComponentResolver; |
| 23 |
|
import org.apache.camel.CamelContext; |
| 24 |
|
import org.apache.camel.spring.spi.SpringComponentResolver; |
| 25 |
|
import org.apache.camel.spring.spi.SpringInjector; |
| 26 |
|
import org.apache.camel.spring.component.BeanComponent; |
| 27 |
|
import org.springframework.beans.factory.InitializingBean; |
| 28 |
|
import org.springframework.beans.factory.DisposableBean; |
| 29 |
|
import org.springframework.beans.BeansException; |
| 30 |
|
import org.springframework.context.ApplicationContextAware; |
| 31 |
|
import org.springframework.context.ApplicationContext; |
| 32 |
|
import org.springframework.context.support.ClassPathXmlApplicationContext; |
| 33 |
|
import org.springframework.context.support.AbstractRefreshableApplicationContext; |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
public class SpringCamelContext extends DefaultCamelContext implements InitializingBean, DisposableBean, ApplicationContextAware { |
| 44 |
|
private ApplicationContext applicationContext; |
| 45 |
|
|
| 46 |
0 |
public SpringCamelContext() { |
| 47 |
0 |
} |
| 48 |
|
|
| 49 |
30 |
public SpringCamelContext(ApplicationContext applicationContext) { |
| 50 |
30 |
setApplicationContext(applicationContext); |
| 51 |
30 |
} |
| 52 |
|
|
| 53 |
|
public static SpringCamelContext springCamelContext(ApplicationContext applicationContext) throws Exception { |
| 54 |
|
|
| 55 |
9 |
String[] names = applicationContext.getBeanNamesForType(SpringCamelContext.class); |
| 56 |
9 |
if (names.length == 1) { |
| 57 |
9 |
return (SpringCamelContext) applicationContext.getBean(names[0], SpringCamelContext.class); |
| 58 |
|
} |
| 59 |
0 |
SpringCamelContext answer = new SpringCamelContext(); |
| 60 |
0 |
answer.setApplicationContext(applicationContext); |
| 61 |
0 |
answer.afterPropertiesSet(); |
| 62 |
0 |
return answer; |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
public static SpringCamelContext springCamelContext(String configLocations) throws Exception { |
| 66 |
0 |
return springCamelContext(new ClassPathXmlApplicationContext(configLocations)); |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
public void afterPropertiesSet() throws Exception { |
| 70 |
|
|
| 71 |
0 |
getInjector(); |
| 72 |
|
|
| 73 |
0 |
start(); |
| 74 |
0 |
} |
| 75 |
|
|
| 76 |
|
public void destroy() throws Exception { |
| 77 |
0 |
stop(); |
| 78 |
0 |
} |
| 79 |
|
|
| 80 |
|
public ApplicationContext getApplicationContext() { |
| 81 |
60 |
return applicationContext; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 85 |
30 |
this.applicationContext = applicationContext; |
| 86 |
30 |
addComponent("bean", new BeanComponent(applicationContext)); |
| 87 |
30 |
} |
| 88 |
|
|
| 89 |
|
@Override |
| 90 |
|
protected Injector createInjector() { |
| 91 |
30 |
return new SpringInjector((AbstractRefreshableApplicationContext) getApplicationContext()); |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
@Override |
| 95 |
|
protected ComponentResolver createComponentResolver() { |
| 96 |
30 |
ComponentResolver defaultResolver = super.createComponentResolver(); |
| 97 |
30 |
return new SpringComponentResolver(getApplicationContext(), defaultResolver); |
| 98 |
|
} |
| 99 |
|
} |