View Javadoc
1   /*
2    * Copyright 2012 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.ole.sys.web.servlet;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.servlet.ServletConfig;
25  import javax.servlet.ServletException;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.directwebremoting.Container;
29  import org.directwebremoting.extend.Configurator;
30  import org.directwebremoting.impl.DwrXmlConfigurator;
31  import org.directwebremoting.impl.StartupUtil;
32  import org.directwebremoting.servlet.DwrServlet;
33  import org.kuali.ole.sys.OLEConstants;
34  import org.kuali.ole.sys.context.SpringContext;
35  import org.kuali.rice.core.api.config.property.ConfigurationService;
36  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
37  import org.kuali.rice.core.api.util.ClassLoaderUtils;
38  import org.kuali.rice.core.framework.util.spring.NamedOrderedListBean;
39  import org.kuali.rice.krad.service.KRADServiceLocator;
40  import org.kuali.rice.krad.service.KualiModuleService;
41  import org.kuali.rice.krad.service.ModuleService;
42  import org.springframework.core.io.DefaultResourceLoader;
43  
44  public class OleDWRServlet extends DwrServlet {
45      /**
46       * 
47       */
48      private static final long serialVersionUID = -3903455224197903186L;
49  
50      protected static final String CLASSPATH_RESOURCE_PREFIX = "classpath.resource.prefix";
51      public static List<String> HACK_ADDITIONAL_FILES = new ArrayList<String>();
52  
53      private Boolean springBasedConfigPath;
54  
55      /**
56       * This method calls the super version then loads the dwr config file
57       * specified in the loaded module definitions.
58       * 
59       * @see uk.ltd.getahead.dwr.DWRServlet#configure(javax.servlet.ServletConfig,
60       *      uk.ltd.getahead.dwr.Configuration)
61       */
62      protected List<NamedOrderedListBean> getDwrNamedOrderedListBeans(String listName) {
63          List <NamedOrderedListBean> dwrListBeans = new ArrayList<NamedOrderedListBean>();
64          Map<String, NamedOrderedListBean> namedOrderedListBeans = SpringContext.getBeansOfType(NamedOrderedListBean.class);
65           for (NamedOrderedListBean nameOrderedListBean : namedOrderedListBeans.values()) {
66              if (nameOrderedListBean.getName().equals(listName)) {
67                  dwrListBeans.add((NamedOrderedListBean) nameOrderedListBean);
68              }
69          }
70          return dwrListBeans;
71      }
72      
73      protected DwrXmlConfigurator generateConfigurator(DefaultResourceLoader resourceLoader, String scriptConfigurationFilePath ) throws ServletException {
74          try {
75              InputStream is = resourceLoader.getResource(scriptConfigurationFilePath).getInputStream();
76              DwrXmlConfigurator dwrXmlConfigurator = new DwrXmlConfigurator();
77              dwrXmlConfigurator.setInputStream(is);
78              return dwrXmlConfigurator;
79          } catch (Exception e) {
80              throw new ServletException(e);
81          }
82      }
83      
84      @Override
85      protected void configureContainer(Container container, ServletConfig servletConfig) throws ServletException, IOException {       
86          List<Configurator> configurators = new ArrayList<Configurator>();
87          DefaultResourceLoader resourceLoader = new DefaultResourceLoader(ClassLoaderUtils.getDefaultClassLoader());
88        ConfigurationService  configurationService=GlobalResourceLoader.getService("kualiConfigurationService");
89          String classpathResourcePrefix = configurationService.getPropertyValueAsString(CLASSPATH_RESOURCE_PREFIX);
90          for (NamedOrderedListBean namedOrderedListBean : this.getDwrNamedOrderedListBeans(OLEConstants.SCRIPT_CONFIGURATION_FILES_LIST_NAME)) {
91              for (String scriptConfigurationFilePath : namedOrderedListBean.getList()) {
92                  if (getSpringBasedConfigPath()) {
93                        try {
94                          configurators.add( this.generateConfigurator(resourceLoader, scriptConfigurationFilePath));                       
95                         } catch (Exception e) {
96                          throw new ServletException(e);
97                      }
98                  } 
99              }
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 }