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 | 0 | protected boolean useDefaultConfigResource = true; |
36 | |
|
37 | |
protected Registry registry; |
38 | |
|
39 | |
protected ApplicationContext parentContext; |
40 | |
|
41 | |
public SpringXmlConfigurationBuilder(String[] configResources) throws ConfigurationException |
42 | |
{ |
43 | 0 | super(configResources); |
44 | 0 | } |
45 | |
|
46 | |
public SpringXmlConfigurationBuilder(String configResources) throws ConfigurationException |
47 | |
{ |
48 | 0 | super(configResources); |
49 | 0 | } |
50 | |
|
51 | |
public SpringXmlConfigurationBuilder(ConfigResource[] configResources) |
52 | |
{ |
53 | 0 | super(configResources); |
54 | 0 | } |
55 | |
|
56 | |
@Override |
57 | |
protected void doConfigure(MuleContext muleContext) throws Exception |
58 | |
{ |
59 | |
ConfigResource[] allResources; |
60 | 0 | if (useDefaultConfigResource) |
61 | |
{ |
62 | 0 | allResources = new ConfigResource[configResources.length + 2]; |
63 | 0 | allResources[0] = new ConfigResource(MULE_SPRING_CONFIG); |
64 | 0 | allResources[1] = new ConfigResource(MULE_DEFAULTS_CONFIG); |
65 | 0 | System.arraycopy(configResources, 0, allResources, 2, configResources.length); |
66 | |
} |
67 | |
else |
68 | |
{ |
69 | 0 | allResources = new ConfigResource[configResources.length + 1]; |
70 | 0 | allResources[0] = new ConfigResource(MULE_SPRING_CONFIG); |
71 | 0 | System.arraycopy(configResources, 0, allResources, 1, configResources.length); |
72 | |
} |
73 | 0 | createSpringRegistry(muleContext, createApplicationContext(muleContext, allResources)); |
74 | 0 | } |
75 | |
|
76 | |
public void unconfigure(MuleContext muleContext) |
77 | |
{ |
78 | 0 | registry.dispose(); |
79 | 0 | muleContext.removeRegistry(registry); |
80 | 0 | registry = null; |
81 | 0 | configured = false; |
82 | 0 | } |
83 | |
|
84 | |
protected ApplicationContext createApplicationContext(MuleContext muleContext, |
85 | |
ConfigResource[] configResources) throws Exception |
86 | |
{ |
87 | 0 | return new MuleApplicationContext(muleContext, configResources); |
88 | |
} |
89 | |
|
90 | |
protected void createSpringRegistry(MuleContext muleContext, ApplicationContext applicationContext) |
91 | |
throws Exception |
92 | |
{ |
93 | 0 | if (parentContext != null) |
94 | |
{ |
95 | 0 | if (applicationContext instanceof ConfigurableApplicationContext) |
96 | |
{ |
97 | 0 | registry = new SpringRegistry((ConfigurableApplicationContext) applicationContext, |
98 | |
parentContext, muleContext); |
99 | |
} |
100 | |
else |
101 | |
{ |
102 | 0 | throw new ConfigurationException( |
103 | |
MessageFactory.createStaticMessage("Cannot set a parent context if the ApplicationContext does not implement ConfigurableApplicationContext")); |
104 | |
} |
105 | |
} |
106 | |
else |
107 | |
{ |
108 | 0 | registry = new SpringRegistry(applicationContext, muleContext); |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | 0 | muleContext.addRegistry(registry); |
116 | 0 | registry.initialise(); |
117 | 0 | } |
118 | |
|
119 | |
@Override |
120 | |
protected void applyLifecycle(LifecycleManager lifecycleManager) throws Exception |
121 | |
{ |
122 | |
|
123 | 0 | if (lifecycleManager.isPhaseComplete(Startable.PHASE_NAME)) |
124 | |
{ |
125 | 0 | lifecycleManager.fireLifecycle(Startable.PHASE_NAME); |
126 | |
} |
127 | 0 | } |
128 | |
|
129 | |
public boolean isUseDefaultConfigResource() |
130 | |
{ |
131 | 0 | return useDefaultConfigResource; |
132 | |
} |
133 | |
|
134 | |
public void setUseDefaultConfigResource(boolean useDefaultConfigResource) |
135 | |
{ |
136 | 0 | this.useDefaultConfigResource = useDefaultConfigResource; |
137 | 0 | } |
138 | |
|
139 | |
protected ApplicationContext getParentContext() |
140 | |
{ |
141 | 0 | return parentContext; |
142 | |
} |
143 | |
|
144 | |
public void setParentContext(ApplicationContext parentContext) |
145 | |
{ |
146 | 0 | this.parentContext = parentContext; |
147 | 0 | } |
148 | |
|
149 | |
} |