Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GotoStep |
|
| 2.0;2 |
1 | /* | |
2 | * Copyright 1999-2001,2004 The Apache Software Foundation. | |
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 | ||
17 | package org.apache.commons.workflow.web; | |
18 | ||
19 | ||
20 | import java.io.IOException; | |
21 | import java.util.EmptyStackException; | |
22 | import javax.servlet.RequestDispatcher; | |
23 | import javax.servlet.ServletException; | |
24 | import javax.servlet.ServletRequest; | |
25 | import javax.servlet.ServletResponse; | |
26 | import org.apache.commons.workflow.Context; | |
27 | import org.apache.commons.workflow.Step; | |
28 | import org.apache.commons.workflow.StepException; | |
29 | import org.apache.commons.workflow.base.BaseStep; | |
30 | ||
31 | ||
32 | /** | |
33 | * <p>Unconditionally transfer control to the step that is identified by | |
34 | * a request parameter with the specified name.</p> | |
35 | * | |
36 | * <p>Supported Attributes:</p> | |
37 | * <ul> | |
38 | * <li><strong>step</strong> - Name of a request parameter, included on the | |
39 | * current request, that contains the identifier of the Step (within the | |
40 | * current Activity) to which control should be transferred. If not | |
41 | * specified, a request parameter named <code>step</code> is used.</li> | |
42 | * </ul> | |
43 | * | |
44 | * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $ | |
45 | * @author Craig R. McClanahan | |
46 | */ | |
47 | ||
48 | public class GotoStep extends BaseStep { | |
49 | ||
50 | ||
51 | // ----------------------------------------------------------= Constructors | |
52 | ||
53 | ||
54 | /** | |
55 | * Construct a default instance of this Step. | |
56 | */ | |
57 | public GotoStep() { | |
58 | ||
59 | 0 | super(); |
60 | ||
61 | 0 | } |
62 | ||
63 | ||
64 | /** | |
65 | * Construct an instance of this Step with the specified identifier. | |
66 | * | |
67 | * @param id Step identifier | |
68 | */ | |
69 | public GotoStep(String id) { | |
70 | ||
71 | 0 | super(); |
72 | 0 | setId(id); |
73 | ||
74 | 0 | } |
75 | ||
76 | ||
77 | /** | |
78 | * Construct a fully configured instance of this Step. | |
79 | * | |
80 | * @param id Step identifier | |
81 | * @param step Request parameter name containing our step identifier | |
82 | */ | |
83 | public GotoStep(String id, String step) { | |
84 | ||
85 | 0 | super(); |
86 | 0 | setId(id); |
87 | 0 | setStep(step); |
88 | ||
89 | 0 | } |
90 | ||
91 | ||
92 | // ------------------------------------------------------------- Properties | |
93 | ||
94 | ||
95 | /** | |
96 | * The request parameter containing the identifier of the Step to which | |
97 | * control should be transferred. | |
98 | */ | |
99 | 0 | protected String step = "step"; |
100 | ||
101 | public String getStep() { | |
102 | 0 | return (this.step); |
103 | } | |
104 | ||
105 | public void setStep(String step) { | |
106 | 0 | this.step = step; |
107 | 0 | } |
108 | ||
109 | ||
110 | // --------------------------------------------------------- Public Methods | |
111 | ||
112 | ||
113 | /** | |
114 | * Perform the executable actions related to this Step, in the context of | |
115 | * the specified Context. | |
116 | * | |
117 | * @param context The Context that is tracking our execution state | |
118 | * | |
119 | * @exception StepException if a processing error has occurred | |
120 | */ | |
121 | public void execute(Context context) throws StepException { | |
122 | ||
123 | // Make sure our executing Context is a WebContext | |
124 | 0 | if (!(context instanceof WebContext)) |
125 | 0 | throw new StepException("Execution context is not a WebContext", |
126 | this); | |
127 | 0 | WebContext webContext = (WebContext) context; |
128 | ||
129 | // Locate the step identifier to which we will transfer control | |
130 | 0 | ServletRequest request = webContext.getServletRequest(); |
131 | 0 | String id = request.getParameter(step); |
132 | 0 | if (id == null) |
133 | 0 | throw new StepException("No request parameter '" + step + "'", |
134 | this); | |
135 | ||
136 | ||
137 | // Locate the step to which we will transfer control | |
138 | 0 | Step next = getOwner().findStep(id); |
139 | 0 | if (next == null) |
140 | 0 | throw new StepException("Cannot find step '" + id + "'", this); |
141 | ||
142 | // Tell our Context to transfer control | |
143 | 0 | context.setNextStep(next); |
144 | ||
145 | 0 | } |
146 | ||
147 | ||
148 | /** | |
149 | * Render a string representation of this Step. | |
150 | */ | |
151 | public String toString() { | |
152 | ||
153 | 0 | StringBuffer sb = new StringBuffer("<web:goto"); |
154 | 0 | if (getId() != null) { |
155 | 0 | sb.append(" id=\""); |
156 | 0 | sb.append(getId()); |
157 | 0 | sb.append("\""); |
158 | } | |
159 | 0 | sb.append(" step=\""); |
160 | 0 | sb.append(getStep()); |
161 | 0 | sb.append("\"/>"); |
162 | 0 | return (sb.toString()); |
163 | ||
164 | } | |
165 | ||
166 | ||
167 | } |