KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > spring > config > AutowireUMOManagerFactoryBean


1 /*
2  * $Id: AutowireUMOManagerFactoryBean.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.extras.spring.config;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.mule.MuleManager;
16 import org.mule.config.MuleConfiguration;
17 import org.mule.extras.spring.SpringContainerContext;
18 import org.mule.impl.model.ModelFactory;
19 import org.mule.umo.UMODescriptor;
20 import org.mule.umo.UMOException;
21 import org.mule.umo.UMOInterceptorStack;
22 import org.mule.umo.endpoint.UMOEndpoint;
23 import org.mule.umo.lifecycle.InitialisationException;
24 import org.mule.umo.lifecycle.UMOLifecycleAdapterFactory;
25 import org.mule.umo.manager.UMOAgent;
26 import org.mule.umo.manager.UMOContainerContext;
27 import org.mule.umo.manager.UMOManager;
28 import org.mule.umo.manager.UMOTransactionManagerFactory;
29 import org.mule.umo.model.UMOEntryPointResolver;
30 import org.mule.umo.model.UMOModel;
31 import org.mule.umo.provider.UMOConnector;
32 import org.mule.umo.security.UMOSecurityManager;
33 import org.mule.umo.transformer.UMOTransformer;
34 import org.springframework.beans.BeansException;
35 import org.springframework.beans.factory.BeanInitializationException;
36 import org.springframework.beans.factory.DisposableBean;
37 import org.springframework.beans.factory.FactoryBean;
38 import org.springframework.beans.factory.InitializingBean;
39 import org.springframework.context.ApplicationContext;
40 import org.springframework.context.ApplicationContextAware;
41 import org.springframework.context.support.AbstractApplicationContext;
42
43 import java.beans.ExceptionListener JavaDoc;
44 import java.util.Collection JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.Map JavaDoc;
47
48 /**
49  * <code>UMOManagerFactoryBean</code> is a MuleManager factory bean that is used to
50  * configure the MuleManager from a spring context. This factory bean is responsible
51  * for determining the instance type of UMOManager to create and then delegates
52  * configuration calls to that instance depending on what is available in the
53  * container. <p/> Apart from removing the need to explicitly wire the MuleManager
54  * instance together there another advantage to using the
55  * AutowireUMOManagerFactoryBean. There is no need to declare a UMOModel instance in
56  * the configuration. If the factory doesn't find a UMOModel implementation it
57  * creates a default one of type <i>org.mule.impl.model.seda.SedaModel</i>. The
58  * model is automatically initialised with a SpringContainercontext using the current
59  * beanFactory and defaults are used for the other Model properties. If you want to
60  * override the defaults, such as define your own exception strategy, (which you will
61  * most likely want to do) simply declare your exception strategy bean in the
62  * container and it will automatically be set on the model. <p/> Most Mule objects
63  * have explicit types and can be autowired, however some objects cannot be
64  * autowired, such as a <i>java.util.Map</i> of endpoints for example. For these
65  * objects Mule defines standard bean names that will be looked for in the container
66  * during start up. <p/> muleEnvironmentProperties A map of properties to set on the
67  * MuleManager. Accessible from your code using
68  * AutowireUMOManagerFactoryBean.MULE_ENVIRONMENT_PROPERTIES_BEAN_NAME. <p/>
69  * muleEndpointMappings A Map of logical endpointUri mappings accessible from your
70  * code using AutowireUMOManagerFactoryBean.MULE_ENDPOINT_MAPPINGS_BEAN_NAME. <p/>
71  * muleInterceptorStacks A map of interceptor stacks, where the name of the stack is
72  * the key and a list of interceptors is the value. Accessible using from your code
73  * using AutowireUMOManagerFactoryBean.MULE_INTERCEPTOR_STACK_BEAN_NAME.
74  */

75 public class AutowireUMOManagerFactoryBean
76     implements FactoryBean, InitializingBean, DisposableBean, ApplicationContextAware
77 {
78     /**
79      * logger used by this class
80      */

81     protected static Log logger = LogFactory.getLog(AutowireUMOManagerFactoryBean.class);
82
83     public static final String JavaDoc MULE_ENVIRONMENT_PROPERTIES_BEAN_NAME = "muleEnvironmentProperties";
84     public static final String JavaDoc MULE_ENDPOINT_IDENTIFIERS_BEAN_NAME = "muleEndpointIdentifiers";
85     public static final String JavaDoc MULE_INTERCEPTOR_STACK_BEAN_NAME = "muleInterceptorStacks";
86     public static final String JavaDoc MULE_MODEL_EXCEPTION_STRATEGY_BEAN_NAME = "muleModelExceptionStrategy";
87
88     private UMOManager manager;
89
90     private AbstractApplicationContext context;
91
92     public AutowireUMOManagerFactoryBean() throws Exception JavaDoc
93     {
94         this.manager = MuleManager.getInstance();
95     }
96
97     public Object JavaDoc getObject() throws Exception JavaDoc
98     {
99         return manager;
100     }
101
102     public Class JavaDoc getObjectType()
103     {
104         return UMOManager.class;
105     }
106
107     public boolean isSingleton()
108     {
109         return true;
110     }
111
112     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
113     {
114         context = (AbstractApplicationContext)applicationContext;
115         try
116         {
117             // set mule configuration
118
Map JavaDoc temp = context.getBeansOfType(MuleConfiguration.class, true, true);
119             if (temp.size() > 0)
120             {
121                 MuleManager.setConfiguration((MuleConfiguration)temp.values().iterator().next());
122             }
123
124             // set environment properties
125
setProperties((Map JavaDoc)getBean(MULE_ENVIRONMENT_PROPERTIES_BEAN_NAME, Map JavaDoc.class));
126
127             // set Connectors
128
Map JavaDoc connectors = context.getBeansOfType(UMOConnector.class, true, true);
129             setConnectors(connectors.values());
130
131             // set endpoint Identifiers
132
setMessageEndpointIdentifiers((Map JavaDoc)getBean(MULE_ENDPOINT_IDENTIFIERS_BEAN_NAME, Map JavaDoc.class));
133
134             // set mule transaction manager
135
temp = context.getBeansOfType(UMOTransactionManagerFactory.class, true, true);
136             if (temp.size() > 0)
137             {
138                 manager.setTransactionManager(((UMOTransactionManagerFactory)temp.values().iterator().next()).create());
139             }
140
141             // set security manager
142
temp = context.getBeansOfType(UMOSecurityManager.class, true, true);
143             if (temp.size() > 0)
144             {
145                 manager.setSecurityManager((UMOSecurityManager)temp.values().iterator().next());
146             }
147
148             // set Transformers
149
Map JavaDoc transformers = context.getBeansOfType(UMOTransformer.class, true, true);
150             setTransformers(transformers.values());
151
152             // set Endpoints
153
Map JavaDoc endpoints = context.getBeansOfType(UMOEndpoint.class, true, true);
154             setEndpoints(endpoints.values());
155
156             // set Agents
157
Map JavaDoc agents = context.getBeansOfType(UMOAgent.class, true, true);
158             setAgents(agents.values());
159
160             // Set the container Context
161
Map JavaDoc containers = context.getBeansOfType(UMOContainerContext.class, true, true);
162             setContainerContext(containers);
163
164             // interceptors
165
Map JavaDoc interceptors = context.getBeansOfType(UMOInterceptorStack.class, true, true);
166             setInterceptorStacks(interceptors);
167             // create the model
168
createModel();
169
170             // set Components
171
Map JavaDoc components = context.getBeansOfType(UMODescriptor.class, true, true);
172             setComponents(components.values());
173         }
174         catch (Exception JavaDoc e)
175         {
176             throw new BeanInitializationException("Failed to wire MuleManager together: " + e.getMessage(), e);
177         }
178     }
179
180     public void setManagerId(String JavaDoc managerId)
181     {
182         manager.setId(managerId);
183     }
184
185     protected void createModel() throws UMOException
186     {
187         // set the model
188
Map JavaDoc temp = context.getBeansOfType(UMOModel.class, true, true);
189         UMOModel model;
190         if (temp.size() > 0)
191         {
192             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)temp.entrySet().iterator().next();
193             model = (UMOModel)entry.getValue();
194             model.setName(entry.getKey().toString());
195         }
196         else
197         {
198             // create a defaultModel
199
model = ModelFactory.createModel(MuleManager.getConfiguration().getModelType());
200         }
201
202         // autowire the model so any ExceptionStrategy or PoolingStrategy beans
203
// can be set
204
// context.getBeanFactory().autowireBeanProperties(model,
205
// AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
206
// RM we cant autowire the model by type as some list properties
207
// conflict with each other
208

209         // Entry point resolver
210
Map JavaDoc epr = context.getBeansOfType(UMOEntryPointResolver.class, true, true);
211         if (epr.size() > 0)
212         {
213             model.setEntryPointResolver((UMOEntryPointResolver)epr.values().iterator().next());
214         }
215
216         // Life cycle adapter factory
217
Map JavaDoc lcaf = context.getBeansOfType(UMOLifecycleAdapterFactory.class, true, true);
218         if (lcaf.size() > 0)
219         {
220             model.setLifecycleAdapterFactory((UMOLifecycleAdapterFactory)lcaf.values().iterator().next());
221         }
222
223         // Model exception strategy
224
Object JavaDoc listener = getBean(MULE_MODEL_EXCEPTION_STRATEGY_BEAN_NAME, ExceptionListener JavaDoc.class);
225         if (listener != null)
226         {
227             model.setExceptionListener((ExceptionListener JavaDoc)listener);
228         }
229
230         manager.setModel(model);
231
232     }
233
234     private Object JavaDoc getBean(String JavaDoc name, Class JavaDoc clazz)
235     {
236         try
237         {
238             return context.getBean(name, clazz);
239         }
240         catch (BeansException e)
241         {
242             return null;
243         }
244
245     }
246
247     protected void setContainerContext(Map JavaDoc containers) throws UMOException
248     {
249         if (containers.size() == 0)
250         {
251             // Use this as the default container
252
SpringContainerContext container = new SpringContainerContext();
253             container.setBeanFactory(context);
254             manager.setContainerContext(container);
255         }
256         else if (containers.size() == 1)
257         {
258             manager.setContainerContext((UMOContainerContext)containers.values().iterator().next());
259         }
260         else
261         {
262             UMOContainerContext ctx = (UMOContainerContext)containers.values().iterator().next();
263             logger.warn("There are " + containers.size()
264                         + " container contexts in the spring context. Using the first one: "
265                         + ctx.getClass().getName());
266             manager.setContainerContext(ctx);
267         }
268     }
269
270     protected void setMessageEndpointIdentifiers(Map JavaDoc endpoints) throws InitialisationException
271     {
272         if (endpoints == null)
273         {
274             return;
275         }
276         Map.Entry JavaDoc entry;
277         for (Iterator JavaDoc iterator = endpoints.entrySet().iterator(); iterator.hasNext();)
278         {
279             entry = (Map.Entry JavaDoc)iterator.next();
280             manager.registerEndpointIdentifier(entry.getKey().toString(), entry.getValue().toString());
281
282         }
283     }
284
285     protected void setAgents(Collection JavaDoc agents) throws UMOException
286     {
287         for (Iterator JavaDoc iterator = agents.iterator(); iterator.hasNext();)
288         {
289             manager.registerAgent((UMOAgent)iterator.next());
290         }
291     }
292
293     protected void setProperties(Map JavaDoc props)
294     {
295         if (props == null)
296         {
297             return;
298         }
299         Map.Entry JavaDoc entry;
300         for (Iterator JavaDoc iterator = props.entrySet().iterator(); iterator.hasNext();)
301         {
302             entry = (Map.Entry JavaDoc)iterator.next();
303             manager.setProperty(entry.getKey(), entry.getValue());
304         }
305     }
306
307     protected void setConnectors(Collection JavaDoc connectors) throws UMOException
308     {
309         for (Iterator JavaDoc iterator = connectors.iterator(); iterator.hasNext();)
310         {
311             manager.registerConnector((UMOConnector)iterator.next());
312         }
313     }
314
315     protected void setTransformers(Collection JavaDoc transformers) throws InitialisationException
316     {
317         for (Iterator JavaDoc iterator = transformers.iterator(); iterator.hasNext();)
318         {
319             manager.registerTransformer((UMOTransformer)iterator.next());
320         }
321     }
322
323     protected void setEndpoints(Collection JavaDoc endpoints) throws InitialisationException
324     {
325         for (Iterator JavaDoc iterator = endpoints.iterator(); iterator.hasNext();)
326         {
327             manager.registerEndpoint((UMOEndpoint)iterator.next());
328         }
329     }
330
331     protected void setComponents(Collection JavaDoc components) throws UMOException
332     {
333         UMODescriptor d;
334         for (Iterator JavaDoc iterator = components.iterator(); iterator.hasNext();)
335         {
336             d = (UMODescriptor)iterator.next();
337             if (!manager.getModel().isComponentRegistered(d.getName()))
338             {
339                 manager.getModel().registerComponent(d);
340             }
341         }
342     }
343
344     protected void setInterceptorStacks(Map JavaDoc stacks)
345     {
346         if (stacks == null)
347         {
348             return;
349         }
350         for (Iterator JavaDoc iterator = stacks.entrySet().iterator(); iterator.hasNext();)
351         {
352             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
353             String JavaDoc name = entry.getKey().toString();
354             manager.registerInterceptorStack(name, (UMOInterceptorStack)entry.getValue());
355         }
356     }
357
358     public void afterPropertiesSet() throws Exception JavaDoc
359     {
360         manager.start();
361     }
362
363     public void destroy() throws Exception JavaDoc
364     {
365         manager.dispose();
366     }
367 }
368
Popular Tags