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 this(muleContext, convert(configResources));
53 }
54
55 public MuleApplicationContext(MuleContext muleContext, Resource[] springResources) throws BeansException
56 {
57 this.muleContext = muleContext;
58 this.springResources = springResources;
59 }
60
61 protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory)
62 {
63 super.prepareBeanFactory(beanFactory);
64 beanFactory.addBeanPostProcessor(new MuleContextPostProcessor(muleContext));
65 beanFactory.addBeanPostProcessor(new ExpressionEvaluatorPostProcessor(muleContext));
66 beanFactory.registerSingleton(MuleProperties.OBJECT_MULE_CONTEXT, muleContext);
67 }
68
69 private static Resource[] convert(ConfigResource[] resources)
70 {
71 Resource[] configResources = new Resource[resources.length];
72 for (int i = 0; i < resources.length; i++)
73 {
74 ConfigResource resource = resources[i];
75 if(resource.getUrl()!=null)
76 {
77 configResources[i] = new UrlResource(resource.getUrl());
78 }
79 else
80 {
81 try
82 {
83 configResources[i] = new ByteArrayResource(IOUtils.toByteArray(resource.getInputStream()), resource.getResourceName());
84 }
85 catch (IOException e)
86 {
87 throw new RuntimeException(e);
88 }
89 }
90 }
91 return configResources;
92 }
93
94 @Override
95 protected Resource[] getConfigResources()
96 {
97 return springResources;
98 }
99
100 protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException
101 {
102 XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
103
104 beanDefinitionReader.setDocumentReaderClass(MuleBeanDefinitionDocumentReader.class);
105
106 beanDefinitionReader.setProblemReporter(new MissingParserProblemReporter());
107 beanDefinitionReader.loadBeanDefinitions(springResources);
108 }
109
110 @Override
111 protected DefaultListableBeanFactory createBeanFactory()
112 {
113
114 DefaultListableBeanFactory bf = super.createBeanFactory();
115 if (getParent() != null)
116 {
117
118 AbstractBeanFactory beanFactory = (AbstractBeanFactory)getParent().getAutowireCapableBeanFactory();
119 bf.copyConfigurationFrom(beanFactory);
120 }
121 return bf;
122 }
123
124 public MuleContext getMuleContext()
125 {
126 return muleContext;
127 }
128 }