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.binding.expression.EvaluationContext;
19: import org.springframework.binding.expression.Expression;
20: import org.springframework.util.Assert;
21: import org.springframework.webflow.execution.Event;
22: import org.springframework.webflow.execution.RequestContext;
23:
24: /**
25: * An action that evaluates an expression and optionally exposes its result.
26: * <p>
27: * Delegates to a helper {@link ResultEventFactorySelector} strategy to determine how
28: * to map the evaluation result to an action outcome {@link Event}.
29: *
30: * @see Expression
31: * @see ActionResultExposer
32: * @see ResultEventFactorySelector
33: *
34: * @author Keith Donald
35: */
36: public class EvaluateAction extends AbstractAction {
37:
38: /**
39: * The expression to evaluate when this action is invoked. Required.
40: */
41: private Expression expression;
42:
43: /**
44: * The helper that will expose the expression evaluation result. Optional.
45: */
46: private ActionResultExposer evaluationResultExposer;
47:
48: /**
49: * The selector for the factory that will create the action result event
50: * callers can respond to.
51: */
52: private ResultEventFactorySelector resultEventFactorySelector = new ResultEventFactorySelector();
53:
54: /**
55: * Create a new evaluate action.
56: * @param expression the expression to evaluate
57: */
58: public EvaluateAction(Expression expression) {
59: this (expression, null);
60: }
61:
62: /**
63: * Create a new evaluate action.
64: * @param expression the expression to evaluate
65: * @param evaluationResultExposer the strategy for how the expression result
66: * will be exposed to the flow
67: */
68: public EvaluateAction(Expression expression,
69: ActionResultExposer evaluationResultExposer) {
70: Assert
71: .notNull(expression,
72: "The expression this action should evaluate is required");
73: this .expression = expression;
74: this .evaluationResultExposer = evaluationResultExposer;
75: }
76:
77: protected Event doExecute(RequestContext context) throws Exception {
78: Object result = expression.evaluate(context,
79: getEvaluationContext(context));
80: if (evaluationResultExposer != null) {
81: evaluationResultExposer.exposeResult(result, context);
82: }
83: return resultEventFactorySelector.forResult(result)
84: .createResultEvent(this , result, context);
85: }
86:
87: /**
88: * Template method subclasses may override to customize the expressin
89: * evaluation context. This implementation returns null.
90: * @param context the request context
91: * @return the evaluation context
92: */
93: protected EvaluationContext getEvaluationContext(
94: RequestContext context) {
95: return null;
96: }
97: }
|