View Javadoc

1   /*
2    *
3    *   Copyright 2006 The Apache Software Foundation.
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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   */
18  package org.apache.commons.scxml.invoke;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.commons.scxml.SCXMLExecutor;
23  import org.apache.commons.scxml.TriggerEvent;
24  import org.apache.commons.scxml.model.ModelException;
25  
26  /***
27   * Trigger the given {@link TriggerEvent} (or array of) on the given
28   * state machine executor asynchronously, once.
29   */
30  class AsyncTrigger implements Runnable {
31  
32      /*** The state machine executor. */
33      private SCXMLExecutor executor;
34      /*** The event(s) to be triggered. */
35      private TriggerEvent[] events;
36      /*** The log. */
37      private Log log = LogFactory.getLog(AsyncTrigger.class);
38  
39      /***
40       * Constructor.
41       *
42       * @param executor The {@link SCXMLExecutor} to trigger the event on.
43       * @param event The {@link TriggerEvent}.
44       */
45      AsyncTrigger(final SCXMLExecutor executor, final TriggerEvent event) {
46          this.executor = executor;
47          this.events = new TriggerEvent[1];
48          this.events[0] = event;
49          new Thread(this).start();
50      }
51  
52      /***
53       * Fire the event(s).
54       */
55      public void run() {
56          try {
57              synchronized (executor) {
58                  executor.triggerEvents(events);
59              }
60          } catch (ModelException me) {
61              log.error(me.getMessage(), me);
62          }
63      }
64  
65  }
66