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 org.apache.commons.attributes.AttributeIndex;
  19. import org.apache.commons.attributes.Attributes;
  20. import org.springframework.context.ApplicationContextException;
  21. /**
  22. * Subclass of AbstractPathMapHandlerMapping that recognizes Commons Attributes
  23. * metadata attributes of type PathMap on application Controllers and automatically
  24. * wires them into the current servlet's WebApplicationContext.
  25. * <p>
  26. * Controllers must have class attributes of the form:
  27. * <code>
  28. * &64;org.springframework.web.servlet.handler.commonsattributes.PathMap("/path.cgi")
  29. * </code>
  30. * <br>The path must be mapped to the relevant Spring DispatcherServlet in /WEB-INF/web.xml.
  31. * It's possible to have multiple PathMap attributes on the one controller class.
  32. * <p>To use this feature, you must compile application classes with Commons Attributes,
  33. * and run the Commons Attributes indexer tool on your application classes, which must
  34. * be in a Jar rather than in WEB-INF/classes.
  35. * <p>Controllers instantiated by this class may have dependencies on middle tier
  36. * objects, expressed via JavaBean properties or constructor arguments. These will
  37. * be resolved automatically.
  38. * <p>You will normally use this HandlerMapping with at most one DispatcherServlet in your web
  39. * application. Otherwise you'll end with one instance of the mapped controller for
  40. * each DispatcherServlet's context. You <i>might</i> want this--for example, if
  41. * one's using a .pdf mapping and a PDF view, and another a JSP view, or if
  42. * using different middle tier objects, but should understand the implications. All
  43. * Controllers with attributes will be picked up by each DispatcherServlet's context.
  44. * @author Rod Johnson
  45. * @version $Id: CommonsPathMapHandlerMapping.java,v 1.2 2004/03/18 02:46:17 trisberg Exp $
  46. */
  47. public class CommonsPathMapHandlerMapping extends AbstractPathMapHandlerMapping {
  48. /**
  49. * Use Commons Attributes AttributeIndex to get a Collection of FQNs of
  50. * classes with the required PathMap attribute. Protected so that it can
  51. * be overridden during testing.
  52. */
  53. protected Collection getClassNamesWithPathMapAttributes() {
  54. try {
  55. AttributeIndex ai = new AttributeIndex(getClass().getClassLoader());
  56. return ai.getClassesWithAttribute(PathMap.class);
  57. }
  58. catch (Exception ex) {
  59. throw new ApplicationContextException("Failed to load Commons Attributes attribute index", ex);
  60. }
  61. }
  62. /**
  63. * Use Commons Attributes to find PathMap attributes for the given class.
  64. * We know there's at least one, as the getClassNamesWithPathMapAttributes
  65. * method return this class name.
  66. */
  67. protected PathMap[] getPathMapAttributes(Class handlerClass) {
  68. Collection atts = Attributes.getAttributes(handlerClass, PathMap.class);
  69. return (PathMap[]) atts.toArray(new PathMap[atts.size()]);
  70. }
  71. }