View Javadoc

1   /*
2    * $Id: MuleDispatcher.java 7963 2007-08-21 08:53:15Z 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.jbi.components;
12  
13  import org.mule.impl.MuleEvent;
14  import org.mule.impl.MuleSession;
15  import org.mule.providers.AbstractConnector;
16  import org.mule.providers.jbi.JbiUtils;
17  import org.mule.umo.UMOEvent;
18  import org.mule.umo.UMOMessage;
19  
20  import javax.jbi.messaging.MessageExchange;
21  import javax.jbi.messaging.MessagingException;
22  import javax.jbi.messaging.NormalizedMessage;
23  
24  /**
25   * A JBI component that can dispatch Normalised Messages over a given transport
26   * specified by the muleEndpoint property. This component can deliver events over any
27   * Mule transport such as jms, ftp, htp, jdbc, ejb, etc.
28   */
29  public class MuleDispatcher extends AbstractEndpointComponent implements MessageExchangeListener
30  {
31  
32      public void onExchange(MessageExchange messageExchange) throws MessagingException
33      {
34          if (logger.isDebugEnabled())
35          {
36              logger.debug("In Mule Dispatcher");
37          }
38  
39          try
40          {
41              NormalizedMessage out = messageExchange.getMessage(IN);
42              UMOMessage message = JbiUtils.createMessage(out);
43  
44              if (logger.isDebugEnabled())
45              {
46                  logger.debug("Dispatching Message via Mule: " + message);
47              }
48  
49              MuleSession session = new MuleSession(message,
50                  ((AbstractConnector)muleEndpoint.getConnector()).getSessionHandler());
51  
52              UMOEvent event = new MuleEvent(message, muleEndpoint, session, muleEndpoint.isSynchronous());
53  
54              if (muleEndpoint.isSynchronous())
55              {
56                  logger.debug("Dispatching to: " + muleEndpoint.getEndpointURI());
57                  logger.debug("Payload is: " + event.getMessageAsString());
58  
59                  UMOMessage result = muleEndpoint.send(event);
60                  // TODO send result back
61              }
62              else
63              {
64                  muleEndpoint.dispatch(event);
65              }
66          }
67          catch (Exception e)
68          {
69              handleException(e);
70              error(messageExchange, e);
71          }
72      }
73  }