View Javadoc

1   /*
2    * $Id: ProcessMessageReceiver.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.providers.bpm;
12  
13  import org.mule.impl.MuleMessage;
14  import org.mule.providers.AbstractMessageReceiver;
15  import org.mule.umo.UMOComponent;
16  import org.mule.umo.UMOException;
17  import org.mule.umo.UMOMessage;
18  import org.mule.umo.endpoint.UMOEndpoint;
19  import org.mule.umo.lifecycle.InitialisationException;
20  import org.mule.umo.provider.UMOConnector;
21  
22  import java.util.Map;
23  
24  import javax.resource.spi.work.Work;
25  
26  /**
27   * Generates an incoming Mule event from an executing workflow process.
28   */
29  public class ProcessMessageReceiver extends AbstractMessageReceiver {
30  
31      private ProcessConnector connector = null;
32  
33      public ProcessMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint)
34              throws InitialisationException {
35          super(connector, component, endpoint);
36          this.connector = (ProcessConnector) connector;
37      }
38  
39      public UMOMessage generateSynchronousEvent(String endpoint, Object payload, Map messageProperties) throws UMOException {
40          logger.debug("Executing process is sending an event (synchronously) to Mule endpoint = " + endpoint);
41          UMOMessage response = generateEvent(endpoint, payload, messageProperties, true);
42          if (logger.isDebugEnabled())
43          {
44              logger.debug("Synchronous response is " + (response != null ? response.getPayload() : null ));
45          }
46          return response;
47      }
48  
49      public void generateAsynchronousEvent(String endpoint, Object payload, Map messageProperties) throws UMOException {
50          logger.debug("Executing process is dispatching an event (asynchronously) to Mule endpoint = " + endpoint);
51          try {
52              getWorkManager().scheduleWork(new Worker(endpoint, payload, messageProperties));
53          } catch (Exception e) {
54              handleException(e);
55          }
56      }
57  
58      protected UMOMessage generateEvent(String endpoint, Object payload, Map messageProperties, boolean synchronous) throws UMOException {
59          UMOMessage message;
60          if (payload instanceof UMOMessage) {
61              message = (UMOMessage) payload;
62          } else {
63              message = new MuleMessage(connector.getMessageAdapter(payload));
64          }
65          message.addProperties(messageProperties);
66  
67          if (connector.isAllowGlobalDispatcher()) {
68              // TODO MULE-1221 This should use the "dynamic://" endpoint and not depend on the MuleClient.
69              if (synchronous) {
70                  return connector.getMuleClient().send(endpoint, message);
71              } else {
72                  connector.getMuleClient().dispatch(endpoint, message);
73                  return null;
74              }
75          }
76          else {
77              message.setStringProperty(ProcessConnector.PROPERTY_ENDPOINT, endpoint);
78              return routeMessage(message, synchronous);
79          }
80      }
81  
82      private class Worker implements Work {
83          private String endpoint;
84          private Object payload;
85          private Map messageProperties;
86  
87          public Worker(String endpoint, Object payload, Map messageProperties) {
88              this.endpoint = endpoint;
89              this.payload = payload;
90              this.messageProperties = messageProperties;
91          }
92  
93          public void run() {
94              try {
95                  generateEvent(endpoint, payload, messageProperties, false);
96              } catch (Exception e) {
97                  getConnector().handleException(e);
98              }
99          }
100 
101         public void release() { /*nop*/ }
102     }
103 
104     protected void doConnect() throws Exception
105     {
106         // nothing to do
107     }
108 
109     protected void doDisconnect() throws Exception
110     {
111         // nothing to do
112     }
113 
114     protected void doStart() throws UMOException
115     {
116         // nothing to do
117     }
118 
119     protected void doStop() throws UMOException
120     {
121         // nothing to do
122     }
123 
124     protected void doDispose()
125     {
126         // nothing to do               
127     }
128 
129 }