1 16 17 package org.springframework.web.servlet; 18 19 import java.util.Enumeration ; 20 import java.util.HashSet ; 21 import java.util.Set ; 22 23 import javax.servlet.ServletConfig ; 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServlet ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import org.springframework.beans.BeanWrapper; 32 import org.springframework.beans.BeanWrapperImpl; 33 import org.springframework.beans.BeansException; 34 import org.springframework.beans.MutablePropertyValues; 35 import org.springframework.beans.PropertyValue; 36 import org.springframework.beans.PropertyValues; 37 import org.springframework.core.io.Resource; 38 import org.springframework.core.io.ResourceEditor; 39 import org.springframework.core.io.ResourceLoader; 40 import org.springframework.util.StringUtils; 41 import org.springframework.web.context.support.ServletContextResourceLoader; 42 43 64 public abstract class HttpServletBean extends HttpServlet { 65 66 67 protected final Log logger = LogFactory.getLog(getClass()); 68 69 73 private final Set requiredProperties = new HashSet (); 74 75 76 85 protected final void addRequiredProperty(String property) { 86 this.requiredProperties.add(property); 87 } 88 89 95 public final void init() throws ServletException { 96 if (logger.isDebugEnabled()) { 97 logger.debug("Initializing servlet '" + getServletName() + "'"); 98 } 99 100 try { 102 PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties); 103 BeanWrapper bw = new BeanWrapperImpl(this); 104 ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext()); 105 bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader)); 106 initBeanWrapper(bw); 107 bw.setPropertyValues(pvs, true); 108 } 109 catch (BeansException ex) { 110 logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex); 111 throw ex; 112 } 113 114 initServletBean(); 116 117 if (logger.isDebugEnabled()) { 118 logger.debug("Servlet '" + getServletName() + "' configured successfully"); 119 } 120 } 121 122 130 protected void initBeanWrapper(BeanWrapper bw) throws BeansException { 131 } 132 133 134 139 public final String getServletName() { 140 return (getServletConfig() != null ? getServletConfig().getServletName() : null); 141 } 142 143 148 public final ServletContext getServletContext() { 149 return (getServletConfig() != null ? getServletConfig().getServletContext() : null); 150 } 151 152 153 160 protected void initServletBean() throws ServletException { 161 } 162 163 164 167 private static class ServletConfigPropertyValues extends MutablePropertyValues { 168 169 176 public ServletConfigPropertyValues(ServletConfig config, Set requiredProperties) 177 throws ServletException { 178 179 Set missingProps = (requiredProperties != null && !requiredProperties.isEmpty()) ? 180 new HashSet (requiredProperties) : null; 181 182 Enumeration en = config.getInitParameterNames(); 183 while (en.hasMoreElements()) { 184 String property = (String ) en.nextElement(); 185 Object value = config.getInitParameter(property); 186 addPropertyValue(new PropertyValue(property, value)); 187 if (missingProps != null) { 188 missingProps.remove(property); 189 } 190 } 191 192 if (missingProps != null && missingProps.size() > 0) { 194 throw new ServletException ( 195 "Initialization from ServletConfig for servlet '" + config.getServletName() + 196 "' failed; the following required properties were missing: " + 197 StringUtils.collectionToDelimitedString(missingProps, ", ")); 198 } 199 } 200 } 201 202 } 203 | Popular Tags |