001/*
002 * Copyright 2002-2017 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 *      https://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
017package org.springframework.cache.config;
018
019import org.w3c.dom.Element;
020
021import org.springframework.beans.factory.config.BeanDefinition;
022import org.springframework.beans.factory.config.RuntimeBeanReference;
023import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
024import org.springframework.util.StringUtils;
025
026/**
027 * {@code NamespaceHandler} allowing for the configuration of declarative
028 * cache management using either XML or using annotations.
029 *
030 * <p>This namespace handler is the central piece of functionality in the
031 * Spring cache management facilities.
032 *
033 * @author Costin Leau
034 * @since 3.1
035 */
036public class CacheNamespaceHandler extends NamespaceHandlerSupport {
037
038        static final String CACHE_MANAGER_ATTRIBUTE = "cache-manager";
039
040        static final String DEFAULT_CACHE_MANAGER_BEAN_NAME = "cacheManager";
041
042
043        static String extractCacheManager(Element element) {
044                return (element.hasAttribute(CacheNamespaceHandler.CACHE_MANAGER_ATTRIBUTE) ?
045                                element.getAttribute(CacheNamespaceHandler.CACHE_MANAGER_ATTRIBUTE) :
046                                CacheNamespaceHandler.DEFAULT_CACHE_MANAGER_BEAN_NAME);
047        }
048
049        static BeanDefinition parseKeyGenerator(Element element, BeanDefinition def) {
050                String name = element.getAttribute("key-generator");
051                if (StringUtils.hasText(name)) {
052                        def.getPropertyValues().add("keyGenerator", new RuntimeBeanReference(name.trim()));
053                }
054                return def;
055        }
056
057
058        @Override
059        public void init() {
060                registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenCacheBeanDefinitionParser());
061                registerBeanDefinitionParser("advice", new CacheAdviceParser());
062        }
063
064}