Coverage Report - org.kuali.rice.core.config.ModuleConfigurer
 
Classes in this File Line Coverage Branch Coverage Complexity
ModuleConfigurer
0%
0/97
0%
0/28
1.417
 
 1  
 /*
 2  
  * Copyright 2007-2008 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.core.config;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import javax.xml.namespace.QName;
 22  
 
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.apache.log4j.Logger;
 25  
 import org.kuali.rice.core.config.event.RiceConfigEvent;
 26  
 import org.kuali.rice.core.config.event.RiceConfigEventListener;
 27  
 import org.kuali.rice.core.lifecycle.BaseCompositeLifecycle;
 28  
 import org.kuali.rice.core.lifecycle.Lifecycle;
 29  
 import org.kuali.rice.core.resourceloader.ResourceLoader;
 30  
 import org.kuali.rice.core.resourceloader.SpringResourceLoader;
 31  
 import org.springframework.beans.factory.InitializingBean;
 32  
 
 33  
 public abstract class ModuleConfigurer extends BaseCompositeLifecycle implements Configurer, InitializingBean, RiceConfigEventListener {
 34  
     /**
 35  
      * Protected logger for use by subclasses
 36  
      */
 37  0
     protected final Logger LOG = Logger.getLogger(getClass());
 38  
 
 39  
         public static final String LOCAL_RUN_MODE = "local";
 40  
         public static final String EMBEDDED_RUN_MODE = "embedded";
 41  
         public static final String REMOTE_RUN_MODE = "remote";
 42  
         public static final String THIN_RUN_MODE = "thin";
 43  0
         protected final List<String> VALID_RUN_MODES = new ArrayList<String>();
 44  
         
 45  0
         private String runMode = LOCAL_RUN_MODE;        
 46  0
     private String moduleName = "!!!UNSET!!!";        
 47  0
         protected String webModuleConfigName = "";
 48  0
         protected String webModuleConfigurationFiles = "";
 49  0
         protected String webModuleBaseUrl = "";
 50  0
         protected boolean webInterface = false;
 51  
     protected boolean testMode;
 52  0
     protected String springFileLocations = "";
 53  
     protected String resourceLoaderName;
 54  0
     protected boolean exposeServicesOnBus = true;
 55  0
     protected boolean setSOAPServicesAsDefault = false;
 56  0
     protected boolean includeUserInterfaceComponents = true;
 57  
         
 58  
         /**
 59  
          * 
 60  
          */
 61  0
         public ModuleConfigurer() {
 62  0
                 VALID_RUN_MODES.add( LOCAL_RUN_MODE );
 63  0
                 VALID_RUN_MODES.add( EMBEDDED_RUN_MODE );
 64  0
                 VALID_RUN_MODES.add( REMOTE_RUN_MODE );
 65  0
                 VALID_RUN_MODES.add( THIN_RUN_MODE );
 66  0
         }
 67  
         
 68  
         /**
 69  
          * This overridden method ...
 70  
          * 
 71  
          * @see org.kuali.rice.core.lifecycle.BaseCompositeLifecycle#loadLifecycles()
 72  
          */
 73  
         @Override
 74  
         protected List<Lifecycle> loadLifecycles() throws Exception {
 75  0
                 return new ArrayList<Lifecycle>(0);
 76  
         }
 77  
         /**
 78  
          * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
 79  
          */
 80  
         public void afterPropertiesSet() throws Exception {
 81  0
                 if ( getModuleName().equals( "!!!UNSET!!!" ) ) {
 82  0
                         throw new IllegalArgumentException( "Module Name must be given for each ModuleConfigurer instance" );
 83  
                 }
 84  0
                 if ( hasWebInterface() ) {
 85  0
                         if ( StringUtils.isBlank( webModuleConfigName ) ) {
 86  0
                                 setWebModuleConfigName( "config/" + getModuleName().toLowerCase() );
 87  
                         }
 88  
 //                        if ( StringUtils.isBlank( webModuleConfigurationFiles ) ) {
 89  
 //                                setWebModuleConfigurationFiles( "/" + getModuleName().toLowerCase() + "/WEB-INF/struts-config.xml" );
 90  
 //                        }
 91  
                 }
 92  0
         }
 93  
         
 94  
         
 95  
         
 96  
         public String getRunMode() {
 97  0
                 return this.runMode;
 98  
         }
 99  
 
 100  
         public void setRunMode(String runMode) {
 101  0
                 runMode = runMode.trim();
 102  0
                 if ( !VALID_RUN_MODES.contains( runMode ) ) {
 103  0
                         throw new IllegalArgumentException( "Invalid run mode for the " + this.getClass().getSimpleName() + ": " + runMode + " - Valid Values are: " + VALID_RUN_MODES );
 104  
                 }
 105  0
                 this.runMode = runMode;
 106  0
         }
 107  
 
 108  
         
 109  
         public Config loadConfig(Config parentConfig) throws Exception {
 110  0
             if ( LOG.isInfoEnabled() ) {
 111  0
                     LOG.info("Starting configuration of " + getModuleName() + " for service namespace " + parentConfig.getServiceNamespace());
 112  
             }
 113  0
                 Config config = parentConfig;
 114  0
         if (Boolean.valueOf(config.getProperty("rice." + getModuleName().toLowerCase() + ".testMode"))) {
 115  0
             testMode = true;
 116  
         }                
 117  0
                 configureWebConfiguration(config);
 118  0
                 configureRunMode(config);
 119  0
                 return config;
 120  
         }
 121  
 
 122  
         protected void configureWebConfiguration( Config config ) throws Exception {
 123  0
                 if ( StringUtils.isBlank( getWebModuleConfigurationFiles() ) ) {
 124  0
                         if ( StringUtils.isBlank( config.getProperty( "rice." + getModuleName().toLowerCase() + ".struts.config.files" ) ) ) {
 125  0
                                 setWebModuleConfigurationFiles( "/" + getModuleName().toLowerCase() + "/WEB-INF/struts-config.xml" );
 126  
                         } else {
 127  0
                                 setWebModuleConfigurationFiles( config.getProperty( "rice." + getModuleName().toLowerCase() + ".struts.config.files" ) );
 128  
                         }
 129  
                 }
 130  0
                 config.putProperty( "rice." + getModuleName().toLowerCase() + ".struts.config.files", getWebModuleConfigurationFiles() );
 131  0
                 if ( StringUtils.isBlank( getWebModuleBaseUrl() ) ) {
 132  0
                         if ( StringUtils.isBlank( config.getProperty( getModuleName().toLowerCase() + ".url" ) ) ) {
 133  0
                                 setWebModuleBaseUrl( config.getProperty( "application.url" ) + "/" + getModuleName().toLowerCase() );
 134  
                         } else {
 135  0
                                 setWebModuleBaseUrl( config.getProperty( getModuleName().toLowerCase() + ".url" ) );
 136  
                         }
 137  
                 }
 138  0
                 config.putProperty( getModuleName().toLowerCase() + ".url", getWebModuleBaseUrl() );
 139  0
                 if ( StringUtils.isEmpty( getSpringFileLocations() ) ) {
 140  0
                         setSpringFileLocations( getDefaultSpringBeansPath(getDefaultConfigPackagePath() ) );
 141  
                 }
 142  0
         }
 143  
         
 144  
         /**
 145  
          * Creates a configuration parameter for the run mode by appending the module name (in lower case) plus ".mode"
 146  
          */
 147  
         protected void configureRunMode(Config config) {
 148  0
                 String propertyName = getModuleName().toLowerCase() + ".mode";
 149  0
                 config.putProperty(propertyName, getRunMode());
 150  0
         }
 151  
         
 152  
         /**
 153  
          * 
 154  
          * This method returns a comma separated string of spring file locations for this module.
 155  
          * 
 156  
          * @throws Exception
 157  
          */
 158  
         public String getSpringFileLocations() throws Exception {
 159  0
                 return springFileLocations;
 160  
         }
 161  
         
 162  
     /* helper methods for constructors */
 163  
     protected String getDefaultConfigPackagePath() {
 164  0
             return "org/kuali/rice/" + getModuleName().toLowerCase() + "/config/";
 165  
     }
 166  
     protected String getDefaultSpringBeansPath(String configPackagePath) {
 167  0
         return configPackagePath + getModuleName().toUpperCase() + "SpringBeans.xml"; 
 168  
     }
 169  
     public String getDefaultResourceLoaderName() {
 170  0
         return getModuleName().toUpperCase() + "_SPRING_RESOURCE_LOADER";        
 171  
     }
 172  
     public QName getDefaultResourceLoaderQName() {
 173  0
         return new QName(ConfigContext.getCurrentContextConfig().getServiceNamespace(), getDefaultResourceLoaderName());
 174  
     }
 175  
         
 176  
         /**
 177  
          * 
 178  
          * This method returns a resource loader that this module might want to register with the global resource loader.
 179  
          * 
 180  
          * @throws Exception
 181  
          */
 182  
         public ResourceLoader getResourceLoaderToRegister() throws Exception{
 183  0
                 return null;
 184  
         }
 185  
         
 186  
     /**
 187  
      * Template method for creation of the module resource loader.  Subclasses should override
 188  
      * and return an appropriate resource loader for the module.  If 'null' is returned, no
 189  
      * resource loader is added to the lifecycles by default.  The caller {@link #loadLifecycles()}
 190  
      * implementation will add the ResourceLoader to the GlobalResourceLoader, so that it is not
 191  
      * necessary to do so in the subclass.
 192  
      * @return a resource loader for the module, or null
 193  
      */
 194  
     /**
 195  
      * Constructs a SpringResourceLoader from the appropriate Spring context resource and with the configured
 196  
      * resource loader name (and current context config service namespace)
 197  
      */
 198  
     protected ResourceLoader createResourceLoader() throws Exception {
 199  0
         String context = getSpringFileLocations();
 200  0
         ResourceLoader resourceLoader = new SpringResourceLoader(new QName(ConfigContext.getCurrentContextConfig().getServiceNamespace(), resourceLoaderName), context);
 201  0
         return resourceLoader;
 202  
     }
 203  
 
 204  
         
 205  
         public void onEvent(RiceConfigEvent event) throws Exception {
 206  0
                 if ( LOG.isInfoEnabled() ) {
 207  0
                         LOG.info( "ModuleConfigurer.onEvent() called: " + event );
 208  
                 }
 209  0
         }
 210  
 
 211  
         public String getModuleName() {
 212  0
                 return this.moduleName;
 213  
         }
 214  
 
 215  
         public void setModuleName(String moduleName) {
 216  0
                 this.moduleName = moduleName;
 217  0
         }
 218  
 
 219  
         public String getWebModuleConfigName() {
 220  0
                 return this.webModuleConfigName;
 221  
         }
 222  
 
 223  
         public void setWebModuleConfigName(String webModuleConfigName) {
 224  0
                 this.webModuleConfigName = webModuleConfigName;
 225  0
         }
 226  
 
 227  
         public String getWebModuleConfigurationFiles() {
 228  0
                 return this.webModuleConfigurationFiles;
 229  
         }
 230  
 
 231  
         public void setWebModuleConfigurationFiles(String webModuleConfigurationFiles) {
 232  0
                 this.webModuleConfigurationFiles = webModuleConfigurationFiles;
 233  0
         }
 234  
 
 235  
         public boolean hasWebInterface() {
 236  0
                 return this.webInterface;
 237  
         }
 238  
 
 239  
         protected void setHasWebInterface(boolean webInterface) {
 240  0
                 this.webInterface = webInterface;
 241  0
         }
 242  
 
 243  
         /**
 244  
          * This base implementation returns true when the module has a web interface and the
 245  
          * runMode is "local".
 246  
          * 
 247  
          * Subclasses can override this method if there are different requirements for inclusion
 248  
          * of the web UI for the module.
 249  
          */
 250  
         public boolean shouldRenderWebInterface() {
 251  0
                 return hasWebInterface() &&        getRunMode().equals( ModuleConfigurer.LOCAL_RUN_MODE );
 252  
         }
 253  
         
 254  
         /**
 255  
          * @return the testMode
 256  
          */
 257  
         public boolean isTestMode() {
 258  0
                 return this.testMode;
 259  
         }
 260  
 
 261  
         /**
 262  
          * @param testMode the testMode to set
 263  
          */
 264  
         public void setTestMode(boolean testMode) {
 265  0
                 this.testMode = testMode;
 266  0
         }
 267  
 
 268  
         /**
 269  
          * @param springFileLocations the springFileLocations to set
 270  
          */
 271  
         public void setSpringFileLocations(String springFileLocations) {
 272  0
                 this.springFileLocations = springFileLocations;
 273  0
         }
 274  
 
 275  
         /**
 276  
          * @return the webModuleBaseUrl
 277  
          */
 278  
         public String getWebModuleBaseUrl() {
 279  0
                 return this.webModuleBaseUrl;
 280  
         }
 281  
 
 282  
         /**
 283  
          * @param webModuleBaseUrl the webModuleBaseUrl to set
 284  
          */
 285  
         public void setWebModuleBaseUrl(String webModuleBaseUrl) {
 286  0
                 this.webModuleBaseUrl = webModuleBaseUrl;
 287  0
         }
 288  
 
 289  
         public boolean isExposeServicesOnBus() {
 290  0
                 return this.exposeServicesOnBus;
 291  
         }
 292  
 
 293  
         public void setExposeServicesOnBus(boolean exposeServicesOnBus) {
 294  0
                 this.exposeServicesOnBus = exposeServicesOnBus;
 295  0
         }
 296  
 
 297  
         public boolean isIncludeUserInterfaceComponents() {
 298  0
                 return this.includeUserInterfaceComponents;
 299  
         }
 300  
 
 301  
         public void setIncludeUserInterfaceComponents(
 302  
                         boolean includeUserInterfaceComponents) {
 303  0
                 this.includeUserInterfaceComponents = includeUserInterfaceComponents;
 304  0
         }
 305  
 
 306  
         public boolean isSetSOAPServicesAsDefault() {
 307  0
                 return this.setSOAPServicesAsDefault;
 308  
         }
 309  
 
 310  
         public void setSetSOAPServicesAsDefault(boolean setSOAPServicesAsDefault) {
 311  0
                 this.setSOAPServicesAsDefault = setSOAPServicesAsDefault;
 312  0
         }
 313  
 
 314  
         
 315  
 }