KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > handler > BeanNameUrlHandlerMapping


1 /*
2  * Copyright 2002-2007 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;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.springframework.beans.BeansException;
23 import org.springframework.beans.factory.BeanFactoryUtils;
24 import org.springframework.context.ApplicationContextException;
25 import org.springframework.util.StringUtils;
26
27 /**
28  * Implementation of the {@link org.springframework.web.servlet.HandlerMapping}
29  * interface that map from URLs to beans with names that start with a slash ("/"),
30  * similar to how Struts maps URLs to action names.
31  *
32  * <p>This is the default implementation used by the
33  * {@link org.springframework.web.servlet.DispatcherServlet}, but it is somewhat naive.
34  * A {@link SimpleUrlHandlerMapping} or a custom handler mapping should be used
35  * by preference.
36  *
37  * <p>The mapping is from URL to bean name. Thus an incoming URL "/foo" would map
38  * to a handler named "/foo", or to "/foo /foo2" in case of multiple mappings to
39  * a single handler. Note: In XML definitions, you'll need to use an alias
40  * name="/foo" in the bean definition, as the XML id may not contain slashes.
41  *
42  * <p>Supports direct matches (given "/test" -> registered "/test") and "*"
43  * matches (given "/test" -> registered "/t*"). Note that the default is
44  * to map within the current servlet mapping if applicable; see the
45  * {@link #setAlwaysUseFullPath "alwaysUseFullPath"} property for details.
46  * For details on the pattern options, see the
47  * {@link org.springframework.util.AntPathMatcher} javadoc.
48  *
49  * @author Rod Johnson
50  * @author Juergen Hoeller
51  * @see SimpleUrlHandlerMapping
52  */

53 public class BeanNameUrlHandlerMapping extends AbstractUrlHandlerMapping {
54
55     private boolean detectHandlersInAncestorContexts = false;
56
57
58     /**
59      * Set whether to detect handler beans in ancestor ApplicationContexts.
60      * <p>Default is "false": Only handler beans in the current
61      * ApplicationContext will be detected, that is, only in the context
62      * that this BeanNameUrlHandlerMapping itself is defined in (typically
63      * the current DispatcherServlet's context).
64      * <p>Switch this flag on to detect handler beans in ancestor contexts
65      * (typically the Spring root WebApplicationContext) as well.
66      */

67     public void setDetectHandlersInAncestorContexts(boolean detectHandlersInAncestorContexts) {
68         this.detectHandlersInAncestorContexts = detectHandlersInAncestorContexts;
69     }
70
71
72     /**
73      * Calls the {@link #detectHandlers()} method in addition to the
74      * superclass's initialization.
75      */

76     public void initApplicationContext() throws ApplicationContextException {
77         super.initApplicationContext();
78         detectHandlers();
79     }
80
81     /**
82      * Register all handlers found in the current ApplicationContext.
83      * Any bean whose name appears to be a URL is considered a handler.
84      * @throws BeansException if the handler couldn't be registered
85      * @see #determineUrlsForHandler
86      */

87     protected void detectHandlers() throws BeansException {
88         if (logger.isDebugEnabled()) {
89             logger.debug("Looking for URL mappings in application context: " + getApplicationContext());
90         }
91         String JavaDoc[] beanNames = (this.detectHandlersInAncestorContexts ?
92                 BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object JavaDoc.class) :
93                 getApplicationContext().getBeanNamesForType(Object JavaDoc.class));
94
95         // Take any bean name or alias that begins with a slash.
96
for (int i = 0; i < beanNames.length; i++) {
97             String JavaDoc beanName = beanNames[i];
98             String JavaDoc[] urls = determineUrlsForHandler(beanName);
99             if (urls.length > 0) {
100                 // URL paths found: Let's consider it a handler.
101
registerHandler(urls, beanName);
102             }
103             else {
104                 if (logger.isDebugEnabled()) {
105                     logger.debug("Rejected bean name '" + beanNames[i] + "': no URL paths identified");
106                 }
107             }
108         }
109     }
110
111     /**
112      * Check name and aliases of the given bean for URLs,
113      * detected by starting with "/".
114      * @param beanName the name of the candidate bean
115      * @return the URLs determined for the bean, or an empty array if none
116      */

117     protected String JavaDoc[] determineUrlsForHandler(String JavaDoc beanName) {
118         List JavaDoc urls = new ArrayList JavaDoc();
119         if (beanName.startsWith("/")) {
120             urls.add(beanName);
121         }
122         String JavaDoc[] aliases = getApplicationContext().getAliases(beanName);
123         for (int j = 0; j < aliases.length; j++) {
124             if (aliases[j].startsWith("/")) {
125                 urls.add(aliases[j]);
126             }
127         }
128         return StringUtils.toStringArray(urls);
129     }
130
131 }
132
Popular Tags