KickJava   Java API By Example, From Geeks To Geeks.

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


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.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, ActionResultExposer evaluationResultExposer) {
69         Assert.notNull(expression, "The expression this action should evaluate is required");
70         this.expression = expression;
71         this.evaluationResultExposer = evaluationResultExposer;
72     }
73
74     protected Event doExecute(RequestContext context) throws Exception JavaDoc {
75         Object JavaDoc result = expression.evaluate(context, getEvaluationContext(context));
76         if (evaluationResultExposer != null) {
77             evaluationResultExposer.exposeResult(result, context);
78         }
79         return resultEventFactorySelector.forResult(result).createResultEvent(this, result, context);
80     }
81
82     /**
83      * Template method subclasses may override to customize the expressin
84      * evaluation context. This implementation returns null.
85      * @param context the request context
86      * @return the evaluation context
87      */

88     protected EvaluationContext getEvaluationContext(RequestContext context) {
89         return null;
90     }
91 }
Popular Tags