KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: MuleManagerBean.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.mule.MuleManager;
14 import org.mule.config.builders.MuleXmlConfigurationBuilder;
15 import org.mule.config.ConfigurationBuilder;
16 import org.mule.extras.spring.SpringContainerContext;
17 import org.mule.umo.manager.UMOManager;
18 import org.springframework.beans.BeansException;
19 import org.springframework.beans.factory.DisposableBean;
20 import org.springframework.beans.factory.InitializingBean;
21 import org.springframework.context.ApplicationContext;
22 import org.springframework.context.ApplicationContextAware;
23 import org.springframework.context.ApplicationEvent;
24 import org.springframework.context.ApplicationListener;
25 import org.springframework.core.io.Resource;
26 import org.springframework.util.StringUtils;
27
28 import java.io.IOException JavaDoc;
29
30 /**
31  * This Bean can e used to bootstrap a MuleManager instance in a Spring context. This
32  * is different to the <code>AutoWireUMOManagerFactoryBean</code> in that the
33  * Manager is not initialised using beans from the ApplicationContext. Instead, a
34  * list of Mule Configuration resources can be passed in. The Configuration builder
35  * can be overloaded so that other types of configuration resources, such as
36  * BeanShell or Groovy scripts cn be used to actually configure the server. For
37  * example to pick up all Mule confuration resources from the classpath, use
38  * something like - <beans> <bean id="muleManager" class="eg.mule.MuleManagerBean"
39  * depends-on="jms.broker"> <property name="configResources"
40  * value="classpath*:META-INF/services/*.mule.xml"/> </bean> .... </beans>
41  */

42 public class MuleManagerBean
43     implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener
44 {
45
46     private Resource[] configResources;
47     private SpringContainerContext containerContext;
48     private UMOManager muleManager;
49     private ConfigurationBuilder configurationBuilder;
50
51     public void afterPropertiesSet() throws Exception JavaDoc
52     {
53         if (configurationBuilder == null)
54         {
55             configurationBuilder = new MuleXmlConfigurationBuilder();
56         }
57     }
58
59     public void setConfigResources(Resource[] configResources)
60     {
61         this.configResources = configResources;
62     }
63
64     public void destroy() throws Exception JavaDoc
65     {
66         if (muleManager != null)
67         {
68             muleManager.dispose();
69             muleManager = null;
70         }
71     }
72
73     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
74     {
75         containerContext = new SpringContainerContext();
76         containerContext.setBeanFactory(applicationContext);
77     }
78
79     private UMOManager createMuleManager() throws Exception JavaDoc
80     {
81         UMOManager muleManager = MuleManager.getInstance();
82         muleManager.setContainerContext(containerContext);
83
84         String JavaDoc configFilenames = getConfigFilenames();
85         configurationBuilder.configure(configFilenames);
86
87         return muleManager;
88     }
89
90     private String JavaDoc getConfigFilenames()
91     {
92         String JavaDoc[] result = new String JavaDoc[configResources.length];
93         for (int i = 0; i < result.length; i++)
94         {
95             try
96             {
97                 result[i] = configResources[i].getURL().getPath();
98             }
99             catch (IOException JavaDoc e)
100             {
101                 throw new RuntimeException JavaDoc(e);
102             }
103         }
104         return StringUtils.arrayToCommaDelimitedString(result);
105     }
106
107     public void onApplicationEvent(ApplicationEvent event)
108     {
109         if (muleManager == null)
110         {
111             try
112             {
113                 muleManager = createMuleManager();
114             }
115             catch (Exception JavaDoc e)
116             {
117                 throw new RuntimeException JavaDoc(e);
118             }
119         }
120     }
121
122     public ConfigurationBuilder getConfigurationBuilder()
123     {
124         return configurationBuilder;
125     }
126
127     public void setConfigurationBuilder(ConfigurationBuilder configurationBuilder)
128     {
129         this.configurationBuilder = configurationBuilder;
130     }
131 }
132
Popular Tags