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