001/*
002 * Copyright 2002-2012 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.beans.factory.support;
018
019import java.util.Map;
020import java.util.concurrent.ConcurrentHashMap;
021
022import org.springframework.beans.factory.BeanDefinitionStoreException;
023import org.springframework.beans.factory.NoSuchBeanDefinitionException;
024import org.springframework.beans.factory.config.BeanDefinition;
025import org.springframework.core.SimpleAliasRegistry;
026import org.springframework.util.Assert;
027import org.springframework.util.StringUtils;
028
029/**
030 * Simple implementation of the {@link BeanDefinitionRegistry} interface.
031 * Provides registry capabilities only, with no factory capabilities built in.
032 * Can for example be used for testing bean definition readers.
033 *
034 * @author Juergen Hoeller
035 * @since 2.5.2
036 */
037public class SimpleBeanDefinitionRegistry extends SimpleAliasRegistry implements BeanDefinitionRegistry {
038
039        /** Map of bean definition objects, keyed by bean name */
040        private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(64);
041
042
043        @Override
044        public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
045                        throws BeanDefinitionStoreException {
046
047                Assert.hasText(beanName, "'beanName' must not be empty");
048                Assert.notNull(beanDefinition, "BeanDefinition must not be null");
049                this.beanDefinitionMap.put(beanName, beanDefinition);
050        }
051
052        @Override
053        public void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
054                if (this.beanDefinitionMap.remove(beanName) == null) {
055                        throw new NoSuchBeanDefinitionException(beanName);
056                }
057        }
058
059        @Override
060        public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
061                BeanDefinition bd = this.beanDefinitionMap.get(beanName);
062                if (bd == null) {
063                        throw new NoSuchBeanDefinitionException(beanName);
064                }
065                return bd;
066        }
067
068        @Override
069        public boolean containsBeanDefinition(String beanName) {
070                return this.beanDefinitionMap.containsKey(beanName);
071        }
072
073        @Override
074        public String[] getBeanDefinitionNames() {
075                return StringUtils.toStringArray(this.beanDefinitionMap.keySet());
076        }
077
078        @Override
079        public int getBeanDefinitionCount() {
080                return this.beanDefinitionMap.size();
081        }
082
083        @Override
084        public boolean isBeanNameInUse(String beanName) {
085                return isAlias(beanName) || containsBeanDefinition(beanName);
086        }
087
088}