Coverage Report - org.kuali.student.common.ws.ServletWrappingController
 
Classes in this File Line Coverage Branch Coverage Complexity
ServletWrappingController
0%
0/38
0%
0/6
1.583
ServletWrappingController$1
N/A
N/A
1.583
ServletWrappingController$DelegatingServletConfig
0%
0/5
N/A
1.583
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.common.ws;
 17  
 
 18  
 import java.util.Enumeration;
 19  
 import java.util.Properties;
 20  
 
 21  
 import javax.servlet.Servlet;
 22  
 import javax.servlet.ServletConfig;
 23  
 import javax.servlet.ServletContext;
 24  
 import javax.servlet.http.HttpServletRequest;
 25  
 import javax.servlet.http.HttpServletResponse;
 26  
 
 27  
 import org.apache.log4j.Logger;
 28  
 import org.springframework.beans.factory.BeanNameAware;
 29  
 import org.springframework.beans.factory.DisposableBean;
 30  
 import org.springframework.beans.factory.InitializingBean;
 31  
 import org.springframework.web.servlet.ModelAndView;
 32  
 import org.springframework.web.servlet.mvc.AbstractController;
 33  
 
 34  
 /**
 35  
  * Spring Controller implementation that mimics standard
 36  
  * ServletWrappingController behaviour (see its documentation), but with the
 37  
  * important difference that it doesn't instantiate the Servlet instance
 38  
  * directly but delegate for this the BeanContext, so that we can also use IoC.*
 39  
  */
 40  0
 public class ServletWrappingController extends AbstractController implements
 41  
                 BeanNameAware, InitializingBean, DisposableBean {
 42  
         private Class<? extends Servlet> servletClass;
 43  
         private String servletName;
 44  0
         private Properties initParameters = new Properties();
 45  
         private String beanName;
 46  
         private Servlet servletInstance;
 47  
 
 48  0
         private static org.apache.log4j.Logger log = Logger.getLogger(ServletWrappingController.class);
 49  
 
 50  
         
 51  
         public void setServletClass(Class<? extends Servlet> servletClass) {
 52  0
                 log.info("setServletClass : " + servletClass);
 53  0
                 this.servletClass = servletClass;
 54  0
         }
 55  
 
 56  
         public void setServletName(String servletName) {
 57  0
                 log.info("setServletName : " + servletName);
 58  0
                 this.servletName = servletName;
 59  0
         }
 60  
 
 61  
         public void setInitParameters(Properties initParameters) {
 62  0
                 log.info("setInitParameters : " + initParameters);
 63  0
                 this.initParameters = initParameters;
 64  0
         }
 65  
 
 66  
         public void setBeanName(String name) {
 67  0
                 log.info("setBeanName : " + name);
 68  0
                 this.beanName = name;
 69  0
         }
 70  
 
 71  
         public void setServletInstance(Servlet servletInstance) {
 72  0
                 log.info("setServletInstance : " + servletInstance);
 73  0
                 this.servletInstance = servletInstance;
 74  0
         }
 75  
 
 76  
         public void afterPropertiesSet() throws Exception {
 77  0
                 log.info("afterPropertiesSet");
 78  0
                 if (this.servletInstance == null) {
 79  0
                         throw new IllegalArgumentException("servletInstance is required");
 80  
                 }
 81  0
                 if (!Servlet.class.isAssignableFrom(servletInstance.getClass())) {
 82  0
                         throw new IllegalArgumentException("servletInstance ["
 83  
                                         + this.servletClass.getName()
 84  
                                         + "] needs to implement interface [javax.servlet.Servlet]");
 85  
                 }
 86  0
                 if (this.servletName == null) {
 87  0
                         this.servletName = this.beanName;
 88  
                 }
 89  0
                 this.servletInstance.init(new DelegatingServletConfig());
 90  0
         }
 91  
 
 92  
         protected ModelAndView handleRequestInternal(HttpServletRequest request,
 93  
                         HttpServletResponse response) throws Exception {
 94  0
                 log.info("handleRequestInternal : " + servletName);
 95  
                 try{
 96  0
                         this.servletInstance.service(request, response);
 97  0
                 }catch(Exception e){
 98  0
                         log.error(e.getMessage());
 99  0
                         throw e;
 100  0
                 }
 101  
                 
 102  0
                 return null;
 103  
         }
 104  
 
 105  
         public void destroy() {
 106  0
                 log.info("destroy : " + servletName);
 107  0
                 this.servletInstance.destroy();
 108  0
         }
 109  
 
 110  0
         private class DelegatingServletConfig implements ServletConfig {
 111  
                 public String getServletName() {
 112  0
                         return servletName;
 113  
                 }
 114  
 
 115  
                 public ServletContext getServletContext() {
 116  0
                         return getWebApplicationContext().getServletContext();
 117  
                 }
 118  
 
 119  
                 public String getInitParameter(String paramName) {
 120  0
                         return initParameters.getProperty(paramName);
 121  
 
 122  
                 }
 123  
 
 124  
                 public Enumeration<?> getInitParameterNames() {
 125  0
                         return initParameters.keys();
 126  
                 }
 127  
         }
 128  
 }