View Javadoc
1   /*
2    * Copyright 2002-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.scripting.support;
18  
19  import org.springframework.aop.target.dynamic.BeanFactoryRefreshableTargetSource;
20  import org.springframework.beans.factory.BeanFactory;
21  import org.springframework.scripting.ScriptFactory;
22  import org.springframework.scripting.ScriptSource;
23  import org.springframework.util.Assert;
24  
25  /**
26   * Subclass of {@link BeanFactoryRefreshableTargetSource} that determines whether
27   * a refresh is required through the given {@link ScriptFactory}.
28   *
29   * @author Rob Harrop
30   * @author Juergen Hoeller
31   * @author Mark Fisher
32   * @since 2.0
33   */
34  public class RefreshableScriptTargetSource extends BeanFactoryRefreshableTargetSource {
35  
36  	private final ScriptFactory scriptFactory;
37  
38  	private final ScriptSource scriptSource;
39  
40  	private final boolean isFactoryBean;
41  
42  
43  	/**
44  	 * Create a new RefreshableScriptTargetSource.
45  	 * @param beanFactory the BeanFactory to fetch the scripted bean from
46  	 * @param beanName the name of the target bean
47  	 * @param scriptFactory the ScriptFactory to delegate to for determining
48  	 * whether a refresh is required
49  	 * @param scriptSource the ScriptSource for the script definition
50  	 * @param isFactoryBean whether the target script defines a FactoryBean
51  	 */
52  	public RefreshableScriptTargetSource(BeanFactory beanFactory, String beanName,
53  			ScriptFactory scriptFactory, ScriptSource scriptSource, boolean isFactoryBean) {
54  
55  		super(beanFactory, beanName);
56  		Assert.notNull(scriptFactory, "ScriptFactory must not be null");
57  		Assert.notNull(scriptSource, "ScriptSource must not be null");
58  		this.scriptFactory = scriptFactory;
59  		this.scriptSource = scriptSource;
60  		this.isFactoryBean = isFactoryBean;
61  	}
62  
63  
64  	/**
65  	 * Determine whether a refresh is required through calling
66  	 * ScriptFactory's {@code requiresScriptedObjectRefresh} method.
67  	 * @see ScriptFactory#requiresScriptedObjectRefresh(ScriptSource)
68  	 */
69  	@Override
70  	protected boolean requiresRefresh() {
71  		return this.scriptFactory.requiresScriptedObjectRefresh(this.scriptSource);
72  	}
73  
74  	/**
75  	 * Obtain a fresh target object, retrieving a FactoryBean if necessary.
76  	 */
77  	@Override
78  	protected Object obtainFreshBean(BeanFactory beanFactory, String beanName) {
79  		return super.obtainFreshBean(beanFactory,
80  				(this.isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName));
81  	}
82  
83  }