1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.scxml.invoke;
18
19 import java.util.Map;
20
21 import org.apache.commons.scxml.SCInstance;
22 import org.apache.commons.scxml.TriggerEvent;
23
24 /**
25 * <p>The Invoker interface is used to define the possible interactions
26 * between the parent state machine (executor) and the types of invocable
27 * activities.</p>
28 *
29 * <p>Invocable activities must first register an Invoker implementation class
30 * for the appropriate "type" (attribute of <invoke>) with the
31 * parent <code>SCXMLExecutor</code>.</p>
32 *
33 * <p>The communication link between the parent state machine executor and
34 * the invoked activity is a bi-directional events pipe.</p>
35 *
36 * <p>All events triggered on the parent state machine get forwarded to the
37 * invoked activity. The processing semantics for these events depend
38 * upon the "type", and thereby vary per concrete implementation of
39 * this interface.</p>
40 *
41 * <p>The invoked activity in turn must fire a special "done" event
42 * when it concludes. It may fire additional events before the "done"
43 * event. The semantics of any additional events depend upon the
44 * "type". The invoked activity must not fire any events after the "done"
45 * event. The name of the special "done" event must be the ID of the parent
46 * state wherein the corresponding <invoke> resides, with the String
47 * ".invoke.done" appended.</p>
48 *
49 * <p>The Invoker "lifecycle" is outlined below:
50 * <ol>
51 * <li>Instantiation via {@link Class#newInstance()}
52 * (Invoker implementation requires accessible constructor).</li>
53 * <li>Configuration (setters for parent state ID and
54 * {@link SCInstance}).</li>
55 * <li>Initiation of invoked activity via invoke() method, passing
56 * the source URI and the map of params.</li>
57 * <li>Zero or more bi-directional event triggering.</li>
58 * <li>Either completion or cancellation.</li>
59 * </ol>
60 * </p>
61 *
62 * <p><b>Note:</b> The semantics of <invoke> are necessarily
63 * asynchronous, tending towards long(er) running interactions with external
64 * processes. Implementations must not communicate with the parent state
65 * machine executor in a synchronous manner. For synchronous
66 * communication semantics, use <event> or custom actions instead.</p>
67 */
68 public interface Invoker {
69
70 /**
71 * Set the state ID of the owning state for the <invoke>.
72 * Implementations must use this ID for constructing the event name for
73 * the special "done" event (and optionally, for other event names
74 * as well).
75 *
76 * @param parentStateId The ID of the parent state.
77 */
78 void setParentStateId(String parentStateId);
79
80 /**
81 * Set the "context" of the parent state machine, which provides the
82 * channel.
83 *
84 * @param scInstance The "context" of the parent state machine.
85 */
86 void setSCInstance(SCInstance scInstance);
87
88 /**
89 * Begin this invocation.
90 *
91 * @param source The source URI of the activity being invoked.
92 * @param params The <param> values
93 * @throws InvokerException In case there is a fatal problem with
94 * invoking the source.
95 */
96 void invoke(String source, Map params)
97 throws InvokerException;
98
99 /**
100 * Forwards the events triggered on the parent state machine
101 * on to the invoked activity.
102 *
103 * @param evts
104 * an array of external events which triggered during the last
105 * time quantum
106 *
107 * @throws InvokerException In case there is a fatal problem with
108 * processing the events forwarded by the
109 * parent state machine.
110 */
111 void parentEvents(TriggerEvent[] evts)
112 throws InvokerException;
113
114 /**
115 * Cancel this invocation.
116 *
117 * @throws InvokerException In case there is a fatal problem with
118 * canceling this invoke.
119 */
120 void cancel()
121 throws InvokerException;
122
123 }