Example usage for org.apache.commons.scxml.env SimpleSCXMLListener SimpleSCXMLListener

List of usage examples for org.apache.commons.scxml.env SimpleSCXMLListener SimpleSCXMLListener

Introduction

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

Prototype

SimpleSCXMLListener

Source Link

Usage

From source file:com.korwe.thecore.scxml.ScxmlMessageProcessor.java

@Override
public void initialize(String sessionId) {
    super.initialize(sessionId);
    try {//from   w w  w  . j av a 2 s. c  o  m
        String scxmlPath = CoreConfig.getInstance().getProperty("scxml_path");
        if (LOG.isDebugEnabled()) {
            LOG.debug("SCXML path = " + scxmlPath);
        }
        File scfile = new File(scxmlPath);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Absolute path: [" + scfile.getAbsolutePath() + "]");
        }

        InputSource source = new InputSource(new BufferedReader(new FileReader(scxmlPath)));

        scxml = SCXMLParser.parse(source, new SimpleErrorHandler());
        exec = new SCXMLExecutor(new JexlEvaluator(), new SimpleDispatcher(), new SimpleErrorReporter());
        exec.setStateMachine(scxml);
        exec.addListener(scxml, new SimpleSCXMLListener());
        exec.registerInvokerClass("x-coremessage", SendCoreMessageInvoker.class);

        Context context = new JexlContext();
        context.set("sessionId", sessionId);
        context.set("lastMsg", null);
        exec.setRootContext(context);

        exec.go();

    } catch (Exception e) {
        LOG.error("Failed to parse SCXML", e);
    }
}

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

/**
 * Initializes the state machine.//from  ww  w.j a v  a2 s  .  co 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));
}