View Javadoc

    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 org.apache.commons.logging.Log;
    20  import org.apache.commons.logging.LogFactory;
    21  import org.apache.commons.scxml.SCXMLExecutor;
    22  import org.apache.commons.scxml.TriggerEvent;
    23  import org.apache.commons.scxml.model.ModelException;
    24
    25  /***
    26   * Trigger the given {@link TriggerEvent} (or array of) on the given
    27   * state machine executor asynchronously, once.
    28   */
    29  class AsyncTrigger implements Runnable {
    30
    31      /*** The state machine executor. */
    32      private final SCXMLExecutor executor;
    33      /*** The event(s) to be triggered. */
    34      private final TriggerEvent[] events;
    35      /*** The log. */
    36      private final Log log = LogFactory.getLog(AsyncTrigger.class);
    37
    38      /***
    39       * Constructor.
    40       *
    41       * @param executor The {@link SCXMLExecutor} to trigger the event on.
    42       * @param event The {@link TriggerEvent}.
    43       */
    44      AsyncTrigger(final SCXMLExecutor executor, final TriggerEvent event) {
    45          this.executor = executor;
    46          this.events = new TriggerEvent[1];
    47          this.events[0] = event;
    48      }
    49
    50      /***
    51       * Fire the trigger(s) asynchronously.
    52       */
    53      public void start() {
    54          new Thread(this).start();
    55      }
    56
    57      /***
    58       * Fire the event(s).
    59       */
    60      public void run() {
    61          try {
    62              synchronized (executor) {
    63                  executor.triggerEvents(events);
    64              }
    65          } catch (ModelException me) {
    66              log.error(me.getMessage(), me);
    67          }
    68      }
    69
    70  }
    71