1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.boretti.drools.integration.drools5; |
20 | |
|
21 | |
import java.lang.reflect.Method; |
22 | |
|
23 | |
import org.apache.log4j.Logger; |
24 | |
import org.springframework.beans.BeansException; |
25 | |
import org.springframework.beans.factory.BeanFactory; |
26 | |
import org.springframework.beans.factory.BeanFactoryAware; |
27 | |
import org.springframework.beans.factory.FactoryBean; |
28 | |
import org.springframework.beans.factory.ListableBeanFactory; |
29 | |
import org.springframework.beans.factory.annotation.Autowired; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 2 | public class DroolsBeanFactory implements FactoryBean,BeanFactoryAware { |
46 | |
|
47 | 2 | private final Logger logger = Logger.getLogger(DroolsBeanFactory.class); |
48 | |
|
49 | |
private Class<?> droolsInterface; |
50 | |
|
51 | |
private BeanFactory beanFactory; |
52 | |
|
53 | 2 | private DroolsProvider droolsProvider = new DroolsProvider(); |
54 | |
|
55 | |
@Override |
56 | |
public Object getObject() throws Exception { |
57 | 4 | Object o = droolsProvider.getService(droolsInterface); |
58 | 16 | for(Method m:droolsInterface.getMethods()) { |
59 | 12 | Autowired a = null; |
60 | |
try { |
61 | 12 | a=m.getAnnotation(Autowired.class); |
62 | 12 | } catch (NullPointerException ex) {} |
63 | 12 | if (a!=null) { |
64 | 4 | String methodName = m.getName(); |
65 | 4 | if (methodName.startsWith("set") && m.getParameterTypes().length==1) { |
66 | 4 | String fieldName = methodName.substring(3,4).toLowerCase()+methodName.substring(4); |
67 | 4 | if (beanFactory instanceof ListableBeanFactory) { |
68 | 4 | ListableBeanFactory lbf = (ListableBeanFactory)beanFactory; |
69 | 4 | String names[] = lbf.getBeanNamesForType(m.getParameterTypes()[0]); |
70 | 4 | if (names.length>0) { |
71 | 4 | if (logger.isDebugEnabled()) logger.debug("Injection into "+fieldName); |
72 | 4 | m.invoke(o, lbf.getBean(names[0])); |
73 | |
} |
74 | |
} |
75 | |
} |
76 | |
} |
77 | |
} |
78 | 4 | return o; |
79 | |
} |
80 | |
|
81 | |
@Override |
82 | |
public Class<?> getObjectType() { |
83 | 8 | return droolsInterface; |
84 | |
} |
85 | |
|
86 | |
@Override |
87 | |
public boolean isSingleton() { |
88 | 4 | return false; |
89 | |
} |
90 | |
|
91 | |
public void setDroolsInterface(Class<?> droolsInterface) { |
92 | 2 | this.droolsInterface = droolsInterface; |
93 | 2 | } |
94 | |
|
95 | |
@Override |
96 | |
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
97 | 2 | this.beanFactory=beanFactory; |
98 | 2 | } |
99 | |
|
100 | |
|
101 | |
} |