View Javadoc
1   /*
2    * Copyright 2002-2014 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.config;
18  
19  import org.springframework.beans.factory.config.BeanDefinition;
20  import org.springframework.beans.factory.config.RuntimeBeanReference;
21  import org.springframework.beans.factory.parsing.BeanComponentDefinition;
22  import org.springframework.beans.factory.support.RootBeanDefinition;
23  import org.springframework.beans.factory.xml.ParserContext;
24  import org.springframework.util.AntPathMatcher;
25  import org.springframework.util.PathMatcher;
26  import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
27  import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
28  import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
29  import org.springframework.web.util.UrlPathHelper;
30  
31  /**
32   * Convenience methods for use in MVC namespace BeanDefinitionParsers.
33   *
34   * @author Rossen Stoyanchev
35   * @author Brian Clozel
36   * @since 3.1
37   */
38  abstract class MvcNamespaceUtils {
39  
40  	private static final String BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME =
41  			BeanNameUrlHandlerMapping.class.getName();
42  
43  	private static final String SIMPLE_CONTROLLER_HANDLER_ADAPTER_BEAN_NAME =
44  			SimpleControllerHandlerAdapter.class.getName();
45  
46  	private static final String HTTP_REQUEST_HANDLER_ADAPTER_BEAN_NAME =
47  			HttpRequestHandlerAdapter.class.getName();
48  
49  	private static final String URL_PATH_HELPER_BEAN_NAME = "mvcUrlPathHelper";
50  
51  	private static final String PATH_MATCHER_BEAN_NAME = "mvcPathMatcher";
52  
53  
54  	public static void registerDefaultComponents(ParserContext parserContext, Object source) {
55  		registerBeanNameUrlHandlerMapping(parserContext, source);
56  		registerHttpRequestHandlerAdapter(parserContext, source);
57  		registerSimpleControllerHandlerAdapter(parserContext, source);
58  	}
59  
60  	/**
61  	 * Adds an alias to an existing well-known name or registers a new instance of a {@link UrlPathHelper}
62  	 * under that well-known name, unless already registered.
63  	 * @return a RuntimeBeanReference to this {@link UrlPathHelper} instance
64  	 */
65  	public static RuntimeBeanReference registerUrlPathHelper(RuntimeBeanReference urlPathHelperRef, ParserContext parserContext, Object source) {
66  		if (urlPathHelperRef != null) {
67  			if (parserContext.getRegistry().isAlias(URL_PATH_HELPER_BEAN_NAME)) {
68  				parserContext.getRegistry().removeAlias(URL_PATH_HELPER_BEAN_NAME);
69  			}
70  			parserContext.getRegistry().registerAlias(urlPathHelperRef.getBeanName(), URL_PATH_HELPER_BEAN_NAME);
71  		}
72  		else if (!parserContext.getRegistry().isAlias(URL_PATH_HELPER_BEAN_NAME)
73  				&& !parserContext.getRegistry().containsBeanDefinition(URL_PATH_HELPER_BEAN_NAME)) {
74  			RootBeanDefinition urlPathHelperDef = new RootBeanDefinition(UrlPathHelper.class);
75  			urlPathHelperDef.setSource(source);
76  			urlPathHelperDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
77  			parserContext.getRegistry().registerBeanDefinition(URL_PATH_HELPER_BEAN_NAME, urlPathHelperDef);
78  			parserContext.registerComponent(new BeanComponentDefinition(urlPathHelperDef, URL_PATH_HELPER_BEAN_NAME));
79  		}
80  		return new RuntimeBeanReference(URL_PATH_HELPER_BEAN_NAME);
81  	}
82  
83  	/**
84  	 * Adds an alias to an existing well-known name or registers a new instance of a {@link PathMatcher}
85  	 * under that well-known name, unless already registered.
86  	 * @return a RuntimeBeanReference to this {@link PathMatcher} instance
87  	 */
88  	public static RuntimeBeanReference registerPathMatcher(RuntimeBeanReference pathMatcherRef, ParserContext parserContext, Object source) {
89  		if (pathMatcherRef != null) {
90  			if (parserContext.getRegistry().isAlias(PATH_MATCHER_BEAN_NAME)) {
91  				parserContext.getRegistry().removeAlias(PATH_MATCHER_BEAN_NAME);
92  			}
93  			parserContext.getRegistry().registerAlias(pathMatcherRef.getBeanName(), PATH_MATCHER_BEAN_NAME);
94  		}
95  		else if (!parserContext.getRegistry().isAlias(PATH_MATCHER_BEAN_NAME)
96  				&& !parserContext.getRegistry().containsBeanDefinition(PATH_MATCHER_BEAN_NAME)) {
97  			RootBeanDefinition pathMatcherDef = new RootBeanDefinition(AntPathMatcher.class);
98  			pathMatcherDef.setSource(source);
99  			pathMatcherDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
100 			parserContext.getRegistry().registerBeanDefinition(PATH_MATCHER_BEAN_NAME, pathMatcherDef);
101 			parserContext.registerComponent(new BeanComponentDefinition(pathMatcherDef, PATH_MATCHER_BEAN_NAME));
102 		}
103 		return new RuntimeBeanReference(PATH_MATCHER_BEAN_NAME);
104 	}
105 
106 	/**
107 	 * Registers  an {@link HttpRequestHandlerAdapter} under a well-known
108 	 * name unless already registered.
109 	 */
110 	private static void registerBeanNameUrlHandlerMapping(ParserContext parserContext, Object source) {
111 		if (!parserContext.getRegistry().containsBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME)){
112 			RootBeanDefinition beanNameMappingDef = new RootBeanDefinition(BeanNameUrlHandlerMapping.class);
113 			beanNameMappingDef.setSource(source);
114 			beanNameMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
115 			beanNameMappingDef.getPropertyValues().add("order", 2);	// consistent with WebMvcConfigurationSupport
116 			parserContext.getRegistry().registerBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME, beanNameMappingDef);
117 			parserContext.registerComponent(new BeanComponentDefinition(beanNameMappingDef, BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME));
118 		}
119 	}
120 
121 	/**
122 	 * Registers  an {@link HttpRequestHandlerAdapter} under a well-known
123 	 * name unless already registered.
124 	 */
125 	private static void registerHttpRequestHandlerAdapter(ParserContext parserContext, Object source) {
126 		if (!parserContext.getRegistry().containsBeanDefinition(HTTP_REQUEST_HANDLER_ADAPTER_BEAN_NAME)) {
127 			RootBeanDefinition handlerAdapterDef = new RootBeanDefinition(HttpRequestHandlerAdapter.class);
128 			handlerAdapterDef.setSource(source);
129 			handlerAdapterDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
130 			parserContext.getRegistry().registerBeanDefinition(HTTP_REQUEST_HANDLER_ADAPTER_BEAN_NAME, handlerAdapterDef);
131 			parserContext.registerComponent(new BeanComponentDefinition(handlerAdapterDef, HTTP_REQUEST_HANDLER_ADAPTER_BEAN_NAME));
132 		}
133 	}
134 
135 	/**
136 	 * Registers a {@link SimpleControllerHandlerAdapter} under a well-known
137 	 * name unless already registered.
138 	 */
139 	private static void registerSimpleControllerHandlerAdapter(ParserContext parserContext, Object source) {
140 		if (!parserContext.getRegistry().containsBeanDefinition(SIMPLE_CONTROLLER_HANDLER_ADAPTER_BEAN_NAME)) {
141 			RootBeanDefinition handlerAdapterDef = new RootBeanDefinition(SimpleControllerHandlerAdapter.class);
142 			handlerAdapterDef.setSource(source);
143 			handlerAdapterDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
144 			parserContext.getRegistry().registerBeanDefinition(SIMPLE_CONTROLLER_HANDLER_ADAPTER_BEAN_NAME, handlerAdapterDef);
145 			parserContext.registerComponent(new BeanComponentDefinition(handlerAdapterDef, SIMPLE_CONTROLLER_HANDLER_ADAPTER_BEAN_NAME));
146 		}
147 	}
148 
149 }