1 16 17 package org.springframework.web.servlet.mvc.support; 18 19 import java.util.Arrays ; 20 import java.util.Collections ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 25 import org.springframework.beans.BeansException; 26 import org.springframework.util.ClassUtils; 27 import org.springframework.web.servlet.HandlerMapping; 28 import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping; 29 import org.springframework.web.servlet.mvc.Controller; 30 import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 31 import org.springframework.web.servlet.mvc.throwaway.ThrowawayController; 32 33 68 public class ControllerClassNameHandlerMapping extends AbstractUrlHandlerMapping implements HandlerMapping { 69 70 74 private static final String CONTROLLER_SUFFIX = "Controller"; 75 76 77 private Set excludedPackages = Collections.singleton("org.springframework.web.servlet.mvc"); 78 79 private Set excludedClasses = Collections.EMPTY_SET; 80 81 82 93 public void setExcludedPackages(String [] excludedPackages) { 94 this.excludedPackages = 95 (excludedPackages != null ? new HashSet (Arrays.asList(excludedPackages)) : Collections.EMPTY_SET); 96 } 97 98 102 public void setExcludedClasses(Class [] excludedClasses) { 103 this.excludedClasses = 104 (excludedClasses != null ? new HashSet (Arrays.asList(excludedClasses)) : Collections.EMPTY_SET); 105 } 106 107 108 112 protected void initApplicationContext() { 113 super.initApplicationContext(); 114 detectControllers(); 115 } 116 117 125 protected void detectControllers() throws BeansException { 126 registerControllers(Controller.class); 127 registerControllers(ThrowawayController.class); 128 } 129 130 136 protected void registerControllers(Class controllerType) throws BeansException { 137 String [] beanNames = getApplicationContext().getBeanNamesForType(controllerType); 138 for (int i = 0; i < beanNames.length; i++) { 139 String beanName = beanNames[i]; 140 Class beanClass = getApplicationContext().getType(beanName); 141 if (isEligibleForMapping(beanName, beanClass)) { 142 registerController(beanName, beanClass); 143 } 144 } 145 } 146 147 155 protected boolean isEligibleForMapping(String beanName, Class beanClass) { 156 if (beanClass == null) { 157 if (logger.isDebugEnabled()) { 158 logger.debug("Excluding controller bean '" + beanName + "' from class name mapping " + 159 "because its bean type could not be determined"); 160 } 161 return false; 162 } 163 if (this.excludedClasses.contains(beanClass)) { 164 if (logger.isDebugEnabled()) { 165 logger.debug("Excluding controller bean '" + beanName + "' from class name mapping " + 166 "because its bean class is explicitly excluded: " + beanClass.getName()); 167 } 168 return false; 169 } 170 String beanClassName = beanClass.getName(); 171 for (Iterator it = this.excludedPackages.iterator(); it.hasNext();) { 172 String packageName = (String ) it.next(); 173 if (beanClassName.startsWith(packageName)) { 174 if (logger.isDebugEnabled()) { 175 logger.debug("Excluding controller bean '" + beanName + "' from class name mapping " + 176 "because its bean class is defined in an excluded package: " + beanClass.getName()); 177 } 178 return false; 179 } 180 } 181 return true; 182 } 183 184 193 protected void registerController(String beanName, Class beanClass) throws BeansException, IllegalStateException { 194 String urlPath = generatePathMapping(beanClass); 195 if (logger.isDebugEnabled()) { 196 logger.debug("Registering Controller '" + beanName + "' as handler for URL path [" + urlPath + "]"); 197 } 198 registerHandler(urlPath, beanName); 199 } 200 201 208 protected String generatePathMapping(Class beanClass) { 209 StringBuffer pathMapping = new StringBuffer ("/"); 210 String className = ClassUtils.getShortName(beanClass.getName()); 211 String path = (className.endsWith(CONTROLLER_SUFFIX) ? 212 className.substring(0, className.indexOf(CONTROLLER_SUFFIX)) : className); 213 pathMapping.append(path.toLowerCase()); 214 if (MultiActionController.class.isAssignableFrom(beanClass)) { 215 pathMapping.append("/*"); 216 } 217 else { 218 pathMapping.append("*"); 219 } 220 return pathMapping.toString(); 221 } 222 223 } 224 | Popular Tags |