View Javadoc

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