1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.extras.spring.config; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.config.MuleConfiguration; |
15 | |
import org.mule.extras.spring.SpringContainerContext; |
16 | |
import org.mule.impl.model.ModelFactory; |
17 | |
import org.mule.umo.UMODescriptor; |
18 | |
import org.mule.umo.UMOException; |
19 | |
import org.mule.umo.UMOInterceptorStack; |
20 | |
import org.mule.umo.endpoint.UMOEndpoint; |
21 | |
import org.mule.umo.lifecycle.InitialisationException; |
22 | |
import org.mule.umo.lifecycle.UMOLifecycleAdapterFactory; |
23 | |
import org.mule.umo.manager.UMOAgent; |
24 | |
import org.mule.umo.manager.UMOContainerContext; |
25 | |
import org.mule.umo.manager.UMOManager; |
26 | |
import org.mule.umo.manager.UMOTransactionManagerFactory; |
27 | |
import org.mule.umo.model.UMOEntryPointResolver; |
28 | |
import org.mule.umo.model.UMOModel; |
29 | |
import org.mule.umo.provider.UMOConnector; |
30 | |
import org.mule.umo.security.UMOSecurityManager; |
31 | |
import org.mule.umo.transformer.UMOTransformer; |
32 | |
|
33 | |
import java.beans.ExceptionListener; |
34 | |
import java.util.Collection; |
35 | |
import java.util.Iterator; |
36 | |
import java.util.Map; |
37 | |
|
38 | |
import org.apache.commons.logging.Log; |
39 | |
import org.apache.commons.logging.LogFactory; |
40 | |
import org.springframework.beans.BeansException; |
41 | |
import org.springframework.beans.factory.BeanInitializationException; |
42 | |
import org.springframework.beans.factory.DisposableBean; |
43 | |
import org.springframework.beans.factory.FactoryBean; |
44 | |
import org.springframework.beans.factory.InitializingBean; |
45 | |
import org.springframework.context.ApplicationContext; |
46 | |
import org.springframework.context.ApplicationContextAware; |
47 | |
import org.springframework.context.support.AbstractApplicationContext; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public class AutowireUMOManagerFactoryBean |
77 | |
implements FactoryBean, InitializingBean, DisposableBean, ApplicationContextAware |
78 | |
{ |
79 | |
|
80 | |
|
81 | |
|
82 | 36 | protected static final Log logger = LogFactory.getLog(AutowireUMOManagerFactoryBean.class); |
83 | |
|
84 | |
public static final String MULE_ENVIRONMENT_PROPERTIES_BEAN_NAME = "muleEnvironmentProperties"; |
85 | |
public static final String MULE_ENDPOINT_IDENTIFIERS_BEAN_NAME = "muleEndpointIdentifiers"; |
86 | |
public static final String MULE_INTERCEPTOR_STACK_BEAN_NAME = "muleInterceptorStacks"; |
87 | |
public static final String MULE_MODEL_EXCEPTION_STRATEGY_BEAN_NAME = "muleModelExceptionStrategy"; |
88 | |
|
89 | |
private UMOManager manager; |
90 | |
private UMOModel model; |
91 | |
|
92 | |
private AbstractApplicationContext context; |
93 | |
|
94 | |
public AutowireUMOManagerFactoryBean() throws Exception |
95 | 44 | { |
96 | 44 | this.manager = MuleManager.getInstance(); |
97 | 44 | } |
98 | |
|
99 | |
public Object getObject() throws Exception |
100 | |
{ |
101 | 0 | return manager; |
102 | |
} |
103 | |
|
104 | |
public Class getObjectType() |
105 | |
{ |
106 | 590 | return UMOManager.class; |
107 | |
} |
108 | |
|
109 | |
public boolean isSingleton() |
110 | |
{ |
111 | 18 | return true; |
112 | |
} |
113 | |
|
114 | |
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException |
115 | |
{ |
116 | 44 | context = (AbstractApplicationContext)applicationContext; |
117 | |
try |
118 | |
{ |
119 | |
|
120 | 44 | Map temp = context.getBeansOfType(MuleConfiguration.class, true, true); |
121 | 44 | if (temp.size() > 0) |
122 | |
{ |
123 | 40 | MuleManager.setConfiguration((MuleConfiguration)temp.values().iterator().next()); |
124 | |
} |
125 | |
|
126 | |
|
127 | 44 | setProperties((Map)getBean(MULE_ENVIRONMENT_PROPERTIES_BEAN_NAME, Map.class)); |
128 | |
|
129 | |
|
130 | 44 | Map connectors = context.getBeansOfType(UMOConnector.class, true, true); |
131 | 44 | setConnectors(connectors.values()); |
132 | |
|
133 | |
|
134 | 44 | setMessageEndpointIdentifiers((Map)getBean(MULE_ENDPOINT_IDENTIFIERS_BEAN_NAME, Map.class)); |
135 | |
|
136 | |
|
137 | 44 | temp = context.getBeansOfType(UMOTransactionManagerFactory.class, true, true); |
138 | 44 | if (temp.size() > 0) |
139 | |
{ |
140 | 20 | manager.setTransactionManager(((UMOTransactionManagerFactory)temp.values().iterator().next()).create()); |
141 | |
} |
142 | |
|
143 | |
|
144 | 44 | temp = context.getBeansOfType(UMOSecurityManager.class, true, true); |
145 | 44 | if (temp.size() > 0) |
146 | |
{ |
147 | 0 | manager.setSecurityManager((UMOSecurityManager)temp.values().iterator().next()); |
148 | |
} |
149 | |
|
150 | |
|
151 | 44 | Map transformers = context.getBeansOfType(UMOTransformer.class, true, true); |
152 | 44 | setTransformers(transformers.values()); |
153 | |
|
154 | |
|
155 | 44 | Map endpoints = context.getBeansOfType(UMOEndpoint.class, true, true); |
156 | 44 | setEndpoints(endpoints.values()); |
157 | |
|
158 | |
|
159 | 44 | Map agents = context.getBeansOfType(UMOAgent.class, true, true); |
160 | 44 | setAgents(agents.values()); |
161 | |
|
162 | |
|
163 | 44 | Map containers = context.getBeansOfType(UMOContainerContext.class, true, true); |
164 | 44 | setContainerContext(containers); |
165 | |
|
166 | |
|
167 | 44 | Map interceptors = context.getBeansOfType(UMOInterceptorStack.class, true, true); |
168 | 44 | setInterceptorStacks(interceptors); |
169 | |
|
170 | 44 | createModel(); |
171 | |
|
172 | |
|
173 | 44 | Map components = context.getBeansOfType(UMODescriptor.class, true, true); |
174 | 44 | setComponents(components.values()); |
175 | |
} |
176 | 0 | catch (Exception e) |
177 | |
{ |
178 | 0 | throw new BeanInitializationException("Failed to wire MuleManager together: " + e.getMessage(), e); |
179 | 44 | } |
180 | 44 | } |
181 | |
|
182 | |
public void setManagerId(String managerId) |
183 | |
{ |
184 | 20 | manager.setId(managerId); |
185 | 20 | } |
186 | |
|
187 | |
protected void createModel() throws UMOException |
188 | |
{ |
189 | |
|
190 | 44 | Map temp = context.getBeansOfType(UMOModel.class, true, true); |
191 | 44 | if (temp.size() > 1) |
192 | |
{ |
193 | 0 | throw new IllegalStateException("In Mule 1.x only one model can be created when using Spring"); |
194 | |
} |
195 | 44 | else if(temp.size()==1) |
196 | |
{ |
197 | 20 | Map.Entry entry = (Map.Entry)temp.entrySet().iterator().next(); |
198 | 20 | model = (UMOModel)entry.getValue(); |
199 | 20 | model.setName(entry.getKey().toString()); |
200 | 20 | } |
201 | |
else |
202 | |
{ |
203 | |
|
204 | 24 | model = ModelFactory.createModel(MuleManager.getConfiguration().getModelType()); |
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | 44 | Map epr = context.getBeansOfType(UMOEntryPointResolver.class, true, true); |
216 | 44 | if (epr.size() > 0) |
217 | |
{ |
218 | 20 | model.setEntryPointResolver((UMOEntryPointResolver)epr.values().iterator().next()); |
219 | |
} |
220 | |
|
221 | |
|
222 | 44 | Map lcaf = context.getBeansOfType(UMOLifecycleAdapterFactory.class, true, true); |
223 | 44 | if (lcaf.size() > 0) |
224 | |
{ |
225 | 20 | model.setLifecycleAdapterFactory((UMOLifecycleAdapterFactory)lcaf.values().iterator().next()); |
226 | |
} |
227 | |
|
228 | |
|
229 | 44 | Object listener = getBean(MULE_MODEL_EXCEPTION_STRATEGY_BEAN_NAME, ExceptionListener.class); |
230 | 44 | if (listener != null) |
231 | |
{ |
232 | 18 | model.setExceptionListener((ExceptionListener)listener); |
233 | |
} |
234 | |
|
235 | 44 | manager.registerModel(model); |
236 | |
|
237 | 44 | } |
238 | |
|
239 | |
private Object getBean(String name, Class clazz) |
240 | |
{ |
241 | |
try |
242 | |
{ |
243 | 132 | return context.getBean(name, clazz); |
244 | |
} |
245 | 54 | catch (BeansException e) |
246 | |
{ |
247 | 54 | return null; |
248 | |
} |
249 | |
|
250 | |
} |
251 | |
|
252 | |
protected void setContainerContext(Map containers) throws UMOException |
253 | |
{ |
254 | 44 | if (containers.size() == 0) |
255 | |
{ |
256 | |
|
257 | 42 | SpringContainerContext container = new SpringContainerContext(); |
258 | 42 | container.setBeanFactory(context); |
259 | 42 | manager.setContainerContext(container); |
260 | 42 | } |
261 | 2 | else if (containers.size() == 1) |
262 | |
{ |
263 | 2 | manager.setContainerContext((UMOContainerContext)containers.values().iterator().next()); |
264 | |
} |
265 | |
else |
266 | |
{ |
267 | 0 | UMOContainerContext ctx = (UMOContainerContext)containers.values().iterator().next(); |
268 | 0 | logger.warn("There are " + containers.size() |
269 | |
+ " container contexts in the spring context. Using the first one: " |
270 | |
+ ctx.getClass().getName()); |
271 | 0 | manager.setContainerContext(ctx); |
272 | |
} |
273 | 44 | } |
274 | |
|
275 | |
protected void setMessageEndpointIdentifiers(Map endpoints) throws InitialisationException |
276 | |
{ |
277 | 44 | if (endpoints == null) |
278 | |
{ |
279 | 24 | return; |
280 | |
} |
281 | |
Map.Entry entry; |
282 | 20 | for (Iterator iterator = endpoints.entrySet().iterator(); iterator.hasNext();) |
283 | |
{ |
284 | 60 | entry = (Map.Entry)iterator.next(); |
285 | 60 | manager.registerEndpointIdentifier(entry.getKey().toString(), entry.getValue().toString()); |
286 | |
|
287 | |
} |
288 | 20 | } |
289 | |
|
290 | |
protected void setAgents(Collection agents) throws UMOException |
291 | |
{ |
292 | 44 | for (Iterator iterator = agents.iterator(); iterator.hasNext();) |
293 | |
{ |
294 | 20 | manager.registerAgent((UMOAgent)iterator.next()); |
295 | |
} |
296 | 44 | } |
297 | |
|
298 | |
protected void setProperties(Map props) |
299 | |
{ |
300 | 44 | if (props == null) |
301 | |
{ |
302 | 4 | return; |
303 | |
} |
304 | |
Map.Entry entry; |
305 | 40 | for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();) |
306 | |
{ |
307 | 100 | entry = (Map.Entry)iterator.next(); |
308 | 100 | manager.setProperty(entry.getKey(), entry.getValue()); |
309 | |
} |
310 | 40 | } |
311 | |
|
312 | |
protected void setConnectors(Collection connectors) throws UMOException |
313 | |
{ |
314 | 44 | for (Iterator iterator = connectors.iterator(); iterator.hasNext();) |
315 | |
{ |
316 | 26 | manager.registerConnector((UMOConnector)iterator.next()); |
317 | |
} |
318 | 44 | } |
319 | |
|
320 | |
protected void setTransformers(Collection transformers) throws InitialisationException |
321 | |
{ |
322 | 44 | for (Iterator iterator = transformers.iterator(); iterator.hasNext();) |
323 | |
{ |
324 | 38 | manager.registerTransformer((UMOTransformer)iterator.next()); |
325 | |
} |
326 | 44 | } |
327 | |
|
328 | |
protected void setEndpoints(Collection endpoints) throws InitialisationException |
329 | |
{ |
330 | 44 | for (Iterator iterator = endpoints.iterator(); iterator.hasNext();) |
331 | |
{ |
332 | 104 | manager.registerEndpoint((UMOEndpoint)iterator.next()); |
333 | |
} |
334 | 44 | } |
335 | |
|
336 | |
protected void setComponents(Collection components) throws UMOException |
337 | |
{ |
338 | |
UMODescriptor d; |
339 | 44 | for (Iterator iterator = components.iterator(); iterator.hasNext();) |
340 | |
{ |
341 | 52 | d = (UMODescriptor)iterator.next(); |
342 | 52 | if (!model.isComponentRegistered(d.getName())) |
343 | |
{ |
344 | 52 | model.registerComponent(d); |
345 | |
} |
346 | |
} |
347 | 44 | } |
348 | |
|
349 | |
protected void setInterceptorStacks(Map stacks) |
350 | |
{ |
351 | 44 | if (stacks == null) |
352 | |
{ |
353 | 0 | return; |
354 | |
} |
355 | 44 | for (Iterator iterator = stacks.entrySet().iterator(); iterator.hasNext();) |
356 | |
{ |
357 | 20 | Map.Entry entry = (Map.Entry)iterator.next(); |
358 | 20 | String name = entry.getKey().toString(); |
359 | 20 | manager.registerInterceptorStack(name, (UMOInterceptorStack)entry.getValue()); |
360 | 20 | } |
361 | 44 | } |
362 | |
|
363 | |
public void afterPropertiesSet() throws Exception |
364 | |
{ |
365 | 44 | manager.start(); |
366 | 44 | } |
367 | |
|
368 | |
public void destroy() throws Exception |
369 | |
{ |
370 | 20 | manager.dispose(); |
371 | 20 | } |
372 | |
} |