001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.scxml2.invoke;
018
019import java.util.Map;
020
021import org.apache.commons.scxml2.SCXMLExecutor;
022import org.apache.commons.scxml2.SCXMLIOProcessor;
023import org.apache.commons.scxml2.TriggerEvent;
024
025/**
026 * <p>The Invoker interface is used to define the possible interactions
027 * between the parent state machine (executor) and the types of invocable
028 * activities.</p>
029 *
030 * <p>Invocable activities must first register an Invoker implementation class
031 * for the appropriate "target" (attribute of &lt;invoke&gt;) with the
032 * parent <code>SCXMLParentIOProcessor</code>.</p>
033 *
034 * <p>The communication link between the parent state machine executor and
035 * the invoked activity is a asynchronous bi-directional events pipe.</p>
036 *
037 * <p>All events triggered on the parent state machine get forwarded to the
038 * invoked activity. The processing semantics for these events depend
039 * upon the "target", and thereby vary per concrete implementation of
040 * this interface.</p>
041 *
042 * <p>The invoked activity in turn must fire a special "done" event
043 * when it concludes. It may fire additional events before the "done"
044 * event. The semantics of any additional events depend upon the
045 * "target". The invoked activity must not fire any events after the "done"
046 * event. The name of the special "done" event must be "done.invoke.id" with
047 * the ID of the parent state wherein the corresponding &lt;invoke&gt; resides,</p>
048 *
049 * <p>The Invoker "lifecycle" is outlined below:
050 *  <ol>
051 *   <li>Instantiation via {@link Class#newInstance()}
052 *       (Invoker implementation requires accessible constructor).</li>
053 *   <li>Configuration (setters for invoke ID and
054 *       {@link org.apache.commons.scxml2.SCXMLExecutor}).</li>
055 *   <li>Initiation of invoked activity via invoke() method, passing
056 *       the source URI and the map of params.</li>
057 *   <li>Zero or more bi-directional event triggering.</li>
058 *   <li>Either completion or cancellation.</li>
059 *  </ol>
060 * </p>
061 *
062 * <p><b>Note:</b> The semantics of &lt;invoke&gt; are necessarily
063 * asynchronous, tending towards long(er) running interactions with external
064 * processes. Implementations cannot communicate with the parent state
065 * machine executor in a synchronous manner. For synchronous
066 * communication semantics, use &lt;event&gt; or custom actions instead.</p>
067 */
068public interface Invoker {
069
070    /**
071     * @return get the invoke ID provided by the parent state machine executor
072     */
073    String getInvokeId();
074
075    /**
076     * Set the invoke ID provided by the parent state machine executor
077     * Implementations must use this ID for constructing the event name for
078     * the special "done" event (and optionally, for other event names
079     * as well).
080     *
081     * @param invokeId The invoke ID provided by the parent state machine executor.
082     */
083    void setInvokeId(String invokeId);
084
085    /**
086     * Sets the parent SCXMLExecutor through which this Invoker is initiated
087     * @param scxmlExecutor the parent SCXMLExecutor
088     */
089    void setParentSCXMLExecutor(SCXMLExecutor scxmlExecutor);
090
091    /**
092     * Get the child IO Processor to register for communication with
093     * the parent session.
094     *
095     * @return Child IO Processor
096     */
097    SCXMLIOProcessor getChildIOProcessor();
098
099    /**
100     * Begin this invocation.
101     *
102     * @param source The source URI of the activity being invoked.
103     * @param params The &lt;param&gt; values
104     * @throws InvokerException In case there is a fatal problem with
105     *                          invoking the source.
106     */
107    void invoke(String source, Map<String, Object> params)
108    throws InvokerException;
109
110    /**
111     * Forwards the event triggered on the parent state machine
112     * on to the invoked activity.
113     *
114     * @param event
115     *            an external event which triggered during the last
116     *            time quantum
117     *
118     * @throws InvokerException In case there is a fatal problem with
119     *                          processing the events forwarded by the
120     *                          parent state machine.
121     */
122    void parentEvent(TriggerEvent event)
123    throws InvokerException;
124
125    /**
126     * Cancel this invocation.
127     *
128     * @throws InvokerException In case there is a fatal problem with
129     *                          canceling this invoke.
130     */
131    void cancel()
132    throws InvokerException;
133}
134