Coverage Report - org.mule.extras.spring.config.MuleManagerBean
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleManagerBean
0%
0/34
0%
0/4
1.889
 
 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  0
 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  0
         if (configurationBuilder == null)
 55  
         {
 56  0
             configurationBuilder = new MuleXmlConfigurationBuilder();
 57  
         }
 58  0
     }
 59  
 
 60  
     public void setConfigResources(Resource[] configResources)
 61  
     {
 62  0
         this.configResources = configResources;
 63  0
     }
 64  
 
 65  
     public void destroy() throws Exception
 66  
     {
 67  0
         if (muleManager != null)
 68  
         {
 69  0
             muleManager.dispose();
 70  0
             muleManager = null;
 71  
         }
 72  0
     }
 73  
 
 74  
     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
 75  
     {
 76  0
         containerContext = new SpringContainerContext();
 77  0
         containerContext.setBeanFactory(applicationContext);
 78  0
     }
 79  
 
 80  
     private UMOManager createMuleManager() throws Exception
 81  
     {
 82  0
         UMOManager muleManager = MuleManager.getInstance();
 83  0
         muleManager.setContainerContext(containerContext);
 84  
 
 85  0
         String configFilenames = getConfigFilenames();
 86  0
         configurationBuilder.configure(configFilenames);
 87  
 
 88  0
         return muleManager;
 89  
     }
 90  
 
 91  
     private String getConfigFilenames()
 92  
     {
 93  0
         String[] result = new String[configResources.length];
 94  0
         for (int i = 0; i < result.length; i++)
 95  
         {
 96  
             try
 97  
             {
 98  0
                 result[i] = configResources[i].getURL().getPath();
 99  
             }
 100  0
             catch (IOException e)
 101  
             {
 102  0
                 throw new RuntimeException(e);
 103  0
             }
 104  
         }
 105  0
         return StringUtils.arrayToCommaDelimitedString(result);
 106  
     }
 107  
 
 108  
     public void onApplicationEvent(ApplicationEvent event)
 109  
     {
 110  0
         if (muleManager == null)
 111  
         {
 112  
             try
 113  
             {
 114  0
                 muleManager = createMuleManager();
 115  
             }
 116  0
             catch (Exception e)
 117  
             {
 118  0
                 throw new RuntimeException(e);
 119  0
             }
 120  
         }
 121  0
     }
 122  
 
 123  
     public ConfigurationBuilder getConfigurationBuilder()
 124  
     {
 125  0
         return configurationBuilder;
 126  
     }
 127  
 
 128  
     public void setConfigurationBuilder(ConfigurationBuilder configurationBuilder)
 129  
     {
 130  0
         this.configurationBuilder = configurationBuilder;
 131  0
     }
 132  
 }