1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.config.spring; |
8 | |
|
9 | |
import org.mule.api.MuleContext; |
10 | |
import org.mule.api.config.MuleProperties; |
11 | |
import org.mule.config.ConfigResource; |
12 | |
import org.mule.util.IOUtils; |
13 | |
|
14 | |
import java.io.IOException; |
15 | |
|
16 | |
import org.springframework.beans.BeansException; |
17 | |
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
18 | |
import org.springframework.beans.factory.support.AbstractBeanFactory; |
19 | |
import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
20 | |
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; |
21 | |
import org.springframework.context.support.AbstractXmlApplicationContext; |
22 | |
import org.springframework.core.io.ByteArrayResource; |
23 | |
import org.springframework.core.io.Resource; |
24 | |
import org.springframework.core.io.UrlResource; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class MuleApplicationContext extends AbstractXmlApplicationContext |
33 | |
{ |
34 | |
private MuleContext muleContext; |
35 | |
private Resource[] springResources; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public MuleApplicationContext(MuleContext muleContext, ConfigResource[] configResources) |
46 | |
throws BeansException |
47 | |
{ |
48 | 0 | this(muleContext, convert(configResources)); |
49 | 0 | } |
50 | |
|
51 | |
public MuleApplicationContext(MuleContext muleContext, Resource[] springResources) throws BeansException |
52 | 0 | { |
53 | 0 | this.muleContext = muleContext; |
54 | 0 | this.springResources = springResources; |
55 | 0 | } |
56 | |
|
57 | |
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) |
58 | |
{ |
59 | 0 | super.prepareBeanFactory(beanFactory); |
60 | 0 | beanFactory.addBeanPostProcessor(new MuleContextPostProcessor(muleContext)); |
61 | 0 | beanFactory.addBeanPostProcessor(new ExpressionEvaluatorPostProcessor(muleContext)); |
62 | 0 | beanFactory.registerSingleton(MuleProperties.OBJECT_MULE_CONTEXT, muleContext); |
63 | 0 | } |
64 | |
|
65 | |
private static Resource[] convert(ConfigResource[] resources) |
66 | |
{ |
67 | 0 | Resource[] configResources = new Resource[resources.length]; |
68 | 0 | for (int i = 0; i < resources.length; i++) |
69 | |
{ |
70 | 0 | ConfigResource resource = resources[i]; |
71 | 0 | if(resource.getUrl()!=null) |
72 | |
{ |
73 | 0 | configResources[i] = new UrlResource(resource.getUrl()); |
74 | |
} |
75 | |
else |
76 | |
{ |
77 | |
try |
78 | |
{ |
79 | 0 | configResources[i] = new ByteArrayResource(IOUtils.toByteArray(resource.getInputStream()), resource.getResourceName()); |
80 | |
} |
81 | 0 | catch (IOException e) |
82 | |
{ |
83 | 0 | throw new RuntimeException(e); |
84 | 0 | } |
85 | |
} |
86 | |
} |
87 | 0 | return configResources; |
88 | |
} |
89 | |
|
90 | |
@Override |
91 | |
protected Resource[] getConfigResources() |
92 | |
{ |
93 | 0 | return springResources; |
94 | |
} |
95 | |
|
96 | |
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException |
97 | |
{ |
98 | 0 | XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); |
99 | |
|
100 | 0 | beanDefinitionReader.setDocumentReaderClass(MuleBeanDefinitionDocumentReader.class); |
101 | |
|
102 | 0 | beanDefinitionReader.setProblemReporter(new MissingParserProblemReporter()); |
103 | 0 | beanDefinitionReader.loadBeanDefinitions(springResources); |
104 | 0 | } |
105 | |
|
106 | |
@Override |
107 | |
protected DefaultListableBeanFactory createBeanFactory() |
108 | |
{ |
109 | |
|
110 | 0 | DefaultListableBeanFactory bf = super.createBeanFactory(); |
111 | 0 | if (getParent() != null) |
112 | |
{ |
113 | |
|
114 | 0 | AbstractBeanFactory beanFactory = (AbstractBeanFactory)getParent().getAutowireCapableBeanFactory(); |
115 | 0 | bf.copyConfigurationFrom(beanFactory); |
116 | |
} |
117 | 0 | return bf; |
118 | |
} |
119 | |
|
120 | |
public MuleContext getMuleContext() |
121 | |
{ |
122 | 0 | return muleContext; |
123 | |
} |
124 | |
} |