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