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.action;
17:
18: import org.springframework.beans.factory.BeanFactory;
19: import org.springframework.binding.convert.ConversionService;
20: import org.springframework.binding.method.MethodKey;
21: import org.springframework.binding.method.MethodSignature;
22: import org.springframework.webflow.core.collection.AttributeMap;
23: import org.springframework.webflow.execution.Action;
24:
25: /**
26: * A helper factory for {@link Action} instances that invoke methods on beans
27: * managed in a Spring bean factory.
28: * <p>
29: * This factory encapsulates the logic required to take an arbitrary
30: * <code>java.lang.Object</code> from a Spring bean factory and adapt a method
31: * on it to the {@link Action} interface. If the bean you want to use is not
32: * managed in a Spring bean factory, consider subclassing
33: * {@link AbstractBeanInvokingAction} and using it directly.
34: *
35: * @see AbstractBeanInvokingAction
36: *
37: * @author Keith Donald
38: */
39: public class BeanInvokingActionFactory {
40:
41: /**
42: * Determines which result event factory should be used for each bean
43: * invoking action created by this factory.
44: */
45: private ResultEventFactorySelector resultEventFactorySelector = new ResultEventFactorySelector();
46:
47: /**
48: * Returns the strategy for calculating the result event factory to
49: * configure for each bean invoking action created by this factory.
50: */
51: public ResultEventFactorySelector getResultEventFactorySelector() {
52: return resultEventFactorySelector;
53: }
54:
55: /**
56: * Sets the strategy to calculate the result event factory to configure for
57: * each bean invoking action created by this factory.
58: */
59: public void setResultEventFactorySelector(
60: ResultEventFactorySelector resultEventFactorySelector) {
61: this .resultEventFactorySelector = resultEventFactorySelector;
62: }
63:
64: /**
65: * Factory method that creates a bean invoking action, an adapter that
66: * adapts a method on an abitrary {@link Object} to the {@link Action}
67: * interface. This method is an atomic operation that returns a fully
68: * initialized Action. It encapsulates the selection of the action
69: * implementation as well as the action assembly.
70: * @param beanId the id of the bean to be adapted to an Action instance
71: * @param beanFactory the bean factory where the bean is managed
72: * @param methodSignature the method to invoke on the bean when the action
73: * is executed (required)
74: * @param resultExposer the specification for what to do with the method
75: * return value (optional)
76: * @param conversionService the conversion service to be used to convert
77: * method parameters (optional)
78: * @param attributes attributes that may be used to affect the bean invoking
79: * action's construction
80: * @return the fully configured bean invoking action instance
81: */
82: public Action createBeanInvokingAction(String beanId,
83: BeanFactory beanFactory, MethodSignature methodSignature,
84: ActionResultExposer resultExposer,
85: ConversionService conversionService, AttributeMap attributes) {
86: Object bean = beanFactory.getBean(beanId);
87: AbstractBeanInvokingAction action = new LocalBeanInvokingAction(
88: methodSignature, bean);
89: action.setMethodResultExposer(resultExposer);
90: MethodKey methodKey = new MethodKey(bean.getClass(),
91: methodSignature.getMethodName(), methodSignature
92: .getParameters().getTypesArray());
93: action.setResultEventFactory(resultEventFactorySelector
94: .forMethod(methodKey.getMethod()));
95: action.setConversionService(conversionService);
96: return action;
97: }
98: }
|