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.scripting.support;
018
019import org.springframework.aop.target.dynamic.BeanFactoryRefreshableTargetSource;
020import org.springframework.beans.factory.BeanFactory;
021import org.springframework.scripting.ScriptFactory;
022import org.springframework.scripting.ScriptSource;
023import org.springframework.util.Assert;
024
025/**
026 * Subclass of {@link BeanFactoryRefreshableTargetSource} that determines whether
027 * a refresh is required through the given {@link ScriptFactory}.
028 *
029 * @author Rob Harrop
030 * @author Juergen Hoeller
031 * @author Mark Fisher
032 * @since 2.0
033 */
034public class RefreshableScriptTargetSource extends BeanFactoryRefreshableTargetSource {
035
036        private final ScriptFactory scriptFactory;
037
038        private final ScriptSource scriptSource;
039
040        private final boolean isFactoryBean;
041
042
043        /**
044         * Create a new RefreshableScriptTargetSource.
045         * @param beanFactory the BeanFactory to fetch the scripted bean from
046         * @param beanName the name of the target bean
047         * @param scriptFactory the ScriptFactory to delegate to for determining
048         * whether a refresh is required
049         * @param scriptSource the ScriptSource for the script definition
050         * @param isFactoryBean whether the target script defines a FactoryBean
051         */
052        public RefreshableScriptTargetSource(BeanFactory beanFactory, String beanName,
053                        ScriptFactory scriptFactory, ScriptSource scriptSource, boolean isFactoryBean) {
054
055                super(beanFactory, beanName);
056                Assert.notNull(scriptFactory, "ScriptFactory must not be null");
057                Assert.notNull(scriptSource, "ScriptSource must not be null");
058                this.scriptFactory = scriptFactory;
059                this.scriptSource = scriptSource;
060                this.isFactoryBean = isFactoryBean;
061        }
062
063
064        /**
065         * Determine whether a refresh is required through calling
066         * ScriptFactory's {@code requiresScriptedObjectRefresh} method.
067         * @see ScriptFactory#requiresScriptedObjectRefresh(ScriptSource)
068         */
069        @Override
070        protected boolean requiresRefresh() {
071                return this.scriptFactory.requiresScriptedObjectRefresh(this.scriptSource);
072        }
073
074        /**
075         * Obtain a fresh target object, retrieving a FactoryBean if necessary.
076         */
077        @Override
078        protected Object obtainFreshBean(BeanFactory beanFactory, String beanName) {
079                return super.obtainFreshBean(beanFactory,
080                                (this.isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName));
081        }
082
083}