/** * Registers all the provided names for given Spring {@link BeanFactory}. * * @param names {@link Set} of names and aliases to associate with the Spring {@link BeanFactory}. * @param beanFactory reference to the Spring {@link BeanFactory}. * @see org.springframework.beans.factory.BeanFactory * @throws IllegalArgumentException if {@link BeanFactory} is {@literal null}. * @throws IllegalStateException if one of the provided names is already associated with * an existing, other than given, Spring {@link BeanFactory}. * @see org.springframework.beans.factory.BeanFactory */ protected static synchronized void registerAliases(Set<String> names, BeanFactory beanFactory) { Set<String> safeNames = nullSafeSet(names); Assert.isTrue(safeNames.isEmpty() || beanFactory != null, "BeanFactory must not be null when aliases are specified"); for (String name : safeNames) { BeanFactory existingBeanFactory = BEAN_FACTORIES.putIfAbsent(name, beanFactory); Assert.isTrue(nullOrEquals(existingBeanFactory, beanFactory), String.format("BeanFactory reference already exists for key [%s]", name)); } }
/** * Registers all the provided names for given Spring {@link BeanFactory}. * * @param names {@link Set} of names and aliases to associate with the Spring {@link BeanFactory}. * @param beanFactory reference to the Spring {@link BeanFactory}. * @see org.springframework.beans.factory.BeanFactory * @throws IllegalArgumentException if {@link BeanFactory} is {@literal null}. * @throws IllegalStateException if one of the provided names is already associated with * an existing, other than given, Spring {@link BeanFactory}. * @see org.springframework.beans.factory.BeanFactory */ protected static synchronized void registerAliases(Set<String> names, BeanFactory beanFactory) { Set<String> safeNames = nullSafeSet(names); Assert.isTrue(safeNames.isEmpty() || beanFactory != null, "BeanFactory must not be null when aliases are specified"); for (String name : safeNames) { BeanFactory existingBeanFactory = BEAN_FACTORIES.putIfAbsent(name, beanFactory); Assert.isTrue(nullOrEquals(existingBeanFactory, beanFactory), String.format("BeanFactory reference already exists for key [%s]", name)); } }
/** * Resolves a single Spring {@link BeanFactory} from the mapping of registered bean factories. * * This class method is synchronized because it contains a "compound action", even though separate actions * are performed on a {@link ConcurrentMap}, the actions are not independent and therefore must operate * atomically. * * @return a single Spring {@link BeanFactory} from the registry. * @throws IllegalStateException if the registry contains more than 1, or no * Spring {@link BeanFactory bean factories}. * @see org.springframework.beans.factory.BeanFactory */ protected static synchronized BeanFactory resolveSingleBeanFactory() { if (!BEAN_FACTORIES.isEmpty()) { boolean allTheSameBeanFactory = true; BeanFactory currentBeanFactory = null; for (BeanFactory beanFactory : BEAN_FACTORIES.values()) { allTheSameBeanFactory &= nullOrEquals(currentBeanFactory, beanFactory); currentBeanFactory = beanFactory; if (!allTheSameBeanFactory) { break; } } Assert.state(allTheSameBeanFactory, String.format("BeanFactory key must be specified when more than one BeanFactory %s is registered", new TreeSet(BEAN_FACTORIES.keySet()).toString())); return BEAN_FACTORIES.values().iterator().next(); } return null; }
/** * Resolves a single Spring {@link BeanFactory} from the mapping of registered bean factories. * * This class method is synchronized because it contains a "compound action", even though separate actions * are performed on a {@link ConcurrentMap}, the actions are not independent and therefore must operate * atomically. * * @return a single Spring {@link BeanFactory} from the registry. * @throws IllegalStateException if the registry contains more than 1, or no * Spring {@link BeanFactory bean factories}. * @see org.springframework.beans.factory.BeanFactory */ protected static synchronized BeanFactory resolveSingleBeanFactory() { if (!BEAN_FACTORIES.isEmpty()) { boolean allTheSameBeanFactory = true; BeanFactory currentBeanFactory = null; for (BeanFactory beanFactory : BEAN_FACTORIES.values()) { allTheSameBeanFactory &= nullOrEquals(currentBeanFactory, beanFactory); currentBeanFactory = beanFactory; if (!allTheSameBeanFactory) { break; } } Assert.state(allTheSameBeanFactory, String.format("BeanFactory key must be specified when more than one BeanFactory %s is registered", new TreeSet(BEAN_FACTORIES.keySet()).toString())); return BEAN_FACTORIES.values().iterator().next(); } return null; }