001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.servlet.handler;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Properties;
023:
024: import org.springframework.beans.BeansException;
025:
026: /**
027: * Implementation of the {@link org.springframework.web.servlet.HandlerMapping}
028: * interface to map from URLs to request handler beans. Supports both mapping to bean
029: * instances and mapping to bean names; the latter is required for non-singleton handlers.
030: *
031: * <p>The "urlMap" property is suitable for populating the handler map with
032: * bean references, e.g. via the map element in XML bean definitions.
033: *
034: * <p>Mappings to bean names can be set via the "mappings" property, in a form
035: * accepted by the <code>java.util.Properties</code> class, like as follows:<br>
036: * <code>
037: * /welcome.html=ticketController
038: * /show.html=ticketController
039: * </code><br>
040: * The syntax is <code>PATH=HANDLER_BEAN_NAME</code>.
041: * If the path doesn't begin with a slash, one is prepended.
042: *
043: * <p>Supports direct matches (given "/test" -> registered "/test") and "*"
044: * matches (given "/test" -> registered "/t*"). Note that the default is
045: * to map within the current servlet mapping if applicable; see the
046: * {@link #setAlwaysUseFullPath "alwaysUseFullPath"} property for details.
047: * For details on the pattern options, see the
048: * {@link org.springframework.util.AntPathMatcher} javadoc.
049:
050: * @author Rod Johnson
051: * @author Juergen Hoeller
052: * @see #setMappings
053: * @see #setUrlMap
054: * @see BeanNameUrlHandlerMapping
055: */
056: public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
057:
058: private final Map urlMap = new HashMap();
059:
060: /**
061: * Map URL paths to handler bean names.
062: * This is the typical way of configuring this HandlerMapping.
063: * <p>Supports direct URL matches and Ant-style pattern matches. For syntax
064: * details, see the {@link org.springframework.util.AntPathMatcher} javadoc.
065: * @param mappings properties with URLs as keys and bean names as values
066: * @see #setUrlMap
067: */
068: public void setMappings(Properties mappings) {
069: this .urlMap.putAll(mappings);
070: }
071:
072: /**
073: * Set a Map with URL paths as keys and handler beans (or handler bean names)
074: * as values. Convenient for population with bean references.
075: * <p>Supports direct URL matches and Ant-style pattern matches. For syntax
076: * details, see the {@link org.springframework.util.AntPathMatcher} javadoc.
077: * @param urlMap map with URLs as keys and beans as values
078: * @see #setMappings
079: */
080: public void setUrlMap(Map urlMap) {
081: this .urlMap.putAll(urlMap);
082: }
083:
084: /**
085: * Allow Map access to the URL path mappings, with the option to add or
086: * override specific entries.
087: * <p>Useful for specifying entries directly, for example via "urlMap[myKey]".
088: * This is particularly useful for adding or overriding entries in child
089: * bean definitions.
090: */
091: public Map getUrlMap() {
092: return this .urlMap;
093: }
094:
095: /**
096: * Calls the {@link #registerHandlers} method in addition to the
097: * superclass's initialization.
098: */
099: public void initApplicationContext() throws BeansException {
100: super .initApplicationContext();
101: registerHandlers(this .urlMap);
102: }
103:
104: /**
105: * Register all handlers specified in the URL map for the corresponding paths.
106: * @param urlMap Map with URL paths as keys and handler beans or bean names as values
107: * @throws BeansException if a handler couldn't be registered
108: * @throws IllegalStateException if there is a conflicting handler registered
109: */
110: protected void registerHandlers(Map urlMap) throws BeansException {
111: if (urlMap.isEmpty()) {
112: logger
113: .warn("Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping");
114: } else {
115: Iterator it = urlMap.keySet().iterator();
116: while (it.hasNext()) {
117: String url = (String) it.next();
118: Object handler = urlMap.get(url);
119: // Prepend with slash if not already present.
120: if (!url.startsWith("/")) {
121: url = "/" + url;
122: }
123: registerHandler(url, handler);
124: }
125: }
126: }
127:
128: }
|