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