001/*
002 * Copyright 2002-2018 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.scripting.config;
018
019import org.springframework.beans.factory.config.BeanDefinition;
020import org.springframework.beans.factory.support.BeanDefinitionRegistry;
021import org.springframework.beans.factory.support.RootBeanDefinition;
022import org.springframework.scripting.support.ScriptFactoryPostProcessor;
023
024/**
025 * Utilities for use with {@link LangNamespaceHandler}.
026 *
027 * @author Rob Harrop
028 * @author Mark Fisher
029 * @since 2.5
030 */
031public abstract class LangNamespaceUtils {
032
033        /**
034         * The unique name under which the internally managed {@link ScriptFactoryPostProcessor} is
035         * registered in the {@link BeanDefinitionRegistry}.
036         */
037        private static final String SCRIPT_FACTORY_POST_PROCESSOR_BEAN_NAME =
038                        "org.springframework.scripting.config.scriptFactoryPostProcessor";
039
040
041        /**
042         * Register a {@link ScriptFactoryPostProcessor} bean definition in the supplied
043         * {@link BeanDefinitionRegistry} if the {@link ScriptFactoryPostProcessor} hasn't
044         * already been registered.
045         * @param registry the {@link BeanDefinitionRegistry} to register the script processor with
046         * @return the {@link ScriptFactoryPostProcessor} bean definition (new or already registered)
047         */
048        public static BeanDefinition registerScriptFactoryPostProcessorIfNecessary(BeanDefinitionRegistry registry) {
049                BeanDefinition beanDefinition;
050                if (registry.containsBeanDefinition(SCRIPT_FACTORY_POST_PROCESSOR_BEAN_NAME)) {
051                        beanDefinition = registry.getBeanDefinition(SCRIPT_FACTORY_POST_PROCESSOR_BEAN_NAME);
052                }
053                else {
054                        beanDefinition = new RootBeanDefinition(ScriptFactoryPostProcessor.class);
055                        registry.registerBeanDefinition(SCRIPT_FACTORY_POST_PROCESSOR_BEAN_NAME, beanDefinition);
056                }
057                return beanDefinition;
058        }
059
060}