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