01: /*
02: * Copyright 2004-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: package org.springframework.webflow.engine.support;
17:
18: import org.springframework.beans.factory.BeanFactory;
19: import org.springframework.util.Assert;
20: import org.springframework.util.StringUtils;
21: import org.springframework.webflow.engine.FlowVariable;
22: import org.springframework.webflow.execution.RequestContext;
23: import org.springframework.webflow.execution.ScopeType;
24:
25: /**
26: * A concrete flow variable subclass that obtains variable values from a Spring
27: * {@link BeanFactory}.
28: *
29: * @author Keith Donald
30: * @author Erwin Vervaet
31: */
32: public class BeanFactoryFlowVariable extends FlowVariable {
33:
34: /**
35: * The name of the bean whose value will be used as the flow
36: * variable. The bean should be a prototype.
37: */
38: private String beanName;
39:
40: /**
41: * The bean factory where initial variable values will be obtained.
42: */
43: private BeanFactory beanFactory;
44:
45: /**
46: * Convenience constructor to create a new bean factory flow variable.
47: * Defaults the bean name to the variable name.
48: * @param name the variable name which will also be used as the bean name
49: * @param beanFactory the bean factory where initial variable values will be
50: * obtained
51: * @param scope the variable scope
52: * @since 1.0.2
53: */
54: public BeanFactoryFlowVariable(String name,
55: BeanFactory beanFactory, ScopeType scope) {
56: this (name, name, beanFactory, scope);
57: }
58:
59: /**
60: * Creates a new bean factory flow variable.
61: * @param variableName the variable name
62: * @param beanName the bean name, will default to the variable name if not specified
63: * @param beanFactory the bean factory where initial variable values will be
64: * obtained
65: * @param scope the variable scope
66: */
67: public BeanFactoryFlowVariable(String variableName,
68: String beanName, BeanFactory beanFactory, ScopeType scope) {
69: super (variableName, scope);
70: if (StringUtils.hasText(beanName)) {
71: this .beanName = beanName;
72: } else {
73: this .beanName = variableName;
74: }
75: Assert.notNull(beanFactory, "The bean factory is required");
76: Assert.isTrue(!beanFactory.isSingleton(this .beanName),
77: "The bean with name '" + this .beanName
78: + "' must be a prototype (singleton=false)");
79: this .beanFactory = beanFactory;
80: }
81:
82: protected Object createVariableValue(RequestContext context) {
83: return beanFactory.getBean(beanName);
84: }
85: }
|