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