Example usage for org.apache.commons.scxml SCXMLExecutor SCXMLExecutor

List of usage examples for org.apache.commons.scxml SCXMLExecutor SCXMLExecutor

Introduction

In this page you can find the example usage for org.apache.commons.scxml SCXMLExecutor SCXMLExecutor.

Prototype

public SCXMLExecutor() 

Source Link

Document

Convenience constructor.

Usage

From source file:com.headstrong.fusion.statemachine.SCXMLStateMachine.java

/**
 * Initializes the state machine.//  ww  w  .j  ava  2s  . c o  m
 * 
 * @throws StateMachineException
 */
public void init() throws StateMachineException {
    // Create a list of custom actions, add as many as are needed
    // currently only one custom action is exposed which internally would
    // execute action service registered by the component.
    // Need to take care of the parsing part.
    SCXML scxml = null;
    URL url = null;
    File tempConfigFile = null;
    try {
        tempConfigFile = this.persistConfiguration(this.definition);
        url = tempConfigFile.toURI().toURL();
    } catch (MalformedURLException e) {
        logger.error(
                "Error persisting the configuration in temp file for state machine id " + this.getId() + ".",
                e);
        throw new StateMachineException(
                "Error creating the state machine for state machine id " + this.getId() + ".", e);
    } catch (IOException e) {
        logger.error(
                "Error persisting the configuration in temp file for state machine id " + this.getId() + ".",
                e);
        throw new StateMachineException(
                "Error creating the state machine for state machine id " + this.getId() + ".", e);
    }
    try {
        Digester scxmlParser = SCXMLParser.newInstance(null, new URLResolver(url), getCustomActions());
        scxmlParser.addObjectCreate("*/" + CUSTOM_ACTION_SERVICE + "/properties", HashMap.class);
        scxmlParser.addSetNext("*/" + CUSTOM_ACTION_SERVICE + "/properties", "setProperties");

        // call the put method on the top object on the digester stack
        // passing the key attribute as the 0th parameter
        // and the element body text as the 1th parameter..
        scxmlParser.addCallMethod("*/" + CUSTOM_ACTION_SERVICE + "/properties/property", "put", 2);
        scxmlParser.addCallParam("*/" + CUSTOM_ACTION_SERVICE + "/properties/property", 0, "name");
        scxmlParser.addCallParam("*/" + CUSTOM_ACTION_SERVICE + "/properties/property", 1, "value");

        scxmlParser.setErrorHandler(new SimpleErrorHandler());

        scxml = (SCXML) scxmlParser.parse(url.toString());
        SCXMLParser.updateSCXML(scxml);
    } catch (IOException e) {
        logger.error(
                "Error persisting the configuration in temp file for state machine id " + this.getId() + ".");
        throw new StateMachineException(
                "Error creating the state machine for state machine id " + this.getId() + ".", e);
    } catch (SAXException e) {
        logger.error("Error parsing the configuration for state machine id " + this.getId() + ".");
        throw new StateMachineException(
                "Error creating the state machine for state machine id " + this.getId() + ".", e);
    } catch (ModelException e) {
        logger.error("Error persisting the configuration for state machine id " + this.getId() + ".");
        throw new StateMachineException(
                "Error creating the state machine for state machine id " + this.getId() + ".", e);
    }
    // Once done remove the file.
    tempConfigFile.delete();
    // initialize the executor.
    scxmlExecutor = new SCXMLExecutor();
    scxmlExecutor.setStateMachine(scxml);
    scxmlExecutor.setEvaluator(this.getEvaluator());
    scxmlExecutor.setErrorReporter(this.getErrorReporter());
    scxmlExecutor.setEventdispatcher(this.getEventDispatcher(scxmlExecutor));
    scxmlExecutor.addListener(scxml, new SimpleSCXMLListener());
    scxmlExecutor.registerInvokerClass("scxml", SimpleSCXMLInvoker.class);
    scxmlExecutor.setRootContext(scxmlExecutor.getEvaluator().newContext(null));
}