1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.impl.config.module; |
18 | |
|
19 | |
import org.apache.commons.collections.CollectionUtils; |
20 | |
import org.apache.commons.lang.StringUtils; |
21 | |
import org.apache.log4j.Logger; |
22 | |
import org.kuali.rice.core.api.config.ConfigurationException; |
23 | |
import org.kuali.rice.core.api.config.CoreConfigHelper; |
24 | |
import org.kuali.rice.core.api.config.module.Configurer; |
25 | |
import org.kuali.rice.core.api.config.module.RunMode; |
26 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
27 | |
import org.kuali.rice.core.api.lifecycle.BaseCompositeLifecycle; |
28 | |
import org.kuali.rice.core.api.lifecycle.Lifecycle; |
29 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
30 | |
import org.kuali.rice.core.api.resourceloader.ResourceLoader; |
31 | |
import org.kuali.rice.core.api.resourceloader.ResourceLoaderContainer; |
32 | |
import org.kuali.rice.core.api.util.RiceConstants; |
33 | |
import org.kuali.rice.core.framework.resourceloader.BaseResourceLoader; |
34 | |
import org.kuali.rice.core.impl.resourceloader.RiceResourceLoaderFactory; |
35 | |
import org.springframework.beans.factory.DisposableBean; |
36 | |
import org.springframework.beans.factory.InitializingBean; |
37 | |
import org.springframework.context.ApplicationEvent; |
38 | |
import org.springframework.context.ApplicationListener; |
39 | |
import org.springframework.context.event.ContextClosedEvent; |
40 | |
import org.springframework.context.event.ContextRefreshedEvent; |
41 | |
import org.springframework.web.context.ServletContextAware; |
42 | |
|
43 | |
import javax.servlet.ServletContext; |
44 | |
import javax.xml.namespace.QName; |
45 | |
import java.util.ArrayList; |
46 | |
import java.util.Arrays; |
47 | |
import java.util.Collection; |
48 | |
import java.util.Collections; |
49 | |
import java.util.List; |
50 | |
import java.util.Properties; |
51 | |
|
52 | |
|
53 | |
public class ModuleConfigurer extends BaseCompositeLifecycle implements Configurer, InitializingBean, DisposableBean, ApplicationListener<ApplicationEvent>, ServletContextAware { |
54 | 0 | protected final Logger LOG = Logger.getLogger(getClass()); |
55 | |
|
56 | 0 | private List<RunMode> validRunModes = new ArrayList<RunMode>(); |
57 | |
private boolean hasWebInterface; |
58 | |
|
59 | 0 | private Properties properties = new Properties(); |
60 | |
private String moduleName; |
61 | |
private ServletContext servletContext; |
62 | |
|
63 | 0 | public ModuleConfigurer() { |
64 | 0 | } |
65 | |
|
66 | 0 | public ModuleConfigurer(String moduleName) { |
67 | 0 | this.moduleName = moduleName; |
68 | 0 | } |
69 | |
|
70 | |
@Override |
71 | |
public final void start() throws Exception { |
72 | 0 | super.start(); |
73 | 0 | doAdditionalModuleStartLogic(); |
74 | 0 | } |
75 | |
|
76 | |
protected void doAdditionalModuleStartLogic() throws Exception { |
77 | |
|
78 | 0 | } |
79 | |
|
80 | |
@Override |
81 | |
public final void afterPropertiesSet() throws Exception { |
82 | 0 | validateConfigurerState(); |
83 | 0 | addToConfig(); |
84 | 0 | initializeResourceLoaders(); |
85 | 0 | start(); |
86 | 0 | } |
87 | |
|
88 | |
@Override |
89 | |
public final void stop() throws Exception { |
90 | |
try { |
91 | 0 | doAdditionalModuleStopLogic(); |
92 | |
} finally { |
93 | 0 | super.stop(); |
94 | 0 | } |
95 | 0 | } |
96 | |
|
97 | |
protected void doAdditionalModuleStopLogic() throws Exception { |
98 | |
|
99 | 0 | } |
100 | |
|
101 | |
@Override |
102 | |
public final void destroy() throws Exception { |
103 | 0 | stop(); |
104 | |
|
105 | 0 | GlobalResourceLoader.stop(); |
106 | 0 | } |
107 | |
|
108 | |
@Override |
109 | |
public List<Lifecycle> loadLifecycles() throws Exception { |
110 | 0 | return Collections.emptyList(); |
111 | |
|
112 | |
} |
113 | |
|
114 | |
public RunMode getRunMode() { |
115 | 0 | String propertyName = getModuleName().toLowerCase() + ".mode"; |
116 | 0 | String runMode = ConfigContext.getCurrentContextConfig().getProperty(propertyName); |
117 | 0 | if (StringUtils.isBlank(runMode)) { |
118 | 0 | throw new ConfigurationException("Failed to determine run mode for module '" + getModuleName() + "'. Please be sure to set configuration parameter '" + propertyName + "'"); |
119 | |
} |
120 | 0 | return RunMode.valueOf(runMode.toUpperCase()); |
121 | |
} |
122 | |
|
123 | |
public String getWebModuleConfigName() { |
124 | 0 | return "config/" + getModuleName().toLowerCase(); |
125 | |
} |
126 | |
|
127 | |
public String getWebModuleConfigurationFiles() { |
128 | 0 | return ConfigContext.getCurrentContextConfig().getProperty("rice." + getModuleName().toLowerCase() + ".struts.config.files"); |
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public boolean shouldRenderWebInterface() { |
139 | 0 | return hasWebInterface() && getRunMode().equals( RunMode.LOCAL ); |
140 | |
} |
141 | |
|
142 | |
public boolean isSetSOAPServicesAsDefault() { |
143 | 0 | return Boolean.valueOf(ConfigContext.getCurrentContextConfig().getProperty("rice." + getModuleName().toLowerCase() + ".set.soap.services.as.default")).booleanValue(); |
144 | |
} |
145 | |
|
146 | |
public boolean isExposeServicesOnBus() { |
147 | 0 | return Boolean.valueOf(ConfigContext.getCurrentContextConfig().getProperty("rice." + getModuleName().toLowerCase() + ".expose.services.on.bus")).booleanValue(); |
148 | |
} |
149 | |
|
150 | |
public boolean isIncludeUserInterfaceComponents() { |
151 | 0 | return Boolean.valueOf(ConfigContext.getCurrentContextConfig().getProperty("rice." + getModuleName().toLowerCase() + ".include.user.interface.components")).booleanValue(); |
152 | |
} |
153 | |
|
154 | |
public String getWebModuleBaseUrl() { |
155 | 0 | return ConfigContext.getCurrentContextConfig().getProperty(getModuleName().toLowerCase() + ".url"); |
156 | |
} |
157 | |
|
158 | |
@Override |
159 | |
public List<String> getPrimarySpringFiles() { |
160 | 0 | return Collections.singletonList(getDefaultSpringBeansPath(getDefaultConfigPackagePath())); |
161 | |
} |
162 | |
|
163 | |
public boolean hasWebInterface() { |
164 | 0 | return this.hasWebInterface; |
165 | |
} |
166 | |
|
167 | |
public void setHasWebInterface(boolean hasWebInterface) { |
168 | 0 | this.hasWebInterface = hasWebInterface; |
169 | 0 | } |
170 | |
|
171 | |
public Properties getProperties() { |
172 | 0 | return this.properties; |
173 | |
} |
174 | |
|
175 | |
public void setProperties(Properties properties) { |
176 | 0 | this.properties = properties; |
177 | 0 | } |
178 | |
|
179 | |
public List<String> getAdditionalSpringFiles() { |
180 | 0 | final String files = ConfigContext.getCurrentContextConfig().getProperty("rice." + getModuleName() + ".additionalSpringFiles"); |
181 | 0 | return files == null ? Collections.<String>emptyList() : parseFileList(files); |
182 | |
} |
183 | |
|
184 | |
private List<String> parseFileList(String files) { |
185 | 0 | final List<String> parsedFiles = new ArrayList<String>(); |
186 | 0 | for (String file : Arrays.asList(files.split(","))) { |
187 | 0 | final String trimmedFile = file.trim(); |
188 | 0 | if (!trimmedFile.isEmpty()) { |
189 | 0 | parsedFiles.add(trimmedFile); |
190 | |
} |
191 | 0 | } |
192 | |
|
193 | 0 | return parsedFiles; |
194 | |
} |
195 | |
|
196 | |
public String getModuleName() { |
197 | 0 | return this.moduleName; |
198 | |
} |
199 | |
|
200 | |
public void setModuleName(String moduleName) { |
201 | 0 | this.moduleName = moduleName; |
202 | 0 | } |
203 | |
|
204 | |
|
205 | |
protected String getDefaultConfigPackagePath() { |
206 | 0 | return "classpath:org/kuali/rice/" + getModuleName().toLowerCase() + "/config/"; |
207 | |
} |
208 | |
protected String getDefaultSpringBeansPath(String configPackagePath) { |
209 | 0 | return configPackagePath + getModuleName().toUpperCase() + "SpringBeans.xml"; |
210 | |
} |
211 | |
|
212 | |
public List<RunMode> getValidRunModes() { |
213 | 0 | return this.validRunModes; |
214 | |
} |
215 | |
|
216 | |
public void setValidRunModes(List<RunMode> validRunModes) { |
217 | 0 | this.validRunModes = validRunModes; |
218 | 0 | } |
219 | |
|
220 | |
@Override |
221 | |
public final void validateConfigurerState() { |
222 | 0 | if (StringUtils.isBlank(this.moduleName)) { |
223 | 0 | throw new IllegalStateException("the module name for this module has not been set"); |
224 | |
} |
225 | |
|
226 | 0 | if (CollectionUtils.isEmpty(this.validRunModes)) { |
227 | 0 | throw new IllegalStateException("the valid run modes for this module has not been set"); |
228 | |
} |
229 | |
|
230 | |
|
231 | 0 | if (!ConfigContext.isInitialized()) { |
232 | 0 | throw new ConfigurationException("ConfigContext has not yet been initialized, please initialize prior to using."); |
233 | |
} |
234 | |
|
235 | 0 | validateRunMode(); |
236 | |
|
237 | 0 | doAdditonalConfigurerValidations(); |
238 | 0 | } |
239 | |
|
240 | |
private void validateRunMode() { |
241 | 0 | if ( !validRunModes.contains( getRunMode() ) ) { |
242 | 0 | throw new IllegalArgumentException( "Invalid run mode for the " + this.getClass().getSimpleName() + ": " + getRunMode() + " - Valid Values are: " + validRunModes ); |
243 | |
} |
244 | 0 | } |
245 | |
|
246 | |
protected void doAdditonalConfigurerValidations() { |
247 | |
|
248 | 0 | } |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
@Override |
260 | |
public final void addToConfig() { |
261 | |
|
262 | 0 | if (this.properties != null) { |
263 | 0 | ConfigContext.getCurrentContextConfig().putProperties(this.properties); |
264 | |
} |
265 | |
|
266 | 0 | registerConfigurerWithConfig(); |
267 | 0 | addAdditonalToConfig(); |
268 | 0 | } |
269 | |
|
270 | |
protected void addAdditonalToConfig() { |
271 | |
|
272 | 0 | } |
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
private void registerConfigurerWithConfig() { |
279 | |
@SuppressWarnings("unchecked") |
280 | 0 | Collection<ModuleConfigurer> configurers = (Collection<ModuleConfigurer>) ConfigContext.getCurrentContextConfig().getObject("ModuleConfigurers"); |
281 | 0 | if (configurers == null) { |
282 | 0 | configurers = new ArrayList<ModuleConfigurer>(); |
283 | |
} |
284 | 0 | configurers.add(this); |
285 | |
|
286 | 0 | ConfigContext.getCurrentContextConfig().putObject("ModuleConfigurers", configurers); |
287 | 0 | } |
288 | |
|
289 | |
@Override |
290 | |
public final void initializeResourceLoaders() throws Exception { |
291 | 0 | List<String> files = new ArrayList<String>(); |
292 | 0 | files.addAll(getPrimarySpringFiles()); |
293 | 0 | files.addAll(getAdditionalSpringFiles()); |
294 | |
|
295 | 0 | ResourceLoader rootResourceLoader = GlobalResourceLoader.getResourceLoader(); |
296 | 0 | if (rootResourceLoader == null) { |
297 | 0 | rootResourceLoader = createRootResourceLoader(); |
298 | |
} |
299 | |
|
300 | 0 | if (!files.isEmpty()) { |
301 | 0 | ResourceLoader rl = RiceResourceLoaderFactory.createRootRiceResourceLoader(servletContext, files, getModuleName()); |
302 | 0 | rl.start(); |
303 | 0 | GlobalResourceLoader.addResourceLoader(rl); |
304 | |
} |
305 | |
|
306 | 0 | final Collection<ResourceLoader> rls = getResourceLoadersToRegister(); |
307 | |
|
308 | 0 | for (ResourceLoader rl : rls) { |
309 | 0 | GlobalResourceLoader.addResourceLoader(rl); |
310 | |
} |
311 | 0 | } |
312 | |
|
313 | |
protected Collection<ResourceLoader> getResourceLoadersToRegister() throws Exception { |
314 | 0 | return Collections.emptyList(); |
315 | |
|
316 | |
} |
317 | |
|
318 | |
private ResourceLoader createRootResourceLoader() throws Exception { |
319 | 0 | final ResourceLoaderContainer container = new ResourceLoaderContainer(new QName(CoreConfigHelper.getApplicationId(), RiceConstants.ROOT_RESOURCE_LOADER_CONTAINER_NAME)); |
320 | 0 | ResourceLoader rootResourceLoader = new BaseResourceLoader(new QName(CoreConfigHelper.getApplicationId(), RiceConstants.DEFAULT_ROOT_RESOURCE_LOADER_NAME)); |
321 | |
|
322 | 0 | container.addResourceLoader(rootResourceLoader); |
323 | 0 | GlobalResourceLoader.addResourceLoader(container); |
324 | 0 | GlobalResourceLoader.start(); |
325 | |
|
326 | 0 | return container; |
327 | |
} |
328 | |
|
329 | |
@Override |
330 | |
public final void onApplicationEvent(ApplicationEvent event) { |
331 | 0 | if (event instanceof ContextRefreshedEvent) { |
332 | 0 | doContextStartedLogic(); |
333 | 0 | } else if (event instanceof ContextClosedEvent) { |
334 | 0 | doContextStoppedLogic(); |
335 | |
} |
336 | 0 | } |
337 | |
|
338 | |
@Override |
339 | |
public final void doContextStartedLogic() { |
340 | 0 | doAdditionalContextStartedLogic(); |
341 | 0 | } |
342 | |
|
343 | |
@Override |
344 | |
public final void doContextStoppedLogic() { |
345 | 0 | doAdditionalContextStoppedLogic(); |
346 | 0 | } |
347 | |
|
348 | |
protected void doAdditionalContextStartedLogic() { |
349 | |
|
350 | 0 | } |
351 | |
|
352 | |
protected void doAdditionalContextStoppedLogic() { |
353 | |
|
354 | 0 | } |
355 | |
|
356 | |
@Override |
357 | |
public void setServletContext(ServletContext servletContext) { |
358 | 0 | this.servletContext = servletContext; |
359 | 0 | } |
360 | |
} |