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 super();
35 }
36
37 public IntegrationBeanAnnotatedObjectProcessor(MuleContext muleContext)
38 {
39 this();
40 setMuleContext(muleContext);
41 }
42
43 public void setMuleContext(MuleContext context)
44 {
45 this.muleContext = context;
46 this.plugin = new MuleIBeansPlugin(context);
47 }
48
49 public Object process(Object object)
50 {
51 Set<AnnotationMetaData> annos = AnnotationUtils.getFieldAnnotationsForHierarchy(object.getClass(), IntegrationBean.class);
52
53 for (AnnotationMetaData data : annos)
54 {
55 Field field = (Field) data.getMember();
56 field.setAccessible(true);
57 try
58 {
59 if (field.get(object) != null)
60 {
61 continue;
62 }
63 }
64 catch (IllegalAccessException e)
65 {
66 continue;
67 }
68 IBeanBinding binding = createBinding(field.getType().getSimpleName());
69 binding.setInterface(field.getType());
70 Object proxy = binding.createProxy(new Object());
71 try
72 {
73 field.set(object, proxy);
74 }
75 catch (IllegalAccessException e)
76 {
77 throw new RuntimeException("Failed to create IntegrationBean proxy for: " + field.getType(), e);
78 }
79 }
80 return object;
81 }
82
83 protected IBeanBinding createBinding(String name)
84 {
85 IBeanFlowConstruct flow = new IBeanFlowConstruct(name + ".ibean", muleContext);
86 try
87 {
88 muleContext.getRegistry().registerObject(flow.getName(), flow, FlowConstruct.class);
89 }
90 catch (MuleException e)
91 {
92 throw new MuleRuntimeException(e);
93 }
94 return new IBeanBinding(flow, muleContext, plugin);
95 }
96 }