1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config.spring; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.registry.Registry; |
15 | |
import org.mule.config.ConfigResource; |
16 | |
import org.mule.util.ClassUtils; |
17 | |
import org.mule.util.IOUtils; |
18 | |
|
19 | |
import java.io.IOException; |
20 | |
|
21 | |
import org.springframework.beans.BeansException; |
22 | |
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
23 | |
import org.springframework.beans.factory.support.AbstractBeanFactory; |
24 | |
import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
25 | |
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; |
26 | |
import org.springframework.context.ApplicationContext; |
27 | |
import org.springframework.context.support.AbstractXmlApplicationContext; |
28 | |
import org.springframework.core.io.ByteArrayResource; |
29 | |
import org.springframework.core.io.Resource; |
30 | |
import org.springframework.core.io.UrlResource; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class MuleApplicationContext extends AbstractXmlApplicationContext |
39 | |
{ |
40 | |
public static final String LEGACY_BEAN_READER_CLASS = "org.mule.config.spring.MuleBeanDefinitionReader"; |
41 | |
|
42 | |
private MuleContext muleContext; |
43 | |
private Resource[] springResources; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public MuleApplicationContext(MuleContext muleContext, Registry registry, ConfigResource[] configResources) |
55 | |
{ |
56 | 0 | this(muleContext, registry, configResources, true); |
57 | 0 | } |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public MuleApplicationContext(MuleContext muleContext, Registry registry, ConfigResource[] configResources, ApplicationContext parent) |
70 | |
{ |
71 | 0 | super(parent); |
72 | 0 | setupParentSpringRegistry(registry); |
73 | 0 | this.muleContext = muleContext; |
74 | 0 | this.springResources = convert(configResources); |
75 | 0 | refresh(); |
76 | 0 | } |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public MuleApplicationContext(MuleContext muleContext, Registry registry, Resource[] configResources) |
84 | |
{ |
85 | 0 | this(muleContext, registry, configResources, true); |
86 | 0 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public MuleApplicationContext(MuleContext muleContext, Registry registry, ConfigResource[] configResources, boolean refresh) |
95 | |
throws BeansException |
96 | 0 | { |
97 | 0 | this.muleContext = muleContext; |
98 | 0 | setupParentSpringRegistry(registry); |
99 | 0 | this.springResources = convert(configResources); |
100 | 0 | if (refresh) |
101 | |
{ |
102 | 0 | refresh(); |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public MuleApplicationContext(MuleContext muleContext, Registry registry, Resource[] springResources, ApplicationContext parent) throws IOException |
112 | |
{ |
113 | 0 | super(parent); |
114 | 0 | this.muleContext = muleContext; |
115 | 0 | setupParentSpringRegistry(registry); |
116 | 0 | this.springResources = springResources; |
117 | 0 | refresh(); |
118 | 0 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public MuleApplicationContext(MuleContext muleContext, Registry registry, Resource[] springResources, boolean refresh) |
127 | |
throws BeansException |
128 | 0 | { |
129 | 0 | setupParentSpringRegistry(registry); |
130 | 0 | this.muleContext = muleContext; |
131 | 0 | this.springResources = springResources; |
132 | 0 | if (refresh) |
133 | |
{ |
134 | 0 | refresh(); |
135 | |
} |
136 | 0 | } |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
protected void setupParentSpringRegistry(Registry registry) |
150 | |
{ |
151 | 0 | registry.setParent(new SpringRegistry(this)); |
152 | 0 | } |
153 | |
|
154 | |
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { |
155 | 0 | super.prepareBeanFactory(beanFactory); |
156 | 0 | beanFactory.addBeanPostProcessor(new MuleContextPostProcessor(muleContext)); |
157 | 0 | } |
158 | |
|
159 | |
private Resource[] convert(ConfigResource[] resources) |
160 | |
{ |
161 | 0 | Resource[] configResources = new Resource[resources.length]; |
162 | 0 | for (int i = 0; i < resources.length; i++) |
163 | |
{ |
164 | 0 | ConfigResource resource = resources[i]; |
165 | 0 | if(resource.getUrl()!=null) |
166 | |
{ |
167 | 0 | configResources[i] = new UrlResource(resource.getUrl()); |
168 | |
} |
169 | |
else |
170 | |
{ |
171 | |
try |
172 | |
{ |
173 | 0 | configResources[i] = new ByteArrayResource(IOUtils.toByteArray(resource.getInputStream()), resource.getResourceName()); |
174 | |
} |
175 | 0 | catch (IOException e) |
176 | |
{ |
177 | |
|
178 | 0 | } |
179 | |
} |
180 | |
} |
181 | 0 | return configResources; |
182 | |
} |
183 | |
|
184 | |
|
185 | |
protected Resource[] getConfigResources() |
186 | |
{ |
187 | 0 | return springResources; |
188 | |
} |
189 | |
|
190 | |
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException |
191 | |
{ |
192 | |
XmlBeanDefinitionReader beanDefinitionReader; |
193 | |
|
194 | |
|
195 | |
|
196 | 0 | if (ClassUtils.isClassOnPath(LEGACY_BEAN_READER_CLASS, getClass())) |
197 | |
{ |
198 | |
try |
199 | |
{ |
200 | 0 | beanDefinitionReader = (XmlBeanDefinitionReader) ClassUtils.instanciateClass( |
201 | |
LEGACY_BEAN_READER_CLASS, new Object[] {beanFactory, springResources}); |
202 | |
} |
203 | 0 | catch (Exception e) |
204 | |
{ |
205 | 0 | throw new RuntimeException(e); |
206 | 0 | } |
207 | |
} |
208 | |
else |
209 | |
{ |
210 | 0 | beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); |
211 | |
} |
212 | |
|
213 | 0 | beanDefinitionReader.setDocumentReaderClass(MuleBeanDefinitionDocumentReader.class); |
214 | |
|
215 | 0 | beanDefinitionReader.setProblemReporter(new MissingParserProblemReporter()); |
216 | 0 | beanDefinitionReader.loadBeanDefinitions(springResources); |
217 | 0 | } |
218 | |
|
219 | |
|
220 | |
protected DefaultListableBeanFactory createBeanFactory() |
221 | |
{ |
222 | |
|
223 | 0 | DefaultListableBeanFactory bf = super.createBeanFactory(); |
224 | 0 | if(getParent()!=null) |
225 | |
{ |
226 | |
|
227 | 0 | AbstractBeanFactory beanFactory = (AbstractBeanFactory)getParent().getAutowireCapableBeanFactory(); |
228 | 0 | bf.copyConfigurationFrom(beanFactory); |
229 | |
} |
230 | 0 | return bf; |
231 | |
} |
232 | |
|
233 | |
} |