1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.spring; |
18 |
|
|
19 |
|
import org.apache.camel.Endpoint; |
20 |
|
import org.apache.camel.Processor; |
21 |
|
import org.apache.camel.RuntimeCamelException; |
22 |
|
import org.apache.camel.component.bean.BeanProcessor; |
23 |
|
import org.apache.camel.component.event.EventComponent; |
24 |
|
import org.apache.camel.component.event.EventEndpoint; |
25 |
|
import org.apache.camel.impl.DefaultCamelContext; |
26 |
|
import org.apache.camel.impl.ProcessorEndpoint; |
27 |
|
import org.apache.camel.spi.ComponentResolver; |
28 |
|
import org.apache.camel.spi.Injector; |
29 |
|
import org.apache.camel.spi.Registry; |
30 |
|
import org.apache.camel.spring.spi.ApplicationContextRegistry; |
31 |
|
import org.apache.camel.spring.spi.SpringComponentResolver; |
32 |
|
import org.apache.camel.spring.spi.SpringInjector; |
33 |
|
import org.apache.commons.logging.Log; |
34 |
|
import org.apache.commons.logging.LogFactory; |
35 |
|
|
36 |
|
import org.springframework.beans.BeansException; |
37 |
|
import org.springframework.beans.factory.DisposableBean; |
38 |
|
import org.springframework.beans.factory.InitializingBean; |
39 |
|
import org.springframework.context.ApplicationContext; |
40 |
|
import org.springframework.context.ApplicationContextAware; |
41 |
|
import org.springframework.context.ApplicationEvent; |
42 |
|
import org.springframework.context.ApplicationListener; |
43 |
|
import org.springframework.context.ConfigurableApplicationContext; |
44 |
|
import org.springframework.context.event.ContextRefreshedEvent; |
45 |
|
import org.springframework.context.support.AbstractRefreshableApplicationContext; |
46 |
|
import org.springframework.context.support.ClassPathXmlApplicationContext; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
public class SpringCamelContext extends DefaultCamelContext implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener { |
59 |
1 |
private static final transient Log LOG = LogFactory.getLog(SpringCamelContext.class); |
60 |
|
private ApplicationContext applicationContext; |
61 |
|
private EventEndpoint eventEndpoint; |
62 |
|
|
63 |
0 |
public SpringCamelContext() { |
64 |
0 |
} |
65 |
|
|
66 |
56 |
public SpringCamelContext(ApplicationContext applicationContext) { |
67 |
56 |
setApplicationContext(applicationContext); |
68 |
56 |
} |
69 |
|
|
70 |
|
public static SpringCamelContext springCamelContext(ApplicationContext applicationContext) throws Exception { |
71 |
|
|
72 |
31 |
String[] names = applicationContext.getBeanNamesForType(SpringCamelContext.class); |
73 |
31 |
if (names.length == 1) { |
74 |
31 |
return (SpringCamelContext)applicationContext.getBean(names[0], SpringCamelContext.class); |
75 |
|
} |
76 |
0 |
SpringCamelContext answer = new SpringCamelContext(); |
77 |
0 |
answer.setApplicationContext(applicationContext); |
78 |
0 |
answer.afterPropertiesSet(); |
79 |
0 |
return answer; |
80 |
|
} |
81 |
|
|
82 |
|
public static SpringCamelContext springCamelContext(String configLocations) throws Exception { |
83 |
0 |
return springCamelContext(new ClassPathXmlApplicationContext(configLocations)); |
84 |
|
} |
85 |
|
|
86 |
|
public void afterPropertiesSet() throws Exception { |
87 |
0 |
start(); |
88 |
0 |
} |
89 |
|
|
90 |
|
public void destroy() throws Exception { |
91 |
0 |
stop(); |
92 |
0 |
} |
93 |
|
|
94 |
|
public void onApplicationEvent(ApplicationEvent event) { |
95 |
82 |
if (LOG.isDebugEnabled()) { |
96 |
0 |
LOG.debug("Publishing event: " + event); |
97 |
|
} |
98 |
|
|
99 |
82 |
if (event instanceof ContextRefreshedEvent) { |
100 |
|
|
101 |
|
|
102 |
|
try { |
103 |
53 |
LOG.debug("Starting the CamelContext now that the ApplicationContext has started"); |
104 |
53 |
start(); |
105 |
0 |
} catch (Exception e) { |
106 |
0 |
throw new RuntimeCamelException(e); |
107 |
53 |
} |
108 |
53 |
if (eventEndpoint != null) { |
109 |
53 |
eventEndpoint.onApplicationEvent(event); |
110 |
53 |
} |
111 |
|
} else { |
112 |
29 |
if (eventEndpoint != null) { |
113 |
29 |
eventEndpoint.onApplicationEvent(event); |
114 |
29 |
} else { |
115 |
0 |
LOG.warn("No eventEndpoint enabled for event: " + event); |
116 |
|
} |
117 |
|
} |
118 |
82 |
} |
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
public ApplicationContext getApplicationContext() { |
124 |
136 |
return applicationContext; |
125 |
|
} |
126 |
|
|
127 |
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
128 |
56 |
this.applicationContext = applicationContext; |
129 |
|
|
130 |
56 |
if (applicationContext instanceof ConfigurableApplicationContext) { |
131 |
56 |
addComponent("event", new EventComponent(applicationContext)); |
132 |
|
} |
133 |
56 |
} |
134 |
|
|
135 |
|
public EventEndpoint getEventEndpoint() { |
136 |
0 |
return eventEndpoint; |
137 |
|
} |
138 |
|
|
139 |
|
public void setEventEndpoint(EventEndpoint eventEndpoint) { |
140 |
0 |
this.eventEndpoint = eventEndpoint; |
141 |
0 |
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
@Override |
147 |
|
protected void doStart() throws Exception { |
148 |
56 |
super.doStart(); |
149 |
56 |
if (eventEndpoint == null) { |
150 |
56 |
eventEndpoint = createEventEndpoint(); |
151 |
|
} |
152 |
56 |
} |
153 |
|
|
154 |
|
@Override |
155 |
|
protected Injector createInjector() { |
156 |
56 |
return new SpringInjector((AbstractRefreshableApplicationContext)getApplicationContext()); |
157 |
|
} |
158 |
|
|
159 |
|
@Override |
160 |
|
protected ComponentResolver createComponentResolver() { |
161 |
53 |
ComponentResolver defaultResolver = super.createComponentResolver(); |
162 |
53 |
return new SpringComponentResolver(getApplicationContext(), defaultResolver); |
163 |
|
} |
164 |
|
|
165 |
|
protected EventEndpoint createEventEndpoint() { |
166 |
56 |
EventEndpoint endpoint = getEndpoint("event:default", EventEndpoint.class); |
167 |
56 |
return endpoint; |
168 |
|
} |
169 |
|
|
170 |
|
protected Endpoint convertBeanToEndpoint(String uri, Object bean) { |
171 |
1 |
Processor processor = new BeanProcessor(bean, this); |
172 |
1 |
return new ProcessorEndpoint(uri, this, processor); |
173 |
|
} |
174 |
|
|
175 |
|
@Override |
176 |
|
protected Registry createRegistry() { |
177 |
20 |
return new ApplicationContextRegistry(getApplicationContext()); |
178 |
|
} |
179 |
|
} |