1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.workflow.core;
18
19
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 import java.util.ArrayList;
23 import org.apache.commons.jxpath.JXPathContext;
24 import org.apache.commons.workflow.Context;
25 import org.apache.commons.workflow.Descriptor;
26 import org.apache.commons.workflow.StepException;
27 import org.apache.commons.workflow.base.DescriptorStep;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class InvokeStep extends DescriptorStep {
50
51
52
53
54
55
56
57
58 public InvokeStep() {
59
60 super();
61
62 }
63
64
65
66
67
68
69
70 public InvokeStep(String id) {
71
72 super();
73 setId(id);
74
75 }
76
77
78
79
80
81
82
83
84 public InvokeStep(String id, String method) {
85
86 super();
87 setId(id);
88 setMethod(method);
89
90 }
91
92
93
94
95
96
97
98
99
100 public InvokeStep(String id, String method, Descriptor descriptor) {
101
102 super();
103 setId(id);
104 setMethod(method);
105 addDescriptor(descriptor);
106
107 }
108
109
110
111
112
113
114
115
116 protected String method = null;
117
118 public String getMethod() {
119 return (this.method);
120 }
121
122 public void setMethod(String method) {
123 this.method = method;
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138 public void execute(Context context) throws StepException {
139
140
141 Descriptor descriptors[] = findDescriptors();
142 if (descriptors.length < 1)
143 throw new StepException("No object descriptor on which to invoke",
144 this);
145 Object bean = descriptors[0].get(context);
146 if (bean == null)
147 throw new StepException("No object bean on which to invoke",
148 this);
149
150
151 Class types[] = new Class[descriptors.length - 1];
152 Object values[] = new Object[descriptors.length - 1];
153 for (int i = 1; i < descriptors.length; i++) {
154 values[i-1] = descriptors[i].get(context);
155 types[i-1] = descriptors[i].getType();
156 if (types[i-1] == null) {
157 if (values[i-1] == null)
158 types[i-1] = Object.class;
159 else
160 types[i-1] = values[i-1].getClass();
161 }
162 }
163
164
165 Method method = findMethod(bean, this.method, types);
166 if (method == null)
167 throw new StepException("No available method " +
168 signature(this.method, types),
169 this);
170
171
172
173 try {
174 Class clazz = method.getReturnType();
175 if ((clazz != null) && (clazz != Void.TYPE)) {
176 Object result = method.invoke(bean, values);
177 context.push(result);
178 } else {
179 method.invoke(bean, values);
180 }
181 } catch (InvocationTargetException t) {
182 Throwable cause = t.getTargetException();
183 throw new StepException("Invoke exception", cause, this);
184 } catch (Throwable t) {
185 throw new StepException("Invoke exception", t, this);
186 }
187
188 }
189
190
191
192
193
194 public String toString() {
195
196 StringBuffer sb = new StringBuffer("<core:invoke");
197 if (getId() != null) {
198 sb.append(" id=\"");
199 sb.append(getId());
200 sb.append("\"");
201 }
202 sb.append(" method=\"");
203 sb.append(getMethod());
204 sb.append("\"");
205 sb.append(">");
206 Descriptor descriptors[] = findDescriptors();
207 for (int i = 0; i < descriptors.length; i++)
208 sb.append(descriptors[i].toString());
209 sb.append("</core:invoke>");
210 return (sb.toString());
211
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227 protected Method findMethod(Object bean, String name, Class types[]) {
228
229 try {
230 return (bean.getClass().getMethod(name, types));
231 } catch (NoSuchMethodException e) {
232 return (null);
233 }
234
235 }
236
237
238
239
240
241
242
243
244 protected String signature(String name, Class types[]) {
245
246 StringBuffer sb = new StringBuffer(name);
247 sb.append('(');
248 for (int i = 0; i < types.length; i++) {
249 if (i > 0)
250 sb.append(',');
251 sb.append(types[i].getName());
252 }
253 sb.append(')');
254 return (sb.toString());
255
256 }
257
258
259 }