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