1 16 17 package org.springframework.web.servlet.mvc; 18 19 import java.util.Enumeration ; 20 import java.util.Properties ; 21 22 import javax.servlet.Servlet ; 23 import javax.servlet.ServletConfig ; 24 import javax.servlet.ServletContext ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 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 33 103 public class ServletWrappingController extends AbstractController 104 implements BeanNameAware, InitializingBean, DisposableBean { 105 106 private Class servletClass; 107 108 private String servletName; 109 110 private Properties initParameters = new Properties (); 111 112 private String beanName; 113 114 private Servlet servletInstance; 115 116 117 122 public void setServletClass(Class servletClass) { 123 this.servletClass = servletClass; 124 } 125 126 130 public void setServletName(String servletName) { 131 this.servletName = servletName; 132 } 133 134 138 public void setInitParameters(Properties initParameters) { 139 this.initParameters = initParameters; 140 } 141 142 public void setBeanName(String name) { 143 this.beanName = name; 144 } 145 146 147 151 public void afterPropertiesSet() throws Exception { 152 if (this.servletClass == null) { 153 throw new IllegalArgumentException ("servletClass is required"); 154 } 155 if (!Servlet .class.isAssignableFrom(this.servletClass)) { 156 throw new IllegalArgumentException ("servletClass [" + this.servletClass.getName() + 157 "] needs to implement interface [javax.servlet.Servlet]"); 158 } 159 if (this.servletName == null) { 160 this.servletName = this.beanName; 161 } 162 this.servletInstance = (Servlet ) this.servletClass.newInstance(); 163 this.servletInstance.init(new DelegatingServletConfig()); 164 } 165 166 167 171 protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) 172 throws Exception { 173 174 this.servletInstance.service(request, response); 175 return null; 176 } 177 178 179 183 public void destroy() { 184 this.servletInstance.destroy(); 185 } 186 187 188 193 private class DelegatingServletConfig implements ServletConfig { 194 195 public String getServletName() { 196 return servletName; 197 } 198 199 public ServletContext getServletContext() { 200 return ServletWrappingController.this.getServletContext(); 201 } 202 203 public String getInitParameter(String paramName) { 204 return initParameters.getProperty(paramName); 205 } 206 207 public Enumeration getInitParameterNames() { 208 return initParameters.keys(); 209 } 210 } 211 212 } 213 | Popular Tags |