Coverage Report - org.mule.transport.bpm.jbpm.actions.SendMuleEvent
 
Classes in this File Line Coverage Branch Coverage Complexity
SendMuleEvent
58%
22/38
28%
5/18
11
 
 1  
 /*
 2  
  * $Id: SendMuleEvent.java 11539 2008-04-08 18:05:21Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.transport.bpm.jbpm.actions;
 12  
 
 13  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.config.MuleProperties;
 15  
 import org.mule.transport.bpm.ProcessConnector;
 16  
 import org.mule.transport.bpm.jbpm.MuleMessageService;
 17  
 import org.mule.util.StringUtils;
 18  
 
 19  
 import java.util.HashMap;
 20  
 import java.util.Map;
 21  
 
 22  
 import org.apache.commons.jxpath.JXPathContext;
 23  
 import org.jbpm.graph.exe.ExecutionContext;
 24  
 
 25  
 /**
 26  
  * Sends a Mule message to the specified endpoint. If the message is synchronous, 
 27  
  * the response from Mule will be automatically stored in PROCESS_VARIABLE_INCOMING.
 28  
  * 
 29  
  * @param endpoint - the Mule endpoint
 30  
  * @param transformers - any transformers to be applied
 31  
  * @param payload - specify the payload as a string directly in the jPDL
 32  
  * @param payloadSource - process variable from which to generate the message
 33  
  *            payload, defaults to {@link ProcessConnector.PROCESS_VARIABLE_DATA} or
 34  
  *            {@link ProcessConnector.PROCESS_VARIABLE_INCOMING}
 35  
  * @param messageProperties - any properties to be applied to the message
 36  
  */
 37  2
 public class SendMuleEvent extends LoggingActionHandler
 38  
 {
 39  
 
 40  
     private static final long serialVersionUID = 1L;
 41  
 
 42  2
     boolean synchronous = true;
 43  2
     String endpoint = null;
 44  2
     String transformers = null;
 45  2
     Map properties = null;
 46  
     
 47  
     // Use "payload" to easily specify the payload as a string directly in the jPDL.
 48  
     // Use "payloadSource" to get the payload from a process variable. 
 49  2
     String payload = null;
 50  2
     String payloadSource = null;
 51  
     
 52  
     // The actual payload (as an object) will be stored here.
 53  
     private Object payloadObject;
 54  
 
 55  
     public void execute(ExecutionContext executionContext) throws Exception
 56  
     {
 57  2
         super.execute(executionContext);
 58  
 
 59  2
         MuleMessageService mule = (MuleMessageService) executionContext.getJbpmContext().getServices()
 60  
             .getMessageService();
 61  
 
 62  2
         if (transformers != null)
 63  
         {
 64  0
             endpoint += "?transformers=" + transformers;
 65  
         }
 66  
 
 67  2
         if (payload == null)
 68  
         {
 69  0
             if (payloadSource == null)
 70  
             {
 71  0
                 payloadObject = executionContext.getVariable(ProcessConnector.PROCESS_VARIABLE_DATA);
 72  0
                 if (payloadObject == null)
 73  
                 {
 74  0
                     payloadObject = executionContext.getVariable(ProcessConnector.PROCESS_VARIABLE_INCOMING);
 75  
                 }
 76  
             }
 77  
             else
 78  
             {
 79  
                 // The payloadSource may be specified using JavaBean notation (e.g.,
 80  
                 // "myObject.myStuff.myField" would first retrieve the process
 81  
                 // variable "myObject" and then call .getMyStuff().getMyField()
 82  0
                 String[] tokens = StringUtils.split(payloadSource, ".", 2);
 83  0
                 payloadObject = executionContext.getVariable(tokens[0]);
 84  0
                 if (tokens.length > 1)
 85  
                 {
 86  0
                     JXPathContext context = JXPathContext.newContext(payloadObject);
 87  0
                     payloadObject = context.getValue(tokens[1].replaceAll("\\.", "/"));
 88  
                 }
 89  0
             }
 90  
         }
 91  
         else
 92  
         {
 93  2
             payloadObject = payload;
 94  
         }
 95  2
         if (payloadObject == null)
 96  
         {
 97  0
             throw new IllegalArgumentException("Payload for message is null.  Payload source is \""
 98  
                             + payloadSource + "\"");
 99  
         }
 100  
 
 101  2
         Map props = new HashMap();
 102  2
         props.put(ProcessConnector.PROPERTY_PROCESS_TYPE, executionContext.getProcessDefinition().getName());
 103  2
         props.put(ProcessConnector.PROPERTY_PROCESS_ID, new Long(executionContext.getProcessInstance()
 104  
             .getId()));
 105  2
         props.put(MuleProperties.MULE_CORRELATION_ID_PROPERTY, new Long(executionContext.getProcessInstance()
 106  
             .getId()).toString());
 107  2
         props
 108  
             .put(ProcessConnector.PROPERTY_PROCESS_STARTED, executionContext.getProcessInstance().getStart());
 109  2
         if (properties != null)
 110  
         {
 111  0
             props.putAll(properties);
 112  
         }
 113  
 
 114  2
         MuleMessage response = mule.generateMessage(endpoint, payloadObject, props, synchronous);
 115  2
         if (synchronous)
 116  
         {
 117  0
             if (response != null)
 118  
             {
 119  0
                 executionContext.setVariable(ProcessConnector.PROCESS_VARIABLE_INCOMING, response.getPayload());
 120  
             }
 121  
             else 
 122  
             {
 123  0
                 logger.info("Synchronous message was sent to endpoint " + endpoint
 124  
                     + ", but no response was returned.");
 125  
             }
 126  
         }
 127  2
     }
 128  
 
 129  
 }