View Javadoc

1   /*
2    * Copyright 2007 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.kns.web.servlet;
17  
18  import java.io.InputStream;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.servlet.ServletConfig;
23  import javax.servlet.ServletException;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.rice.core.util.ClassLoaderUtils;
27  import org.kuali.rice.kns.service.KNSServiceLocator;
28  import org.kuali.rice.kns.service.ModuleService;
29  import org.kuali.rice.kns.util.KNSConstants;
30  import org.kuali.rice.kns.util.spring.NamedOrderedListBean;
31  import org.springframework.core.io.DefaultResourceLoader;
32  
33  import uk.ltd.getahead.dwr.Configuration;
34  import uk.ltd.getahead.dwr.DWRServlet;
35  
36  public class KualiDWRServlet extends DWRServlet {
37  	/**
38  	 * 
39  	 */
40  	private static final long serialVersionUID = -3903455224197903186L;
41  
42  	private static final String CLASSPATH_RESOURCE_PREFIX = "classpath.resource.prefix";
43  	
44  	public static List<String> HACK_ADDITIONAL_FILES = new ArrayList<String>();
45  
46  	private Boolean springBasedConfigPath;
47  
48  	@Override
49  	public void init(ServletConfig config) throws ServletException {
50  		setSpringBasedConfigPath(new Boolean(config.getInitParameter("springpath")));
51  		super.init(config);
52  	}
53  
54  	/**
55  	 * This method calls the super version then loads the dwr config file
56  	 * specified in the loaded module definitions.
57  	 * 
58  	 * @see uk.ltd.getahead.dwr.DWRServlet#configure(javax.servlet.ServletConfig,
59  	 *      uk.ltd.getahead.dwr.Configuration)
60  	 */
61  	@Override
62  	public void configure(ServletConfig servletConfig, Configuration configuration) throws ServletException {
63  		String classpathResourcePrefix = KNSServiceLocator.getKualiConfigurationService().getPropertyString(CLASSPATH_RESOURCE_PREFIX);
64  		for (NamedOrderedListBean namedOrderedListBean : KNSServiceLocator.getNamedOrderedListBeans(KNSConstants.SCRIPT_CONFIGURATION_FILES_LIST_NAME)) {
65  			for (String scriptConfigurationFilePath : namedOrderedListBean.getList()) {
66  				if (getSpringBasedConfigPath()) {
67  					DefaultResourceLoader resourceLoader = new DefaultResourceLoader(ClassLoaderUtils.getDefaultClassLoader());
68  					try {
69  						InputStream is = resourceLoader.getResource(scriptConfigurationFilePath).getInputStream();
70  						configuration.addConfig(is);
71  					} catch (Exception e) {
72  						throw new ServletException(e);
73  					}
74  				} else {
75  					super.readFile(classpathResourcePrefix + 
76  							scriptConfigurationFilePath, configuration);
77  				}
78  			}
79  		}
80  		for (ModuleService moduleService : KNSServiceLocator.getKualiModuleService().getInstalledModuleServices()) {
81  			for (String scriptConfigurationFilePath : moduleService.getModuleConfiguration().getScriptConfigurationFilePaths()) {
82  				if (!StringUtils.isBlank(scriptConfigurationFilePath))
83  					super.readFile(classpathResourcePrefix + scriptConfigurationFilePath, configuration);
84  			}	
85  		}
86  		
87  		for (String configFile : HACK_ADDITIONAL_FILES) {
88  			super.readFile(classpathResourcePrefix + configFile, configuration);
89  		}
90  	}
91  
92  	public Boolean getSpringBasedConfigPath() {
93  		return springBasedConfigPath;
94  	}
95  
96  	public void setSpringBasedConfigPath(Boolean springBasedConfigPath) {
97  		this.springBasedConfigPath = springBasedConfigPath;
98  	}
99  }