Java Code Examples for org.springframework.beans.factory.xml.ParserContext#registerComponent()

The following examples show how to use org.springframework.beans.factory.xml.ParserContext#registerComponent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationDrivenBeanDefinitionParser.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Nullable
private RuntimeBeanReference getValidator(Element element, @Nullable Object source, ParserContext context) {
	if (element.hasAttribute("validator")) {
		return new RuntimeBeanReference(element.getAttribute("validator"));
	}
	else if (javaxValidationPresent) {
		RootBeanDefinition validatorDef = new RootBeanDefinition(
				"org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean");
		validatorDef.setSource(source);
		validatorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		String validatorName = context.getReaderContext().registerWithGeneratedName(validatorDef);
		context.registerComponent(new BeanComponentDefinition(validatorDef, validatorName));
		return new RuntimeBeanReference(validatorName);
	}
	else {
		return null;
	}
}
 
Example 2
Source File: AnnotationDrivenBeanDefinitionParser.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private RuntimeBeanReference getValidator(Element element, Object source, ParserContext parserContext) {
	if (element.hasAttribute("validator")) {
		return new RuntimeBeanReference(element.getAttribute("validator"));
	}
	else if (javaxValidationPresent) {
		RootBeanDefinition validatorDef = new RootBeanDefinition(
				"org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean");
		validatorDef.setSource(source);
		validatorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		String validatorName = parserContext.getReaderContext().registerWithGeneratedName(validatorDef);
		parserContext.registerComponent(new BeanComponentDefinition(validatorDef, validatorName));
		return new RuntimeBeanReference(validatorName);
	}
	else {
		return null;
	}
}
 
Example 3
Source File: ViewControllerBeanDefinitionParser.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private BeanDefinition registerHandlerMapping(ParserContext context, @Nullable Object source) {
	if (context.getRegistry().containsBeanDefinition(HANDLER_MAPPING_BEAN_NAME)) {
		return context.getRegistry().getBeanDefinition(HANDLER_MAPPING_BEAN_NAME);
	}
	RootBeanDefinition beanDef = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
	beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	context.getRegistry().registerBeanDefinition(HANDLER_MAPPING_BEAN_NAME, beanDef);
	context.registerComponent(new BeanComponentDefinition(beanDef, HANDLER_MAPPING_BEAN_NAME));

	beanDef.setSource(source);
	beanDef.getPropertyValues().add("order", "1");
	beanDef.getPropertyValues().add("pathMatcher", MvcNamespaceUtils.registerPathMatcher(null, context, source));
	beanDef.getPropertyValues().add("urlPathHelper", MvcNamespaceUtils.registerUrlPathHelper(null, context, source));
	RuntimeBeanReference corsConfigurationsRef = MvcNamespaceUtils.registerCorsConfigurations(null, context, source);
	beanDef.getPropertyValues().add("corsConfigurations", corsConfigurationsRef);

	return beanDef;
}
 
Example 4
Source File: MvcNamespaceUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Adds an alias to an existing well-known name or registers a new instance of a {@link PathMatcher}
 * under that well-known name, unless already registered.
 * @return a RuntimeBeanReference to this {@link PathMatcher} instance
 */
public static RuntimeBeanReference registerPathMatcher(@Nullable RuntimeBeanReference pathMatcherRef,
		ParserContext parserContext, @Nullable Object source) {

	if (pathMatcherRef != null) {
		if (parserContext.getRegistry().isAlias(PATH_MATCHER_BEAN_NAME)) {
			parserContext.getRegistry().removeAlias(PATH_MATCHER_BEAN_NAME);
		}
		parserContext.getRegistry().registerAlias(pathMatcherRef.getBeanName(), PATH_MATCHER_BEAN_NAME);
	}
	else if (!parserContext.getRegistry().isAlias(PATH_MATCHER_BEAN_NAME) &&
			!parserContext.getRegistry().containsBeanDefinition(PATH_MATCHER_BEAN_NAME)) {
		RootBeanDefinition pathMatcherDef = new RootBeanDefinition(AntPathMatcher.class);
		pathMatcherDef.setSource(source);
		pathMatcherDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		parserContext.getRegistry().registerBeanDefinition(PATH_MATCHER_BEAN_NAME, pathMatcherDef);
		parserContext.registerComponent(new BeanComponentDefinition(pathMatcherDef, PATH_MATCHER_BEAN_NAME));
	}
	return new RuntimeBeanReference(PATH_MATCHER_BEAN_NAME);
}
 
Example 5
Source File: MvcNamespaceUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Registers  an {@link HttpRequestHandlerAdapter} under a well-known
 * name unless already registered.
 */
private static void registerBeanNameUrlHandlerMapping(ParserContext parserContext, Object source) {
	if (!parserContext.getRegistry().containsBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME)){
		RootBeanDefinition beanNameMappingDef = new RootBeanDefinition(BeanNameUrlHandlerMapping.class);
		beanNameMappingDef.setSource(source);
		beanNameMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		beanNameMappingDef.getPropertyValues().add("order", 2);	// consistent with WebMvcConfigurationSupport
		RuntimeBeanReference corsConfigurationsRef = MvcNamespaceUtils.registerCorsConfigurations(null, parserContext, source);
		beanNameMappingDef.getPropertyValues().add("corsConfigurations", corsConfigurationsRef);
		parserContext.getRegistry().registerBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME, beanNameMappingDef);
		parserContext.registerComponent(new BeanComponentDefinition(beanNameMappingDef, BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME));
	}
}
 
Example 6
Source File: MvcNamespaceUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Registers  an {@link HandlerMappingIntrospector} under a well-known name
 * unless already registered.
 */
private static void registerHandlerMappingIntrospector(ParserContext parserContext, Object source) {
	if (!parserContext.getRegistry().containsBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)){
		RootBeanDefinition beanDef = new RootBeanDefinition(HandlerMappingIntrospector.class);
		beanDef.setSource(source);
		beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		beanDef.setLazyInit(true);
		parserContext.getRegistry().registerBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME, beanDef);
		parserContext.registerComponent(new BeanComponentDefinition(beanDef, HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME));
	}
}
 
Example 7
Source File: WebSocketNamespaceUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
public static RuntimeBeanReference registerScheduler(
		String schedulerName, ParserContext context, @Nullable Object source) {

	if (!context.getRegistry().containsBeanDefinition(schedulerName)) {
		RootBeanDefinition taskSchedulerDef = new RootBeanDefinition(ThreadPoolTaskScheduler.class);
		taskSchedulerDef.setSource(source);
		taskSchedulerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		taskSchedulerDef.getPropertyValues().add("poolSize", Runtime.getRuntime().availableProcessors());
		taskSchedulerDef.getPropertyValues().add("threadNamePrefix", schedulerName + "-");
		taskSchedulerDef.getPropertyValues().add("removeOnCancelPolicy", true);
		context.getRegistry().registerBeanDefinition(schedulerName, taskSchedulerDef);
		context.registerComponent(new BeanComponentDefinition(taskSchedulerDef, schedulerName));
	}
	return new RuntimeBeanReference(schedulerName);
}
 
Example 8
Source File: MvcNamespaceUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Registers  an {@link HttpRequestHandlerAdapter} under a well-known
 * name unless already registered.
 */
private static void registerHttpRequestHandlerAdapter(ParserContext parserContext, Object source) {
	if (!parserContext.getRegistry().containsBeanDefinition(HTTP_REQUEST_HANDLER_ADAPTER_BEAN_NAME)) {
		RootBeanDefinition handlerAdapterDef = new RootBeanDefinition(HttpRequestHandlerAdapter.class);
		handlerAdapterDef.setSource(source);
		handlerAdapterDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		parserContext.getRegistry().registerBeanDefinition(HTTP_REQUEST_HANDLER_ADAPTER_BEAN_NAME, handlerAdapterDef);
		parserContext.registerComponent(new BeanComponentDefinition(handlerAdapterDef, HTTP_REQUEST_HANDLER_ADAPTER_BEAN_NAME));
	}
}
 
Example 9
Source File: DefaultServletHandlerBeanDefinitionParser.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
	Object source = parserContext.extractSource(element);

	String defaultServletName = element.getAttribute("default-servlet-name");
	RootBeanDefinition defaultServletHandlerDef = new RootBeanDefinition(DefaultServletHttpRequestHandler.class);
	defaultServletHandlerDef.setSource(source);
	defaultServletHandlerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	if (StringUtils.hasText(defaultServletName)) {
		defaultServletHandlerDef.getPropertyValues().add("defaultServletName", defaultServletName);
	}
	String defaultServletHandlerName = parserContext.getReaderContext().generateBeanName(defaultServletHandlerDef);
	parserContext.getRegistry().registerBeanDefinition(defaultServletHandlerName, defaultServletHandlerDef);
	parserContext.registerComponent(new BeanComponentDefinition(defaultServletHandlerDef, defaultServletHandlerName));

	Map<String, String> urlMap = new ManagedMap<>();
	urlMap.put("/**", defaultServletHandlerName);

	RootBeanDefinition handlerMappingDef = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
	handlerMappingDef.setSource(source);
	handlerMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	handlerMappingDef.getPropertyValues().add("urlMap", urlMap);

	String handlerMappingBeanName = parserContext.getReaderContext().generateBeanName(handlerMappingDef);
	parserContext.getRegistry().registerBeanDefinition(handlerMappingBeanName, handlerMappingDef);
	parserContext.registerComponent(new BeanComponentDefinition(handlerMappingDef, handlerMappingBeanName));

	// Ensure BeanNameUrlHandlerMapping (SPR-8289) and default HandlerAdapters are not "turned off"
	MvcNamespaceUtils.registerDefaultComponents(parserContext, source);

	return null;
}
 
Example 10
Source File: MvcNamespaceUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Registers a {@link SimpleControllerHandlerAdapter} under a well-known
 * name unless already registered.
 */
private static void registerSimpleControllerHandlerAdapter(ParserContext context, @Nullable Object source) {
	if (!context.getRegistry().containsBeanDefinition(SIMPLE_CONTROLLER_HANDLER_ADAPTER_BEAN_NAME)) {
		RootBeanDefinition beanDef = new RootBeanDefinition(SimpleControllerHandlerAdapter.class);
		beanDef.setSource(source);
		beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		context.getRegistry().registerBeanDefinition(SIMPLE_CONTROLLER_HANDLER_ADAPTER_BEAN_NAME, beanDef);
		context.registerComponent(new BeanComponentDefinition(beanDef, SIMPLE_CONTROLLER_HANDLER_ADAPTER_BEAN_NAME));
	}
}
 
Example 11
Source File: MvcNamespaceUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Registers  an {@link HttpRequestHandlerAdapter} under a well-known
 * name unless already registered.
 */
private static void registerHttpRequestHandlerAdapter(ParserContext context, @Nullable Object source) {
	if (!context.getRegistry().containsBeanDefinition(HTTP_REQUEST_HANDLER_ADAPTER_BEAN_NAME)) {
		RootBeanDefinition adapterDef = new RootBeanDefinition(HttpRequestHandlerAdapter.class);
		adapterDef.setSource(source);
		adapterDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		context.getRegistry().registerBeanDefinition(HTTP_REQUEST_HANDLER_ADAPTER_BEAN_NAME, adapterDef);
		context.registerComponent(new BeanComponentDefinition(adapterDef, HTTP_REQUEST_HANDLER_ADAPTER_BEAN_NAME));
	}
}
 
Example 12
Source File: MvcNamespaceUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Registers  an {@link HttpRequestHandlerAdapter} under a well-known
 * name unless already registered.
 */
private static void registerBeanNameUrlHandlerMapping(ParserContext parserContext, Object source) {
	if (!parserContext.getRegistry().containsBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME)){
		RootBeanDefinition beanNameMappingDef = new RootBeanDefinition(BeanNameUrlHandlerMapping.class);
		beanNameMappingDef.setSource(source);
		beanNameMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		beanNameMappingDef.getPropertyValues().add("order", 2);	// consistent with WebMvcConfigurationSupport
		RuntimeBeanReference corsConfigurationsRef = MvcNamespaceUtils.registerCorsConfigurations(null, parserContext, source);
		beanNameMappingDef.getPropertyValues().add("corsConfigurations", corsConfigurationsRef);
		parserContext.getRegistry().registerBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME, beanNameMappingDef);
		parserContext.registerComponent(new BeanComponentDefinition(beanNameMappingDef, BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME));
	}
}
 
Example 13
Source File: AnnotationDrivenBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private static void registerPostProcessor(
		ParserContext parserContext, BeanDefinitionBuilder builder, String beanName) {

	builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	parserContext.getRegistry().registerBeanDefinition(beanName, builder.getBeanDefinition());
	BeanDefinitionHolder holder = new BeanDefinitionHolder(builder.getBeanDefinition(), beanName);
	parserContext.registerComponent(new BeanComponentDefinition(holder));
}
 
Example 14
Source File: ConfigBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the supplied {@code <pointcut>} and registers the resulting
 * Pointcut with the BeanDefinitionRegistry.
 */
private AbstractBeanDefinition parsePointcut(Element pointcutElement, ParserContext parserContext) {
	String id = pointcutElement.getAttribute(ID);
	String expression = pointcutElement.getAttribute(EXPRESSION);

	AbstractBeanDefinition pointcutDefinition = null;

	try {
		this.parseState.push(new PointcutEntry(id));
		pointcutDefinition = createPointcutDefinition(expression);
		pointcutDefinition.setSource(parserContext.extractSource(pointcutElement));

		String pointcutBeanName = id;
		if (StringUtils.hasText(pointcutBeanName)) {
			parserContext.getRegistry().registerBeanDefinition(pointcutBeanName, pointcutDefinition);
		}
		else {
			pointcutBeanName = parserContext.getReaderContext().registerWithGeneratedName(pointcutDefinition);
		}

		parserContext.registerComponent(
				new PointcutComponentDefinition(pointcutBeanName, pointcutDefinition, expression));
	}
	finally {
		this.parseState.pop();
	}

	return pointcutDefinition;
}
 
Example 15
Source File: MessageBrokerBeanDefinitionParser.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static void registerBeanDefByName(
		String name, RootBeanDefinition beanDef, ParserContext context, @Nullable Object source) {

	beanDef.setSource(source);
	beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	context.getRegistry().registerBeanDefinition(name, beanDef);
	context.registerComponent(new BeanComponentDefinition(beanDef, name));
}
 
Example 16
Source File: AnnotationDrivenCacheBeanDefinitionParser.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static void registerCacheAdvisor(Element element, ParserContext parserContext) {
	if (!parserContext.getRegistry().containsBeanDefinition(CacheManagementConfigUtils.CACHE_ADVISOR_BEAN_NAME)) {
		Object eleSource = parserContext.extractSource(element);

		// Create the CacheOperationSource definition.
		RootBeanDefinition sourceDef = new RootBeanDefinition("org.springframework.cache.annotation.AnnotationCacheOperationSource");
		sourceDef.setSource(eleSource);
		sourceDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		String sourceName = parserContext.getReaderContext().registerWithGeneratedName(sourceDef);

		// Create the CacheInterceptor definition.
		RootBeanDefinition interceptorDef = new RootBeanDefinition(CacheInterceptor.class);
		interceptorDef.setSource(eleSource);
		interceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		parseCacheResolution(element, interceptorDef, false);
		parseErrorHandler(element, interceptorDef);
		CacheNamespaceHandler.parseKeyGenerator(element, interceptorDef);
		interceptorDef.getPropertyValues().add("cacheOperationSources", new RuntimeBeanReference(sourceName));
		String interceptorName = parserContext.getReaderContext().registerWithGeneratedName(interceptorDef);

		// Create the CacheAdvisor definition.
		RootBeanDefinition advisorDef = new RootBeanDefinition(BeanFactoryCacheOperationSourceAdvisor.class);
		advisorDef.setSource(eleSource);
		advisorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		advisorDef.getPropertyValues().add("cacheOperationSource", new RuntimeBeanReference(sourceName));
		advisorDef.getPropertyValues().add("adviceBeanName", interceptorName);
		if (element.hasAttribute("order")) {
			advisorDef.getPropertyValues().add("order", element.getAttribute("order"));
		}
		parserContext.getRegistry().registerBeanDefinition(CacheManagementConfigUtils.CACHE_ADVISOR_BEAN_NAME, advisorDef);

		CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), eleSource);
		compositeDef.addNestedComponent(new BeanComponentDefinition(sourceDef, sourceName));
		compositeDef.addNestedComponent(new BeanComponentDefinition(interceptorDef, interceptorName));
		compositeDef.addNestedComponent(new BeanComponentDefinition(advisorDef, CacheManagementConfigUtils.CACHE_ADVISOR_BEAN_NAME));
		parserContext.registerComponent(compositeDef);
	}
}
 
Example 17
Source File: DefaultServletHandlerBeanDefinitionParser.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
	Object source = parserContext.extractSource(element);

	String defaultServletName = element.getAttribute("default-servlet-name");
	RootBeanDefinition defaultServletHandlerDef = new RootBeanDefinition(DefaultServletHttpRequestHandler.class);
	defaultServletHandlerDef.setSource(source);
	defaultServletHandlerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	if (StringUtils.hasText(defaultServletName)) {
		defaultServletHandlerDef.getPropertyValues().add("defaultServletName", defaultServletName);
	}
	String defaultServletHandlerName = parserContext.getReaderContext().generateBeanName(defaultServletHandlerDef);
	parserContext.getRegistry().registerBeanDefinition(defaultServletHandlerName, defaultServletHandlerDef);
	parserContext.registerComponent(new BeanComponentDefinition(defaultServletHandlerDef, defaultServletHandlerName));

	Map<String, String> urlMap = new ManagedMap<>();
	urlMap.put("/**", defaultServletHandlerName);

	RootBeanDefinition handlerMappingDef = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
	handlerMappingDef.setSource(source);
	handlerMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	handlerMappingDef.getPropertyValues().add("urlMap", urlMap);

	String handlerMappingBeanName = parserContext.getReaderContext().generateBeanName(handlerMappingDef);
	parserContext.getRegistry().registerBeanDefinition(handlerMappingBeanName, handlerMappingDef);
	parserContext.registerComponent(new BeanComponentDefinition(handlerMappingDef, handlerMappingBeanName));

	// Ensure BeanNameUrlHandlerMapping (SPR-8289) and default HandlerAdapters are not "turned off"
	MvcNamespaceUtils.registerDefaultComponents(parserContext, source);

	return null;
}
 
Example 18
Source File: ConfigBeanDefinitionParser.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Parses the supplied {@code <pointcut>} and registers the resulting
 * Pointcut with the BeanDefinitionRegistry.
 */
private AbstractBeanDefinition parsePointcut(Element pointcutElement, ParserContext parserContext) {
	String id = pointcutElement.getAttribute(ID);
	String expression = pointcutElement.getAttribute(EXPRESSION);

	AbstractBeanDefinition pointcutDefinition = null;

	try {
		this.parseState.push(new PointcutEntry(id));
		pointcutDefinition = createPointcutDefinition(expression);
		pointcutDefinition.setSource(parserContext.extractSource(pointcutElement));

		String pointcutBeanName = id;
		if (StringUtils.hasText(pointcutBeanName)) {
			parserContext.getRegistry().registerBeanDefinition(pointcutBeanName, pointcutDefinition);
		}
		else {
			pointcutBeanName = parserContext.getReaderContext().registerWithGeneratedName(pointcutDefinition);
		}

		parserContext.registerComponent(
				new PointcutComponentDefinition(pointcutBeanName, pointcutDefinition, expression));
	}
	finally {
		this.parseState.pop();
	}

	return pointcutDefinition;
}
 
Example 19
Source File: ResourcesBeanDefinitionParser.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Nullable
private String registerResourceHandler(ParserContext context, Element element,
		RuntimeBeanReference pathHelperRef, @Nullable Object source) {

	String locationAttr = element.getAttribute("location");
	if (!StringUtils.hasText(locationAttr)) {
		context.getReaderContext().error("The 'location' attribute is required.", context.extractSource(element));
		return null;
	}

	RootBeanDefinition resourceHandlerDef = new RootBeanDefinition(ResourceHttpRequestHandler.class);
	resourceHandlerDef.setSource(source);
	resourceHandlerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);

	MutablePropertyValues values = resourceHandlerDef.getPropertyValues();
	values.add("urlPathHelper", pathHelperRef);
	values.add("locationValues", StringUtils.commaDelimitedListToStringArray(locationAttr));

	String cacheSeconds = element.getAttribute("cache-period");
	if (StringUtils.hasText(cacheSeconds)) {
		values.add("cacheSeconds", cacheSeconds);
	}

	Element cacheControlElement = DomUtils.getChildElementByTagName(element, "cache-control");
	if (cacheControlElement != null) {
		CacheControl cacheControl = parseCacheControl(cacheControlElement);
		values.add("cacheControl", cacheControl);
	}

	Element resourceChainElement = DomUtils.getChildElementByTagName(element, "resource-chain");
	if (resourceChainElement != null) {
		parseResourceChain(resourceHandlerDef, context, resourceChainElement, source);
	}

	Object manager = MvcNamespaceUtils.getContentNegotiationManager(context);
	if (manager != null) {
		values.add("contentNegotiationManager", manager);
	}

	String beanName = context.getReaderContext().generateBeanName(resourceHandlerDef);
	context.getRegistry().registerBeanDefinition(beanName, resourceHandlerDef);
	context.registerComponent(new BeanComponentDefinition(resourceHandlerDef, beanName));
	return beanName;
}
 
Example 20
Source File: HandlersBeanDefinitionParser.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext context) {
	Object source = context.extractSource(element);
	CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
	context.pushContainingComponent(compDefinition);

	String orderAttribute = element.getAttribute("order");
	int order = orderAttribute.isEmpty() ? DEFAULT_MAPPING_ORDER : Integer.valueOf(orderAttribute);

	RootBeanDefinition handlerMappingDef = new RootBeanDefinition(WebSocketHandlerMapping.class);
	handlerMappingDef.setSource(source);
	handlerMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	handlerMappingDef.getPropertyValues().add("order", order);
	String handlerMappingName = context.getReaderContext().registerWithGeneratedName(handlerMappingDef);

	RuntimeBeanReference sockJsService = WebSocketNamespaceUtils.registerSockJsService(
			element, SOCK_JS_SCHEDULER_NAME, context, source);

	HandlerMappingStrategy strategy;
	if (sockJsService != null) {
		strategy = new SockJsHandlerMappingStrategy(sockJsService);
	}
	else {
		RuntimeBeanReference handler = WebSocketNamespaceUtils.registerHandshakeHandler(element, context, source);
		Element interceptElem = DomUtils.getChildElementByTagName(element, "handshake-interceptors");
		ManagedList<Object> interceptors = WebSocketNamespaceUtils.parseBeanSubElements(interceptElem, context);
		String allowedOrigins = element.getAttribute("allowed-origins");
		List<String> origins = Arrays.asList(StringUtils.tokenizeToStringArray(allowedOrigins, ","));
		interceptors.add(new OriginHandshakeInterceptor(origins));
		strategy = new WebSocketHandlerMappingStrategy(handler, interceptors);
	}

	ManagedMap<String, Object> urlMap = new ManagedMap<>();
	urlMap.setSource(source);
	for (Element mappingElement : DomUtils.getChildElementsByTagName(element, "mapping")) {
		strategy.addMapping(mappingElement, urlMap, context);
	}
	handlerMappingDef.getPropertyValues().add("urlMap", urlMap);

	context.registerComponent(new BeanComponentDefinition(handlerMappingDef, handlerMappingName));
	context.popAndRegisterContainingComponent();
	return null;
}