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.CamelContext; |
21 |
|
import org.apache.camel.builder.RouteBuilder; |
22 |
|
import org.springframework.beans.factory.DisposableBean; |
23 |
|
import org.springframework.beans.factory.FactoryBean; |
24 |
|
import org.springframework.beans.factory.InitializingBean; |
25 |
|
import org.springframework.context.ApplicationContext; |
26 |
|
import org.springframework.context.ApplicationContextAware; |
27 |
|
|
28 |
|
import java.util.ArrayList; |
29 |
|
import java.util.List; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
30 |
public class CamelContextFactoryBean implements FactoryBean, InitializingBean, DisposableBean, ApplicationContextAware { |
39 |
|
private CamelContext context; |
40 |
|
private RouteBuilder routeBuilder; |
41 |
30 |
private List<RouteBuilder> additionalBuilders = new ArrayList<RouteBuilder>(); |
42 |
30 |
private String[] packages = {}; |
43 |
|
private ApplicationContext applicationContext; |
44 |
|
|
45 |
|
public Object getObject() throws Exception { |
46 |
28 |
return getContext(); |
47 |
|
} |
48 |
|
|
49 |
|
public Class getObjectType() { |
50 |
42 |
return SpringCamelContext.class; |
51 |
|
} |
52 |
|
|
53 |
|
public boolean isSingleton() { |
54 |
79 |
return true; |
55 |
|
} |
56 |
|
|
57 |
|
public void afterPropertiesSet() throws Exception { |
58 |
|
|
59 |
|
|
60 |
30 |
getContext(); |
61 |
|
|
62 |
30 |
findRouteBuiders(); |
63 |
30 |
installRoutes(); |
64 |
|
|
65 |
|
|
66 |
30 |
getContext().start(); |
67 |
30 |
} |
68 |
|
|
69 |
|
public void destroy() throws Exception { |
70 |
11 |
getContext().stop(); |
71 |
11 |
} |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
public CamelContext getContext() throws Exception { |
76 |
105 |
if (context == null) { |
77 |
30 |
context = new SpringCamelContext(getApplicationContext()); |
78 |
|
} |
79 |
105 |
return context; |
80 |
|
} |
81 |
|
|
82 |
|
public void setContext(CamelContext context) { |
83 |
0 |
this.context = context; |
84 |
0 |
} |
85 |
|
|
86 |
|
public RouteBuilder getRouteBuilder() { |
87 |
0 |
return routeBuilder; |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
public void setRouteBuilder(RouteBuilder routeBuilder) { |
94 |
0 |
this.routeBuilder = routeBuilder; |
95 |
0 |
} |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
public void setRouteBuilders(RouteBuilder[] builders) { |
101 |
0 |
for (RouteBuilder builder : builders) { |
102 |
0 |
additionalBuilders.add(builder); |
103 |
|
} |
104 |
0 |
} |
105 |
|
|
106 |
|
public ApplicationContext getApplicationContext() { |
107 |
36 |
return applicationContext; |
108 |
|
} |
109 |
|
|
110 |
|
public void setApplicationContext(ApplicationContext applicationContext) { |
111 |
30 |
this.applicationContext = applicationContext; |
112 |
30 |
} |
113 |
|
|
114 |
|
public String[] getPackages() { |
115 |
6 |
return packages; |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
public void setPackages(String[] packages) { |
125 |
6 |
this.packages = packages; |
126 |
6 |
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
protected void installRoutes() throws Exception { |
135 |
30 |
for (RouteBuilder routeBuilder : additionalBuilders) { |
136 |
6 |
getContext().addRoutes(routeBuilder); |
137 |
6 |
} |
138 |
30 |
if (routeBuilder != null) { |
139 |
0 |
getContext().addRoutes(routeBuilder); |
140 |
|
} |
141 |
30 |
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
protected void findRouteBuiders() throws IllegalAccessException, InstantiationException { |
147 |
30 |
if (packages != null && packages.length > 0) { |
148 |
6 |
RouteBuilderFinder finder = new RouteBuilderFinder(this); |
149 |
6 |
finder.appendBuilders(additionalBuilders); |
150 |
|
} |
151 |
30 |
} |
152 |
|
} |