View Javadoc
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      private static final Log log = Log.getLog(MuleReceiveActivity.class.getName());
43  
44      public MuleReceiveActivity(boolean startState)
45      {
46          super();
47          this.startState = startState;
48      }
49      
50      @Override
51      public void execute(ExecutionImpl execution)
52      {
53          execution.historyActivityStart();
54  
55          if (startState)
56          {
57              execution.signal();
58          }
59          else
60          {
61              execution.waitForSignal();
62          }
63      }
64      
65      @Override
66      public void signal(ActivityExecution execution, String signalName, Map<String, ?> parameters)
67      throws Exception
68      {
69          Object payload = execution.getVariable(Process.PROCESS_VARIABLE_INCOMING);
70  
71          // Validate expected inbound endpoint
72          if (endpoint != null)
73          {
74              String messageSource = (String) execution.getVariable(Process.PROCESS_VARIABLE_INCOMING_SOURCE);
75              log.debug("Validating message source = " + messageSource + ", expected = " + endpoint);
76              if (!endpoint.equalsIgnoreCase(messageSource))
77              {
78                  throw new JbpmException("Incoming message source is " + messageSource + " but expected source is " + endpoint);
79              }
80          }
81  
82          // Validate expected message type
83          if (payloadClass != null)
84          {
85              log.debug("Validating message type = " + payload.getClass() + ", expected = " + payloadClass);
86              if (!payloadClass.isAssignableFrom(payload.getClass()))
87              {
88                  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          if (variableName != null)
94          {
95              if (payload != null)
96              {
97                  log.debug("Storing incoming message to variable " + variableName + ", payload = " + payload);
98                  execution.setVariable(variableName, payload);
99              }
100             else
101             {
102                 log.info("Synchronous message was sent to endpoint " + endpoint + ", but no response was returned.");
103             }
104         }
105 
106         super.signal(execution, signalName, parameters);
107     }
108 
109     public String getEndpoint()
110     {
111         return endpoint;
112     }
113 
114     public void setEndpoint(String endpoint)
115     {
116         this.endpoint = endpoint;
117     }
118 
119     public String getVariableName()
120     {
121         return variableName;
122     }
123 
124     public void setVariableName(String variableName)
125     {
126         this.variableName = variableName;
127     }
128 
129     public void setPayloadClass(String className)
130     {
131         if (className != null)
132         {
133             try
134             {
135                 payloadClass = ClassUtils.loadClass(className, this.getClass());
136             }
137             catch (ClassNotFoundException e)
138             {
139                 log.error("Expected message type not valid: " + e.getMessage());
140             }
141         }
142     }
143 
144     public Class getPayloadClass()
145     {
146         return payloadClass;
147     }
148 }