1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
70
71
72
73 @Override
74 protected List<Lifecycle> loadLifecycles() throws Exception {
75 return new ArrayList<Lifecycle>(0);
76 }
77
78
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
89
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
146
147 protected void configureRunMode(Config config) {
148 String propertyName = getModuleName().toLowerCase() + ".mode";
149 config.putProperty(propertyName, getRunMode());
150 }
151
152
153
154
155
156
157
158 public String getSpringFileLocations() throws Exception {
159 return springFileLocations;
160 }
161
162
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
179
180
181
182 public ResourceLoader getResourceLoaderToRegister() throws Exception{
183 return null;
184 }
185
186
187
188
189
190
191
192
193
194
195
196
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
245
246
247
248
249
250 public boolean shouldRenderWebInterface() {
251 return hasWebInterface() && getRunMode().equals( ModuleConfigurer.LOCAL_RUN_MODE );
252 }
253
254
255
256
257 public boolean isTestMode() {
258 return this.testMode;
259 }
260
261
262
263
264 public void setTestMode(boolean testMode) {
265 this.testMode = testMode;
266 }
267
268
269
270
271 public void setSpringFileLocations(String springFileLocations) {
272 this.springFileLocations = springFileLocations;
273 }
274
275
276
277
278 public String getWebModuleBaseUrl() {
279 return this.webModuleBaseUrl;
280 }
281
282
283
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 }