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