KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > AnnotatedAction


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.engine;
17
18 import org.springframework.core.style.ToStringCreator;
19 import org.springframework.util.Assert;
20 import org.springframework.util.StringUtils;
21 import org.springframework.webflow.core.collection.AttributeMap;
22 import org.springframework.webflow.execution.Action;
23 import org.springframework.webflow.execution.Event;
24 import org.springframework.webflow.execution.RequestContext;
25
26 /**
27  * An action proxy/decorator that stores arbitrary properties about a target
28  * <code>Action</code> implementation for use within a specific Action
29  * execution context, for example an <code>ActionState</code> definition, a
30  * <code>TransitionCriteria</code> definition, or in a test environment.
31  * <p>
32  * An annotated action is an action that wraps another action (referred to as
33  * the <i>target</i> action), setting up the target action's execution attributes
34  * before invoking {@link Action#execute}.
35  *
36  * @author Keith Donald
37  * @author Erwin Vervaet
38  */

39 public class AnnotatedAction extends AnnotatedObject implements Action {
40
41     // well known attributes
42

43     /**
44      * The action name attribute ("name").
45      * <p>
46      * The name attribute is often used as a qualifier for an action's result
47      * event, and is typically used to allow the flow to respond to a specific
48      * action's outcome within a larger action execution chain.
49      * @see ActionState
50      */

51     public static final String JavaDoc NAME_ATTRIBUTE = "name";
52
53     /**
54      * The action execution method attribute ("method").
55      * <p>
56      * The method property is a hint about what method should be invoked; for
57      * example, the name of a specific target method on a
58      * {@link org.springframework.webflow.action.MultiAction multi action}.
59      * @see ActionState
60      */

61     public static final String JavaDoc METHOD_ATTRIBUTE = "method";
62
63     /**
64      * The target action to execute.
65      */

66     private Action targetAction;
67
68     /**
69      * Creates a new annotated action object for the specified action. No
70      * contextual properties are provided.
71      * @param targetAction the action
72      */

73     public AnnotatedAction(Action targetAction) {
74         setTargetAction(targetAction);
75     }
76
77     /**
78      * Returns the wrapped target action.
79      * @return the action
80      */

81     public Action getTargetAction() {
82         return targetAction;
83     }
84
85     /**
86      * Set the target action wrapped by this decorator.
87      */

88     public void setTargetAction(Action targetAction) {
89         Assert.notNull(targetAction, "The targetAction to annotate is required");
90         this.targetAction = targetAction;
91     }
92
93     /**
94      * Returns the name of a named action, or <code>null</code> if the action
95      * is unnamed. Used when mapping action result events to transitions.
96      * @see #isNamed()
97      * @see #postProcessResult(Event)
98      */

99     public String JavaDoc getName() {
100         return getAttributeMap().getString(NAME_ATTRIBUTE);
101     }
102
103     /**
104      * Sets the name of a named action. This is optional and can be
105      * <code>null</code>.
106      * @param name the action name
107      */

108     public void setName(String JavaDoc name) {
109         getAttributeMap().put(NAME_ATTRIBUTE, name);
110     }
111
112     /**
113      * Returns whether or not the wrapped target action is a named action.
114      * @see #getName()
115      * @see #setName(String)
116      */

117     public boolean isNamed() {
118         return StringUtils.hasText(getName());
119     }
120     
121     /**
122      * Returns the name of the action method to invoke when the target action is
123      * executed.
124      */

125     public String JavaDoc getMethod() {
126         return getAttributeMap().getString(METHOD_ATTRIBUTE);
127     }
128
129     /**
130      * Sets the name of the action method to invoke when the target action is
131      * executed.
132      * @param method the action method name
133      */

134     public void setMethod(String JavaDoc method) {
135         getAttributeMap().put(METHOD_ATTRIBUTE, method);
136     }
137
138     public Event execute(RequestContext context) throws Exception JavaDoc {
139         AttributeMap originalAttributes = getAttributeMap();
140         try {
141             context.setAttributes(getAttributeMap());
142             Event result = getTargetAction().execute(context);
143             return postProcessResult(result);
144         }
145         finally {
146             // restore original attributes
147
context.setAttributes(originalAttributes);
148         }
149     }
150
151     /**
152      * Get the event id to be used as grounds for a transition in the containing
153      * state, based on given result returned from action execution.
154      * <p>
155      * If the wrapped action is named, the name will be used as a qualifier for
156      * the event (e.g. "myAction.success").
157      * @param resultEvent the action result event
158      */

159     protected Event postProcessResult(Event resultEvent) {
160         if (resultEvent == null) {
161             return null;
162         }
163         if (isNamed()) {
164             // qualify result event id with action name for a named action
165
String JavaDoc qualifiedId = getName() + "." + resultEvent.getId();
166             resultEvent = new Event(resultEvent.getSource(), qualifiedId, resultEvent.getAttributes());
167         }
168         return resultEvent;
169     }
170
171     public String JavaDoc toString() {
172         return new ToStringCreator(this).append("targetAction", getTargetAction())
173             .append("attributes", getAttributeMap()).toString();
174     }
175 }
Popular Tags