1 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 ; 29 30 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 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 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 80 { 81 UMOManager muleManager = MuleManager.getInstance(); 82 muleManager.setContainerContext(containerContext); 83 84 String configFilenames = getConfigFilenames(); 85 configurationBuilder.configure(configFilenames); 86 87 return muleManager; 88 } 89 90 private String getConfigFilenames() 91 { 92 String [] result = new String [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 e) 100 { 101 throw new RuntimeException (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 e) 116 { 117 throw new RuntimeException (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 |