1
2
3
4
5
6
7 package org.mule.config.spring;
8
9 import org.mule.api.MuleContext;
10 import org.mule.api.config.ConfigurationException;
11 import org.mule.api.lifecycle.LifecycleManager;
12 import org.mule.api.lifecycle.Startable;
13 import org.mule.api.registry.Registry;
14 import org.mule.config.ConfigResource;
15 import org.mule.config.builders.AbstractResourceConfigurationBuilder;
16 import org.mule.config.i18n.MessageFactory;
17 import org.springframework.context.ApplicationContext;
18 import org.springframework.context.ConfigurableApplicationContext;
19
20
21
22
23
24
25 public class SpringXmlConfigurationBuilder extends AbstractResourceConfigurationBuilder
26 {
27 public static final String MULE_DEFAULTS_CONFIG = "default-mule-config.xml";
28 public static final String MULE_SPRING_CONFIG = "mule-spring-config.xml";
29
30
31 protected boolean useDefaultConfigResource = true;
32
33 protected Registry registry;
34
35 protected ApplicationContext parentContext;
36
37 public SpringXmlConfigurationBuilder(String[] configResources) throws ConfigurationException
38 {
39 super(configResources);
40 }
41
42 public SpringXmlConfigurationBuilder(String configResources) throws ConfigurationException
43 {
44 super(configResources);
45 }
46
47 public SpringXmlConfigurationBuilder(ConfigResource[] configResources)
48 {
49 super(configResources);
50 }
51
52 @Override
53 protected void doConfigure(MuleContext muleContext) throws Exception
54 {
55 ConfigResource[] allResources;
56 if (useDefaultConfigResource)
57 {
58 allResources = new ConfigResource[configResources.length + 2];
59 allResources[0] = new ConfigResource(MULE_SPRING_CONFIG);
60 allResources[1] = new ConfigResource(MULE_DEFAULTS_CONFIG);
61 System.arraycopy(configResources, 0, allResources, 2, configResources.length);
62 }
63 else
64 {
65 allResources = new ConfigResource[configResources.length + 1];
66 allResources[0] = new ConfigResource(MULE_SPRING_CONFIG);
67 System.arraycopy(configResources, 0, allResources, 1, configResources.length);
68 }
69 createSpringRegistry(muleContext, createApplicationContext(muleContext, allResources));
70 }
71
72 public void unconfigure(MuleContext muleContext)
73 {
74 registry.dispose();
75 muleContext.removeRegistry(registry);
76 registry = null;
77 configured = false;
78 }
79
80 protected ApplicationContext createApplicationContext(MuleContext muleContext,
81 ConfigResource[] configResources) throws Exception
82 {
83 return new MuleApplicationContext(muleContext, configResources);
84 }
85
86 protected void createSpringRegistry(MuleContext muleContext, ApplicationContext applicationContext)
87 throws Exception
88 {
89 if (parentContext != null)
90 {
91 if (applicationContext instanceof ConfigurableApplicationContext)
92 {
93 registry = new SpringRegistry((ConfigurableApplicationContext) applicationContext,
94 parentContext, muleContext);
95 }
96 else
97 {
98 throw new ConfigurationException(
99 MessageFactory.createStaticMessage("Cannot set a parent context if the ApplicationContext does not implement ConfigurableApplicationContext"));
100 }
101 }
102 else
103 {
104 registry = new SpringRegistry(applicationContext, muleContext);
105 }
106
107
108
109
110
111 muleContext.addRegistry(registry);
112 registry.initialise();
113 }
114
115 @Override
116 protected void applyLifecycle(LifecycleManager lifecycleManager) throws Exception
117 {
118
119 if (lifecycleManager.isPhaseComplete(Startable.PHASE_NAME))
120 {
121 lifecycleManager.fireLifecycle(Startable.PHASE_NAME);
122 }
123 }
124
125 public boolean isUseDefaultConfigResource()
126 {
127 return useDefaultConfigResource;
128 }
129
130 public void setUseDefaultConfigResource(boolean useDefaultConfigResource)
131 {
132 this.useDefaultConfigResource = useDefaultConfigResource;
133 }
134
135 protected ApplicationContext getParentContext()
136 {
137 return parentContext;
138 }
139
140 public void setParentContext(ApplicationContext parentContext)
141 {
142 this.parentContext = parentContext;
143 }
144
145 }