View Javadoc

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      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  	protected final List<String> VALID_RUN_MODES = new ArrayList<String>();
44  	
45  	private String runMode = LOCAL_RUN_MODE;	
46      private String moduleName = "!!!UNSET!!!";	
47  	protected String webModuleConfigName = "";
48  	protected String webModuleConfigurationFiles = "";
49  	protected String webModuleBaseUrl = "";
50  	protected boolean webInterface = false;
51      protected boolean testMode;
52      protected String springFileLocations = "";
53      protected String resourceLoaderName;
54      protected boolean exposeServicesOnBus = true;
55      protected boolean setSOAPServicesAsDefault = false;
56      protected boolean includeUserInterfaceComponents = true;
57  	
58  	/**
59  	 * 
60  	 */
61  	public ModuleConfigurer() {
62  		VALID_RUN_MODES.add( LOCAL_RUN_MODE );
63  		VALID_RUN_MODES.add( EMBEDDED_RUN_MODE );
64  		VALID_RUN_MODES.add( REMOTE_RUN_MODE );
65  		VALID_RUN_MODES.add( THIN_RUN_MODE );
66  	}
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  		return new ArrayList<Lifecycle>(0);
76  	}
77  	/**
78  	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
79  	 */
80  	public void afterPropertiesSet() throws Exception {
81  		if ( getModuleName().equals( "!!!UNSET!!!" ) ) {
82  			throw new IllegalArgumentException( "Module Name must be given for each ModuleConfigurer instance" );
83  		}
84  		if ( hasWebInterface() ) {
85  			if ( StringUtils.isBlank( webModuleConfigName ) ) {
86  				setWebModuleConfigName( "config/" + getModuleName().toLowerCase() );
87  			}
88  //			if ( StringUtils.isBlank( webModuleConfigurationFiles ) ) {
89  //				setWebModuleConfigurationFiles( "/" + getModuleName().toLowerCase() + "/WEB-INF/struts-config.xml" );
90  //			}
91  		}
92  	}
93  	
94  	
95  	
96  	public String getRunMode() {
97  		return this.runMode;
98  	}
99  
100 	public void setRunMode(String runMode) {
101 		runMode = runMode.trim();
102 		if ( !VALID_RUN_MODES.contains( runMode ) ) {
103 			throw new IllegalArgumentException( "Invalid run mode for the " + this.getClass().getSimpleName() + ": " + runMode + " - Valid Values are: " + VALID_RUN_MODES );
104 		}
105 		this.runMode = runMode;
106 	}
107 
108 	
109 	public Config loadConfig(Config parentConfig) throws Exception {
110     	if ( LOG.isInfoEnabled() ) {
111     		LOG.info("Starting configuration of " + getModuleName() + " for service namespace " + parentConfig.getServiceNamespace());
112     	}
113 		Config config = parentConfig;
114         if (Boolean.valueOf(config.getProperty("rice." + getModuleName().toLowerCase() + ".testMode"))) {
115             testMode = true;
116         }		
117 		configureWebConfiguration(config);
118 		configureRunMode(config);
119 		return config;
120 	}
121 
122 	protected void configureWebConfiguration( Config config ) throws Exception {
123 		if ( StringUtils.isBlank( getWebModuleConfigurationFiles() ) ) {
124 			if ( StringUtils.isBlank( config.getProperty( "rice." + getModuleName().toLowerCase() + ".struts.config.files" ) ) ) {
125 				setWebModuleConfigurationFiles( "/" + getModuleName().toLowerCase() + "/WEB-INF/struts-config.xml" );
126 			} else {
127 				setWebModuleConfigurationFiles( config.getProperty( "rice." + getModuleName().toLowerCase() + ".struts.config.files" ) );
128 			}
129 		}
130 		config.putProperty( "rice." + getModuleName().toLowerCase() + ".struts.config.files", getWebModuleConfigurationFiles() );
131 		if ( StringUtils.isBlank( getWebModuleBaseUrl() ) ) {
132 			if ( StringUtils.isBlank( config.getProperty( getModuleName().toLowerCase() + ".url" ) ) ) {
133 				setWebModuleBaseUrl( config.getProperty( "application.url" ) + "/" + getModuleName().toLowerCase() );
134 			} else {
135 				setWebModuleBaseUrl( config.getProperty( getModuleName().toLowerCase() + ".url" ) );
136 			}
137 		}
138 		config.putProperty( getModuleName().toLowerCase() + ".url", getWebModuleBaseUrl() );
139 		if ( StringUtils.isEmpty( getSpringFileLocations() ) ) {
140 			setSpringFileLocations( getDefaultSpringBeansPath(getDefaultConfigPackagePath() ) );
141 		}
142 	}
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 		String propertyName = getModuleName().toLowerCase() + ".mode";
149 		config.putProperty(propertyName, getRunMode());
150 	}
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 		return springFileLocations;
160 	}
161 	
162     /* helper methods for constructors */
163     protected String getDefaultConfigPackagePath() {
164     	return "org/kuali/rice/" + getModuleName().toLowerCase() + "/config/";
165     }
166     protected String getDefaultSpringBeansPath(String configPackagePath) {
167         return configPackagePath + getModuleName().toUpperCase() + "SpringBeans.xml"; 
168     }
169     public String getDefaultResourceLoaderName() {
170         return getModuleName().toUpperCase() + "_SPRING_RESOURCE_LOADER";        
171     }
172     public QName getDefaultResourceLoaderQName() {
173         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 		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         String context = getSpringFileLocations();
200         ResourceLoader resourceLoader = new SpringResourceLoader(new QName(ConfigContext.getCurrentContextConfig().getServiceNamespace(), resourceLoaderName), context);
201         return resourceLoader;
202     }
203 
204 	
205 	public void onEvent(RiceConfigEvent event) throws Exception {
206 		if ( LOG.isInfoEnabled() ) {
207 			LOG.info( "ModuleConfigurer.onEvent() called: " + event );
208 		}
209 	}
210 
211 	public String getModuleName() {
212 		return this.moduleName;
213 	}
214 
215 	public void setModuleName(String moduleName) {
216 		this.moduleName = moduleName;
217 	}
218 
219 	public String getWebModuleConfigName() {
220 		return this.webModuleConfigName;
221 	}
222 
223 	public void setWebModuleConfigName(String webModuleConfigName) {
224 		this.webModuleConfigName = webModuleConfigName;
225 	}
226 
227 	public String getWebModuleConfigurationFiles() {
228 		return this.webModuleConfigurationFiles;
229 	}
230 
231 	public void setWebModuleConfigurationFiles(String webModuleConfigurationFiles) {
232 		this.webModuleConfigurationFiles = webModuleConfigurationFiles;
233 	}
234 
235 	public boolean hasWebInterface() {
236 		return this.webInterface;
237 	}
238 
239 	protected void setHasWebInterface(boolean webInterface) {
240 		this.webInterface = webInterface;
241 	}
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 		return hasWebInterface() &&	getRunMode().equals( ModuleConfigurer.LOCAL_RUN_MODE );
252 	}
253 	
254 	/**
255 	 * @return the testMode
256 	 */
257 	public boolean isTestMode() {
258 		return this.testMode;
259 	}
260 
261 	/**
262 	 * @param testMode the testMode to set
263 	 */
264 	public void setTestMode(boolean testMode) {
265 		this.testMode = testMode;
266 	}
267 
268 	/**
269 	 * @param springFileLocations the springFileLocations to set
270 	 */
271 	public void setSpringFileLocations(String springFileLocations) {
272 		this.springFileLocations = springFileLocations;
273 	}
274 
275 	/**
276 	 * @return the webModuleBaseUrl
277 	 */
278 	public String getWebModuleBaseUrl() {
279 		return this.webModuleBaseUrl;
280 	}
281 
282 	/**
283 	 * @param webModuleBaseUrl the webModuleBaseUrl to set
284 	 */
285 	public void setWebModuleBaseUrl(String webModuleBaseUrl) {
286 		this.webModuleBaseUrl = webModuleBaseUrl;
287 	}
288 
289 	public boolean isExposeServicesOnBus() {
290 		return this.exposeServicesOnBus;
291 	}
292 
293 	public void setExposeServicesOnBus(boolean exposeServicesOnBus) {
294 		this.exposeServicesOnBus = exposeServicesOnBus;
295 	}
296 
297 	public boolean isIncludeUserInterfaceComponents() {
298 		return this.includeUserInterfaceComponents;
299 	}
300 
301 	public void setIncludeUserInterfaceComponents(
302 			boolean includeUserInterfaceComponents) {
303 		this.includeUserInterfaceComponents = includeUserInterfaceComponents;
304 	}
305 
306 	public boolean isSetSOAPServicesAsDefault() {
307 		return this.setSOAPServicesAsDefault;
308 	}
309 
310 	public void setSetSOAPServicesAsDefault(boolean setSOAPServicesAsDefault) {
311 		this.setSOAPServicesAsDefault = setSOAPServicesAsDefault;
312 	}
313 
314 	
315 }