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