1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.config; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Arrays; |
21 | |
import java.util.Collections; |
22 | |
import java.util.LinkedList; |
23 | |
import java.util.List; |
24 | |
import java.util.Properties; |
25 | |
|
26 | |
import org.apache.commons.lang.StringUtils; |
27 | |
import org.apache.log4j.Logger; |
28 | |
import org.kuali.rice.core.config.event.AfterStartEvent; |
29 | |
import org.kuali.rice.core.config.event.AfterStopEvent; |
30 | |
import org.kuali.rice.core.config.event.BeforeStartEvent; |
31 | |
import org.kuali.rice.core.config.event.BeforeStopEvent; |
32 | |
import org.kuali.rice.core.config.event.RiceConfigEvent; |
33 | |
import org.kuali.rice.core.config.logging.Log4jLifeCycle; |
34 | |
import org.kuali.rice.core.exception.RiceRuntimeException; |
35 | |
import org.kuali.rice.core.lifecycle.BaseCompositeLifecycle; |
36 | |
import org.kuali.rice.core.lifecycle.Lifecycle; |
37 | |
import org.kuali.rice.core.resourceloader.GlobalResourceLoader; |
38 | |
import org.kuali.rice.core.resourceloader.ResourceLoader; |
39 | |
import org.kuali.rice.core.resourceloader.RiceResourceLoaderFactory; |
40 | |
import org.kuali.rice.core.resourceloader.RootResourceLoaderLifecycle; |
41 | |
import org.kuali.rice.core.resourceloader.SpringLoader; |
42 | |
import org.kuali.rice.core.util.RiceConstants; |
43 | |
import org.springframework.beans.BeansException; |
44 | |
import org.springframework.beans.factory.BeanFactory; |
45 | |
import org.springframework.beans.factory.BeanFactoryAware; |
46 | |
import org.springframework.beans.factory.DisposableBean; |
47 | |
import org.springframework.beans.factory.InitializingBean; |
48 | |
import org.springframework.context.ApplicationEvent; |
49 | |
import org.springframework.context.ApplicationListener; |
50 | |
import org.springframework.context.event.ContextClosedEvent; |
51 | |
import org.springframework.context.event.ContextRefreshedEvent; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 0 | public abstract class RiceConfigurerBase extends BaseCompositeLifecycle implements Configurer, InitializingBean, DisposableBean, ApplicationListener, BeanFactoryAware { |
61 | |
|
62 | 0 | private static final Logger LOG = Logger.getLogger(RiceConfigurerBase.class); |
63 | |
|
64 | 0 | private String environment = "dev"; |
65 | |
private String serviceNamespace; |
66 | |
|
67 | |
private Config rootConfig; |
68 | |
private ResourceLoader rootResourceLoader; |
69 | |
private Properties properties; |
70 | 0 | private List<String> configLocations = new ArrayList<String>(); |
71 | 0 | private List<String> additionalSpringFiles = new ArrayList<String>(); |
72 | |
private BeanFactory beanFactory; |
73 | |
|
74 | 0 | private List<ModuleConfigurer> modules = new LinkedList<ModuleConfigurer>(); |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public void afterPropertiesSet() throws Exception { |
80 | 0 | start(); |
81 | 0 | } |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public void destroy() throws Exception { |
87 | 0 | stop(); |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public void start() throws Exception { |
94 | 0 | notify(new BeforeStartEvent()); |
95 | 0 | initializeFullConfiguration(); |
96 | 0 | initializeResourceLoaders(); |
97 | 0 | super.start(); |
98 | 0 | addModulesResourceLoaders(); |
99 | 0 | if (getRootResourceLoader() != null ) { |
100 | 0 | if (!getRootResourceLoader().isStarted()) { |
101 | 0 | getRootResourceLoader().start(); |
102 | |
} |
103 | 0 | GlobalResourceLoader.addResourceLoader(getRootResourceLoader()); |
104 | |
} |
105 | 0 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
private void initializeResourceLoaders() throws Exception { |
114 | 0 | (new RootResourceLoaderLifecycle(getRootResourceLoader())).start(); |
115 | 0 | loadSpringContext(); |
116 | 0 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
protected void addModulesResourceLoaders() throws Exception { |
129 | 0 | for (ModuleConfigurer module: modules) { |
130 | 0 | GlobalResourceLoader.addResourceLoader(module.getResourceLoaderToRegister()); |
131 | |
} |
132 | 0 | } |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public ResourceLoader loadSpringContext() throws Exception { |
143 | 0 | String springFileLocations = ""; |
144 | 0 | for(ModuleConfigurer module: modules){ |
145 | 0 | if(StringUtils.isNotBlank(module.getSpringFileLocations())) |
146 | 0 | springFileLocations += module.getSpringFileLocations()+SpringLoader.SPRING_SEPARATOR_CHARACTER; |
147 | |
} |
148 | 0 | for (String springFile : additionalSpringFiles) { |
149 | 0 | springFileLocations += springFile + SpringLoader.SPRING_SEPARATOR_CHARACTER; |
150 | |
} |
151 | 0 | ResourceLoader resourceLoader = RiceResourceLoaderFactory.createRootRiceResourceLoader(springFileLocations); |
152 | 0 | resourceLoader.start(); |
153 | 0 | GlobalResourceLoader.addResourceLoader(resourceLoader); |
154 | 0 | return resourceLoader; |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public void stop() throws Exception { |
161 | 0 | LOG.info("Stopping Rice..."); |
162 | 0 | notify(new BeforeStopEvent()); |
163 | 0 | super.stop(); |
164 | 0 | GlobalResourceLoader.stop(); |
165 | 0 | LOG.info("...Rice stopped successfully."); |
166 | 0 | } |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
protected List<Lifecycle> loadLifecycles() throws Exception { |
172 | 0 | List<Lifecycle> lifecycles = new LinkedList<Lifecycle>(); |
173 | 0 | if (isConfigureLogging()) { |
174 | 0 | lifecycles.add(new Log4jLifeCycle()); |
175 | |
} |
176 | 0 | for (ModuleConfigurer module : this.modules) { |
177 | 0 | lifecycles.add(module); |
178 | |
} |
179 | 0 | return lifecycles; |
180 | |
} |
181 | |
|
182 | |
protected boolean isConfigureLogging() { |
183 | 0 | return ConfigContext.getCurrentContextConfig().getBooleanProperty(RiceConstants.RICE_LOGGING_CONFIGURE, false); |
184 | |
} |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public void onApplicationEvent(ApplicationEvent event) { |
190 | |
try { |
191 | |
|
192 | 0 | if (event instanceof ContextRefreshedEvent) { |
193 | 0 | notify(new AfterStartEvent()); |
194 | |
} |
195 | |
|
196 | 0 | else if (event instanceof ContextClosedEvent && !super.isStarted()) |
197 | |
{ |
198 | 0 | notify(new AfterStopEvent()); |
199 | |
} |
200 | 0 | } catch (Exception e) { |
201 | 0 | throw new RiceRuntimeException(e); |
202 | 0 | } |
203 | 0 | } |
204 | |
|
205 | |
protected void notify(RiceConfigEvent event) throws Exception { |
206 | 0 | for (ModuleConfigurer module : modules) { |
207 | 0 | module.onEvent(event); |
208 | |
} |
209 | 0 | } |
210 | |
|
211 | |
@SuppressWarnings("unchecked") |
212 | |
protected void initializeFullConfiguration() throws Exception { |
213 | 0 | if ( LOG.isInfoEnabled() ) { |
214 | 0 | LOG.info("Starting Rice configuration for environment " + this.environment); |
215 | |
} |
216 | 0 | Config currentConfig = parseConfig(); |
217 | 0 | initializeBaseConfiguration(currentConfig); |
218 | 0 | parseModuleConfigs(currentConfig); |
219 | 0 | } |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
protected void initializeBaseConfiguration(Config currentConfig) throws Exception { |
229 | 0 | configureEnvironment(currentConfig); |
230 | 0 | configureServiceNamespace(currentConfig); |
231 | |
|
232 | 0 | } |
233 | |
|
234 | |
protected Config parseConfig() throws Exception { |
235 | 0 | if (this.rootConfig == null) { |
236 | 0 | this.rootConfig = new JAXBConfigImpl(); |
237 | |
} |
238 | |
|
239 | 0 | Config currentRootConfig = ConfigContext.getCurrentContextConfig(); |
240 | 0 | if (currentRootConfig != null) { |
241 | 0 | currentRootConfig.putConfig(rootConfig); |
242 | 0 | this.rootConfig = currentRootConfig; |
243 | |
} else { |
244 | 0 | ConfigContext.init(this.rootConfig); |
245 | |
} |
246 | |
|
247 | 0 | if (this.configLocations != null && !this.configLocations.isEmpty()) { |
248 | 0 | Config tmpConfig = new JAXBConfigImpl(this.configLocations, this.properties); |
249 | 0 | tmpConfig.parseConfig(); |
250 | 0 | this.rootConfig.putConfig(tmpConfig); |
251 | 0 | } else if (this.properties != null) { |
252 | 0 | this.rootConfig.putProperties(this.properties); |
253 | |
} |
254 | |
|
255 | |
|
256 | 0 | this.rootConfig.putObject( RiceConstants.RICE_CONFIGURER_CONFIG_NAME, this ); |
257 | 0 | return this.rootConfig; |
258 | |
} |
259 | |
|
260 | |
protected void parseModuleConfigs(Config rootConfig) throws Exception { |
261 | 0 | for (ModuleConfigurer module : this.modules) { |
262 | |
|
263 | 0 | Config moduleConfig = module.loadConfig(rootConfig); |
264 | 0 | if (moduleConfig != null) { |
265 | 0 | rootConfig.putConfig(moduleConfig); |
266 | |
} |
267 | 0 | } |
268 | 0 | } |
269 | |
|
270 | |
protected void configureEnvironment(Config config) { |
271 | 0 | if (!StringUtils.isBlank(this.environment)) { |
272 | 0 | config.putProperty(Config.ENVIRONMENT, this.environment); |
273 | |
} |
274 | 0 | } |
275 | |
|
276 | |
protected void configureServiceNamespace(Config config) { |
277 | 0 | if (!StringUtils.isBlank(this.serviceNamespace)) { |
278 | 0 | config.putProperty(Config.SERVICE_NAMESPACE, this.serviceNamespace); |
279 | |
} |
280 | 0 | } |
281 | |
|
282 | |
public String getEnvironment() { |
283 | 0 | return this.environment; |
284 | |
} |
285 | |
|
286 | |
public void setEnvironment(String environment) { |
287 | 0 | this.environment = environment; |
288 | 0 | } |
289 | |
|
290 | |
public String getServiceNamespace() { |
291 | 0 | return this.serviceNamespace; |
292 | |
} |
293 | |
|
294 | |
public void setServiceNamespace(String ServiceNamespace) { |
295 | 0 | this.serviceNamespace = ServiceNamespace; |
296 | 0 | } |
297 | |
|
298 | |
public ResourceLoader getRootResourceLoader() { |
299 | 0 | return this.rootResourceLoader; |
300 | |
} |
301 | |
|
302 | |
public void setRootResourceLoader(ResourceLoader rootResourceLoader) { |
303 | 0 | this.rootResourceLoader = rootResourceLoader; |
304 | 0 | } |
305 | |
|
306 | |
public Properties getProperties() { |
307 | 0 | return this.properties; |
308 | |
} |
309 | |
|
310 | |
public void setProperties(Properties properties) { |
311 | 0 | this.properties = properties; |
312 | 0 | } |
313 | |
|
314 | |
public List<String> getConfigLocations() { |
315 | 0 | return this.configLocations; |
316 | |
} |
317 | |
|
318 | |
public void setConfigLocations(List<String> configLocations) { |
319 | 0 | this.configLocations = configLocations; |
320 | 0 | } |
321 | |
|
322 | |
public Config getRootConfig() { |
323 | 0 | return this.rootConfig; |
324 | |
} |
325 | |
|
326 | |
public void setRootConfig(Config rootConfig) { |
327 | 0 | this.rootConfig = rootConfig; |
328 | 0 | } |
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
public List<ModuleConfigurer> getModules() { |
334 | 0 | return this.modules; |
335 | |
} |
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
public void setModules(List<ModuleConfigurer> modules) { |
341 | 0 | this.modules = modules; |
342 | 0 | } |
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
public List<String> getAdditionalSpringFiles() { |
348 | 0 | return this.additionalSpringFiles; |
349 | |
} |
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
@SuppressWarnings("unchecked") |
356 | |
public void setAdditionalSpringFiles(List<String> additionalSpringFiles) { |
357 | |
|
358 | 0 | if (null != additionalSpringFiles && |
359 | |
additionalSpringFiles.size() >= 1) { |
360 | |
|
361 | |
|
362 | 0 | this.additionalSpringFiles = new ArrayList<String>(); |
363 | |
|
364 | 0 | for (String fileName : additionalSpringFiles) { |
365 | 0 | if (fileName.contains(",")) { |
366 | 0 | this.additionalSpringFiles.addAll(new ArrayList<String>(Arrays.asList(fileName.split(",")))); |
367 | |
} else { |
368 | 0 | this.additionalSpringFiles.add(fileName); |
369 | |
} |
370 | |
} |
371 | |
} else { |
372 | 0 | this.additionalSpringFiles = Collections.emptyList(); |
373 | |
} |
374 | 0 | } |
375 | |
|
376 | |
public BeanFactory getBeanFactory() { |
377 | 0 | return beanFactory; |
378 | |
} |
379 | |
|
380 | |
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
381 | 0 | this.beanFactory = beanFactory; |
382 | 0 | } |
383 | |
|
384 | |
} |