KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > handler > metadata > CommonsPathMapHandlerMapping


1 /*
2  * Copyright 2002-2005 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
17 package org.springframework.web.servlet.handler.metadata;
18
19 import java.util.Collection JavaDoc;
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 AbstractPathMapHandlerMapping {
57     
58     /**
59      * Use Commons Attributes AttributeIndex to get a Collection of Class
60      * objects with the required PathMap attribute. Protected so that it can
61      * be overridden during testing.
62      */

63     protected Class JavaDoc[] getClassesWithPathMapAttributes() throws Exception JavaDoc {
64         AttributeIndex ai = new AttributeIndex(getClass().getClassLoader());
65         Collection JavaDoc classes = ai.getClasses(PathMap.class);
66         return (Class JavaDoc[]) classes.toArray(new Class JavaDoc[classes.size()]);
67     }
68     
69     /**
70      * Use Commons Attributes to find PathMap attributes for the given class.
71      * We know there's at least one, as the getClassNamesWithPathMapAttributes
72      * method return this class name.
73      */

74     protected PathMap[] getPathMapAttributes(Class JavaDoc handlerClass) {
75         Collection JavaDoc atts = Attributes.getAttributes(handlerClass, PathMap.class);
76         return (PathMap[]) atts.toArray(new PathMap[atts.size()]);
77     }
78
79 }
80
Popular Tags