CommonsPathMapHandlerMapping.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 3k
Category:

Java Develop

Development Platform:

Java

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