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 | 0 | super("spring"); |
60 | 0 | } |
61 | |
|
62 | |
public Object getComponent(Object key) throws ObjectNotFoundException |
63 | |
{ |
64 | 0 | if (getBeanFactory() == null) |
65 | |
{ |
66 | 0 | throw new IllegalStateException("Spring Application context has not been set"); |
67 | |
} |
68 | 0 | if (key == null) |
69 | |
{ |
70 | 0 | throw new ObjectNotFoundException("Component not found for null key"); |
71 | |
} |
72 | |
|
73 | 0 | if (key instanceof Class) |
74 | |
{ |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | 0 | 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 | 0 | return getBeanFactory().getBean(key.toString()); |
92 | |
} |
93 | 0 | catch (BeansException e) |
94 | |
{ |
95 | 0 | 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 | 0 | resources = new Resource[]{new CachedResource(reader, MuleManager.getConfiguration().getEncoding())}; |
106 | |
} |
107 | 0 | catch (IOException e) |
108 | |
{ |
109 | 0 | throw new ContainerException(MessageFactory.createStaticMessage("Unable to read resource"), e); |
110 | 0 | } |
111 | 0 | setExternalBeanFactory(new MuleApplicationContext(resources)); |
112 | 0 | } |
113 | |
|
114 | |
public void initialise() throws InitialisationException |
115 | |
{ |
116 | |
|
117 | 0 | if (configXml != null) |
118 | |
{ |
119 | 0 | final String encoding = MuleManager.getConfiguration().getEncoding(); |
120 | |
Resource[] resources; |
121 | |
try |
122 | |
{ |
123 | 0 | resources = new Resource[]{new CachedResource(configXml, MuleManager.getConfiguration().getEncoding())}; |
124 | |
} |
125 | 0 | catch (UnsupportedEncodingException e) |
126 | |
{ |
127 | 0 | throw new InitialisationException(CoreMessages.failedToConvertStringUsingEncoding(encoding), e); |
128 | 0 | } |
129 | 0 | setExternalBeanFactory(new MuleApplicationContext(resources)); |
130 | |
} |
131 | |
|
132 | |
|
133 | 0 | else if (configResources != null) |
134 | |
{ |
135 | 0 | String[] resources = StringUtils.splitAndTrim(configResources, ","); |
136 | 0 | if (logger.isDebugEnabled()) |
137 | |
{ |
138 | 0 | logger.debug("There is/are " + resources.length + " configuration resource(s): " + ArrayUtils.toString(resources)); |
139 | |
} |
140 | 0 | setExternalBeanFactory(new MuleApplicationContext(resources)); |
141 | |
} |
142 | 0 | } |
143 | |
|
144 | |
public void dispose() |
145 | |
{ |
146 | 0 | if (externalBeanFactory instanceof ConfigurableApplicationContext) |
147 | |
{ |
148 | 0 | ((ConfigurableApplicationContext)externalBeanFactory).close(); |
149 | |
} |
150 | 0 | super.dispose(); |
151 | 0 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public BeanFactory getBeanFactory() |
163 | |
{ |
164 | 0 | if (externalBeanFactory != null) |
165 | |
{ |
166 | 0 | return externalBeanFactory; |
167 | |
} |
168 | 0 | return beanFactory; |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
public void setBeanFactory(BeanFactory beanFactory) |
177 | |
{ |
178 | 0 | this.beanFactory = beanFactory; |
179 | 0 | } |
180 | |
|
181 | |
public void setExternalBeanFactory(BeanFactory factory) |
182 | |
{ |
183 | 0 | this.externalBeanFactory = factory; |
184 | 0 | } |
185 | |
|
186 | |
|
187 | |
public String getConfigXml() |
188 | |
{ |
189 | 0 | return configXml; |
190 | |
} |
191 | |
|
192 | |
|
193 | |
public void setConfigXml(String configXml) |
194 | |
{ |
195 | 0 | this.configXml = configXml; |
196 | 0 | } |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
public String getConfiguration() |
203 | |
{ |
204 | 0 | return configXml; |
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
public void setConfiguration(String configuration) |
212 | |
{ |
213 | 0 | this.configXml = configuration; |
214 | 0 | } |
215 | |
|
216 | |
|
217 | |
public String getConfigResources() |
218 | |
{ |
219 | 0 | return configResources; |
220 | |
} |
221 | |
|
222 | |
|
223 | |
public void setConfigResources(String configResources) |
224 | |
{ |
225 | 0 | this.configResources = configResources; |
226 | 0 | } |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
public String getConfigFile() |
232 | |
{ |
233 | 0 | return configResources; |
234 | |
} |
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
public void setConfigFile(String configFile) throws ConfigurationException |
240 | |
{ |
241 | 0 | this.configResources = configFile; |
242 | 0 | } |
243 | |
} |