View Javadoc

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  public class ServletWrappingController extends AbstractController implements
41  		BeanNameAware, InitializingBean, DisposableBean {
42  	private Class<? extends Servlet> servletClass;
43  	private String servletName;
44  	private Properties initParameters = new Properties();
45  	private String beanName;
46  	private Servlet servletInstance;
47  
48  	private static org.apache.log4j.Logger log = Logger.getLogger(ServletWrappingController.class);
49  
50  	
51  	public void setServletClass(Class<? extends Servlet> servletClass) {
52  		log.info("setServletClass : " + servletClass);
53  		this.servletClass = servletClass;
54  	}
55  
56  	public void setServletName(String servletName) {
57  		log.info("setServletName : " + servletName);
58  		this.servletName = servletName;
59  	}
60  
61  	public void setInitParameters(Properties initParameters) {
62  		log.info("setInitParameters : " + initParameters);
63  		this.initParameters = initParameters;
64  	}
65  
66  	public void setBeanName(String name) {
67  		log.info("setBeanName : " + name);
68  		this.beanName = name;
69  	}
70  
71  	public void setServletInstance(Servlet servletInstance) {
72  		log.info("setServletInstance : " + servletInstance);
73  		this.servletInstance = servletInstance;
74  	}
75  
76  	public void afterPropertiesSet() throws Exception {
77  		log.info("afterPropertiesSet");
78  		if (this.servletInstance == null) {
79  			throw new IllegalArgumentException("servletInstance is required");
80  		}
81  		if (!Servlet.class.isAssignableFrom(servletInstance.getClass())) {
82  			throw new IllegalArgumentException("servletInstance ["
83  					+ this.servletClass.getName()
84  					+ "] needs to implement interface [javax.servlet.Servlet]");
85  		}
86  		if (this.servletName == null) {
87  			this.servletName = this.beanName;
88  		}
89  		this.servletInstance.init(new DelegatingServletConfig());
90  	}
91  
92  	protected ModelAndView handleRequestInternal(HttpServletRequest request,
93  			HttpServletResponse response) throws Exception {
94  		log.info("handleRequestInternal : " + servletName);
95  		try{
96  			this.servletInstance.service(request, response);
97  		}catch(Exception e){
98  			log.error(e.getMessage());
99  			throw e;
100 		}
101 		
102 		return null;
103 	}
104 
105 	public void destroy() {
106 		log.info("destroy : " + servletName);
107 		this.servletInstance.destroy();
108 	}
109 
110 	private class DelegatingServletConfig implements ServletConfig {
111 		public String getServletName() {
112 			return servletName;
113 		}
114 
115 		public ServletContext getServletContext() {
116 			return getWebApplicationContext().getServletContext();
117 		}
118 
119 		public String getInitParameter(String paramName) {
120 			return initParameters.getProperty(paramName);
121 
122 		}
123 
124 		public Enumeration<?> getInitParameterNames() {
125 			return initParameters.keys();
126 		}
127 	}
128 }