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