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