001/* 002 * Copyright 2012 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.ole.sys.web.servlet; 017 018import java.io.IOException; 019import java.io.InputStream; 020import java.util.ArrayList; 021import java.util.List; 022import java.util.Map; 023 024import javax.servlet.ServletConfig; 025import javax.servlet.ServletException; 026 027import org.apache.commons.lang.StringUtils; 028import org.directwebremoting.Container; 029import org.directwebremoting.extend.Configurator; 030import org.directwebremoting.impl.DwrXmlConfigurator; 031import org.directwebremoting.impl.StartupUtil; 032import org.directwebremoting.servlet.DwrServlet; 033import org.kuali.ole.sys.OLEConstants; 034import org.kuali.ole.sys.context.SpringContext; 035import org.kuali.rice.core.api.config.property.ConfigurationService; 036import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 037import org.kuali.rice.core.api.util.ClassLoaderUtils; 038import org.kuali.rice.core.framework.util.spring.NamedOrderedListBean; 039import org.kuali.rice.krad.service.KRADServiceLocator; 040import org.kuali.rice.krad.service.KualiModuleService; 041import org.kuali.rice.krad.service.ModuleService; 042import org.springframework.core.io.DefaultResourceLoader; 043 044public class OleDWRServlet extends DwrServlet { 045 /** 046 * 047 */ 048 private static final long serialVersionUID = -3903455224197903186L; 049 050 protected static final String CLASSPATH_RESOURCE_PREFIX = "classpath.resource.prefix"; 051 public static List<String> HACK_ADDITIONAL_FILES = new ArrayList<String>(); 052 053 private Boolean springBasedConfigPath; 054 055 /** 056 * This method calls the super version then loads the dwr config file 057 * specified in the loaded module definitions. 058 * 059 * @see uk.ltd.getahead.dwr.DWRServlet#configure(javax.servlet.ServletConfig, 060 * uk.ltd.getahead.dwr.Configuration) 061 */ 062 protected List<NamedOrderedListBean> getDwrNamedOrderedListBeans(String listName) { 063 List <NamedOrderedListBean> dwrListBeans = new ArrayList<NamedOrderedListBean>(); 064 Map<String, NamedOrderedListBean> namedOrderedListBeans = SpringContext.getBeansOfType(NamedOrderedListBean.class); 065 for (NamedOrderedListBean nameOrderedListBean : namedOrderedListBeans.values()) { 066 if (nameOrderedListBean.getName().equals(listName)) { 067 dwrListBeans.add((NamedOrderedListBean) nameOrderedListBean); 068 } 069 } 070 return dwrListBeans; 071 } 072 073 protected DwrXmlConfigurator generateConfigurator(DefaultResourceLoader resourceLoader, String scriptConfigurationFilePath ) throws ServletException { 074 try { 075 InputStream is = resourceLoader.getResource(scriptConfigurationFilePath).getInputStream(); 076 DwrXmlConfigurator dwrXmlConfigurator = new DwrXmlConfigurator(); 077 dwrXmlConfigurator.setInputStream(is); 078 return dwrXmlConfigurator; 079 } catch (Exception e) { 080 throw new ServletException(e); 081 } 082 } 083 084 @Override 085 protected void configureContainer(Container container, ServletConfig servletConfig) throws ServletException, IOException { 086 List<Configurator> configurators = new ArrayList<Configurator>(); 087 DefaultResourceLoader resourceLoader = new DefaultResourceLoader(ClassLoaderUtils.getDefaultClassLoader()); 088 ConfigurationService configurationService=GlobalResourceLoader.getService("kualiConfigurationService"); 089 String classpathResourcePrefix = configurationService.getPropertyValueAsString(CLASSPATH_RESOURCE_PREFIX); 090 for (NamedOrderedListBean namedOrderedListBean : this.getDwrNamedOrderedListBeans(OLEConstants.SCRIPT_CONFIGURATION_FILES_LIST_NAME)) { 091 for (String scriptConfigurationFilePath : namedOrderedListBean.getList()) { 092 if (getSpringBasedConfigPath()) { 093 try { 094 configurators.add( this.generateConfigurator(resourceLoader, scriptConfigurationFilePath)); 095 } catch (Exception e) { 096 throw new ServletException(e); 097 } 098 } 099 } 100 } 101 102 KualiModuleService kmi = SpringContext.getBean(KualiModuleService.class); 103 List<ModuleService> modules = kmi.getInstalledModuleServices(); 104 105 for (ModuleService moduleService : modules) { 106 for (String scriptConfigurationFilePath : moduleService.getModuleConfiguration().getScriptConfigurationFilePaths()) { 107 if (!StringUtils.isBlank(scriptConfigurationFilePath)) 108 try { 109 configurators.add( this.generateConfigurator(resourceLoader, scriptConfigurationFilePath)); 110 } catch (Exception e) { 111 throw new ServletException(e); 112 } 113 } 114 } 115 116 for (String configFile : HACK_ADDITIONAL_FILES) { 117 try { 118 String scriptConfigurationFilePath = classpathResourcePrefix + configFile; 119 configurators.add( this.generateConfigurator(resourceLoader, scriptConfigurationFilePath)); 120 } catch (Exception e) { 121 throw new ServletException(e); 122 } 123 } 124 try 125 { 126 super.configureContainer(container, servletConfig); 127 StartupUtil.configure(container, configurators); 128 } 129 catch (IOException ex) 130 { 131 throw ex; 132 } 133 catch (Exception ex) 134 { 135 throw new ServletException(ex); 136 } 137 } 138 139 public Boolean getSpringBasedConfigPath() { 140 return springBasedConfigPath; 141 } 142 143 public void setSpringBasedConfigPath(Boolean springBasedConfigPath) { 144 this.springBasedConfigPath = springBasedConfigPath; 145 } 146 147 /** 148 * @see javax.servlet.GenericServlet#init() 149 */ 150 151 @Override 152 public void init() throws ServletException { 153 setSpringBasedConfigPath(new Boolean(this.getInitParameter("springpath"))); 154 super.init(); 155 } 156 157}