Coverage Report - org.mule.module.jbpm.MuleReceiveActivity
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleReceiveActivity
0%
0/39
0%
0/16
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.jbpm;
 8  
 
 9  
 import org.mule.module.bpm.Process;
 10  
 import org.mule.util.ClassUtils;
 11  
 
 12  
 import java.util.Map;
 13  
 
 14  
 import org.jbpm.api.JbpmException;
 15  
 import org.jbpm.api.activity.ActivityExecution;
 16  
 import org.jbpm.internal.log.Log;
 17  
 import org.jbpm.jpdl.internal.activity.StateActivity;
 18  
 import org.jbpm.pvm.internal.model.ExecutionImpl;
 19  
 
 20  
 public class MuleReceiveActivity extends StateActivity
 21  
 {
 22  
     /**
 23  
      *  Expected incoming endpoint; if a message was received from a different endpoint, an exception will be thrown.
 24  
      */
 25  
     private String endpoint;
 26  
 
 27  
     /**
 28  
      *  Expected incoming message type; if the payload received is not assignable to this class, an exception will be thrown.
 29  
      */
 30  
     private Class payloadClass;
 31  
 
 32  
     /**
 33  
      *  Variable into which the incoming message payload will be stored. If null, the payload will not be stored at all.
 34  
      */
 35  
     private String variableName;
 36  
 
 37  
     /** 
 38  
      * Is this the first state in the process? 
 39  
      */
 40  
     private final boolean startState;
 41  
     
 42  0
     private static final Log log = Log.getLog(MuleReceiveActivity.class.getName());
 43  
 
 44  
     public MuleReceiveActivity(boolean startState)
 45  
     {
 46  0
         super();
 47  0
         this.startState = startState;
 48  0
     }
 49  
     
 50  
     @Override
 51  
     public void execute(ExecutionImpl execution)
 52  
     {
 53  0
         execution.historyActivityStart();
 54  
 
 55  0
         if (startState)
 56  
         {
 57  0
             execution.signal();
 58  
         }
 59  
         else
 60  
         {
 61  0
             execution.waitForSignal();
 62  
         }
 63  0
     }
 64  
     
 65  
     @Override
 66  
     public void signal(ActivityExecution execution, String signalName, Map<String, ?> parameters)
 67  
     throws Exception
 68  
     {
 69  0
         Object payload = execution.getVariable(Process.PROCESS_VARIABLE_INCOMING);
 70  
 
 71  
         // Validate expected inbound endpoint
 72  0
         if (endpoint != null)
 73  
         {
 74  0
             String messageSource = (String) execution.getVariable(Process.PROCESS_VARIABLE_INCOMING_SOURCE);
 75  0
             log.debug("Validating message source = " + messageSource + ", expected = " + endpoint);
 76  0
             if (!endpoint.equalsIgnoreCase(messageSource))
 77  
             {
 78  0
                 throw new JbpmException("Incoming message source is " + messageSource + " but expected source is " + endpoint);
 79  
             }
 80  
         }
 81  
 
 82  
         // Validate expected message type
 83  0
         if (payloadClass != null)
 84  
         {
 85  0
             log.debug("Validating message type = " + payload.getClass() + ", expected = " + payloadClass);
 86  0
             if (!payloadClass.isAssignableFrom(payload.getClass()))
 87  
             {
 88  0
                 throw new JbpmException("Incoming message is of type " + payload.getClass() + " but expected type is " + payloadClass);
 89  
             }
 90  
         }
 91  
         
 92  
         // Store message payload into variable
 93  0
         if (variableName != null)
 94  
         {
 95  0
             if (payload != null)
 96  
             {
 97  0
                 log.debug("Storing incoming message to variable " + variableName + ", payload = " + payload);
 98  0
                 execution.setVariable(variableName, payload);
 99  
             }
 100  
             else
 101  
             {
 102  0
                 log.info("Synchronous message was sent to endpoint " + endpoint + ", but no response was returned.");
 103  
             }
 104  
         }
 105  
 
 106  0
         super.signal(execution, signalName, parameters);
 107  0
     }
 108  
 
 109  
     public String getEndpoint()
 110  
     {
 111  0
         return endpoint;
 112  
     }
 113  
 
 114  
     public void setEndpoint(String endpoint)
 115  
     {
 116  0
         this.endpoint = endpoint;
 117  0
     }
 118  
 
 119  
     public String getVariableName()
 120  
     {
 121  0
         return variableName;
 122  
     }
 123  
 
 124  
     public void setVariableName(String variableName)
 125  
     {
 126  0
         this.variableName = variableName;
 127  0
     }
 128  
 
 129  
     public void setPayloadClass(String className)
 130  
     {
 131  0
         if (className != null)
 132  
         {
 133  
             try
 134  
             {
 135  0
                 payloadClass = ClassUtils.loadClass(className, this.getClass());
 136  
             }
 137  0
             catch (ClassNotFoundException e)
 138  
             {
 139  0
                 log.error("Expected message type not valid: " + e.getMessage());
 140  0
             }
 141  
         }
 142  0
     }
 143  
 
 144  
     public Class getPayloadClass()
 145  
     {
 146  0
         return payloadClass;
 147  
     }
 148  
 }