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 this(muleContext, registry, configResources, true);
57 }
58
59
60
61
62
63
64
65
66
67
68
69 public MuleApplicationContext(MuleContext muleContext, Registry registry, ConfigResource[] configResources, ApplicationContext parent)
70 {
71 super(parent);
72 setupParentSpringRegistry(registry);
73 this.muleContext = muleContext;
74 this.springResources = convert(configResources);
75 refresh();
76 }
77
78
79
80
81
82
83 public MuleApplicationContext(MuleContext muleContext, Registry registry, Resource[] configResources)
84 {
85 this(muleContext, registry, configResources, true);
86 }
87
88
89
90
91
92
93
94 public MuleApplicationContext(MuleContext muleContext, Registry registry, ConfigResource[] configResources, boolean refresh)
95 throws BeansException
96 {
97 this.muleContext = muleContext;
98 setupParentSpringRegistry(registry);
99 this.springResources = convert(configResources);
100 if (refresh)
101 {
102 refresh();
103 }
104 }
105
106
107
108
109
110
111 public MuleApplicationContext(MuleContext muleContext, Registry registry, Resource[] springResources, ApplicationContext parent) throws IOException
112 {
113 super(parent);
114 this.muleContext = muleContext;
115 setupParentSpringRegistry(registry);
116 this.springResources = springResources;
117 refresh();
118 }
119
120
121
122
123
124
125
126 public MuleApplicationContext(MuleContext muleContext, Registry registry, Resource[] springResources, boolean refresh)
127 throws BeansException
128 {
129 setupParentSpringRegistry(registry);
130 this.muleContext = muleContext;
131 this.springResources = springResources;
132 if (refresh)
133 {
134 refresh();
135 }
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149 protected void setupParentSpringRegistry(Registry registry)
150 {
151 registry.setParent(new SpringRegistry(this));
152 }
153
154 protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
155 super.prepareBeanFactory(beanFactory);
156 beanFactory.addBeanPostProcessor(new MuleContextPostProcessor(muleContext));
157 }
158
159 private Resource[] convert(ConfigResource[] resources)
160 {
161 Resource[] configResources = new Resource[resources.length];
162 for (int i = 0; i < resources.length; i++)
163 {
164 ConfigResource resource = resources[i];
165 if(resource.getUrl()!=null)
166 {
167 configResources[i] = new UrlResource(resource.getUrl());
168 }
169 else
170 {
171 try
172 {
173 configResources[i] = new ByteArrayResource(IOUtils.toByteArray(resource.getInputStream()), resource.getResourceName());
174 }
175 catch (IOException e)
176 {
177
178 }
179 }
180 }
181 return configResources;
182 }
183
184
185 protected Resource[] getConfigResources()
186 {
187 return springResources;
188 }
189
190 protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException
191 {
192 XmlBeanDefinitionReader beanDefinitionReader;
193
194
195
196 if (ClassUtils.isClassOnPath(LEGACY_BEAN_READER_CLASS, getClass()))
197 {
198 try
199 {
200 beanDefinitionReader = (XmlBeanDefinitionReader) ClassUtils.instanciateClass(
201 LEGACY_BEAN_READER_CLASS, new Object[] {beanFactory, springResources});
202 }
203 catch (Exception e)
204 {
205 throw new RuntimeException(e);
206 }
207 }
208 else
209 {
210 beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
211 }
212
213 beanDefinitionReader.setDocumentReaderClass(MuleBeanDefinitionDocumentReader.class);
214
215 beanDefinitionReader.setProblemReporter(new MissingParserProblemReporter());
216 beanDefinitionReader.loadBeanDefinitions(springResources);
217 }
218
219
220 protected DefaultListableBeanFactory createBeanFactory()
221 {
222
223 DefaultListableBeanFactory bf = super.createBeanFactory();
224 if(getParent()!=null)
225 {
226
227 AbstractBeanFactory beanFactory = (AbstractBeanFactory)getParent().getAutowireCapableBeanFactory();
228 bf.copyConfigurationFrom(beanFactory);
229 }
230 return bf;
231 }
232
233 }