KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > mvc > multiaction > PropertiesMethodNameResolver


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.mvc.multiaction;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import org.springframework.beans.factory.InitializingBean;
23 import org.springframework.util.AntPathMatcher;
24 import org.springframework.util.Assert;
25 import org.springframework.util.PathMatcher;
26
27 /**
28  * The most flexible out-of-the-box implementation of the MethodNameResolver
29  * interface. Uses <code>java.util.Properties</code> to define the mapping
30  * between the URL of incoming requests and the corresponding method name.
31  * Such properties can be held in an XML document.
32  *
33  * <p>Properties format is
34  * <code>
35  * /welcome.html=displayGenresPage
36  * </code>
37  * Note that method overloading isn't allowed, so there's no need to
38  * specify arguments.
39  *
40  * <p>Supports direct matches, e.g. a registered "/test" matches "/test",
41  * and a various Ant-style pattern matches, e.g. a registered "/t*" matches
42  * both "/test" and "/team". For details, see the AntPathMatcher javadoc.
43  *
44  * @author Rod Johnson
45  * @author Juergen Hoeller
46  * @see java.util.Properties
47  * @see org.springframework.util.AntPathMatcher
48  */

49 public class PropertiesMethodNameResolver extends AbstractUrlMethodNameResolver
50         implements InitializingBean {
51     
52     private Properties JavaDoc mappings;
53
54     private PathMatcher pathMatcher = new AntPathMatcher();
55
56
57     /**
58      * Set explicit URL to method name mappings through a Properties object.
59      * @param mappings Properties with URL as key and method name as value
60      */

61     public void setMappings(Properties JavaDoc mappings) {
62         this.mappings = mappings;
63     }
64
65     /**
66      * Set the PathMatcher implementation to use for matching URL paths
67      * against registered URL patterns. Default is AntPathMatcher.
68      * @see org.springframework.util.AntPathMatcher
69      */

70     public void setPathMatcher(PathMatcher pathMatcher) {
71         Assert.notNull(pathMatcher, "PathMatcher must not be null");
72         this.pathMatcher = pathMatcher;
73     }
74
75     public void afterPropertiesSet() {
76         if (this.mappings == null || this.mappings.isEmpty()) {
77             throw new IllegalArgumentException JavaDoc("'mappings' property is required");
78         }
79     }
80
81
82     protected String JavaDoc getHandlerMethodNameForUrlPath(String JavaDoc urlPath) {
83         String JavaDoc methodName = this.mappings.getProperty(urlPath);
84         if (methodName != null) {
85             return methodName;
86         }
87         for (Iterator JavaDoc it = this.mappings.keySet().iterator(); it.hasNext();) {
88             String JavaDoc registeredPath = (String JavaDoc) it.next();
89             if (this.pathMatcher.match(registeredPath, urlPath)) {
90                 return (String JavaDoc) this.mappings.get(registeredPath);
91             }
92         }
93         return null;
94     }
95
96 }
97
Popular Tags