1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.web.context;
18
19 import javax.servlet.ServletContext;
20
21 import org.springframework.beans.BeanUtils;
22 import org.springframework.beans.BeansException;
23 import org.springframework.context.ApplicationContext;
24 import org.springframework.context.ApplicationContextException;
25 import org.springframework.util.ClassUtils;
26 import org.springframework.util.StringUtils;
27 import org.springframework.web.context.ConfigurableWebApplicationContext;
28 import org.springframework.web.context.ContextLoader;
29 import org.springframework.web.context.WebApplicationContext;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class ModuleContextLoader extends ContextLoader {
49
50
51
52
53
54
55 public static final String INCLUSIVE_LOCATIONS_PARAM = "inclusiveLocations";
56
57
58
59
60
61 public static final String EXCLUSIVE_LOCATIONS_PARAM = "exclusiveLocations";
62
63
64
65
66
67 public static final String BEAN_OVERRIDING_PARAM
68 = "overrideBeanDefinitions";
69
70
71
72
73
74
75 public static final String MERGE_RESOURCES_PARAM = "mergeResources";
76
77
78
79
80 protected WebApplicationContext createWebApplicationContext(
81 ServletContext servletContext, ApplicationContext parent)
82 throws BeansException {
83
84 Class<?> contextClass = determineContextClass(servletContext);
85 if (!ConfigurableWebApplicationContext.class
86 .isAssignableFrom(contextClass)) {
87 throw new ApplicationContextException("Custom context class ["
88 + contextClass.getName()
89 + "] is not of type ConfigurableWebApplicationContext");
90 }
91
92 ConfigurableWebApplicationContext wac;
93
94 if (ModuleWebApplicationContext.class.isAssignableFrom(contextClass)) {
95 String[] inclusiveConfigLocations = getInclusiveConfigLocations(
96 servletContext);
97 String[] exclusiveConfigLocations = getExclusiveConfigLocations(
98 servletContext);
99 boolean beanOverriding = isBeanDefinitionOverridingAllowed(
100 servletContext);
101 boolean mergeResources = isMergingWithOuterResourcesAllowed(
102 servletContext);
103
104 wac = new ModuleWebApplicationContext(inclusiveConfigLocations,
105 exclusiveConfigLocations, beanOverriding, servletContext,
106 mergeResources, parent);
107
108 } else {
109 wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
110 wac.setServletContext(servletContext);
111 String configLocation = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
112 if (configLocation != null) {
113 wac.setConfigLocations(toStringArray(configLocation));
114 }
115 wac.setParent(parent);
116 }
117 wac.refresh();
118 return wac;
119 }
120
121
122
123
124 protected Class<?> determineContextClass(ServletContext servletContext)
125 throws ApplicationContextException {
126 String contextClassName = servletContext
127 .getInitParameter(CONTEXT_CLASS_PARAM);
128 if (contextClassName != null) {
129 try {
130 return ClassUtils.forName(contextClassName);
131 } catch (ClassNotFoundException ex) {
132 throw new ApplicationContextException(
133 "Failed to load custom context class [" + contextClassName
134 + "]", ex);
135 }
136 } else {
137 contextClassName
138 = "ch.elca.el4j.web.context.ModuleWebApplicationContext";
139 try {
140 return ClassUtils.forName(contextClassName);
141 } catch (ClassNotFoundException ex) {
142 throw new ApplicationContextException(
143 "Failed to load default context class [" + contextClassName
144 + "]", ex);
145 }
146 }
147 }
148
149
150
151
152
153
154
155
156 protected String[] getInclusiveConfigLocations(ServletContext
157 servletContext) {
158 String inclusiveConfigLocations = servletContext
159 .getInitParameter(INCLUSIVE_LOCATIONS_PARAM);
160 if (inclusiveConfigLocations == null) {
161 inclusiveConfigLocations = servletContext
162 .getInitParameter(CONFIG_LOCATION_PARAM);
163 if (inclusiveConfigLocations == null) {
164 return null;
165 }
166 }
167 return toStringArray(inclusiveConfigLocations);
168 }
169
170
171
172
173
174
175
176
177 protected String[] getExclusiveConfigLocations(ServletContext
178 servletContext) {
179 String exclusiveConfigLocations = servletContext.
180 getInitParameter(EXCLUSIVE_LOCATIONS_PARAM);
181 if (exclusiveConfigLocations == null) {
182 return null;
183 }
184 return toStringArray(exclusiveConfigLocations);
185 }
186
187
188
189
190
191
192 protected boolean isBeanDefinitionOverridingAllowed(ServletContext
193 servletContext) {
194 boolean override = false;
195 String beanOverriding = servletContext.getInitParameter(
196 BEAN_OVERRIDING_PARAM);
197 if (beanOverriding != null) {
198 if (beanOverriding.equals("true")) {
199 override = true;
200 }
201 }
202 return override;
203 }
204
205
206
207
208
209
210
211
212 protected boolean isMergingWithOuterResourcesAllowed(ServletContext
213 servletContext) {
214 boolean merge = true;
215 String mergeResources = servletContext.getInitParameter(
216 MERGE_RESOURCES_PARAM);
217 if (mergeResources != null) {
218 if (mergeResources.equals("false")) {
219 merge = false;
220 }
221 }
222 return merge;
223 }
224
225
226
227
228
229
230
231
232
233
234
235 private String[] toStringArray(String str) {
236 String[] result = new String[0];
237 if (str != null) {
238 result = StringUtils.tokenizeToStringArray(str,
239 ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS);
240 }
241 return result;
242 }
243 }