KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > support > BeanFactoryFlowVariable


1 /*
2  * Copyright 2002-2006 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 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  */

31 public class BeanFactoryFlowVariable extends FlowVariable {
32
33     /**
34      * The name of the bean whose value will be used as the flow
35      * variable. The bean should be a prototype.
36      */

37     private String JavaDoc beanName;
38
39     /**
40      * The bean factory where initial variable values will be obtained.
41      */

42     private BeanFactory beanFactory;
43
44     /**
45      * Creates a new bean factory flow variable.
46      * @param variableName the variable name
47      * @param beanName the bean name, will default to the variable name if not specified
48      * @param beanFactory the bean factory where initial variable values will be
49      * obtained
50      * @param scope the variable scope
51      */

52     public BeanFactoryFlowVariable(String JavaDoc variableName, String JavaDoc beanName, BeanFactory beanFactory, ScopeType scope) {
53         super(variableName, scope);
54         if (StringUtils.hasText(beanName)) {
55             this.beanName = beanName;
56         }
57         else {
58             this.beanName = variableName;
59         }
60         Assert.notNull(beanFactory, "The bean factory is required");
61         Assert.isTrue(!beanFactory.isSingleton(this.beanName), "The bean with name '" + this.beanName
62                 + "' must be a prototype (singleton=false)");
63         this.beanFactory = beanFactory;
64     }
65
66     protected Object JavaDoc createVariableValue(RequestContext context) {
67         return beanFactory.getBean(beanName);
68     }
69 }
Popular Tags