1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.ibeans.config; |
8 | |
|
9 | |
import org.mule.api.MuleContext; |
10 | |
import org.mule.api.MuleException; |
11 | |
import org.mule.api.MuleRuntimeException; |
12 | |
import org.mule.api.construct.FlowConstruct; |
13 | |
import org.mule.api.context.MuleContextAware; |
14 | |
import org.mule.api.registry.InjectProcessor; |
15 | |
import org.mule.module.ibeans.spi.MuleIBeansPlugin; |
16 | |
import org.mule.util.annotation.AnnotationMetaData; |
17 | |
import org.mule.util.annotation.AnnotationUtils; |
18 | |
|
19 | |
import java.lang.reflect.Field; |
20 | |
import java.util.Set; |
21 | |
|
22 | |
import org.ibeans.annotation.IntegrationBean; |
23 | |
|
24 | |
public class IntegrationBeanAnnotatedObjectProcessor implements InjectProcessor, MuleContextAware |
25 | |
{ |
26 | |
private MuleContext muleContext; |
27 | |
private MuleIBeansPlugin plugin; |
28 | |
|
29 | |
public IntegrationBeanAnnotatedObjectProcessor() |
30 | |
{ |
31 | 0 | super(); |
32 | 0 | } |
33 | |
|
34 | |
public IntegrationBeanAnnotatedObjectProcessor(MuleContext muleContext) |
35 | |
{ |
36 | 0 | this(); |
37 | 0 | setMuleContext(muleContext); |
38 | 0 | } |
39 | |
|
40 | |
public void setMuleContext(MuleContext context) |
41 | |
{ |
42 | 0 | this.muleContext = context; |
43 | 0 | this.plugin = new MuleIBeansPlugin(context); |
44 | 0 | } |
45 | |
|
46 | |
public Object process(Object object) |
47 | |
{ |
48 | 0 | Set<AnnotationMetaData> annos = AnnotationUtils.getFieldAnnotationsForHierarchy(object.getClass(), IntegrationBean.class); |
49 | |
|
50 | 0 | for (AnnotationMetaData data : annos) |
51 | |
{ |
52 | 0 | Field field = (Field) data.getMember(); |
53 | 0 | field.setAccessible(true); |
54 | |
try |
55 | |
{ |
56 | 0 | if (field.get(object) != null) |
57 | |
{ |
58 | 0 | continue; |
59 | |
} |
60 | |
} |
61 | 0 | catch (IllegalAccessException e) |
62 | |
{ |
63 | 0 | continue; |
64 | 0 | } |
65 | 0 | IBeanBinding binding = createBinding(field.getType().getSimpleName()); |
66 | 0 | binding.setInterface(field.getType()); |
67 | 0 | Object proxy = binding.createProxy(new Object()); |
68 | |
try |
69 | |
{ |
70 | 0 | field.set(object, proxy); |
71 | |
} |
72 | 0 | catch (IllegalAccessException e) |
73 | |
{ |
74 | 0 | throw new RuntimeException("Failed to create IntegrationBean proxy for: " + field.getType(), e); |
75 | 0 | } |
76 | 0 | } |
77 | 0 | return object; |
78 | |
} |
79 | |
|
80 | |
protected IBeanBinding createBinding(String name) |
81 | |
{ |
82 | 0 | IBeanFlowConstruct flow = new IBeanFlowConstruct(name + ".ibean", muleContext); |
83 | |
try |
84 | |
{ |
85 | 0 | muleContext.getRegistry().registerObject(flow.getName(), flow, FlowConstruct.class); |
86 | |
} |
87 | 0 | catch (MuleException e) |
88 | |
{ |
89 | 0 | throw new MuleRuntimeException(e); |
90 | 0 | } |
91 | 0 | return new IBeanBinding(flow, muleContext, plugin); |
92 | |
} |
93 | |
} |