KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.springframework.binding.convert.ConversionService;
19 import org.springframework.binding.convert.support.DefaultConversionService;
20 import org.springframework.binding.method.MethodInvoker;
21 import org.springframework.binding.method.MethodSignature;
22 import org.springframework.util.Assert;
23 import org.springframework.webflow.execution.Action;
24 import org.springframework.webflow.execution.Event;
25 import org.springframework.webflow.execution.RequestContext;
26
27 /**
28  * Base class for actions that delegate to methods on beans (POJOs - Plain Old
29  * Java Objects). Acts as an adapter that adapts an {@link Object} method to the
30  * Spring Web Flow {@link Action} contract.
31  * <p>
32  * Subclasses are required to implement the {@link #getBean(RequestContext)}
33  * method, returning the bean on which a method should be invoked.
34  *
35  * @see BeanInvokingActionFactory
36  *
37  * @author Keith Donald
38  */

39 public abstract class AbstractBeanInvokingAction extends AbstractAction {
40
41     /**
42      * The signature of the method to invoke on the target bean, capable of
43      * resolving the method when used with a {@link MethodInvoker}. Required.
44      */

45     private MethodSignature methodSignature;
46
47     /**
48      * The method invoker that performs the action-&gt;bean method binding,
49      * accepting a {@link MethodSignature} and
50      * {@link #getBean(RequestContext) target bean} instance.
51      */

52     private MethodInvoker methodInvoker = new MethodInvoker();
53
54     /**
55      * The specification (configuration) for how bean method return values
56      * should be exposed to an executing flow that invokes this action.
57      */

58     private ActionResultExposer methodResultExposer;
59
60     /**
61      * The strategy that adapts bean method return values to Event objects.
62      */

63     private ResultEventFactory resultEventFactory = new SuccessEventFactory();
64
65     /**
66      * Creates a new bean invoking action.
67      * @param methodSignature the signature of the method to invoke
68      */

69     protected AbstractBeanInvokingAction(MethodSignature methodSignature) {
70         Assert.notNull(methodSignature, "The signature of the target method to invoke is required");
71         this.methodSignature = methodSignature;
72     }
73
74     /**
75      * Returns the signature of the method to invoke on the target bean.
76      */

77     public MethodSignature getMethodSignature() {
78         return methodSignature;
79     }
80
81     /**
82      * Returns the configuration for how bean method return values should be
83      * exposed to an executing flow that invokes this action.
84      */

85     public ActionResultExposer getMethodResultExposer() {
86         return methodResultExposer;
87     }
88
89     /**
90      * Configures how bean method return values should be exposed to an
91      * executing flow that invokes this action. This is optional. By default the
92      * bean method return values do not get exposed to the executing flow.
93      */

94     public void setMethodResultExposer(ActionResultExposer methodResultExposer) {
95         this.methodResultExposer = methodResultExposer;
96     }
97
98     /**
99      * Returns the event adaption strategy used by this action.
100      */

101     protected ResultEventFactory getResultEventFactory() {
102         return resultEventFactory;
103     }
104
105     /**
106      * Set the bean return value-&gt;event adaption strategy. Defaults to
107      * {@link SuccessEventFactory}, so all bean method return values will be
108      * interpreted as "success".
109      */

110     public void setResultEventFactory(ResultEventFactory resultEventFactory) {
111         this.resultEventFactory = resultEventFactory;
112     }
113
114     /**
115      * Set the conversion service to perform type conversion of event parameters
116      * to method arguments as neccessary.
117      * Defaults to {@link DefaultConversionService}.
118      */

119     public void setConversionService(ConversionService conversionService) {
120         methodInvoker.setConversionService(conversionService);
121     }
122
123     /**
124      * Returns the bean method invoker helper.
125      */

126     protected MethodInvoker getMethodInvoker() {
127         return methodInvoker;
128     }
129
130     protected Event doExecute(RequestContext context) throws Exception JavaDoc {
131         Object JavaDoc bean = getBean(context);
132         Object JavaDoc returnValue = getMethodInvoker().invoke(methodSignature, bean, context);
133         if (methodResultExposer != null) {
134             methodResultExposer.exposeResult(returnValue, context);
135         }
136         return resultEventFactory.createResultEvent(bean, returnValue, context);
137     }
138
139     // subclassing hooks
140

141     /**
142      * Retrieves the bean to invoke a method on. Subclasses need to implement
143      * this method.
144      * @param context the flow execution request context
145      * @return the bean on which to invoke methods
146      * @throws Exception when the bean cannot be retreived
147      */

148     protected abstract Object JavaDoc getBean(RequestContext context) throws Exception JavaDoc;
149
150 }
Popular Tags