1
2
3
4
5
6
7 package org.mule.config.spring.util;
8
9 import org.mule.api.MuleContext;
10 import org.mule.api.construct.FlowConstructAware;
11 import org.mule.api.lifecycle.InitialisationException;
12 import org.mule.api.service.Service;
13 import org.mule.api.service.ServiceAware;
14 import org.mule.config.i18n.MessageFactory;
15 import org.mule.object.AbstractObjectFactory;
16
17 import org.springframework.beans.BeansException;
18 import org.springframework.context.ApplicationContext;
19 import org.springframework.context.ApplicationContextAware;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class SpringBeanLookup extends AbstractObjectFactory implements ApplicationContextAware
52 {
53 private ApplicationContext applicationContext;
54 private String bean;
55
56 @Override
57 public void initialise() throws InitialisationException
58 {
59 if (bean == null)
60 {
61 throw new InitialisationException(MessageFactory.createStaticMessage("Bean name has not been set."), this);
62 }
63 if (applicationContext == null)
64 {
65 throw new InitialisationException(
66 MessageFactory.createStaticMessage("ApplicationContext has not been injected."), this);
67 }
68
69
70
71
72
73
74 objectClass = applicationContext.getBean(bean).getClass();
75 }
76
77 @Override
78 public void dispose()
79 {
80
81 }
82
83 @Override
84 public Object getInstance(MuleContext muleContext) throws Exception
85 {
86 Object instance = applicationContext.getBean(bean);
87 if(instance instanceof FlowConstructAware)
88 {
89
90 ((FlowConstructAware)instance).setFlowConstruct(flowConstruct);
91 }
92 if(instance instanceof ServiceAware && flowConstruct instanceof Service)
93 {
94
95 ((ServiceAware)instance).setService((Service) flowConstruct);
96 }
97 fireInitialisationCallbacks(instance);
98 return instance;
99 }
100
101 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
102 {
103 this.applicationContext = applicationContext;
104 }
105
106 public String getBean()
107 {
108 return bean;
109 }
110
111 public void setBean(String bean)
112 {
113 this.bean = bean;
114 }
115
116 @Override
117 public boolean isSingleton()
118 {
119 return applicationContext.isSingleton(bean);
120 }
121
122 @Override
123 public boolean isExternallyManagedLifecycle()
124 {
125 return true;
126 }
127
128 public boolean isAutoWireObject()
129 {
130
131 return false;
132 }
133 }