View Javadoc

1   /*
2    * $Id: MuleManagerBean.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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 CPAL v1.0
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.ConfigurationBuilder;
15  import org.mule.config.builders.MuleXmlConfigurationBuilder;
16  import org.mule.extras.spring.SpringContainerContext;
17  import org.mule.umo.manager.UMOManager;
18  
19  import java.io.IOException;
20  
21  import org.springframework.beans.BeansException;
22  import org.springframework.beans.factory.DisposableBean;
23  import org.springframework.beans.factory.InitializingBean;
24  import org.springframework.context.ApplicationContext;
25  import org.springframework.context.ApplicationContextAware;
26  import org.springframework.context.ApplicationEvent;
27  import org.springframework.context.ApplicationListener;
28  import org.springframework.core.io.Resource;
29  import org.springframework.util.StringUtils;
30  
31  /**
32   * This Bean can e used to bootstrap a MuleManager instance in a Spring context. This
33   * is different to the <code>AutoWireUMOManagerFactoryBean</code> in that the
34   * Manager is not initialised using beans from the ApplicationContext. Instead, a
35   * list of Mule Configuration resources can be passed in. The Configuration builder
36   * can be overloaded so that other types of configuration resources, such as
37   * BeanShell or Groovy scripts cn be used to actually configure the server. For
38   * example to pick up all Mule confuration resources from the classpath, use
39   * something like - <beans> <bean id="muleManager" class="eg.mule.MuleManagerBean"
40   * depends-on="jms.broker"> <property name="configResources"
41   * value="classpath*:META-INF/services/*.mule.xml"/> </bean> .... </beans>
42   */
43  public class MuleManagerBean
44      implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener
45  {
46  
47      private Resource[] configResources;
48      private SpringContainerContext containerContext;
49      private UMOManager muleManager;
50      private ConfigurationBuilder configurationBuilder;
51  
52      public void afterPropertiesSet() throws Exception
53      {
54          if (configurationBuilder == null)
55          {
56              configurationBuilder = new MuleXmlConfigurationBuilder();
57          }
58      }
59  
60      public void setConfigResources(Resource[] configResources)
61      {
62          this.configResources = configResources;
63      }
64  
65      public void destroy() throws Exception
66      {
67          if (muleManager != null)
68          {
69              muleManager.dispose();
70              muleManager = null;
71          }
72      }
73  
74      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
75      {
76          containerContext = new SpringContainerContext();
77          containerContext.setBeanFactory(applicationContext);
78      }
79  
80      private UMOManager createMuleManager() throws Exception
81      {
82          UMOManager muleManager = MuleManager.getInstance();
83          muleManager.setContainerContext(containerContext);
84  
85          String configFilenames = getConfigFilenames();
86          configurationBuilder.configure(configFilenames);
87  
88          return muleManager;
89      }
90  
91      private String getConfigFilenames()
92      {
93          String[] result = new String[configResources.length];
94          for (int i = 0; i < result.length; i++)
95          {
96              try
97              {
98                  result[i] = configResources[i].getURL().getPath();
99              }
100             catch (IOException e)
101             {
102                 throw new RuntimeException(e);
103             }
104         }
105         return StringUtils.arrayToCommaDelimitedString(result);
106     }
107 
108     public void onApplicationEvent(ApplicationEvent event)
109     {
110         if (muleManager == null)
111         {
112             try
113             {
114                 muleManager = createMuleManager();
115             }
116             catch (Exception e)
117             {
118                 throw new RuntimeException(e);
119             }
120         }
121     }
122 
123     public ConfigurationBuilder getConfigurationBuilder()
124     {
125         return configurationBuilder;
126     }
127 
128     public void setConfigurationBuilder(ConfigurationBuilder configurationBuilder)
129     {
130         this.configurationBuilder = configurationBuilder;
131     }
132 }