1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.commons.scxml.invoke; |
19 |
|
|
20 |
|
import java.io.IOException; |
21 |
|
import java.util.Iterator; |
22 |
|
import java.util.Map; |
23 |
|
|
24 |
|
import org.apache.commons.scxml.Context; |
25 |
|
import org.apache.commons.scxml.Evaluator; |
26 |
|
import org.apache.commons.scxml.SCInstance; |
27 |
|
import org.apache.commons.scxml.SCXMLExecutor; |
28 |
|
import org.apache.commons.scxml.TriggerEvent; |
29 |
|
import org.apache.commons.scxml.env.SimpleDispatcher; |
30 |
|
import org.apache.commons.scxml.env.SimpleErrorHandler; |
31 |
|
import org.apache.commons.scxml.env.SimpleErrorReporter; |
32 |
|
import org.apache.commons.scxml.env.SimpleSCXMLListener; |
33 |
|
import org.apache.commons.scxml.io.SCXMLDigester; |
34 |
|
import org.apache.commons.scxml.model.ModelException; |
35 |
|
import org.apache.commons.scxml.model.SCXML; |
36 |
|
import org.xml.sax.SAXException; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
1 |
public class SimpleSCXMLInvoker implements Invoker { |
43 |
|
|
44 |
|
|
45 |
|
private String parentStateId; |
46 |
|
|
47 |
|
|
48 |
|
private String eventPrefix; |
49 |
|
|
50 |
|
private SCInstance parentSCInstance; |
51 |
|
|
52 |
|
private SCXMLExecutor executor; |
53 |
|
|
54 |
|
private boolean cancelled; |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
1 |
private static String invokePrefix = ".invoke."; |
59 |
|
|
60 |
1 |
private static String invokeDone = "done"; |
61 |
|
|
62 |
1 |
private static String invokeCancelResponse = "cancel.response"; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
public void setParentStateId(final String parentStateId) { |
68 |
1 |
this.parentStateId = parentStateId; |
69 |
1 |
this.eventPrefix = parentStateId + invokePrefix; |
70 |
1 |
this.cancelled = false; |
71 |
1 |
} |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
public void setSCInstance(final SCInstance scInstance) { |
77 |
1 |
this.parentSCInstance = scInstance; |
78 |
1 |
} |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
public void invoke(final String source, final Map params) |
84 |
|
throws InvokerException { |
85 |
1 |
SCXML scxml = null; |
86 |
|
try { |
87 |
1 |
scxml = SCXMLDigester.digest(source, |
88 |
|
new SimpleErrorHandler(), null); |
89 |
0 |
} catch (ModelException me) { |
90 |
0 |
throw new InvokerException(me.getMessage(), me.getCause()); |
91 |
0 |
} catch (IOException ioe) { |
92 |
0 |
throw new InvokerException(ioe.getMessage(), ioe.getCause()); |
93 |
0 |
} catch (SAXException se) { |
94 |
0 |
throw new InvokerException(se.getMessage(), se.getCause()); |
95 |
1 |
} |
96 |
1 |
Evaluator eval = parentSCInstance.getEvaluator(); |
97 |
1 |
executor = new SCXMLExecutor(eval, |
98 |
|
new SimpleDispatcher(), new SimpleErrorReporter()); |
99 |
1 |
Context rootCtx = eval.newContext(null); |
100 |
1 |
for (Iterator iter = params.entrySet().iterator(); iter.hasNext();) { |
101 |
2 |
Map.Entry entry = (Map.Entry) iter.next(); |
102 |
2 |
rootCtx.setLocal((String) entry.getKey(), entry.getValue()); |
103 |
|
} |
104 |
1 |
executor.setRootContext(rootCtx); |
105 |
1 |
executor.setStateMachine(scxml); |
106 |
1 |
executor.addListener(scxml, new SimpleSCXMLListener()); |
107 |
|
try { |
108 |
1 |
executor.go(); |
109 |
0 |
} catch (ModelException me) { |
110 |
0 |
throw new InvokerException(me.getMessage(), me.getCause()); |
111 |
1 |
} |
112 |
1 |
} |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
public void parentEvents(final TriggerEvent[] evts) |
118 |
|
throws InvokerException { |
119 |
0 |
if (cancelled) { |
120 |
0 |
return; |
121 |
|
} |
122 |
0 |
boolean doneBefore = executor.getCurrentStatus().isFinal(); |
123 |
|
try { |
124 |
0 |
executor.triggerEvents(evts); |
125 |
0 |
} catch (ModelException me) { |
126 |
0 |
throw new InvokerException(me.getMessage(), me.getCause()); |
127 |
0 |
} |
128 |
0 |
if (!doneBefore && executor.getCurrentStatus().isFinal()) { |
129 |
0 |
TriggerEvent te = new TriggerEvent(eventPrefix + invokeDone, |
130 |
|
TriggerEvent.SIGNAL_EVENT); |
131 |
0 |
new AsyncTrigger(parentSCInstance.getExecutor(), te); |
132 |
|
} |
133 |
0 |
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
public void cancel() |
139 |
|
throws InvokerException { |
140 |
0 |
cancelled = true; |
141 |
0 |
TriggerEvent te = new TriggerEvent(eventPrefix |
142 |
|
+ invokeCancelResponse, TriggerEvent.SIGNAL_EVENT); |
143 |
0 |
new AsyncTrigger(parentSCInstance.getExecutor(), te); |
144 |
0 |
} |
145 |
|
|
146 |
|
} |
147 |
|
|