KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > action > BeanInvokingActionFactory


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.action;
17
18 import java.lang.reflect.Method JavaDoc;
19
20 import org.springframework.beans.factory.BeanFactory;
21 import org.springframework.binding.convert.ConversionService;
22 import org.springframework.binding.method.ClassMethodKey;
23 import org.springframework.binding.method.MethodSignature;
24 import org.springframework.webflow.core.collection.AttributeMap;
25 import org.springframework.webflow.execution.Action;
26
27 /**
28  * A helper factory for {@link Action} instances that invoke methods on
29  * beans managed in a Spring bean factory.
30  * <p>
31  * This factory encapsulates the logic required to take an arbitrary
32  * <code>java.lang.Object</code> from a Spring bean factory and adapt
33  * a method on it to the {@link Action} interface. If the bean you
34  * want to use is not managed in a Spring bean factory, consider
35  * subclassing {@link AbstractBeanInvokingAction} and using it directly.
36  *
37  * @see AbstractBeanInvokingAction
38  *
39  * @author Keith Donald
40  */

41 public class BeanInvokingActionFactory {
42
43     /**
44      * Determines which result event factory should be used for each bean
45      * invoking action created by this factory.
46      */

47     private ResultEventFactorySelector resultEventFactorySelector = new ResultEventFactorySelector();
48
49     /**
50      * Returns the strategy for calculating the result event factory to configure
51      * for each bean invoking action created by this factory.
52      */

53     public ResultEventFactorySelector getResultEventFactorySelector() {
54         return resultEventFactorySelector;
55     }
56
57     /**
58      * Sets the strategy to calculate the result event factory to configure for
59      * each bean invoking action created by this factory.
60      */

61     public void setResultEventFactorySelector(ResultEventFactorySelector resultEventFactorySelector) {
62         this.resultEventFactorySelector = resultEventFactorySelector;
63     }
64
65     /**
66      * Factory method that creates a bean invoking action, an adapter that
67      * adapts a method on an abitrary {@link Object} to the {@link Action}
68      * interface. This method is an atomic operation that returns a fully
69      * initialized Action. It encapsulates the selection of the action
70      * implementation as well as the action assembly.
71      * @param beanId the id of the bean to be adapted to an Action instance
72      * @param beanFactory the bean factory where the bean is managed
73      * @param methodSignature the method to invoke on the bean when the action
74      * is executed (required)
75      * @param resultExposer the specification for what to do with the method
76      * return value (optional)
77      * @param conversionService the conversion service to be used to convert
78      * method parameters (optional)
79      * @param attributes attributes that may be used to affect the bean invoking
80      * action's construction
81      * @return the fully configured bean invoking action instance
82      */

83     public Action createBeanInvokingAction(String JavaDoc beanId, BeanFactory beanFactory, MethodSignature methodSignature,
84             ActionResultExposer resultExposer, ConversionService conversionService, AttributeMap attributes) {
85         Object JavaDoc bean = beanFactory.getBean(beanId);
86         AbstractBeanInvokingAction action = new LocalBeanInvokingAction(methodSignature, bean);
87         action.setMethodResultExposer(resultExposer);
88         Method JavaDoc method = new ClassMethodKey(bean.getClass(), methodSignature.getMethodName(), methodSignature.getParameters()
89                 .getTypesArray()).getMethod();
90         action.setResultEventFactory(resultEventFactorySelector.forMethod(method));
91         action.setConversionService(conversionService);
92         return action;
93     }
94 }
Popular Tags