Coverage Report - org.apache.commons.scxml.invoke.SimpleSCXMLInvoker

Classes in this File Line Coverage Branch Coverage Complexity
SimpleSCXMLInvoker
52% 
33% 
3.8

 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 java.io.IOException;
 21  
 import java.util.Iterator;
 22  
 import java.util.Map;
 23  
 
 24  
 import org.apache.commons.scxml.Context;
 25  
 import org.apache.commons.scxml.Evaluator;
 26  
 import org.apache.commons.scxml.SCInstance;
 27  
 import org.apache.commons.scxml.SCXMLExecutor;
 28  
 import org.apache.commons.scxml.TriggerEvent;
 29  
 import org.apache.commons.scxml.env.SimpleDispatcher;
 30  
 import org.apache.commons.scxml.env.SimpleErrorHandler;
 31  
 import org.apache.commons.scxml.env.SimpleErrorReporter;
 32  
 import org.apache.commons.scxml.env.SimpleSCXMLListener;
 33  
 import org.apache.commons.scxml.io.SCXMLDigester;
 34  
 import org.apache.commons.scxml.model.ModelException;
 35  
 import org.apache.commons.scxml.model.SCXML;
 36  
 import org.xml.sax.SAXException;
 37  
 
 38  
 /**
 39  
  * A simple {@link Invoker} for SCXML documents. Invoked SCXML document
 40  
  * may not contain external namespace elements, further invokes etc.
 41  
  */
 42  1
 public class SimpleSCXMLInvoker implements Invoker {
 43  
 
 44  
     /** Parent state ID. */
 45  
     private String parentStateId;
 46  
     /** Event prefix, all events sent to the parent executor must begin
 47  
      *  with this prefix. */
 48  
     private String eventPrefix;
 49  
     /** Invoking document's SCInstance. */
 50  
     private SCInstance parentSCInstance;
 51  
     /** The invoked state machine executor. */
 52  
     private SCXMLExecutor executor;
 53  
     /** Cancellation status. */
 54  
     private boolean cancelled;
 55  
 
 56  
     //// Constants
 57  
     /** Prefix for all events sent to the parent state machine. */
 58  1
     private static String invokePrefix = ".invoke.";
 59  
     /** Suffix for invoke done event. */
 60  1
     private static String invokeDone = "done";
 61  
     /** Suffix for invoke cancel response event. */
 62  1
     private static String invokeCancelResponse = "cancel.response";
 63  
 
 64  
     /**
 65  
      * {@inheritDoc}.
 66  
      */
 67  
     public void setParentStateId(final String parentStateId) {
 68  1
         this.parentStateId = parentStateId;
 69  1
         this.eventPrefix = parentStateId + invokePrefix;
 70  1
         this.cancelled = false;
 71  1
     }
 72  
 
 73  
     /**
 74  
      * {@inheritDoc}.
 75  
      */
 76  
     public void setSCInstance(final SCInstance scInstance) {
 77  1
         this.parentSCInstance = scInstance;
 78  1
     }
 79  
 
 80  
     /**
 81  
      * {@inheritDoc}.
 82  
      */
 83  
     public void invoke(final String source, final Map params)
 84  
     throws InvokerException {
 85  1
         SCXML scxml = null;
 86  
         try {
 87  1
             scxml = SCXMLDigester.digest(source,
 88  
                 new SimpleErrorHandler(), null);
 89  0
         } catch (ModelException me) {
 90  0
             throw new InvokerException(me.getMessage(), me.getCause());
 91  0
         } catch (IOException ioe) {
 92  0
             throw new InvokerException(ioe.getMessage(), ioe.getCause());
 93  0
         } catch (SAXException se) {
 94  0
             throw new InvokerException(se.getMessage(), se.getCause());
 95  1
         }
 96  1
         Evaluator eval = parentSCInstance.getEvaluator();
 97  1
         executor = new SCXMLExecutor(eval,
 98  
             new SimpleDispatcher(), new SimpleErrorReporter());
 99  1
         Context rootCtx = eval.newContext(null);
 100  1
         for (Iterator iter = params.entrySet().iterator(); iter.hasNext();) {
 101  2
             Map.Entry entry = (Map.Entry) iter.next();
 102  2
             rootCtx.setLocal((String) entry.getKey(), entry.getValue());
 103  
         }
 104  1
         executor.setRootContext(rootCtx);
 105  1
         executor.setStateMachine(scxml);
 106  1
         executor.addListener(scxml, new SimpleSCXMLListener());
 107  
         try {
 108  1
             executor.go();
 109  0
         } catch (ModelException me) {
 110  0
             throw new InvokerException(me.getMessage(), me.getCause());
 111  1
         }
 112  1
     }
 113  
 
 114  
     /**
 115  
      * {@inheritDoc}.
 116  
      */
 117  
     public void parentEvents(final TriggerEvent[] evts)
 118  
     throws InvokerException {
 119  0
         if (cancelled) {
 120  0
             return; // no further processing should take place
 121  
         }
 122  0
         boolean doneBefore = executor.getCurrentStatus().isFinal();
 123  
         try {
 124  0
             executor.triggerEvents(evts);
 125  0
         } catch (ModelException me) {
 126  0
             throw new InvokerException(me.getMessage(), me.getCause());
 127  0
         }
 128  0
         if (!doneBefore && executor.getCurrentStatus().isFinal()) {
 129  0
             TriggerEvent te = new TriggerEvent(eventPrefix + invokeDone,
 130  
                 TriggerEvent.SIGNAL_EVENT);
 131  0
             new AsyncTrigger(parentSCInstance.getExecutor(), te);
 132  
         }
 133  0
     }
 134  
 
 135  
     /**
 136  
      * {@inheritDoc}.
 137  
      */
 138  
     public void cancel()
 139  
     throws InvokerException {
 140  0
         cancelled = true;
 141  0
         TriggerEvent te = new TriggerEvent(eventPrefix
 142  
             + invokeCancelResponse, TriggerEvent.SIGNAL_EVENT);
 143  0
         new AsyncTrigger(parentSCInstance.getExecutor(), te);
 144  0
     }
 145  
 
 146  
 }
 147