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