1
2
3
4
5
6
7
8
9
10
11 package org.mule.extras.spring;
12
13 import org.mule.MuleManager;
14 import org.mule.config.ConfigurationException;
15 import org.mule.config.i18n.CoreMessages;
16 import org.mule.config.i18n.MessageFactory;
17 import org.mule.extras.spring.config.CachedResource;
18 import org.mule.extras.spring.config.MuleApplicationContext;
19 import org.mule.impl.container.AbstractContainerContext;
20 import org.mule.umo.lifecycle.InitialisationException;
21 import org.mule.umo.manager.ContainerException;
22 import org.mule.umo.manager.ObjectNotFoundException;
23 import org.mule.util.ArrayUtils;
24 import org.mule.util.StringUtils;
25
26 import java.io.IOException;
27 import java.io.Reader;
28 import java.io.UnsupportedEncodingException;
29
30 import org.springframework.beans.BeansException;
31 import org.springframework.beans.factory.BeanFactory;
32 import org.springframework.beans.factory.BeanFactoryAware;
33 import org.springframework.context.ConfigurableApplicationContext;
34 import org.springframework.core.io.Resource;
35
36
37
38
39
40 public class SpringContainerContext extends AbstractContainerContext implements BeanFactoryAware
41 {
42 public static final String SPRING_DOCTYPE_REF = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE beans PUBLIC \"-//SPRING//DTD BEAN//EN\" \"http://www.springframework.org/dtd/spring-beans.dtd\">\n";
43
44
45
46
47 protected BeanFactory beanFactory;
48
49 protected BeanFactory externalBeanFactory;
50
51
52 protected String configResources;
53
54
55 protected String configXml;
56
57 public SpringContainerContext()
58 {
59 super("spring");
60 }
61
62 public Object getComponent(Object key) throws ObjectNotFoundException
63 {
64 if (getBeanFactory() == null)
65 {
66 throw new IllegalStateException("Spring Application context has not been set");
67 }
68 if (key == null)
69 {
70 throw new ObjectNotFoundException("Component not found for null key");
71 }
72
73 if (key instanceof Class)
74 {
75
76
77
78
79
80
81 throw new ObjectNotFoundException("The container is unable to build single instance of "
82 + ((Class)key).getName() + " number of instances found was: 0");
83
84
85
86
87
88 }
89 try
90 {
91 return getBeanFactory().getBean(key.toString());
92 }
93 catch (BeansException e)
94 {
95 throw new ObjectNotFoundException("Component not found for key: " + key.toString(), e);
96 }
97 }
98
99
100 public void configure(Reader reader) throws ContainerException
101 {
102 Resource[] resources;
103 try
104 {
105 resources = new Resource[]{new CachedResource(reader, MuleManager.getConfiguration().getEncoding())};
106 }
107 catch (IOException e)
108 {
109 throw new ContainerException(MessageFactory.createStaticMessage("Unable to read resource"), e);
110 }
111 setExternalBeanFactory(new MuleApplicationContext(resources));
112 }
113
114 public void initialise() throws InitialisationException
115 {
116
117 if (configXml != null)
118 {
119 final String encoding = MuleManager.getConfiguration().getEncoding();
120 Resource[] resources;
121 try
122 {
123 resources = new Resource[]{new CachedResource(configXml, MuleManager.getConfiguration().getEncoding())};
124 }
125 catch (UnsupportedEncodingException e)
126 {
127 throw new InitialisationException(CoreMessages.failedToConvertStringUsingEncoding(encoding), e);
128 }
129 setExternalBeanFactory(new MuleApplicationContext(resources));
130 }
131
132
133 else if (configResources != null)
134 {
135 String[] resources = StringUtils.splitAndTrim(configResources, ",");
136 if (logger.isDebugEnabled())
137 {
138 logger.debug("There is/are " + resources.length + " configuration resource(s): " + ArrayUtils.toString(resources));
139 }
140 setExternalBeanFactory(new MuleApplicationContext(resources));
141 }
142 }
143
144 public void dispose()
145 {
146 if (externalBeanFactory instanceof ConfigurableApplicationContext)
147 {
148 ((ConfigurableApplicationContext)externalBeanFactory).close();
149 }
150 super.dispose();
151 }
152
153
154
155
156
157
158
159
160
161
162 public BeanFactory getBeanFactory()
163 {
164 if (externalBeanFactory != null)
165 {
166 return externalBeanFactory;
167 }
168 return beanFactory;
169 }
170
171
172
173
174
175
176 public void setBeanFactory(BeanFactory beanFactory)
177 {
178 this.beanFactory = beanFactory;
179 }
180
181 public void setExternalBeanFactory(BeanFactory factory)
182 {
183 this.externalBeanFactory = factory;
184 }
185
186
187 public String getConfigXml()
188 {
189 return configXml;
190 }
191
192
193 public void setConfigXml(String configXml)
194 {
195 this.configXml = configXml;
196 }
197
198
199
200
201
202 public String getConfiguration()
203 {
204 return configXml;
205 }
206
207
208
209
210
211 public void setConfiguration(String configuration)
212 {
213 this.configXml = configuration;
214 }
215
216
217 public String getConfigResources()
218 {
219 return configResources;
220 }
221
222
223 public void setConfigResources(String configResources)
224 {
225 this.configResources = configResources;
226 }
227
228
229
230
231 public String getConfigFile()
232 {
233 return configResources;
234 }
235
236
237
238
239 public void setConfigFile(String configFile) throws ConfigurationException
240 {
241 this.configResources = configFile;
242 }
243 }