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 java.lang.reflect.Modifier; |
20 |
|
import java.util.List; |
21 |
|
import java.util.Map; |
22 |
|
import java.util.Set; |
23 |
|
|
24 |
|
import org.apache.camel.builder.RouteBuilder; |
25 |
|
import org.apache.camel.util.ResolverUtil; |
26 |
|
|
27 |
|
import org.springframework.context.ApplicationContext; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
public class RouteBuilderFinder { |
35 |
|
private final SpringCamelContext camelContext; |
36 |
|
private final String[] packages; |
37 |
|
private ApplicationContext applicationContext; |
38 |
7 |
private ResolverUtil resolver = new ResolverUtil(); |
39 |
|
|
40 |
7 |
public RouteBuilderFinder(SpringCamelContext camelContext, String[] packages) { |
41 |
7 |
this.camelContext = camelContext; |
42 |
7 |
this.applicationContext = camelContext.getApplicationContext(); |
43 |
7 |
this.packages = packages; |
44 |
7 |
} |
45 |
|
|
46 |
|
public String[] getPackages() { |
47 |
0 |
return packages; |
48 |
|
} |
49 |
|
|
50 |
|
public ApplicationContext getApplicationContext() { |
51 |
0 |
return applicationContext; |
52 |
|
} |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
public void appendBuilders(List<RouteBuilder> list) throws IllegalAccessException, InstantiationException { |
59 |
7 |
resolver.findImplementations(RouteBuilder.class, packages); |
60 |
|
|
61 |
7 |
Set<Class> classes = resolver.getClasses(); |
62 |
7 |
for (Class aClass : classes) { |
63 |
6 |
if (shouldIgnoreBean(aClass)) { |
64 |
0 |
continue; |
65 |
|
} |
66 |
6 |
if (isValidClass(aClass)) { |
67 |
6 |
RouteBuilder builder = instantiateBuilder(aClass); |
68 |
6 |
list.add(builder); |
69 |
|
} |
70 |
6 |
} |
71 |
7 |
} |
72 |
|
|
73 |
|
public void destroy() throws Exception { |
74 |
0 |
} |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
protected boolean shouldIgnoreBean(Class type) { |
80 |
6 |
Map beans = applicationContext.getBeansOfType(type, true, true); |
81 |
6 |
if (beans == null || beans.isEmpty()) { |
82 |
6 |
return false; |
83 |
|
} |
84 |
|
|
85 |
0 |
return true; |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
protected boolean isValidClass(Class type) { |
92 |
6 |
if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) { |
93 |
6 |
return true; |
94 |
|
} |
95 |
0 |
return false; |
96 |
|
} |
97 |
|
|
98 |
|
protected RouteBuilder instantiateBuilder(Class type) throws IllegalAccessException, InstantiationException { |
99 |
6 |
return (RouteBuilder) camelContext.getInjector().newInstance(type); |
100 |
|
} |
101 |
|
} |