1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
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 | |
} |