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