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