001 /**
002 * Copyright 2010 The Kuali Foundation Licensed under the
003 * Educational Community License, Version 2.0 (the "License"); you may
004 * not use this file except in compliance with the License. You may
005 * obtain a copy of the License at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS"
011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012 * or implied. See the License for the specific language governing
013 * permissions and limitations under the License.
014 */
015
016 package org.kuali.student.common.ws;
017
018 import java.util.Enumeration;
019 import java.util.Properties;
020
021 import javax.servlet.Servlet;
022 import javax.servlet.ServletConfig;
023 import javax.servlet.ServletContext;
024 import javax.servlet.http.HttpServletRequest;
025 import javax.servlet.http.HttpServletResponse;
026
027 import org.apache.log4j.Logger;
028 import org.springframework.beans.factory.BeanNameAware;
029 import org.springframework.beans.factory.DisposableBean;
030 import org.springframework.beans.factory.InitializingBean;
031 import org.springframework.web.servlet.ModelAndView;
032 import org.springframework.web.servlet.mvc.AbstractController;
033
034 /**
035 * Spring Controller implementation that mimics standard
036 * ServletWrappingController behaviour (see its documentation), but with the
037 * important difference that it doesn't instantiate the Servlet instance
038 * directly but delegate for this the BeanContext, so that we can also use IoC.*
039 */
040 public class ServletWrappingController extends AbstractController implements
041 BeanNameAware, InitializingBean, DisposableBean {
042 private Class<? extends Servlet> servletClass;
043 private String servletName;
044 private Properties initParameters = new Properties();
045 private String beanName;
046 private Servlet servletInstance;
047
048 private static org.apache.log4j.Logger log = Logger.getLogger(ServletWrappingController.class);
049
050
051 public void setServletClass(Class<? extends Servlet> servletClass) {
052 log.info("setServletClass : " + servletClass);
053 this.servletClass = servletClass;
054 }
055
056 public void setServletName(String servletName) {
057 log.info("setServletName : " + servletName);
058 this.servletName = servletName;
059 }
060
061 public void setInitParameters(Properties initParameters) {
062 log.info("setInitParameters : " + initParameters);
063 this.initParameters = initParameters;
064 }
065
066 public void setBeanName(String name) {
067 log.info("setBeanName : " + name);
068 this.beanName = name;
069 }
070
071 public void setServletInstance(Servlet servletInstance) {
072 log.info("setServletInstance : " + servletInstance);
073 this.servletInstance = servletInstance;
074 }
075
076 public void afterPropertiesSet() throws Exception {
077 log.info("afterPropertiesSet");
078 if (this.servletInstance == null) {
079 throw new IllegalArgumentException("servletInstance is required");
080 }
081 if (!Servlet.class.isAssignableFrom(servletInstance.getClass())) {
082 throw new IllegalArgumentException("servletInstance ["
083 + this.servletClass.getName()
084 + "] needs to implement interface [javax.servlet.Servlet]");
085 }
086 if (this.servletName == null) {
087 this.servletName = this.beanName;
088 }
089 this.servletInstance.init(new DelegatingServletConfig());
090 }
091
092 protected ModelAndView handleRequestInternal(HttpServletRequest request,
093 HttpServletResponse response) throws Exception {
094 log.info("handleRequestInternal : " + servletName);
095 try{
096 this.servletInstance.service(request, response);
097 }catch(Exception e){
098 log.error(e.getMessage());
099 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 }