01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package org.springframework.web.servlet.handler.metadata;
18:
19: import java.util.Collection;
20:
21: import org.apache.commons.attributes.AttributeIndex;
22: import org.apache.commons.attributes.Attributes;
23:
24: /**
25: * Subclass of AbstractPathMapHandlerMapping that recognizes Commons Attributes
26: * metadata attributes of type PathMap on application Controllers and automatically
27: * wires them into the current servlet's WebApplicationContext.
28: *
29: * <p>
30: * Controllers must have class attributes of the form:
31: * <code>
32: * &64;org.springframework.web.servlet.handler.commonsattributes.PathMap("/path.cgi")
33: * </code>
34: *
35: * <p>The path must be mapped to the relevant Spring DispatcherServlet in /WEB-INF/web.xml.
36: * It's possible to have multiple PathMap attributes on the one controller class.
37: *
38: * <p>To use this feature, you must compile application classes with Commons Attributes,
39: * and run the Commons Attributes indexer tool on your application classes, which must
40: * be in a Jar rather than in WEB-INF/classes.
41: *
42: * <p>Controllers instantiated by this class may have dependencies on middle tier
43: * objects, expressed via JavaBean properties or constructor arguments. These will
44: * be resolved automatically.
45: *
46: * <p>You will normally use this HandlerMapping with at most one DispatcherServlet in
47: * your web application. Otherwise you'll end with one instance of the mapped controller
48: * for each DispatcherServlet's context. You <i>might</i> want this--for example, if
49: * one's using a .pdf mapping and a PDF view, and another a JSP view, or if using
50: * different middle tier objects, but should understand the implications. All
51: * Controllers with attributes will be picked up by each DispatcherServlet's context.
52: *
53: * @author Rod Johnson
54: * @author Juergen Hoeller
55: */
56: public class CommonsPathMapHandlerMapping extends
57: AbstractPathMapHandlerMapping {
58:
59: /**
60: * Use Commons Attributes AttributeIndex to get a Collection of Class
61: * objects with the required PathMap attribute. Protected so that it can
62: * be overridden during testing.
63: */
64: protected Class[] getClassesWithPathMapAttributes()
65: throws Exception {
66: AttributeIndex ai = new AttributeIndex(getClass()
67: .getClassLoader());
68: Collection classes = ai.getClasses(PathMap.class);
69: return (Class[]) classes.toArray(new Class[classes.size()]);
70: }
71:
72: /**
73: * Use Commons Attributes to find PathMap attributes for the given class.
74: * We know there's at least one, as the getClassNamesWithPathMapAttributes
75: * method return this class name.
76: */
77: protected PathMap[] getPathMapAttributes(Class handlerClass) {
78: Collection atts = Attributes.getAttributes(handlerClass,
79: PathMap.class);
80: return (PathMap[]) atts.toArray(new PathMap[atts.size()]);
81: }
82:
83: }
|