1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config.builders; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.config.ConfigurationException; |
15 | |
import org.mule.api.registry.Registry; |
16 | |
import org.mule.config.ConfigResource; |
17 | |
import org.mule.config.spring.MuleApplicationContext; |
18 | |
import org.mule.config.spring.SpringXmlConfigurationBuilder; |
19 | |
|
20 | |
import java.io.FileNotFoundException; |
21 | |
import java.io.IOException; |
22 | |
import java.io.InputStream; |
23 | |
|
24 | |
import javax.servlet.ServletContext; |
25 | |
|
26 | |
import org.apache.commons.logging.Log; |
27 | |
import org.apache.commons.logging.LogFactory; |
28 | |
import org.springframework.beans.BeansException; |
29 | |
import org.springframework.beans.factory.access.BeanFactoryLocator; |
30 | |
import org.springframework.context.ApplicationContext; |
31 | |
import org.springframework.context.ApplicationContextException; |
32 | |
import org.springframework.context.access.ContextSingletonBeanFactoryLocator; |
33 | |
import org.springframework.core.io.AbstractResource; |
34 | |
import org.springframework.core.io.ClassPathResource; |
35 | |
import org.springframework.core.io.Resource; |
36 | |
import org.springframework.util.Assert; |
37 | |
import org.springframework.util.ClassUtils; |
38 | |
import org.springframework.util.StringUtils; |
39 | |
import org.springframework.web.context.ContextLoader; |
40 | |
import org.springframework.web.context.support.ServletContextResource; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public class WebappMuleXmlConfigurationBuilder extends SpringXmlConfigurationBuilder |
54 | |
{ |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 0 | protected transient final Log logger = LogFactory.getLog(getClass()); |
60 | |
|
61 | |
private ServletContext context; |
62 | |
|
63 | |
public WebappMuleXmlConfigurationBuilder(ServletContext servletContext, String configResources) |
64 | |
throws ConfigurationException |
65 | |
{ |
66 | 0 | super(configResources); |
67 | 0 | context = servletContext; |
68 | 0 | } |
69 | |
|
70 | |
public WebappMuleXmlConfigurationBuilder(ServletContext servletContext, String[] configResources) |
71 | |
throws ConfigurationException |
72 | |
{ |
73 | 0 | super(configResources); |
74 | 0 | context = servletContext; |
75 | 0 | } |
76 | |
|
77 | |
protected ConfigResource[] loadConfigResources(String[] configs) throws ConfigurationException |
78 | |
{ |
79 | |
try |
80 | |
{ |
81 | 0 | configResources = new ConfigResource[configs.length]; |
82 | 0 | for (int i = 0; i < configs.length; i++) |
83 | |
{ |
84 | 0 | configResources[i] = new ServletContextOrClassPathConfigResource(configs[i]); |
85 | |
} |
86 | 0 | return configResources; |
87 | |
} |
88 | 0 | catch (IOException e) |
89 | |
{ |
90 | 0 | throw new ConfigurationException(e); |
91 | |
} |
92 | |
} |
93 | |
|
94 | |
protected void createSpringParentRegistry(MuleContext muleContext, Registry registry, ConfigResource[] all) |
95 | |
{ |
96 | 0 | Resource[] servletContextResources = new Resource[all.length]; |
97 | 0 | for (int i = 0; i < all.length; i++) |
98 | |
{ |
99 | 0 | servletContextResources[i] = new ServletContextOrClassPathResource(context, all[i].getResourceName()); |
100 | |
} |
101 | |
|
102 | 0 | parentContext = loadParentContext(context); |
103 | |
|
104 | |
try |
105 | |
{ |
106 | 0 | if (parentContext != null) |
107 | |
{ |
108 | 0 | new MuleApplicationContext(muleContext, registry, servletContextResources, parentContext); |
109 | |
} |
110 | |
else |
111 | |
{ |
112 | 0 | new MuleApplicationContext(muleContext, registry, servletContextResources); |
113 | |
} |
114 | |
} |
115 | 0 | catch (BeansException e) |
116 | |
{ |
117 | |
|
118 | |
|
119 | 0 | registry.setParent(null); |
120 | 0 | throw e; |
121 | |
} |
122 | 0 | catch (IOException e) |
123 | |
{ |
124 | 0 | registry.setParent(null); |
125 | 0 | throw new ApplicationContextException("Failed to load config resource", e); |
126 | 0 | } |
127 | 0 | } |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
protected ApplicationContext loadParentContext(ServletContext servletContext) throws BeansException |
139 | |
{ |
140 | |
|
141 | 0 | ApplicationContext parentContext = null; |
142 | 0 | String locatorFactorySelector = servletContext.getInitParameter(ContextLoader.LOCATOR_FACTORY_SELECTOR_PARAM); |
143 | 0 | String parentContextKey = servletContext.getInitParameter(ContextLoader.LOCATOR_FACTORY_KEY_PARAM); |
144 | |
|
145 | 0 | if (parentContextKey != null) |
146 | |
{ |
147 | |
|
148 | |
|
149 | 0 | BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance(locatorFactorySelector); |
150 | 0 | if (logger.isDebugEnabled()) |
151 | |
{ |
152 | 0 | logger.debug("Getting parent context definition: using parent context key of '" + parentContextKey |
153 | |
+ "' with BeanFactoryLocator"); |
154 | |
} |
155 | 0 | parentContext = (ApplicationContext) locator.useBeanFactory(parentContextKey).getFactory(); |
156 | |
} |
157 | |
|
158 | 0 | return parentContext; |
159 | |
} |
160 | |
|
161 | |
class ServletContextOrClassPathConfigResource extends ConfigResource |
162 | |
{ |
163 | |
public ServletContextOrClassPathConfigResource(String resourceName) throws IOException |
164 | 0 | { |
165 | 0 | super(resourceName, null); |
166 | 0 | } |
167 | |
|
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
class ServletContextOrClassPathResource extends AbstractResource |
177 | |
{ |
178 | |
|
179 | |
private final ServletContext servletContext; |
180 | |
|
181 | |
private final String path; |
182 | |
|
183 | |
public ServletContextOrClassPathResource(ServletContext servletContext, String path) |
184 | 0 | { |
185 | 0 | Assert.notNull(servletContext, "Cannot resolve ServletContextResource without ServletContext"); |
186 | 0 | this.servletContext = servletContext; |
187 | |
|
188 | 0 | Assert.notNull(path, "path is required"); |
189 | 0 | this.path = StringUtils.cleanPath(path); |
190 | 0 | } |
191 | |
|
192 | |
public InputStream getInputStream() throws IOException |
193 | |
{ |
194 | 0 | InputStream is = getServletContextInputStream(); |
195 | 0 | if (is == null) |
196 | |
{ |
197 | 0 | is = getClasspathInputStream(); |
198 | |
} |
199 | 0 | if (is == null) |
200 | |
{ |
201 | 0 | throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist"); |
202 | |
} |
203 | 0 | return is; |
204 | |
} |
205 | |
|
206 | |
protected InputStream getServletContextInputStream() |
207 | |
{ |
208 | 0 | String servletContextPath = path; |
209 | 0 | if (!servletContextPath.startsWith("/")) |
210 | |
{ |
211 | 0 | servletContextPath = "/" + servletContextPath; |
212 | |
} |
213 | 0 | return servletContext.getResourceAsStream(servletContextPath); |
214 | |
} |
215 | |
|
216 | |
protected InputStream getClasspathInputStream() |
217 | |
{ |
218 | 0 | String classpathPath = path; |
219 | 0 | if (classpathPath.startsWith("/")) |
220 | |
{ |
221 | 0 | classpathPath = classpathPath.substring(1); |
222 | |
} |
223 | 0 | return ClassUtils.getDefaultClassLoader().getResourceAsStream(classpathPath); |
224 | |
} |
225 | |
|
226 | |
public String getDescription() |
227 | |
{ |
228 | 0 | return path; |
229 | |
} |
230 | |
} |
231 | |
} |