View Javadoc

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