View Javadoc

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  public class SendMuleEvent extends LoggingActionHandler
38  {
39  
40      private static final long serialVersionUID = 1L;
41  
42      boolean synchronous = true;
43      String endpoint = null;
44      String transformers = null;
45      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      String payload = null;
50      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          super.execute(executionContext);
58  
59          MuleMessageService mule = (MuleMessageService) executionContext.getJbpmContext().getServices()
60              .getMessageService();
61  
62          if (transformers != null)
63          {
64              endpoint += "?transformers=" + transformers;
65          }
66  
67          if (payload == null)
68          {
69              if (payloadSource == null)
70              {
71                  payloadObject = executionContext.getVariable(ProcessConnector.PROCESS_VARIABLE_DATA);
72                  if (payloadObject == null)
73                  {
74                      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                  String[] tokens = StringUtils.split(payloadSource, ".", 2);
83                  payloadObject = executionContext.getVariable(tokens[0]);
84                  if (tokens.length > 1)
85                  {
86                      JXPathContext context = JXPathContext.newContext(payloadObject);
87                      payloadObject = context.getValue(tokens[1].replaceAll("\\.", "/"));
88                  }
89              }
90          }
91          else
92          {
93              payloadObject = payload;
94          }
95          if (payloadObject == null)
96          {
97              throw new IllegalArgumentException("Payload for message is null.  Payload source is \""
98                              + payloadSource + "\"");
99          }
100 
101         Map props = new HashMap();
102         props.put(ProcessConnector.PROPERTY_PROCESS_TYPE, executionContext.getProcessDefinition().getName());
103         props.put(ProcessConnector.PROPERTY_PROCESS_ID, new Long(executionContext.getProcessInstance()
104             .getId()));
105         props.put(MuleProperties.MULE_CORRELATION_ID_PROPERTY, new Long(executionContext.getProcessInstance()
106             .getId()).toString());
107         props
108             .put(ProcessConnector.PROPERTY_PROCESS_STARTED, executionContext.getProcessInstance().getStart());
109         if (properties != null)
110         {
111             props.putAll(properties);
112         }
113 
114         MuleMessage response = mule.generateMessage(endpoint, payloadObject, props, synchronous);
115         if (synchronous)
116         {
117             if (response != null)
118             {
119                 executionContext.setVariable(ProcessConnector.PROCESS_VARIABLE_INCOMING, response.getPayload());
120             }
121             else 
122             {
123                 logger.info("Synchronous message was sent to endpoint " + endpoint
124                     + ", but no response was returned.");
125             }
126         }
127     }
128 
129 }