1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.servlet.handler.metadata;
  17. import java.util.Collection;
  18. import java.util.Iterator;
  19. import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
  20. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  21. import org.springframework.context.ApplicationContextException;
  22. import org.springframework.context.ConfigurableApplicationContext;
  23. import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
  24. /**
  25. * Abstract implementation of the HandlerMapping interface that recognizes
  26. * metadata attributes of type PathMap on application Controllers and automatically
  27. * wires them into the current servlet's WebApplicationContext.
  28. *
  29. * <p>The path must be mapped to the relevant Spring DispatcherServlet in /WEB-INF/web.xml.
  30. * It's possible to have multiple PathMap attributes on the one controller class.
  31. *
  32. * <p>Controllers instantiated by this class may have dependencies on middle tier
  33. * objects, expressed via JavaBean properties or constructor arguments. These will
  34. * be resolved automatically.
  35. *
  36. * <p>You will normally use this HandlerMapping with at most one DispatcherServlet in your
  37. * web application. Otherwise you'll end with one instance of the mapped controller for
  38. * each DispatcherServlet's context. You <i>might</i> want this -- for example, if
  39. * one's using a .pdf mapping and a PDF view, and another a JSP view, or if
  40. * using different middle tier objects, but should understand the implications. All
  41. * Controllers with attributes will be picked up by each DispatcherServlet's context.
  42. *
  43. * @author Rod Johnson
  44. * @version $Id: AbstractPathMapHandlerMapping.java,v 1.7 2004/05/26 10:48:57 jhoeller Exp $
  45. */
  46. public abstract class AbstractPathMapHandlerMapping extends AbstractUrlHandlerMapping {
  47. /**
  48. * Look for all classes with a PathMap class attribute, instantiate them in
  49. * the owning ApplicationContext and register them as MVC handlers usable
  50. * by the current DispatcherServlet.
  51. * @see org.springframework.context.support.ApplicationObjectSupport#initApplicationContext()
  52. */
  53. public void initApplicationContext() throws ApplicationContextException {
  54. try {
  55. logger.info("Looking for attribute-defined URL mappings in application context: " + getApplicationContext());
  56. Collection names = getClassNamesWithPathMapAttributes();
  57. logger.info("Found " + names.size() + " attribute-targeted handlers");
  58. // For each classname returned by the Commons Attribute indexer
  59. for (Iterator itr = names.iterator(); itr.hasNext();) {
  60. String handlerClassName = (String) itr.next();
  61. Class handlerClass = Class.forName(handlerClassName);
  62. if (!(getApplicationContext() instanceof ConfigurableApplicationContext)) {
  63. throw new ApplicationContextException("AbstractPathMapHandlerMapping needs to run in a ConfigurableApplicationContext");
  64. }
  65. ConfigurableListableBeanFactory beanFactory =
  66. ((ConfigurableApplicationContext) getApplicationContext()).getBeanFactory();
  67. // Autowire the given handler class via AutowireCapableBeanFactory.
  68. // Either autowires a constructor or by type, depending on the
  69. // constructors available in the given class.
  70. Object handler = beanFactory.autowire(handlerClass, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, true);
  71. // We now have an "autowired" handler, that may reference beans in the
  72. // application context. We now add the new handler to the factory.
  73. // This isn't necessary for the handler to work, but is useful if we want
  74. // to enumerate controllers in the factory etc.
  75. beanFactory.registerSingleton(handlerClassName, handler);
  76. // There may be multiple paths mapped to this handler,
  77. PathMap[] pathMaps = getPathMapAttributes(handlerClass);
  78. for (int i = 0; i < pathMaps.length; i++) {
  79. PathMap pathMap = pathMaps[i];
  80. String path = pathMap.getUrl();
  81. if (!path.startsWith("/")) {
  82. path = "/" + path;
  83. }
  84. logger.info("Mapping path [" + path + "] to class with name '" + handlerClassName + "'");
  85. registerHandler(path, handler);
  86. }
  87. }
  88. }
  89. catch (ClassNotFoundException ex) {
  90. // Shouldn't happen: Attributes API gave us the class name.
  91. throw new ApplicationContextException("Failed to load a class returned in an attribute index: " +
  92. "internal error in Commons Attributes indexing?", ex);
  93. }
  94. }
  95. /**
  96. * Use an attribute index to get a Collection of FQNs of
  97. * classes with the required PathMap attribute.
  98. */
  99. protected abstract Collection getClassNamesWithPathMapAttributes();
  100. /**
  101. * Use Attributes API to find PathMap attributes for the given class.
  102. * We know there's at least one, as the getClassNamesWithPathMapAttributes
  103. * method return this class name.
  104. */
  105. protected abstract PathMap[] getPathMapAttributes(Class handlerClass);
  106. }